@@ -24,7 +24,7 @@ void BLDCMotor::linkDriver(BLDCDriver* _driver) {
24
24
25
25
// init hardware pins
26
26
void BLDCMotor::init () {
27
- if (monitor_port) monitor_port-> println ( F ( " MOT: Init" ) );
27
+ SIMPLEFOC_DEBUG ( " MOT: Init" );
28
28
29
29
// if no current sensing and the user has set the phase resistance of the motor use current limit to calculate the voltage limit
30
30
if ( !current_sense && _isset (phase_resistance)) {
@@ -54,7 +54,7 @@ void BLDCMotor::init() {
54
54
55
55
_delay (500 );
56
56
// enable motor
57
- if (monitor_port) monitor_port-> println ( F ( " MOT: Enable driver." ) );
57
+ SIMPLEFOC_DEBUG ( " MOT: Enable driver." );
58
58
enable ();
59
59
_delay (500 );
60
60
}
@@ -104,21 +104,22 @@ int BLDCMotor::initFOC( float zero_electric_offset, Direction _sensor_direction
104
104
// added the shaft_angle update
105
105
sensor->update ();
106
106
shaft_angle = shaftAngle ();
107
- }else if (monitor_port) monitor_port->println (F (" MOT: No sensor." ));
107
+ }else
108
+ SIMPLEFOC_DEBUG (" MOT: No sensor." );
108
109
109
110
// aligning the current sensor - can be skipped
110
111
// checks if driver phases are the same as current sense phases
111
112
// and checks the direction of measuremnt.
112
113
_delay (500 );
113
114
if (exit_flag){
114
115
if (current_sense) exit_flag *= alignCurrentSense ();
115
- else if (monitor_port) monitor_port-> println ( F ( " MOT: No current sense." ) );
116
+ else SIMPLEFOC_DEBUG ( " MOT: No current sense." );
116
117
}
117
118
118
119
if (exit_flag){
119
- if (monitor_port) monitor_port-> println ( F ( " MOT: Ready." ) );
120
+ SIMPLEFOC_DEBUG ( " MOT: Ready." );
120
121
}else {
121
- if (monitor_port) monitor_port-> println ( F ( " MOT: Init FOC failed." ) );
122
+ SIMPLEFOC_DEBUG ( " MOT: Init FOC failed." );
122
123
disable ();
123
124
}
124
125
@@ -129,18 +130,17 @@ int BLDCMotor::initFOC( float zero_electric_offset, Direction _sensor_direction
129
130
int BLDCMotor::alignCurrentSense () {
130
131
int exit_flag = 1 ; // success
131
132
132
- if (monitor_port) monitor_port-> println ( F ( " MOT: Align current sense." ) );
133
+ SIMPLEFOC_DEBUG ( " MOT: Align current sense." );
133
134
134
135
// align current sense and the driver
135
136
exit_flag = current_sense->driverAlign (driver, voltage_sensor_align);
136
137
if (!exit_flag){
137
138
// error in current sense - phase either not measured or bad connection
138
- if (monitor_port) monitor_port-> println ( F ( " MOT: Align error!" ) );
139
+ SIMPLEFOC_DEBUG ( " MOT: Align error!" );
139
140
exit_flag = 0 ;
140
141
}else {
141
142
// output the alignment status flag
142
- if (monitor_port) monitor_port->print (F (" MOT: Success: " ));
143
- if (monitor_port) monitor_port->println (exit_flag);
143
+ SIMPLEFOC_DEBUG (" MOT: Success: " , exit_flag);
144
144
}
145
145
146
146
return exit_flag > 0 ;
@@ -149,7 +149,7 @@ int BLDCMotor::alignCurrentSense() {
149
149
// Encoder alignment to electrical 0 angle
150
150
int BLDCMotor::alignSensor () {
151
151
int exit_flag = 1 ; // success
152
- if (monitor_port) monitor_port-> println ( F ( " MOT: Align sensor." ) );
152
+ SIMPLEFOC_DEBUG ( " MOT: Align sensor." );
153
153
154
154
// if unknown natural direction
155
155
if (!_isset (sensor_direction)){
@@ -180,24 +180,23 @@ int BLDCMotor::alignSensor() {
180
180
_delay (200 );
181
181
// determine the direction the sensor moved
182
182
if (mid_angle == end_angle) {
183
- if (monitor_port) monitor_port-> println ( F ( " MOT: Failed to notice movement" ) );
183
+ SIMPLEFOC_DEBUG ( " MOT: Failed to notice movement" );
184
184
return 0 ; // failed calibration
185
185
} else if (mid_angle < end_angle) {
186
- if (monitor_port) monitor_port-> println ( F ( " MOT: sensor_direction==CCW" ) );
186
+ SIMPLEFOC_DEBUG ( " MOT: sensor_direction==CCW" );
187
187
sensor_direction = Direction::CCW;
188
188
} else {
189
- if (monitor_port) monitor_port-> println ( F ( " MOT: sensor_direction==CW" ) );
189
+ SIMPLEFOC_DEBUG ( " MOT: sensor_direction==CW" );
190
190
sensor_direction = Direction::CW;
191
191
}
192
192
// check pole pair number
193
- if (monitor_port) monitor_port->print (F (" MOT: PP check: " ));
194
193
float moved = fabs (mid_angle - end_angle);
195
194
if ( fabs (moved*pole_pairs - _2PI) > 0 .5f ) { // 0.5f is arbitrary number it can be lower or higher!
196
- if (monitor_port) monitor_port-> print ( F ( " fail - estimated pp:" ) );
197
- if (monitor_port) monitor_port-> println (_2PI/moved, 4 );
198
- } else if (monitor_port) monitor_port-> println ( F ( " OK!" ) );
195
+ SIMPLEFOC_DEBUG ( " MOT: PP check: fail - estimated pp: " , _2PI/moved );
196
+ } else
197
+ SIMPLEFOC_DEBUG ( " MOT: PP check: OK!" );
199
198
200
- }else if (monitor_port) monitor_port-> println ( F ( " MOT: Skip dir calib." ) );
199
+ } else SIMPLEFOC_DEBUG ( " MOT: Skip dir calib." );
201
200
202
201
// zero electric angle not known
203
202
if (!_isset (zero_electric_angle)){
@@ -213,13 +212,12 @@ int BLDCMotor::alignSensor() {
213
212
// zero_electric_angle = _normalizeAngle(_electricalAngle(sensor_direction*sensor->getAngle(), pole_pairs));
214
213
_delay (20 );
215
214
if (monitor_port){
216
- monitor_port->print (F (" MOT: Zero elec. angle: " ));
217
- monitor_port->println (zero_electric_angle);
215
+ SIMPLEFOC_DEBUG (" MOT: Zero elec. angle: " , zero_electric_angle);
218
216
}
219
217
// stop everything
220
218
setPhaseVoltage (0 , 0 , 0 );
221
219
_delay (200 );
222
- }else if (monitor_port) monitor_port-> println ( F ( " MOT: Skip offset calib." ) );
220
+ }else SIMPLEFOC_DEBUG ( " MOT: Skip offset calib." );
223
221
return exit_flag;
224
222
}
225
223
@@ -228,7 +226,7 @@ int BLDCMotor::alignSensor() {
228
226
int BLDCMotor::absoluteZeroSearch () {
229
227
// sensor precision: this is all ok, as the search happens near the 0-angle, where the precision
230
228
// of float is sufficient.
231
- if (monitor_port) monitor_port-> println ( F ( " MOT: Index search..." ) );
229
+ SIMPLEFOC_DEBUG ( " MOT: Index search..." );
232
230
// search the absolute zero with small velocity
233
231
float limit_vel = velocity_limit;
234
232
float limit_volt = voltage_limit;
@@ -248,8 +246,8 @@ int BLDCMotor::absoluteZeroSearch() {
248
246
voltage_limit = limit_volt;
249
247
// check if the zero found
250
248
if (monitor_port){
251
- if (sensor->needsSearch ()) monitor_port-> println ( F ( " MOT: Error: Not found!" ) );
252
- else monitor_port-> println ( F ( " MOT: Success!" ) );
249
+ if (sensor->needsSearch ()) SIMPLEFOC_DEBUG ( " MOT: Error: Not found!" );
250
+ else SIMPLEFOC_DEBUG ( " MOT: Success!" );
253
251
}
254
252
return !sensor->needsSearch ();
255
253
}
@@ -298,7 +296,7 @@ void BLDCMotor::loopFOC() {
298
296
break ;
299
297
default :
300
298
// no torque control selected
301
- if (monitor_port) monitor_port-> println ( F ( " MOT: no torque control selected!" ) );
299
+ SIMPLEFOC_DEBUG ( " MOT: no torque control selected!" );
302
300
break ;
303
301
}
304
302
0 commit comments