Skip to content

Commit 5de2e75

Browse files
Merge pull request #143 from runger1101001/motor_status
support motor intialization status tracking
2 parents 6c880c7 + 009686a commit 5de2e75

File tree

9 files changed

+48
-1
lines changed

9 files changed

+48
-1
lines changed

src/BLDCMotor.cpp

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,11 @@ void BLDCMotor::linkDriver(BLDCDriver* _driver) {
2424

2525
// init hardware pins
2626
void BLDCMotor::init() {
27+
if (!driver || !driver->initialized) {
28+
motor_status = FOCMotorStatus::motor_init_failed;
29+
if(monitor_port) monitor_port->println(F("MOT: Init not possible, driver not initialized"));
30+
}
31+
motor_status = FOCMotorStatus::motor_initializing;
2732
if(monitor_port) monitor_port->println(F("MOT: Init"));
2833

2934
// if no current sensing and the user has set the phase resistance of the motor use current limit to calculate the voltage limit
@@ -57,6 +62,7 @@ void BLDCMotor::init() {
5762
if(monitor_port) monitor_port->println(F("MOT: Enable driver."));
5863
enable();
5964
_delay(500);
65+
motor_status = FOCMotorStatus::motor_uncalibrated;
6066
}
6167

6268

@@ -87,6 +93,9 @@ void BLDCMotor::enable()
8793
// FOC initialization function
8894
int BLDCMotor::initFOC( float zero_electric_offset, Direction _sensor_direction) {
8995
int exit_flag = 1;
96+
97+
motor_status = FOCMotorStatus::motor_calibrating;
98+
9099
// align motor if necessary
91100
// alignment necessary for encoders!
92101
if(_isset(zero_electric_offset)){
@@ -117,9 +126,11 @@ int BLDCMotor::initFOC( float zero_electric_offset, Direction _sensor_direction
117126

118127
if(exit_flag){
119128
if(monitor_port) monitor_port->println(F("MOT: Ready."));
129+
motor_status = FOCMotorStatus::motor_ready;
120130
}else{
121131
if(monitor_port) monitor_port->println(F("MOT: Init FOC failed."));
122132
disable();
133+
motor_status = FOCMotorStatus::motor_calib_failed;
123134
}
124135

125136
return exit_flag;

src/StepperMotor.cpp

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,11 @@ void StepperMotor::linkDriver(StepperDriver* _driver) {
2525

2626
// init hardware pins
2727
void StepperMotor::init() {
28+
if (!driver || !driver->initialized) {
29+
motor_status = FOCMotorStatus::motor_init_failed;
30+
if(monitor_port) monitor_port->println(F("MOT: Init not possible, driver not initialized"));
31+
}
32+
motor_status = FOCMotorStatus::motor_initializing;
2833
if(monitor_port) monitor_port->println(F("MOT: Init"));
2934

3035
// if set the phase resistance of the motor use current limit to calculate the voltage limit
@@ -53,6 +58,7 @@ void StepperMotor::init() {
5358
enable();
5459
_delay(500);
5560

61+
motor_status = FOCMotorStatus::motor_uncalibrated;
5662
}
5763

5864

@@ -84,6 +90,9 @@ void StepperMotor::enable()
8490
// FOC initialization function
8591
int StepperMotor::initFOC( float zero_electric_offset, Direction _sensor_direction ) {
8692
int exit_flag = 1;
93+
94+
motor_status = FOCMotorStatus::motor_calibrating;
95+
8796
// align motor if necessary
8897
// alignment necessary for encoders!
8998
if(_isset(zero_electric_offset)){
@@ -105,8 +114,10 @@ int StepperMotor::initFOC( float zero_electric_offset, Direction _sensor_direct
105114

106115
if(exit_flag){
107116
if(monitor_port) monitor_port->println(F("MOT: Ready."));
117+
motor_status = FOCMotorStatus::motor_ready;
108118
}else{
109119
if(monitor_port) monitor_port->println(F("MOT: Init FOC failed."));
120+
motor_status = FOCMotorStatus::motor_calib_failed;
110121
disable();
111122
}
112123

src/common/base_classes/BLDCDriver.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@ class BLDCDriver{
2222
float dc_b; //!< currently set duty cycle on phaseB
2323
float dc_c; //!< currently set duty cycle on phaseC
2424

25+
bool initialized = false; // true if driver was successfully initialized
26+
2527
/**
2628
* Set phase voltages to the harware
2729
*

src/common/base_classes/FOCMotor.h

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,21 @@ enum FOCModulationType : uint8_t {
5151
Trapezoid_150 = 0x03,
5252
};
5353

54+
55+
56+
enum FOCMotorStatus : uint8_t {
57+
motor_uninitialized = 0x00, //!< Motor is not yet initialized
58+
motor_initializing = 0x01, //!< Motor intiialization is in progress
59+
motor_uncalibrated = 0x02, //!< Motor is initialized, but not calibrated (open loop possible)
60+
motor_calibrating = 0x03, //!< Motor calibration in progress
61+
motor_ready = 0x04, //!< Motor is initialized and calibrated (closed loop possible)
62+
motor_error = 0x08, //!< Motor is in error state (recoverable, e.g. overcurrent protection active)
63+
motor_calib_failed = 0x0E, //!< Motor calibration failed (possibly recoverable)
64+
motor_init_failed = 0x0F, //!< Motor initialization failed (not recoverable)
65+
};
66+
67+
68+
5469
/**
5570
Generic motor class
5671
*/
@@ -153,6 +168,7 @@ class FOCMotor
153168

154169
// motor status vairables
155170
int8_t enabled = 0;//!< enabled or disabled motor flag
171+
FOCMotorStatus motor_status = FOCMotorStatus::motor_uninitialized; //!< motor status
156172

157173
// pwm modulation related variables
158174
FOCModulationType foc_modulation;//!< parameter derterniming modulation algorithm

src/common/base_classes/StepperDriver.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@ class StepperDriver{
1414
long pwm_frequency; //!< pwm frequency value in hertz
1515
float voltage_power_supply; //!< power supply voltage
1616
float voltage_limit; //!< limiting voltage set to the motor
17+
18+
bool initialized = false; // true if driver was successfully initialized
1719

1820
/**
1921
* Set phase voltages to the harware

src/drivers/BLDCDriver3PWM.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,7 @@ int BLDCDriver3PWM::init() {
5757
// Set the pwm frequency to the pins
5858
// hardware specific function - depending on driver and mcu
5959
_configure3PWM(pwm_frequency, pwmA, pwmB, pwmC);
60+
initialized = true; // TODO atm the api gives no feedback if initialization succeeded
6061
return 0;
6162
}
6263

src/drivers/BLDCDriver6PWM.cpp

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,9 @@ int BLDCDriver6PWM::init() {
5858

5959
// configure 6pwm
6060
// hardware specific function - depending on driver and mcu
61-
return _configure6PWM(pwm_frequency, dead_zone, pwmA_h,pwmA_l, pwmB_h,pwmB_l, pwmC_h,pwmC_l);
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;
6264
}
6365

6466
// Set voltage to the pwm pin

src/drivers/StepperDriver2PWM.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,7 @@ int StepperDriver2PWM::init() {
8080
// Set the pwm frequency to the pins
8181
// hardware specific function - depending on driver and mcu
8282
_configure2PWM(pwm_frequency, pwm1, pwm2);
83+
initialized = true; // TODO atm the api gives no feedback if initialization succeeded
8384
return 0;
8485
}
8586

src/drivers/StepperDriver4PWM.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@ int StepperDriver4PWM::init() {
5555
// Set the pwm frequency to the pins
5656
// hardware specific function - depending on driver and mcu
5757
_configure4PWM(pwm_frequency, pwm1A, pwm1B, pwm2A, pwm2B);
58+
initialized = true; // TODO atm the api gives no feedback if initialization succeeded
5859
return 0;
5960
}
6061

0 commit comments

Comments
 (0)