Skip to content

Commit 3704a14

Browse files
authored
Merge pull request #153 from runger1101001/driver_refactor
Driver refactor
2 parents 30d131c + 8db941a commit 3704a14

24 files changed

+912
-662
lines changed

src/common/base_classes/BLDCDriver.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ class BLDCDriver{
2323
float dc_c; //!< currently set duty cycle on phaseC
2424

2525
bool initialized = false; // true if driver was successfully initialized
26+
void* params = 0; // pointer to hardware specific parameters of driver
2627

2728
/**
2829
* Set phase voltages to the harware

src/common/base_classes/StepperDriver.h

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
#ifndef STEPPERDRIVER_H
22
#define STEPPERDRIVER_H
33

4+
#include "drivers/hardware_api.h"
5+
46
class StepperDriver{
57
public:
68

@@ -16,7 +18,8 @@ class StepperDriver{
1618
float voltage_limit; //!< limiting voltage set to the motor
1719

1820
bool initialized = false; // true if driver was successfully initialized
19-
21+
void* params = 0; // pointer to hardware specific parameters of driver
22+
2023
/**
2124
* Set phase voltages to the harware
2225
*

src/current_sense/hardware_specific/stm32g4_mcu.cpp

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
#include "../hardware_api.h"
22
#include "stm32g4_hal.h"
3+
#include "Arduino.h"
34

45
#if defined(STM32G4xx)
56
#define _ADC_VOLTAGE 3.3
@@ -29,9 +30,10 @@ float _readADCVoltageInline(const int pin){
2930
raw_adc = adcBuffer1[1];
3031
else if(pin == PA6) // = ADC2_IN3 = phase V (OP2_OUT) on B-G431B-ESC1
3132
raw_adc = adcBuffer2[0];
33+
#ifdef PB1
3234
else if(pin == PB1) // = ADC1_IN12 = phase W (OP3_OUT) on B-G431B-ESC1
3335
raw_adc = adcBuffer1[0];
34-
36+
#endif
3537
return raw_adc * _ADC_CONV;
3638
}
3739
// do the same for low side sensing

src/drivers/BLDCDriver3PWM.cpp

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -56,9 +56,8 @@ int BLDCDriver3PWM::init() {
5656

5757
// Set the pwm frequency to the pins
5858
// hardware specific function - depending on driver and mcu
59-
_configure3PWM(pwm_frequency, pwmA, pwmB, pwmC);
60-
initialized = true; // TODO atm the api gives no feedback if initialization succeeded
61-
return 0;
59+
params = _configure3PWM(pwm_frequency, pwmA, pwmB, pwmC);
60+
return params!=SIMPLEFOC_DRIVER_INIT_FAILED;
6261
}
6362

6463

@@ -88,5 +87,5 @@ void BLDCDriver3PWM::setPwm(float Ua, float Ub, float Uc) {
8887

8988
// hardware specific writing
9089
// hardware specific function - depending on driver and mcu
91-
_writeDutyCycle3PWM(dc_a, dc_b, dc_c, pwmA, pwmB, pwmC);
90+
_writeDutyCycle3PWM(dc_a, dc_b, dc_c, params);
9291
}

src/drivers/BLDCDriver3PWM.h

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,6 @@ class BLDCDriver3PWM: public BLDCDriver
5858
*/
5959
virtual void setPhaseState(int sa, int sb, int sc) override;
6060
private:
61-
6261
};
6362

6463

src/drivers/BLDCDriver6PWM.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -58,11 +58,11 @@ int BLDCDriver6PWM::init() {
5858

5959
// configure 6pwm
6060
// hardware specific function - depending on driver and mcu
61-
int result = _configure6PWM(pwm_frequency, dead_zone, pwmA_h,pwmA_l, pwmB_h,pwmB_l, pwmC_h,pwmC_l);
62-
initialized = (result==0);
63-
return result;
61+
params = _configure6PWM(pwm_frequency, dead_zone, pwmA_h,pwmA_l, pwmB_h,pwmB_l, pwmC_h,pwmC_l);
62+
return params!=SIMPLEFOC_DRIVER_INIT_FAILED;
6463
}
6564

65+
6666
// Set voltage to the pwm pin
6767
void BLDCDriver6PWM::setPwm(float Ua, float Ub, float Uc) {
6868
// limit the voltage in driver
@@ -76,7 +76,7 @@ void BLDCDriver6PWM::setPwm(float Ua, float Ub, float Uc) {
7676
dc_c = _constrain(Uc / voltage_power_supply, 0.0f , 1.0f );
7777
// hardware specific writing
7878
// hardware specific function - depending on driver and mcu
79-
_writeDutyCycle6PWM(dc_a, dc_b, dc_c, dead_zone, pwmA_h,pwmA_l, pwmB_h,pwmB_l, pwmC_h,pwmC_l);
79+
_writeDutyCycle6PWM(dc_a, dc_b, dc_c, params);
8080
}
8181

8282

src/drivers/StepperDriver2PWM.cpp

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -79,9 +79,8 @@ int StepperDriver2PWM::init() {
7979

8080
// Set the pwm frequency to the pins
8181
// hardware specific function - depending on driver and mcu
82-
_configure2PWM(pwm_frequency, pwm1, pwm2);
83-
initialized = true; // TODO atm the api gives no feedback if initialization succeeded
84-
return 0;
82+
params = _configure2PWM(pwm_frequency, pwm1, pwm2);
83+
return params!=SIMPLEFOC_DRIVER_INIT_FAILED;
8584
}
8685

8786

@@ -103,5 +102,5 @@ void StepperDriver2PWM::setPwm(float Ua, float Ub) {
103102
if( _isset(dir2b) ) digitalWrite(dir2b, Ub >= 0 ? HIGH : LOW );
104103

105104
// write to hardware
106-
_writeDutyCycle2PWM(duty_cycle1, duty_cycle2, pwm1, pwm2);
105+
_writeDutyCycle2PWM(duty_cycle1, duty_cycle2, params);
107106
}

src/drivers/StepperDriver4PWM.cpp

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -54,9 +54,8 @@ int StepperDriver4PWM::init() {
5454

5555
// Set the pwm frequency to the pins
5656
// hardware specific function - depending on driver and mcu
57-
_configure4PWM(pwm_frequency, pwm1A, pwm1B, pwm2A, pwm2B);
58-
initialized = true; // TODO atm the api gives no feedback if initialization succeeded
59-
return 0;
57+
params = _configure4PWM(pwm_frequency, pwm1A, pwm1B, pwm2A, pwm2B);
58+
return params!=SIMPLEFOC_DRIVER_INIT_FAILED;
6059
}
6160

6261

@@ -77,5 +76,5 @@ void StepperDriver4PWM::setPwm(float Ualpha, float Ubeta) {
7776
else
7877
duty_cycle2A = _constrain(abs(Ubeta)/voltage_power_supply,0.0f,1.0f);
7978
// write to hardware
80-
_writeDutyCycle4PWM(duty_cycle1A, duty_cycle1B, duty_cycle2A, duty_cycle2B, pwm1A, pwm1B, pwm2A, pwm2B);
79+
_writeDutyCycle4PWM(duty_cycle1A, duty_cycle1B, duty_cycle2A, duty_cycle2B, params);
8180
}

src/drivers/hardware_api.h

Lines changed: 28 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,15 @@
44
#include "../common/foc_utils.h"
55
#include "../common/time_utils.h"
66

7+
#define SIMPLEFOC_DRIVER_INIT_FAILED ((void*)-1)
8+
9+
typedef struct GenericDriverParams {
10+
int pins[6];
11+
long pwm_frequency;
12+
float dead_zone;
13+
} GenericDriverParams;
14+
15+
716
/**
817
* Configuring PWM frequency, resolution and alignment
918
* - Stepper driver - 2PWM setting
@@ -12,8 +21,10 @@
1221
* @param pwm_frequency - frequency in hertz - if applicable
1322
* @param pinA pinA bldc driver
1423
* @param pinB pinB bldc driver
24+
*
25+
* @return -1 if failed, or pointer to internal driver parameters struct if successful
1526
*/
16-
void _configure2PWM(long pwm_frequency, const int pinA, const int pinB);
27+
void* _configure2PWM(long pwm_frequency, const int pinA, const int pinB);
1728

1829
/**
1930
* Configuring PWM frequency, resolution and alignment
@@ -24,8 +35,10 @@ void _configure2PWM(long pwm_frequency, const int pinA, const int pinB);
2435
* @param pinA pinA bldc driver
2536
* @param pinB pinB bldc driver
2637
* @param pinC pinC bldc driver
38+
*
39+
* @return -1 if failed, or pointer to internal driver parameters struct if successful
2740
*/
28-
void _configure3PWM(long pwm_frequency, const int pinA, const int pinB, const int pinC);
41+
void* _configure3PWM(long pwm_frequency, const int pinA, const int pinB, const int pinC);
2942

3043
/**
3144
* Configuring PWM frequency, resolution and alignment
@@ -37,8 +50,10 @@ void _configure3PWM(long pwm_frequency, const int pinA, const int pinB, const in
3750
* @param pin1B pin1B stepper driver
3851
* @param pin2A pin2A stepper driver
3952
* @param pin2B pin2B stepper driver
53+
*
54+
* @return -1 if failed, or pointer to internal driver parameters struct if successful
4055
*/
41-
void _configure4PWM(long pwm_frequency, const int pin1A, const int pin1B, const int pin2A, const int pin2B);
56+
void* _configure4PWM(long pwm_frequency, const int pin1A, const int pin1B, const int pin2A, const int pin2B);
4257

4358
/**
4459
* Configuring PWM frequency, resolution and alignment
@@ -54,9 +69,9 @@ void _configure4PWM(long pwm_frequency, const int pin1A, const int pin1B, const
5469
* @param pinC_h pinA high-side bldc driver
5570
* @param pinC_l pinA low-side bldc driver
5671
*
57-
* @return 0 if config good, -1 if failed
72+
* @return -1 if failed, or pointer to internal driver parameters struct if successful
5873
*/
59-
int _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);
74+
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);
6075

6176
/**
6277
* Function setting the duty cycle to the pwm pin (ex. analogWrite())
@@ -65,10 +80,9 @@ int _configure6PWM(long pwm_frequency, float dead_zone, const int pinA_h, const
6580
*
6681
* @param dc_a duty cycle phase A [0, 1]
6782
* @param dc_b duty cycle phase B [0, 1]
68-
* @param pinA phase A hardware pin number
69-
* @param pinB phase B hardware pin number
83+
* @param params the driver parameters
7084
*/
71-
void _writeDutyCycle2PWM(float dc_a, float dc_b, int pinA, int pinB);
85+
void _writeDutyCycle2PWM(float dc_a, float dc_b, void* params);
7286

7387
/**
7488
* Function setting the duty cycle to the pwm pin (ex. analogWrite())
@@ -78,11 +92,9 @@ void _writeDutyCycle2PWM(float dc_a, float dc_b, int pinA, int pinB);
7892
* @param dc_a duty cycle phase A [0, 1]
7993
* @param dc_b duty cycle phase B [0, 1]
8094
* @param dc_c duty cycle phase C [0, 1]
81-
* @param pinA phase A hardware pin number
82-
* @param pinB phase B hardware pin number
83-
* @param pinC phase C hardware pin number
95+
* @param params the driver parameters
8496
*/
85-
void _writeDutyCycle3PWM(float dc_a, float dc_b, float dc_c, int pinA, int pinB, int pinC);
97+
void _writeDutyCycle3PWM(float dc_a, float dc_b, float dc_c, void* params);
8698

8799
/**
88100
* Function setting the duty cycle to the pwm pin (ex. analogWrite())
@@ -93,12 +105,9 @@ void _writeDutyCycle3PWM(float dc_a, float dc_b, float dc_c, int pinA, int pinB
93105
* @param dc_1b duty cycle phase 1B [0, 1]
94106
* @param dc_2a duty cycle phase 2A [0, 1]
95107
* @param dc_2b duty cycle phase 2B [0, 1]
96-
* @param pin1A phase 1A hardware pin number
97-
* @param pin1B phase 1B hardware pin number
98-
* @param pin2A phase 2A hardware pin number
99-
* @param pin2B phase 2B hardware pin number
108+
* @param params the driver parameters
100109
*/
101-
void _writeDutyCycle4PWM(float dc_1a, float dc_1b, float dc_2a, float dc_2b, int pin1A, int pin1B, int pin2A, int pin2B);
110+
void _writeDutyCycle4PWM(float dc_1a, float dc_1b, float dc_2a, float dc_2b, void* params);
102111

103112

104113
/**
@@ -109,15 +118,9 @@ void _writeDutyCycle4PWM(float dc_1a, float dc_1b, float dc_2a, float dc_2b, in
109118
* @param dc_a duty cycle phase A [0, 1]
110119
* @param dc_b duty cycle phase B [0, 1]
111120
* @param dc_c duty cycle phase C [0, 1]
112-
* @param dead_zone duty cycle protection zone [0, 1] - both low and high side low
113-
* @param pinA_h phase A high-side hardware pin number
114-
* @param pinA_l phase A low-side hardware pin number
115-
* @param pinB_h phase B high-side hardware pin number
116-
* @param pinB_l phase B low-side hardware pin number
117-
* @param pinC_h phase C high-side hardware pin number
118-
* @param pinC_l phase C low-side hardware pin number
121+
* @param params the driver parameters
119122
*
120123
*/
121-
void _writeDutyCycle6PWM(float dc_a, float dc_b, float dc_c, float dead_zone, int pinA_h, int pinA_l, int pinB_h, int pinB_l, int pinC_h, int pinC_l);
124+
void _writeDutyCycle6PWM(float dc_a, float dc_b, float dc_c, void* params);
122125

123126
#endif

src/drivers/hardware_specific/atmega2560_mcu.cpp

Lines changed: 42 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -28,64 +28,79 @@ void _pinHighFrequency(const int pin){
2828
// function setting the high pwm frequency to the supplied pins
2929
// - Stepper motor - 2PWM setting
3030
// - hardware speciffic
31-
void _configure2PWM(long pwm_frequency,const int pinA, const int pinB) {
31+
void* _configure2PWM(long pwm_frequency,const int pinA, const int pinB) {
3232
// High PWM frequency
3333
// - always max 32kHz
3434
_pinHighFrequency(pinA);
3535
_pinHighFrequency(pinB);
36+
GenericDriverParams* params = new GenericDriverParams {
37+
.pins = { pinA, pinB },
38+
.pwm_frequency = pwm_frequency
39+
};
40+
return params;
3641
}
3742

3843
// function setting the high pwm frequency to the supplied pins
3944
// - BLDC motor - 3PWM setting
4045
// - hardware speciffic
41-
void _configure3PWM(long pwm_frequency,const int pinA, const int pinB, const int pinC) {
46+
void* _configure3PWM(long pwm_frequency,const int pinA, const int pinB, const int pinC) {
4247
// High PWM frequency
4348
// - always max 32kHz
4449
_pinHighFrequency(pinA);
4550
_pinHighFrequency(pinB);
4651
_pinHighFrequency(pinC);
52+
GenericDriverParams* params = new GenericDriverParams {
53+
.pins = { pinA, pinB, pinC },
54+
.pwm_frequency = pwm_frequency
55+
};
56+
return params;
4757
}
4858

4959
// function setting the pwm duty cycle to the hardware
5060
// - Stepper motor - 2PWM setting
5161
// - hardware speciffic
52-
void _writeDutyCycle2PWM(float dc_a, float dc_b, int pinA, int pinB){
62+
void _writeDutyCycle2PWM(float dc_a, float dc_b, void* params){
5363
// transform duty cycle from [0,1] to [0,255]
54-
analogWrite(pinA, 255.0f*dc_a);
55-
analogWrite(pinB, 255.0f*dc_b);
64+
analogWrite(((GenericDriverParams*)params)->pins[0], 255.0f*dc_a);
65+
analogWrite(((GenericDriverParams*)params)->pins[1], 255.0f*dc_b);
5666
}
5767

5868
// function setting the pwm duty cycle to the hardware
5969
// - BLDC motor - 3PWM setting
6070
// - hardware speciffic
61-
void _writeDutyCycle3PWM(float dc_a, float dc_b, float dc_c, int pinA, int pinB, int pinC){
71+
void _writeDutyCycle3PWM(float dc_a, float dc_b, float dc_c, void* params){
6272
// transform duty cycle from [0,1] to [0,255]
63-
analogWrite(pinA, 255.0f*dc_a);
64-
analogWrite(pinB, 255.0f*dc_b);
65-
analogWrite(pinC, 255.0f*dc_c);
73+
analogWrite(((GenericDriverParams*)params)->pins[0], 255.0f*dc_a);
74+
analogWrite(((GenericDriverParams*)params)->pins[1], 255.0f*dc_b);
75+
analogWrite(((GenericDriverParams*)params)->pins[2], 255.0f*dc_c);
6676
}
6777

6878
// function setting the high pwm frequency to the supplied pins
6979
// - Stepper motor - 4PWM setting
7080
// - hardware speciffic
71-
void _configure4PWM(long pwm_frequency,const int pin1A, const int pin1B, const int pin2A, const int pin2B) {
81+
void* _configure4PWM(long pwm_frequency,const int pin1A, const int pin1B, const int pin2A, const int pin2B) {
7282
// High PWM frequency
7383
// - always max 32kHz
7484
_pinHighFrequency(pin1A);
7585
_pinHighFrequency(pin1B);
7686
_pinHighFrequency(pin2A);
7787
_pinHighFrequency(pin2B);
88+
GenericDriverParams* params = new GenericDriverParams {
89+
.pins = { pin1A, pin2A, pin2A, pin2B },
90+
.pwm_frequency = pwm_frequency
91+
};
92+
return params;
7893
}
7994

8095
// function setting the pwm duty cycle to the hardware
8196
// - Stepper motor - 4PWM setting
8297
// - hardware speciffic
83-
void _writeDutyCycle4PWM(float dc_1a, float dc_1b, float dc_2a, float dc_2b, int pin1A, int pin1B, int pin2A, int pin2B){
98+
void _writeDutyCycle4PWM(float dc_1a, float dc_1b, float dc_2a, float dc_2b, void* params) {
8499
// transform duty cycle from [0,1] to [0,255]
85-
analogWrite(pin1A, 255.0f*dc_1a);
86-
analogWrite(pin1B, 255.0f*dc_1b);
87-
analogWrite(pin2A, 255.0f*dc_2a);
88-
analogWrite(pin2B, 255.0f*dc_2b);
100+
analogWrite(((GenericDriverParams*)params)->pins[0], 255.0f*dc_1a);
101+
analogWrite(((GenericDriverParams*)params)->pins[1], 255.0f*dc_1b);
102+
analogWrite(((GenericDriverParams*)params)->pins[2], 255.0f*dc_2a);
103+
analogWrite(((GenericDriverParams*)params)->pins[3], 255.0f*dc_2b);
89104
}
90105

91106

@@ -122,14 +137,20 @@ int _configureComplementaryPair(int pinH, int pinL) {
122137
// - BLDC driver - 6PWM setting
123138
// - hardware specific
124139
// supports Arudino/ATmega328
125-
int _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) {
140+
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) {
126141
// High PWM frequency
127142
// - always max 32kHz
128143
int ret_flag = 0;
129144
ret_flag += _configureComplementaryPair(pinA_h, pinA_l);
130145
ret_flag += _configureComplementaryPair(pinB_h, pinB_l);
131146
ret_flag += _configureComplementaryPair(pinC_h, pinC_l);
132-
return ret_flag; // returns -1 if not well configured
147+
if (ret_flag!=0) return SIMPLEFOC_DRIVER_INIT_FAILED;
148+
GenericDriverParams* params = new GenericDriverParams {
149+
.pins = { pinA_h,, pinA_l, pinB_h, pinB_l, pinC_h, pinC_l },
150+
.pwm_frequency = pwm_frequency
151+
.dead_zone = dead_zone
152+
};
153+
return params;
133154
}
134155

135156
// function setting the
@@ -149,10 +170,10 @@ void _setPwmPair(int pinH, int pinL, float val, int dead_time)
149170
// - BLDC driver - 6PWM setting
150171
// - hardware specific
151172
// supports Arudino/ATmega328
152-
void _writeDutyCycle6PWM(float dc_a, float dc_b, float dc_c, float dead_zone, int pinA_h, int pinA_l, int pinB_h, int pinB_l, int pinC_h, int pinC_l){
153-
_setPwmPair(pinA_h, pinA_l, dc_a*255.0, dead_zone*255.0);
154-
_setPwmPair(pinB_h, pinB_l, dc_b*255.0, dead_zone*255.0);
155-
_setPwmPair(pinC_h, pinC_l, dc_c*255.0, dead_zone*255.0);
173+
void _writeDutyCycle6PWM(float dc_a, float dc_b, float dc_c, void* params){
174+
_setPwmPair(((GenericDriverParams*)params)->pins[0], ((GenericDriverParams*)params)->pins[1], dc_a*255.0, ((GenericDriverParams*)params)->dead_zone*255.0);
175+
_setPwmPair(((GenericDriverParams*)params)->pins[2], ((GenericDriverParams*)params)->pins[3], dc_b*255.0, ((GenericDriverParams*)params)->dead_zone*255.0);
176+
_setPwmPair(((GenericDriverParams*)params)->pins[4], ((GenericDriverParams*)params)->pins[5], dc_c*255.0, ((GenericDriverParams*)params)->dead_zone*255.0);
156177
}
157178

158179
#endif

0 commit comments

Comments
 (0)