Skip to content

Commit 977b141

Browse files
committed
added support for openloop in the initFOC #382
1 parent 96b0804 commit 977b141

File tree

2 files changed

+46
-24
lines changed

2 files changed

+46
-24
lines changed

src/BLDCMotor.cpp

Lines changed: 30 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -156,25 +156,31 @@ int BLDCMotor::initFOC() {
156156
// added the shaft_angle update
157157
sensor->update();
158158
shaft_angle = shaftAngle();
159-
}else {
160-
exit_flag = 0; // no FOC without sensor
161-
SIMPLEFOC_DEBUG("MOT: No sensor.");
162-
}
163159

164-
// aligning the current sensor - can be skipped
165-
// checks if driver phases are the same as current sense phases
166-
// and checks the direction of measuremnt.
167-
if(exit_flag){
168-
if(current_sense){
169-
if (!current_sense->initialized) {
170-
motor_status = FOCMotorStatus::motor_calib_failed;
171-
SIMPLEFOC_DEBUG("MOT: Init FOC error, current sense not initialized");
172-
exit_flag = 0;
173-
}else{
174-
exit_flag *= alignCurrentSense();
160+
// aligning the current sensor - can be skipped
161+
// checks if driver phases are the same as current sense phases
162+
// and checks the direction of measuremnt.
163+
if(exit_flag){
164+
if(current_sense){
165+
if (!current_sense->initialized) {
166+
motor_status = FOCMotorStatus::motor_calib_failed;
167+
SIMPLEFOC_DEBUG("MOT: Init FOC error, current sense not initialized");
168+
exit_flag = 0;
169+
}else{
170+
exit_flag *= alignCurrentSense();
171+
}
175172
}
173+
else { SIMPLEFOC_DEBUG("MOT: No current sense."); }
174+
}
175+
176+
} else {
177+
SIMPLEFOC_DEBUG("MOT: No sensor.");
178+
if ((controller == MotionControlType::angle_openloop || controller == MotionControlType::velocity_openloop)){
179+
exit_flag = 1;
180+
SIMPLEFOC_DEBUG("MOT: Openloop only!");
181+
}else{
182+
exit_flag = 0; // no FOC without sensor
176183
}
177-
else { SIMPLEFOC_DEBUG("MOT: No current sense."); }
178184
}
179185

180186
if(exit_flag){
@@ -219,14 +225,18 @@ int BLDCMotor::alignSensor() {
219225
// stop init if not found index
220226
if(!exit_flag) return exit_flag;
221227

228+
// v2.3.3 fix for R_AVR_7_PCREL against symbol" bug for AVR boards
229+
// TODO figure out why this works
230+
float voltage_align = voltage_sensor_align;
231+
222232
// if unknown natural direction
223233
if(sensor_direction==Direction::UNKNOWN){
224234

225235
// find natural direction
226236
// move one electrical revolution forward
227237
for (int i = 0; i <=500; i++ ) {
228238
float angle = _3PI_2 + _2PI * i / 500.0f;
229-
setPhaseVoltage(voltage_sensor_align, 0, angle);
239+
setPhaseVoltage(voltage_align, 0, angle);
230240
sensor->update();
231241
_delay(2);
232242
}
@@ -236,13 +246,13 @@ int BLDCMotor::alignSensor() {
236246
// move one electrical revolution backwards
237247
for (int i = 500; i >=0; i-- ) {
238248
float angle = _3PI_2 + _2PI * i / 500.0f ;
239-
setPhaseVoltage(voltage_sensor_align, 0, angle);
249+
setPhaseVoltage(voltage_align, 0, angle);
240250
sensor->update();
241251
_delay(2);
242252
}
243253
sensor->update();
244254
float end_angle = sensor->getAngle();
245-
setPhaseVoltage(0, 0, 0);
255+
// setPhaseVoltage(0, 0, 0);
246256
_delay(200);
247257
// determine the direction the sensor moved
248258
float moved = fabs(mid_angle - end_angle);
@@ -270,7 +280,7 @@ int BLDCMotor::alignSensor() {
270280
if(!_isset(zero_electric_angle)){
271281
// align the electrical phases of the motor and sensor
272282
// set angle -90(270 = 3PI/2) degrees
273-
setPhaseVoltage(voltage_sensor_align, 0, _3PI_2);
283+
setPhaseVoltage(voltage_align, 0, _3PI_2);
274284
_delay(700);
275285
// read the sensor
276286
sensor->update();

src/StepperMotor.cpp

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -114,7 +114,15 @@ int StepperMotor::initFOC() {
114114
// added the shaft_angle update
115115
sensor->update();
116116
shaft_angle = sensor->getAngle();
117-
} else { SIMPLEFOC_DEBUG("MOT: No sensor."); }
117+
} else {
118+
SIMPLEFOC_DEBUG("MOT: No sensor.");
119+
if ((controller == MotionControlType::angle_openloop || controller == MotionControlType::velocity_openloop)){
120+
exit_flag = 1;
121+
SIMPLEFOC_DEBUG("MOT: Openloop only!");
122+
}else{
123+
exit_flag = 0; // no FOC without sensor
124+
}
125+
}
118126

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

144+
// v2.3.3 fix for R_AVR_7_PCREL against symbol" bug for AVR boards
145+
// TODO figure out why this works
146+
float voltage_align = voltage_sensor_align;
147+
136148
// if unknown natural direction
137149
if(sensor_direction == Direction::UNKNOWN){
138150
// check if sensor needs zero search
@@ -144,7 +156,7 @@ int StepperMotor::alignSensor() {
144156
// move one electrical revolution forward
145157
for (int i = 0; i <=500; i++ ) {
146158
float angle = _3PI_2 + _2PI * i / 500.0f;
147-
setPhaseVoltage(voltage_sensor_align, 0, angle);
159+
setPhaseVoltage(voltage_align, 0, angle);
148160
sensor->update();
149161
_delay(2);
150162
}
@@ -154,7 +166,7 @@ int StepperMotor::alignSensor() {
154166
// move one electrical revolution backwards
155167
for (int i = 500; i >=0; i-- ) {
156168
float angle = _3PI_2 + _2PI * i / 500.0f ;
157-
setPhaseVoltage(voltage_sensor_align, 0, angle);
169+
setPhaseVoltage(voltage_align, 0, angle);
158170
sensor->update();
159171
_delay(2);
160172
}
@@ -190,7 +202,7 @@ int StepperMotor::alignSensor() {
190202
if(!_isset(zero_electric_angle)){
191203
// align the electrical phases of the motor and sensor
192204
// set angle -90(270 = 3PI/2) degrees
193-
setPhaseVoltage(voltage_sensor_align, 0, _3PI_2);
205+
setPhaseVoltage(voltage_align, 0, _3PI_2);
194206
_delay(700);
195207
// read the sensor
196208
sensor->update();

0 commit comments

Comments
 (0)