Skip to content

Commit

Permalink
added support for openloop in the initFOC #382
Browse files Browse the repository at this point in the history
  • Loading branch information
askuric committed Apr 19, 2024
1 parent 96b0804 commit 977b141
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 24 deletions.
50 changes: 30 additions & 20 deletions src/BLDCMotor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -156,25 +156,31 @@ int BLDCMotor::initFOC() {
// added the shaft_angle update
sensor->update();
shaft_angle = shaftAngle();
}else {
exit_flag = 0; // no FOC without sensor
SIMPLEFOC_DEBUG("MOT: No sensor.");
}

// aligning the current sensor - can be skipped
// checks if driver phases are the same as current sense phases
// and checks the direction of measuremnt.
if(exit_flag){
if(current_sense){
if (!current_sense->initialized) {
motor_status = FOCMotorStatus::motor_calib_failed;
SIMPLEFOC_DEBUG("MOT: Init FOC error, current sense not initialized");
exit_flag = 0;
}else{
exit_flag *= alignCurrentSense();
// aligning the current sensor - can be skipped
// checks if driver phases are the same as current sense phases
// and checks the direction of measuremnt.
if(exit_flag){
if(current_sense){
if (!current_sense->initialized) {
motor_status = FOCMotorStatus::motor_calib_failed;
SIMPLEFOC_DEBUG("MOT: Init FOC error, current sense not initialized");
exit_flag = 0;
}else{
exit_flag *= alignCurrentSense();
}
}
else { SIMPLEFOC_DEBUG("MOT: No current sense."); }
}

} else {
SIMPLEFOC_DEBUG("MOT: No sensor.");
if ((controller == MotionControlType::angle_openloop || controller == MotionControlType::velocity_openloop)){
exit_flag = 1;
SIMPLEFOC_DEBUG("MOT: Openloop only!");
}else{
exit_flag = 0; // no FOC without sensor
}
else { SIMPLEFOC_DEBUG("MOT: No current sense."); }
}

if(exit_flag){
Expand Down Expand Up @@ -219,14 +225,18 @@ int BLDCMotor::alignSensor() {
// stop init if not found index
if(!exit_flag) return exit_flag;

// v2.3.3 fix for R_AVR_7_PCREL against symbol" bug for AVR boards
// TODO figure out why this works
float voltage_align = voltage_sensor_align;

// if unknown natural direction
if(sensor_direction==Direction::UNKNOWN){

// find natural direction
// move one electrical revolution forward
for (int i = 0; i <=500; i++ ) {
float angle = _3PI_2 + _2PI * i / 500.0f;
setPhaseVoltage(voltage_sensor_align, 0, angle);
setPhaseVoltage(voltage_align, 0, angle);
sensor->update();
_delay(2);
}
Expand All @@ -236,13 +246,13 @@ int BLDCMotor::alignSensor() {
// move one electrical revolution backwards
for (int i = 500; i >=0; i-- ) {
float angle = _3PI_2 + _2PI * i / 500.0f ;
setPhaseVoltage(voltage_sensor_align, 0, angle);
setPhaseVoltage(voltage_align, 0, angle);
sensor->update();
_delay(2);
}
sensor->update();
float end_angle = sensor->getAngle();
setPhaseVoltage(0, 0, 0);
// setPhaseVoltage(0, 0, 0);
_delay(200);
// determine the direction the sensor moved
float moved = fabs(mid_angle - end_angle);
Expand Down Expand Up @@ -270,7 +280,7 @@ int BLDCMotor::alignSensor() {
if(!_isset(zero_electric_angle)){
// align the electrical phases of the motor and sensor
// set angle -90(270 = 3PI/2) degrees
setPhaseVoltage(voltage_sensor_align, 0, _3PI_2);
setPhaseVoltage(voltage_align, 0, _3PI_2);
_delay(700);
// read the sensor
sensor->update();
Expand Down
20 changes: 16 additions & 4 deletions src/StepperMotor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,15 @@ int StepperMotor::initFOC() {
// added the shaft_angle update
sensor->update();
shaft_angle = sensor->getAngle();
} else { SIMPLEFOC_DEBUG("MOT: No sensor."); }
} else {
SIMPLEFOC_DEBUG("MOT: No sensor.");
if ((controller == MotionControlType::angle_openloop || controller == MotionControlType::velocity_openloop)){
exit_flag = 1;
SIMPLEFOC_DEBUG("MOT: Openloop only!");
}else{
exit_flag = 0; // no FOC without sensor
}
}

if(exit_flag){
SIMPLEFOC_DEBUG("MOT: Ready.");
Expand All @@ -133,6 +141,10 @@ int StepperMotor::alignSensor() {
int exit_flag = 1; //success
SIMPLEFOC_DEBUG("MOT: Align sensor.");

// v2.3.3 fix for R_AVR_7_PCREL against symbol" bug for AVR boards
// TODO figure out why this works
float voltage_align = voltage_sensor_align;

// if unknown natural direction
if(sensor_direction == Direction::UNKNOWN){
// check if sensor needs zero search
Expand All @@ -144,7 +156,7 @@ int StepperMotor::alignSensor() {
// move one electrical revolution forward
for (int i = 0; i <=500; i++ ) {
float angle = _3PI_2 + _2PI * i / 500.0f;
setPhaseVoltage(voltage_sensor_align, 0, angle);
setPhaseVoltage(voltage_align, 0, angle);
sensor->update();
_delay(2);
}
Expand All @@ -154,7 +166,7 @@ int StepperMotor::alignSensor() {
// move one electrical revolution backwards
for (int i = 500; i >=0; i-- ) {
float angle = _3PI_2 + _2PI * i / 500.0f ;
setPhaseVoltage(voltage_sensor_align, 0, angle);
setPhaseVoltage(voltage_align, 0, angle);
sensor->update();
_delay(2);
}
Expand Down Expand Up @@ -190,7 +202,7 @@ int StepperMotor::alignSensor() {
if(!_isset(zero_electric_angle)){
// align the electrical phases of the motor and sensor
// set angle -90(270 = 3PI/2) degrees
setPhaseVoltage(voltage_sensor_align, 0, _3PI_2);
setPhaseVoltage(voltage_align, 0, _3PI_2);
_delay(700);
// read the sensor
sensor->update();
Expand Down

0 comments on commit 977b141

Please sign in to comment.