Skip to content

support motor intialization status tracking #143

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Dec 18, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions src/BLDCMotor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,11 @@ void BLDCMotor::linkDriver(BLDCDriver* _driver) {

// init hardware pins
void BLDCMotor::init() {
if (!driver || !driver->initialized) {
motor_status = FOCMotorStatus::motor_init_failed;
if(monitor_port) monitor_port->println(F("MOT: Init not possible, driver not initialized"));
}
motor_status = FOCMotorStatus::motor_initializing;
if(monitor_port) monitor_port->println(F("MOT: Init"));

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


Expand Down Expand Up @@ -87,6 +93,9 @@ void BLDCMotor::enable()
// FOC initialization function
int BLDCMotor::initFOC( float zero_electric_offset, Direction _sensor_direction) {
int exit_flag = 1;

motor_status = FOCMotorStatus::motor_calibrating;

// align motor if necessary
// alignment necessary for encoders!
if(_isset(zero_electric_offset)){
Expand Down Expand Up @@ -117,9 +126,11 @@ int BLDCMotor::initFOC( float zero_electric_offset, Direction _sensor_direction

if(exit_flag){
if(monitor_port) monitor_port->println(F("MOT: Ready."));
motor_status = FOCMotorStatus::motor_ready;
}else{
if(monitor_port) monitor_port->println(F("MOT: Init FOC failed."));
disable();
motor_status = FOCMotorStatus::motor_calib_failed;
}

return exit_flag;
Expand Down
11 changes: 11 additions & 0 deletions src/StepperMotor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,11 @@ void StepperMotor::linkDriver(StepperDriver* _driver) {

// init hardware pins
void StepperMotor::init() {
if (!driver || !driver->initialized) {
motor_status = FOCMotorStatus::motor_init_failed;
if(monitor_port) monitor_port->println(F("MOT: Init not possible, driver not initialized"));
}
motor_status = FOCMotorStatus::motor_initializing;
if(monitor_port) monitor_port->println(F("MOT: Init"));

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

motor_status = FOCMotorStatus::motor_uncalibrated;
}


Expand Down Expand Up @@ -84,6 +90,9 @@ void StepperMotor::enable()
// FOC initialization function
int StepperMotor::initFOC( float zero_electric_offset, Direction _sensor_direction ) {
int exit_flag = 1;

motor_status = FOCMotorStatus::motor_calibrating;

// align motor if necessary
// alignment necessary for encoders!
if(_isset(zero_electric_offset)){
Expand All @@ -105,8 +114,10 @@ int StepperMotor::initFOC( float zero_electric_offset, Direction _sensor_direct

if(exit_flag){
if(monitor_port) monitor_port->println(F("MOT: Ready."));
motor_status = FOCMotorStatus::motor_ready;
}else{
if(monitor_port) monitor_port->println(F("MOT: Init FOC failed."));
motor_status = FOCMotorStatus::motor_calib_failed;
disable();
}

Expand Down
2 changes: 2 additions & 0 deletions src/common/base_classes/BLDCDriver.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ class BLDCDriver{
float dc_b; //!< currently set duty cycle on phaseB
float dc_c; //!< currently set duty cycle on phaseC

bool initialized = false; // true if driver was successfully initialized

/**
* Set phase voltages to the harware
*
Expand Down
16 changes: 16 additions & 0 deletions src/common/base_classes/FOCMotor.h
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,21 @@ enum FOCModulationType : uint8_t {
Trapezoid_150 = 0x03,
};



enum FOCMotorStatus : uint8_t {
motor_uninitialized = 0x00, //!< Motor is not yet initialized
motor_initializing = 0x01, //!< Motor intiialization is in progress
motor_uncalibrated = 0x02, //!< Motor is initialized, but not calibrated (open loop possible)
motor_calibrating = 0x03, //!< Motor calibration in progress
motor_ready = 0x04, //!< Motor is initialized and calibrated (closed loop possible)
motor_error = 0x08, //!< Motor is in error state (recoverable, e.g. overcurrent protection active)
motor_calib_failed = 0x0E, //!< Motor calibration failed (possibly recoverable)
motor_init_failed = 0x0F, //!< Motor initialization failed (not recoverable)
};



/**
Generic motor class
*/
Expand Down Expand Up @@ -153,6 +168,7 @@ class FOCMotor

// motor status vairables
int8_t enabled = 0;//!< enabled or disabled motor flag
FOCMotorStatus motor_status = FOCMotorStatus::motor_uninitialized; //!< motor status

// pwm modulation related variables
FOCModulationType foc_modulation;//!< parameter derterniming modulation algorithm
Expand Down
2 changes: 2 additions & 0 deletions src/common/base_classes/StepperDriver.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ class StepperDriver{
long pwm_frequency; //!< pwm frequency value in hertz
float voltage_power_supply; //!< power supply voltage
float voltage_limit; //!< limiting voltage set to the motor

bool initialized = false; // true if driver was successfully initialized

/**
* Set phase voltages to the harware
Expand Down
1 change: 1 addition & 0 deletions src/drivers/BLDCDriver3PWM.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ int BLDCDriver3PWM::init() {
// Set the pwm frequency to the pins
// hardware specific function - depending on driver and mcu
_configure3PWM(pwm_frequency, pwmA, pwmB, pwmC);
initialized = true; // TODO atm the api gives no feedback if initialization succeeded
return 0;
}

Expand Down
4 changes: 3 additions & 1 deletion src/drivers/BLDCDriver6PWM.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,9 @@ int BLDCDriver6PWM::init() {

// configure 6pwm
// hardware specific function - depending on driver and mcu
return _configure6PWM(pwm_frequency, dead_zone, pwmA_h,pwmA_l, pwmB_h,pwmB_l, pwmC_h,pwmC_l);
int result = _configure6PWM(pwm_frequency, dead_zone, pwmA_h,pwmA_l, pwmB_h,pwmB_l, pwmC_h,pwmC_l);
initialized = (result==0);
return result;
}

// Set voltage to the pwm pin
Expand Down
1 change: 1 addition & 0 deletions src/drivers/StepperDriver2PWM.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,7 @@ int StepperDriver2PWM::init() {
// Set the pwm frequency to the pins
// hardware specific function - depending on driver and mcu
_configure2PWM(pwm_frequency, pwm1, pwm2);
initialized = true; // TODO atm the api gives no feedback if initialization succeeded
return 0;
}

Expand Down
1 change: 1 addition & 0 deletions src/drivers/StepperDriver4PWM.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ int StepperDriver4PWM::init() {
// Set the pwm frequency to the pins
// hardware specific function - depending on driver and mcu
_configure4PWM(pwm_frequency, pwm1A, pwm1B, pwm2A, pwm2B);
initialized = true; // TODO atm the api gives no feedback if initialization succeeded
return 0;
}

Expand Down