1
1
#include " BLDCMotor.h"
2
+ #include " ./communication/SimpleFOCDebug.h"
2
3
3
4
// BLDCMotor( int pp , float R)
4
5
// - pp - pole pair number
@@ -30,10 +31,11 @@ void BLDCMotor::linkDriver(BLDCDriver* _driver) {
30
31
void BLDCMotor::init () {
31
32
if (!driver || !driver->initialized ) {
32
33
motor_status = FOCMotorStatus::motor_init_failed;
33
- if (monitor_port) monitor_port->println (F (" MOT: Init not possible, driver not initialized" ));
34
+ SIMPLEFOC_DEBUG (" MOT: Init not possible, driver not initialized" );
35
+ return ;
34
36
}
35
37
motor_status = FOCMotorStatus::motor_initializing;
36
- if (monitor_port) monitor_port-> println ( F ( " MOT: Init" ) );
38
+ SIMPLEFOC_DEBUG ( " MOT: Init" );
37
39
38
40
// sanity check for the voltage limit configuration
39
41
if (voltage_limit > driver->voltage_limit ) voltage_limit = driver->voltage_limit ;
@@ -57,7 +59,7 @@ void BLDCMotor::init() {
57
59
58
60
_delay (500 );
59
61
// enable motor
60
- if (monitor_port) monitor_port-> println ( F ( " MOT: Enable driver." ) );
62
+ SIMPLEFOC_DEBUG ( " MOT: Enable driver." );
61
63
enable ();
62
64
_delay (500 );
63
65
motor_status = FOCMotorStatus::motor_uncalibrated;
@@ -111,24 +113,25 @@ int BLDCMotor::initFOC( float zero_electric_offset, Direction _sensor_direction
111
113
// added the shaft_angle update
112
114
sensor->update ();
113
115
shaft_angle = shaftAngle ();
114
- }else if (monitor_port) monitor_port->println (F (" MOT: No sensor." ));
116
+ }else
117
+ SIMPLEFOC_DEBUG (" MOT: No sensor." );
115
118
116
119
// aligning the current sensor - can be skipped
117
120
// checks if driver phases are the same as current sense phases
118
121
// and checks the direction of measuremnt.
119
122
_delay (500 );
120
123
if (exit_flag){
121
124
if (current_sense) exit_flag *= alignCurrentSense ();
122
- else if (monitor_port) monitor_port-> println ( F ( " MOT: No current sense." ) );
125
+ else SIMPLEFOC_DEBUG ( " MOT: No current sense." );
123
126
}
124
127
125
128
if (exit_flag){
126
- if (monitor_port) monitor_port-> println ( F ( " MOT: Ready." ) );
129
+ SIMPLEFOC_DEBUG ( " MOT: Ready." );
127
130
motor_status = FOCMotorStatus::motor_ready;
128
131
}else {
129
- if (monitor_port) monitor_port->println (F (" MOT: Init FOC failed." ));
130
- disable ();
132
+ SIMPLEFOC_DEBUG (" MOT: Init FOC failed." );
131
133
motor_status = FOCMotorStatus::motor_calib_failed;
134
+ disable ();
132
135
}
133
136
134
137
return exit_flag;
@@ -138,18 +141,17 @@ int BLDCMotor::initFOC( float zero_electric_offset, Direction _sensor_direction
138
141
int BLDCMotor::alignCurrentSense () {
139
142
int exit_flag = 1 ; // success
140
143
141
- if (monitor_port) monitor_port-> println ( F ( " MOT: Align current sense." ) );
144
+ SIMPLEFOC_DEBUG ( " MOT: Align current sense." );
142
145
143
146
// align current sense and the driver
144
147
exit_flag = current_sense->driverAlign (voltage_sensor_align);
145
148
if (!exit_flag){
146
149
// error in current sense - phase either not measured or bad connection
147
- if (monitor_port) monitor_port-> println ( F ( " MOT: Align error!" ) );
150
+ SIMPLEFOC_DEBUG ( " MOT: Align error!" );
148
151
exit_flag = 0 ;
149
152
}else {
150
153
// output the alignment status flag
151
- if (monitor_port) monitor_port->print (F (" MOT: Success: " ));
152
- if (monitor_port) monitor_port->println (exit_flag);
154
+ SIMPLEFOC_DEBUG (" MOT: Success: " , exit_flag);
153
155
}
154
156
155
157
return exit_flag > 0 ;
@@ -158,7 +160,7 @@ int BLDCMotor::alignCurrentSense() {
158
160
// Encoder alignment to electrical 0 angle
159
161
int BLDCMotor::alignSensor () {
160
162
int exit_flag = 1 ; // success
161
- if (monitor_port) monitor_port-> println ( F ( " MOT: Align sensor." ) );
163
+ SIMPLEFOC_DEBUG ( " MOT: Align sensor." );
162
164
163
165
// if unknown natural direction
164
166
if (!_isset (sensor_direction)){
@@ -189,24 +191,23 @@ int BLDCMotor::alignSensor() {
189
191
_delay (200 );
190
192
// determine the direction the sensor moved
191
193
if (mid_angle == end_angle) {
192
- if (monitor_port) monitor_port-> println ( F ( " MOT: Failed to notice movement" ) );
194
+ SIMPLEFOC_DEBUG ( " MOT: Failed to notice movement" );
193
195
return 0 ; // failed calibration
194
196
} else if (mid_angle < end_angle) {
195
- if (monitor_port) monitor_port-> println ( F ( " MOT: sensor_direction==CCW" ) );
197
+ SIMPLEFOC_DEBUG ( " MOT: sensor_direction==CCW" );
196
198
sensor_direction = Direction::CCW;
197
199
} else {
198
- if (monitor_port) monitor_port-> println ( F ( " MOT: sensor_direction==CW" ) );
200
+ SIMPLEFOC_DEBUG ( " MOT: sensor_direction==CW" );
199
201
sensor_direction = Direction::CW;
200
202
}
201
203
// check pole pair number
202
- if (monitor_port) monitor_port->print (F (" MOT: PP check: " ));
203
204
float moved = fabs (mid_angle - end_angle);
204
205
if ( fabs (moved*pole_pairs - _2PI) > 0 .5f ) { // 0.5f is arbitrary number it can be lower or higher!
205
- if (monitor_port) monitor_port-> print ( F ( " fail - estimated pp:" ) );
206
- if (monitor_port) monitor_port-> println (_2PI/moved, 4 );
207
- } else if (monitor_port) monitor_port-> println ( F ( " OK!" ) );
206
+ SIMPLEFOC_DEBUG ( " MOT: PP check: fail - estimated pp: " , _2PI/moved );
207
+ } else
208
+ SIMPLEFOC_DEBUG ( " MOT: PP check: OK!" );
208
209
209
- }else if (monitor_port) monitor_port-> println ( F ( " MOT: Skip dir calib." ) );
210
+ } else SIMPLEFOC_DEBUG ( " MOT: Skip dir calib." );
210
211
211
212
// zero electric angle not known
212
213
if (!_isset (zero_electric_angle)){
@@ -222,13 +223,12 @@ int BLDCMotor::alignSensor() {
222
223
// zero_electric_angle = _normalizeAngle(_electricalAngle(sensor_direction*sensor->getAngle(), pole_pairs));
223
224
_delay (20 );
224
225
if (monitor_port){
225
- monitor_port->print (F (" MOT: Zero elec. angle: " ));
226
- monitor_port->println (zero_electric_angle);
226
+ SIMPLEFOC_DEBUG (" MOT: Zero elec. angle: " , zero_electric_angle);
227
227
}
228
228
// stop everything
229
229
setPhaseVoltage (0 , 0 , 0 );
230
230
_delay (200 );
231
- }else if (monitor_port) monitor_port-> println ( F ( " MOT: Skip offset calib." ) );
231
+ }else SIMPLEFOC_DEBUG ( " MOT: Skip offset calib." );
232
232
return exit_flag;
233
233
}
234
234
@@ -237,7 +237,7 @@ int BLDCMotor::alignSensor() {
237
237
int BLDCMotor::absoluteZeroSearch () {
238
238
// sensor precision: this is all ok, as the search happens near the 0-angle, where the precision
239
239
// of float is sufficient.
240
- if (monitor_port) monitor_port-> println ( F ( " MOT: Index search..." ) );
240
+ SIMPLEFOC_DEBUG ( " MOT: Index search..." );
241
241
// search the absolute zero with small velocity
242
242
float limit_vel = velocity_limit;
243
243
float limit_volt = voltage_limit;
@@ -257,8 +257,8 @@ int BLDCMotor::absoluteZeroSearch() {
257
257
voltage_limit = limit_volt;
258
258
// check if the zero found
259
259
if (monitor_port){
260
- if (sensor->needsSearch ()) monitor_port-> println ( F ( " MOT: Error: Not found!" ) );
261
- else monitor_port-> println ( F ( " MOT: Success!" ) );
260
+ if (sensor->needsSearch ()) SIMPLEFOC_DEBUG ( " MOT: Error: Not found!" );
261
+ else SIMPLEFOC_DEBUG ( " MOT: Success!" );
262
262
}
263
263
return !sensor->needsSearch ();
264
264
}
@@ -307,7 +307,7 @@ void BLDCMotor::loopFOC() {
307
307
break ;
308
308
default :
309
309
// no torque control selected
310
- if (monitor_port) monitor_port-> println ( F ( " MOT: no torque control selected!" ) );
310
+ SIMPLEFOC_DEBUG ( " MOT: no torque control selected!" );
311
311
break ;
312
312
}
313
313
0 commit comments