Skip to content

Commit b3b4dc2

Browse files
committed
find direction of the second sensor too
1 parent 905eda6 commit b3b4dc2

2 files changed

Lines changed: 31 additions & 8 deletions

File tree

src/common/base_classes/FOCMotor.cpp

Lines changed: 30 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -714,7 +714,8 @@ void FOCMotor::move(float new_target) {
714714
updateMotionControlTime();
715715

716716
// update the additional sensor values
717-
if (position_loop_sensor) position_loop_sensor->update();
717+
// if the position loop sensor is different from the main sensor
718+
if (position_loop_sensor != sensor) position_loop_sensor->update();
718719

719720
// read value even if motor is disabled to keep the monitoring updated
720721
// except for the open loop modes where the values are updated within angle/velocityOpenLoop functions
@@ -813,10 +814,7 @@ int FOCMotor::initFOC() {
813814
exit_flag *= alignSensor();
814815
// added the shaft_angle update
815816
sensor->update();
816-
//if(position_loop_sensor_direction == Direction::UNKNOWN) position_loop_sensor_direction = sensor_direction;
817-
SIMPLEFOC_MOTOR_DEBUG("Sensor aligned.");
818817
position_loop_sensor->update();
819-
SIMPLEFOC_MOTOR_DEBUG("Sensor aligned1.");
820818
shaft_angle = shaftAngle();
821819
} else {
822820
SIMPLEFOC_MOTOR_DEBUG("No sensor.");
@@ -890,29 +888,40 @@ int FOCMotor::alignSensor() {
890888
// TODO figure out why this works
891889
float voltage_align = voltage_sensor_align;
892890

893-
// if unknown natural direction
894-
if(sensor_direction == Direction::UNKNOWN){
891+
// if unknown natural direction for FOC sensor or position loop sensor, do the direction search
892+
if(sensor_direction == Direction::UNKNOWN ||
893+
(position_loop_sensor != sensor && position_loop_sensor_direction == Direction::UNKNOWN) ) {
895894

896895
// find natural direction
897896
// move one electrical revolution forward
898897
for (int i = 0; i <=500; i++ ) {
899898
float angle = _3PI_2 + _2PI * i / 500.0f;
900899
setPhaseVoltage(voltage_align, 0, angle);
901900
sensor->update();
901+
if(position_loop_sensor != sensor) position_loop_sensor->update();
902902
_delay(2);
903903
}
904-
// take and angle in the middle
904+
// take and angle in the middle for FOC
905905
sensor->update();
906906
float mid_angle = sensor->getAngle();
907+
// take the same position for additional position loop sensor if it is different from the main sensor
908+
if(position_loop_sensor != sensor) position_loop_sensor->update();
909+
float mid_angle_position_loop = position_loop_sensor->getAngle();
910+
907911
// move one electrical revolution backwards
908912
for (int i = 500; i >=0; i-- ) {
909913
float angle = _3PI_2 + _2PI * i / 500.0f ;
910914
setPhaseVoltage(voltage_align, 0, angle);
911915
sensor->update();
916+
if(position_loop_sensor != sensor) position_loop_sensor->update();
912917
_delay(2);
913918
}
919+
// for FOC
914920
sensor->update();
915921
float end_angle = sensor->getAngle();
922+
// take the same position for additional position loop sensor if it is different from the main sensor
923+
if(position_loop_sensor != sensor) position_loop_sensor->update();
924+
float end_angle_position_loop = position_loop_sensor->getAngle();
916925
// setPhaseVoltage(0, 0, 0);
917926
_delay(200);
918927
// determine the direction the sensor moved
@@ -927,6 +936,20 @@ int FOCMotor::alignSensor() {
927936
SIMPLEFOC_MOTOR_DEBUG("sensor dir: CW");
928937
sensor_direction = Direction::CW;
929938
}
939+
// additional position loop sensor if available
940+
// and needs direction check
941+
if (position_loop_sensor != sensor && position_loop_sensor_direction == Direction::UNKNOWN) {
942+
// check if the additional position loop sensor moved in the same direction
943+
if (fabsf(mid_angle_position_loop - end_angle_position_loop) < 1e-5) { // minimum angle to detect movement
944+
SIMPLEFOC_MOTOR_WARN("Failed to notice movement in position loop sensor");
945+
} else if (mid_angle_position_loop < end_angle_position_loop) {
946+
SIMPLEFOC_MOTOR_DEBUG("pos sensor dir: CCW");
947+
position_loop_sensor_direction = Direction::CCW;
948+
} else{
949+
SIMPLEFOC_MOTOR_DEBUG("pos sensor dir: CW");
950+
position_loop_sensor_direction = Direction::CW;
951+
}
952+
}
930953
// check pole pair number
931954
pp_check_result = !(fabsf(moved*pole_pairs - _2PI) > 0.5f); // 0.5f is arbitrary number it can be lower or higher!
932955
if( pp_check_result==false ) {

src/common/base_classes/FOCMotor.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -172,7 +172,7 @@ class FOCMotor
172172
*
173173
* @param sensor Sensor class wrapper for the FOC algorithm to read the motor angle and velocity
174174
*/
175-
void linkPositionLoopSensor(Sensor* sensor, Direction direction = Direction::CW);
175+
void linkPositionLoopSensor(Sensor* sensor, Direction direction = Direction::UNKNOWN);
176176

177177
/**
178178
* Function linking a motor and current sensing

0 commit comments

Comments
 (0)