Skip to content

Commit 922ee50

Browse files
Merge pull request #283 from runger1101001/dev
Change init logic - remove params from initFOC
2 parents 33a381d + 10c5b87 commit 922ee50

File tree

7 files changed

+23
-33
lines changed

7 files changed

+23
-33
lines changed

examples/utils/calibration/find_sensor_offset_and_direction/find_sensor_offset_and_direction.ino

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,9 @@
22
* Simple example intended to help users find the zero offset and natural direction of the sensor.
33
*
44
* These values can further be used to avoid motor and sensor alignment procedure.
5-
*
6-
* motor.initFOC(zero_offset, sensor_direction);
5+
* To use these values add them to the code:");
6+
* motor.sensor_direction=Direction::CW; // or Direction::CCW
7+
* motor.zero_electric_angle=1.2345; // use the real value!
78
*
89
* This will only work for abosolute value sensors - magnetic sensors.
910
* Bypassing the alignment procedure is not possible for the encoders and for the current implementation of the Hall sensors.
@@ -44,6 +45,9 @@ void setup() {
4445
// set motion control loop to be used
4546
motor.controller = MotionControlType::torque;
4647

48+
// force direction search - because default is CW
49+
motor.sensor_direction = Direction::UNKNOWN;
50+
4751
// initialize motor
4852
motor.init();
4953
// align sensor and start FOC
@@ -54,9 +58,16 @@ void setup() {
5458
Serial.println("Sensor zero offset is:");
5559
Serial.println(motor.zero_electric_angle, 4);
5660
Serial.println("Sensor natural direction is: ");
57-
Serial.println(motor.sensor_direction == 1 ? "Direction::CW" : "Direction::CCW");
61+
Serial.println(motor.sensor_direction == Direction::CW ? "Direction::CW" : "Direction::CCW");
62+
63+
Serial.println("To use these values add them to the code:");
64+
Serial.print(" motor.sensor_direction=");
65+
Serial.print(motor.sensor_direction == Direction::CW ? "Direction::CW" : "Direction::CCW");
66+
Serial.println(";");
67+
Serial.print(" motor.zero_electric_angle=");
68+
Serial.print(motor.zero_electric_angle, 4);
69+
Serial.println(";");
5870

59-
Serial.println("To use these values provide them to the: motor.initFOC(offset, direction)");
6071
_delay(1000);
6172
Serial.println("If motor is not moving the alignment procedure was not successfull!!");
6273
}

src/BLDCMotor.cpp

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -126,20 +126,13 @@ void BLDCMotor::enable()
126126
FOC functions
127127
*/
128128
// FOC initialization function
129-
int BLDCMotor::initFOC( float zero_electric_offset, Direction _sensor_direction) {
129+
int BLDCMotor::initFOC() {
130130
int exit_flag = 1;
131131

132132
motor_status = FOCMotorStatus::motor_calibrating;
133133

134134
// align motor if necessary
135135
// alignment necessary for encoders!
136-
if(_isset(zero_electric_offset)){
137-
// abosolute zero offset provided - no need to align
138-
zero_electric_angle = zero_electric_offset;
139-
// set the sensor direction - default CW
140-
sensor_direction = _sensor_direction;
141-
}
142-
143136
// sensor and motor alignment - can be skipped
144137
// by setting motor.sensor_direction and motor.zero_electric_angle
145138
_delay(500);
@@ -213,7 +206,7 @@ int BLDCMotor::alignSensor() {
213206
if(!exit_flag) return exit_flag;
214207

215208
// if unknown natural direction
216-
if(!_isset(sensor_direction)){
209+
if(sensor_direction==Direction::UNKNOWN){
217210

218211
// find natural direction
219212
// move one electrical revolution forward

src/BLDCMotor.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ class BLDCMotor: public FOCMotor
4949
* Function initializing FOC algorithm
5050
* and aligning sensor's and motors' zero position
5151
*/
52-
int initFOC( float zero_electric_offset = NOT_SET , Direction sensor_direction = Direction::CW) override;
52+
int initFOC() override;
5353
/**
5454
* Function running FOC algorithm in real-time
5555
* it calculates the gets motor angle and sets the appropriate voltages

src/StepperMotor.cpp

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -92,20 +92,13 @@ void StepperMotor::enable()
9292
FOC functions
9393
*/
9494
// FOC initialization function
95-
int StepperMotor::initFOC( float zero_electric_offset, Direction _sensor_direction ) {
95+
int StepperMotor::initFOC() {
9696
int exit_flag = 1;
9797

9898
motor_status = FOCMotorStatus::motor_calibrating;
9999

100100
// align motor if necessary
101101
// alignment necessary for encoders!
102-
if(_isset(zero_electric_offset)){
103-
// abosolute zero offset provided - no need to align
104-
zero_electric_angle = zero_electric_offset;
105-
// set the sensor direction - default CW
106-
sensor_direction = _sensor_direction;
107-
}
108-
109102
// sensor and motor alignment - can be skipped
110103
// by setting motor.sensor_direction and motor.zero_electric_angle
111104
_delay(500);

src/StepperMotor.h

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -54,12 +54,8 @@ class StepperMotor: public FOCMotor
5454
* and aligning sensor's and motors' zero position
5555
*
5656
* - If zero_electric_offset parameter is set the alignment procedure is skipped
57-
*
58-
* @param zero_electric_offset value of the sensors absolute position electrical offset in respect to motor's electrical 0 position.
59-
* @param sensor_direction sensor natural direction - default is CW
60-
*
6157
*/
62-
int initFOC( float zero_electric_offset = NOT_SET , Direction sensor_direction = Direction::CW) override;
58+
int initFOC() override;
6359
/**
6460
* Function running FOC algorithm in real-time
6561
* it calculates the gets motor angle and sets the appropriate voltages

src/common/base_classes/FOCMotor.h

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -104,12 +104,8 @@ class FOCMotor
104104
* and aligning sensor's and motors' zero position
105105
*
106106
* - If zero_electric_offset parameter is set the alignment procedure is skipped
107-
*
108-
* @param zero_electric_offset value of the sensors absolute position electrical offset in respect to motor's electrical 0 position.
109-
* @param sensor_direction sensor natural direction - default is CW
110-
*
111107
*/
112-
virtual int initFOC( float zero_electric_offset = NOT_SET , Direction sensor_direction = Direction::CW)=0;
108+
virtual int initFOC()=0;
113109
/**
114110
* Function running FOC algorithm in real-time
115111
* it calculates the gets motor angle and sets the appropriate voltages
@@ -209,7 +205,7 @@ class FOCMotor
209205
// sensor related variabels
210206
float sensor_offset; //!< user defined sensor zero offset
211207
float zero_electric_angle = NOT_SET;//!< absolute zero electric angle - if available
212-
int sensor_direction = NOT_SET; //!< if sensor_direction == Direction::CCW then direction will be flipped to CW
208+
Direction sensor_direction = Direction::CW; //!< default is CW. if sensor_direction == Direction::CCW then direction will be flipped compared to CW. Set to UNKNOWN to set by calibration
213209

214210
/**
215211
* Function providing BLDCMotor class with the

src/communication/Commander.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -593,6 +593,7 @@ bool Commander::isSentinel(char ch)
593593
else if (ch == '\r')
594594
{
595595
printVerbose(F("Warn: \\r detected! \n"));
596+
return true; // lets still consider it to end the line...
596597
}
597598
return false;
598599
}

0 commit comments

Comments
 (0)