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