Skip to content

Commit a520bc1

Browse files
committed
Initial 8PWM
1 parent e7e92e7 commit a520bc1

File tree

6 files changed

+260
-58
lines changed

6 files changed

+260
-58
lines changed

src/StepperMotor.cpp

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -371,6 +371,70 @@ void StepperMotor::setPhaseVoltage(float Uq, float Ud, float angle_el) {
371371
driver->setPwm(Ualpha, Ubeta);
372372
}
373373

374+
// Method using FOC to set Uq and Ud to the motor at the optimal angle
375+
// Function implementing Sine PWM algorithms
376+
// - space vector not implemented yet
377+
//
378+
379+
int32_t float_to_q31(float input) {
380+
int32_t q31 = (int32_t) (input * 2147483648.0f);
381+
q31 = (q31 > 0) ? (q31 << 1) : (-q31 << 1);
382+
return q31;
383+
}
384+
385+
#define pi 3.1415926535897932384626433f
386+
387+
int32_t q31_value;
388+
float value_f32_sine = 0;
389+
float value_f32_cosine = 0;
390+
float cordic_cosine = 0.0f;
391+
float cordic_sine = 0.0f;
392+
393+
float wrap_to_1(float x) {
394+
while (x > 1.0f) {
395+
x -= 2.0f;
396+
}
397+
while (x < -1.0f) {
398+
x += 2.0f;
399+
}
400+
return x;
401+
}
402+
403+
void StepperMotor::setPhaseVoltageCORDIC(float Uq, float Ud, float angle_el) {
404+
// Sinusoidal PWM modulation
405+
// Inverse Park transformation
406+
407+
// WHO U GONNA CALL? CORDIC ->
408+
409+
q31_value = float_to_q31(angle_el / (2.0f * pi));
410+
411+
CORDIC->WDATA = q31_value;
412+
cordic_sine = CORDIC->RDATA;
413+
cordic_cosine = CORDIC->RDATA;
414+
415+
value_f32_sine = (float)cordic_sine/(float)0x80000000;
416+
value_f32_cosine = (float)cordic_cosine/(float)0x80000000;
417+
418+
if (angle < 0){
419+
value_f32_sine = wrap_to_1(value_f32_sine);
420+
value_f32_sine = value_f32_sine * -1;
421+
}
422+
423+
if (angle > 0){
424+
value_f32_sine = wrap_to_1(value_f32_sine);
425+
}
426+
427+
value_f32_cosine = wrap_to_1(value_f32_cosine);
428+
429+
// Inverse park transform
430+
Ualpha = value_f32_cosine * Ud - cordic_sine * Uq; // -sin(angle) * Uq;
431+
Ubeta = cordic_sine * Ud + value_f32_cosine * Uq; // cos(angle) * Uq;
432+
433+
// set the voltages in hardware
434+
driver->setPwm(Ualpha, Ubeta);
435+
}
436+
437+
374438
// Function (iterative) generating open loop movement for target velocity
375439
// - target_velocity - rad/s
376440
// it uses voltage_limit variable

src/StepperMotor.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,10 @@ class StepperMotor: public FOCMotor
8989
*/
9090
void setPhaseVoltage(float Uq, float Ud, float angle_el) override;
9191

92+
// CORDIC
93+
94+
void setPhaseVoltageCORDIC(float Uq, float Ud, float angle_el) override;
95+
9296
private:
9397

9498
/** Sensor alignment to electrical 0 angle of the motor */

src/common/base_classes/StepperDriver.h

Lines changed: 24 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -5,28 +5,30 @@
55

66
class StepperDriver{
77
public:
8-
9-
/** Initialise hardware */
10-
virtual int init() = 0;
11-
/** Enable hardware */
12-
virtual void enable() = 0;
13-
/** Disable hardware */
14-
virtual void disable() = 0;
15-
16-
long pwm_frequency; //!< pwm frequency value in hertz
17-
float voltage_power_supply; //!< power supply voltage
18-
float voltage_limit; //!< limiting voltage set to the motor
19-
20-
bool initialized = false; // true if driver was successfully initialized
21-
void* params = 0; // pointer to hardware specific parameters of driver
22-
23-
/**
24-
* Set phase voltages to the harware
25-
*
26-
* @param Ua phase A voltage
27-
* @param Ub phase B voltage
28-
*/
29-
virtual void setPwm(float Ua, float Ub) = 0;
8+
/** Initialise hardware */
9+
virtual int init() = 0;
10+
/** Enable hardware */
11+
virtual void enable() = 0;
12+
/** Disable hardware */
13+
virtual void disable() = 0;
14+
15+
long pwm_frequency; //!< pwm frequency value in hertz
16+
float voltage_power_supply; //!< power supply voltage
17+
float voltage_limit; //!< limiting voltage set to the motor
18+
19+
bool initialized = false; // true if driver was successfully initialized
20+
void* params = 0; // pointer to hardware specific parameters of driver
21+
22+
/**
23+
* Set phase voltages to the hardware
24+
*
25+
* @param Ua phase A voltage
26+
* @param Ub phase B voltage
27+
*/
28+
virtual void setPwm(float Ua, float Ub) = 0;
29+
30+
31+
3032
};
3133

3234
#endif

src/drivers/hardware_api.h

Lines changed: 41 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@
3636
// will be returned as a void pointer from the _configurexPWM functions
3737
// will be provided to the _writeDutyCyclexPWM() as a void pointer
3838
typedef struct GenericDriverParams {
39-
int pins[6];
39+
int pins[8];
4040
long pwm_frequency;
4141
float dead_zone;
4242
} GenericDriverParams;
@@ -114,6 +114,27 @@ void* _configure4PWM(long pwm_frequency, const int pin1A, const int pin1B, const
114114
*/
115115
void* _configure6PWM(long pwm_frequency, float dead_zone, const int pinA_h, const int pinA_l, const int pinB_h, const int pinB_l, const int pinC_h, const int pinC_l);
116116

117+
/**
118+
* Configuring PWM frequency, resolution and alignment
119+
* - Stepper driver - 8PWM setting
120+
* - hardware specific
121+
*
122+
* @param pwm_frequency - frequency in hertz - if applicable
123+
* @param pin1A pin1A stepper driver
124+
* @param pin1B pin1B stepper driver
125+
* @param pin2A pin2A stepper driver
126+
* @param pin2B pin2B stepper driver
127+
* @param pin3A pin3A stepper driver
128+
* @param pin3B pin3B stepper driver
129+
* @param pin4A pin4A stepper driver
130+
* @param pin4B pin4B stepper driver
131+
*
132+
* @return -1 if failed, or pointer to internal driver parameters struct if successful
133+
*/
134+
void* _configure8PWM(long pwm_frequency, float dead_zone, const int pin1A, const int pin1B, const int pin2A, const int pin2B,
135+
const int pin3A, const int pin3B, const int pin4A, const int pin4B);
136+
137+
117138
/**
118139
* Function setting the duty cycle to the pwm pin (ex. analogWrite())
119140
* - Stepper driver - 2PWM setting
@@ -177,4 +198,23 @@ void _writeDutyCycle4PWM(float dc_1a, float dc_1b, float dc_2a, float dc_2b, vo
177198
void _writeDutyCycle6PWM(float dc_a, float dc_b, float dc_c, PhaseState *phase_state, void* params);
178199

179200

201+
/**
202+
203+
*Function setting the duty cycle to the pwm pin (ex. analogWrite())
204+
* - Stepper driver - 8PWM setting
205+
* - hardware specific
206+
*@param dc_1a duty cycle phase 1A [0, 1]
207+
*@param dc_1b duty cycle phase 1B [0, 1]
208+
*@param dc_1c duty cycle phase 1C [0, 1]
209+
*@param dc_1d duty cycle phase 1D [0, 1]
210+
*@param dc_2a duty cycle phase 2A [0, 1]
211+
*@param dc_2b duty cycle phase 2B [0, 1]
212+
*@param dc_2c duty cycle phase 2C [0, 1]
213+
*@param dc_2d duty cycle phase 2D [0, 1]
214+
*@param params the driver parameters
215+
*/
216+
217+
void _writeDutyCycle8PWM(float dc_1a, float dc_1b, float dc_1c, float dc_1d, float dc_2a, float dc_2b, float dc_2c, float dc_2d, void* params);
218+
219+
180220
#endif

0 commit comments

Comments
 (0)