Newer
Older
Malaurie Bernard
committed
//CONSTRUCTORS
Motor :: Motor (MotorHardware_t& stepper): stepper(stepper)
{
Malaurie Bernard
committed
set_speed_mm_per_sec(1);
set_retain_acceleration_mm_per_sec_per_sec(0.5); // <----- this is not yet configured by user
Malaurie Bernard
committed
}
//SETTERS
void Motor :: set_physical (int steps_per_revolution, float mm_per_revolution, bool clockwise_equals_forward)
Malaurie Bernard
committed
{
//Useful so the user can change motor caracteristics
set_steps_per_revolution(steps_per_revolution);
set_mm_per_revolution(mm_per_revolution);
Malaurie Bernard
committed
}
void Motor :: set_steps_per_revolution(float steps_per_revolution)
{
_steps_per_revolution = steps_per_revolution;
}
void Motor :: set_mm_per_revolution (float mm_per_revolution)
{
_mm_per_revolution = mm_per_revolution;
}
void Motor :: set_retain_acceleration_mm_per_sec_per_sec (float retain_acceleration_mm_per_sec_per_sec)
//This is useful only to use the limit switch
Malaurie Bernard
committed
{
if (_retain_acceleration_mm_per_sec_per_sec == 0)
retain_acceleration_mm_per_sec_per_sec = 1000;
_retain_acceleration_mm_per_sec_per_sec = retain_acceleration_mm_per_sec_per_sec;
Malaurie Bernard
committed
#if !CORE_MOCK
stepper.setAcceleration(mm_to_step(retain_acceleration_mm_per_sec_per_sec));
Malaurie Bernard
committed
#endif
}
void Motor :: set_clockwise_equals_forward (bool clockwise_equals_forward)
Malaurie Bernard
committed
{
Malaurie Bernard
committed
}
void Motor :: set_speed_mm_per_sec (float mm_per_sec)
{
#if !CORE_MOCK
stepper.setMaxSpeed(mm_to_step(mm_per_sec));
#endif
}
//GETTERS
float Motor :: get_steps_per_revolution()
{
return _steps_per_revolution;
}
float Motor :: get_mm_per_revolution () const
{
return _mm_per_revolution;
}
bool Motor :: get_clockwise_equals_forward ()
{
return _clockwise_equals_forward;
}
float Motor :: get_retain_acceleration_mm_per_sec_per_sec () const
Malaurie Bernard
committed
{
return _retain_acceleration_mm_per_sec_per_sec;
Malaurie Bernard
committed
}
//CONVERSIONS
float Motor :: mm_to_step (float mm) const
/***
-Argument : Distance in mm.
-Return : The number of step associated.
-Action : /
***/
{
return mm * _steps_per_revolution / _mm_per_revolution;
}
float Motor :: step_to_mm (int step) const
/***
-Argument : Number of step.
-Return : Distance in mm associated.
-Action : /
***/
{
return step * _mm_per_revolution / _steps_per_revolution;
}
//MOVEMENTS
void Motor :: move_to_step_limit_switch (int step)
Malaurie Bernard
committed
/***
Malaurie Bernard
committed
-Argument : Final position in steps.
Malaurie Bernard
committed
-Return : /
Malaurie Bernard
committed
-Action : Move to the number of step required to go to the final position. Can manage the emergency.
Malaurie Bernard
committed
***/
{
#if !CORE_MOCK
cli();
set_retain_acceleration_mm_per_sec_per_sec(_retain_acceleration_mm_per_sec_per_sec);
Malaurie Bernard
committed
if (_mm_per_revolution > 0 && _steps_per_revolution > 0)
stepper.moveTo(_clockwise_equals_forward? step: -step);
sei();
#endif
Serial.printf("# move to: %d step %g mm\n", step, step_to_mm(step));
}
void Motor :: move_to_mm_limit_switch (float mm)
/***
Malaurie Bernard
committed
-Argument : Final position in mm.
Malaurie Bernard
committed
-Action : Move to the distance required to go to the final position. Can manage the emergency.
***/
{
Serial.printf("# moving to %g mm / %g steps\n",
mm,
mm_to_step(mm));
move_to_step_limit_switch(mm_to_step(mm));
}
void Motor :: move_to_step (int step)
Malaurie Bernard
committed
/***
Malaurie Bernard
committed
/***
-Argument : Final position in steps.
Malaurie Bernard
committed
-Return : /
Malaurie Bernard
committed
-Action : Move to the number of step required to go to the final position.
Malaurie Bernard
committed
***/
{
#if !CORE_MOCK
cli();
if (_mm_per_revolution > 0 && _steps_per_revolution > 0)
stepper.moveTo(_clockwise_equals_forward? step: -step);
sei();
#endif
Serial.printf("# move to: %d step %g mm\n", step, step_to_mm(step));
}
void Motor :: move_to_mm (float mm)
Malaurie Bernard
committed
/***
Malaurie Bernard
committed
-Argument : Final position in mm.
Malaurie Bernard
committed
-Return : /
Malaurie Bernard
committed
-Action : Move to the distance required to go to the final position.
Malaurie Bernard
committed
***/
Malaurie Bernard
committed
{
Serial.printf("# moving to %g mm / %g steps\n",
mm,
mm_to_step(mm));
Malaurie Bernard
committed
move_to_step(mm_to_step(mm));
Malaurie Bernard
committed
}
Malaurie Bernard
committed
void Motor :: stop ()
/***
-Argument : /
-Return : /
-Action : Stop the motor.
***/
{
#if !CORE_MOCK
cli();
Malaurie Bernard
committed
stepper.setAcceleration(1e20);
stepper.moveTo(stepper.currentPosition());
Malaurie Bernard
committed
stepper.stop();
sei();
#endif
}
void Motor :: stay_here ()
/***
-Argument : /
-Return : /
-Action : Stop the movement but the motor is still running (it helps to maintain the position more precisely if there is a resistance)
***/
{
#if !CORE_MOCK
cli();
stepper.setAcceleration(1e20);
Malaurie Bernard
committed
stepper.moveTo(stepper.currentPosition()); // change target to here
set_retain_acceleration_mm_per_sec_per_sec(_retain_acceleration_mm_per_sec_per_sec);
Malaurie Bernard
committed
sei();
#endif
}
Malaurie Bernard
committed
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
//////////////////
void Motor :: move_step_limit_switch (int step)
/***
-Argument : Mouvement required in steps.
-Return : /
-Action : Move the number of step asked. Can manage the emergency.
***/
{
#if !CORE_MOCK
cli();
set_retain_acceleration_mm_per_sec_per_sec(_retain_acceleration_mm_per_sec_per_sec);
if (_mm_per_revolution > 0 && _steps_per_revolution > 0)
stepper.move(_clockwise_equals_forward? step: -step);
sei();
#endif
Serial.printf("# move to: %d step %g mm\n", step, step_to_mm(step));
}
void Motor :: move_mm_limit_switch (float mm)
/***
-Argument : Mouvement required in distance in mm.
-Return : /
-Action : Move the distance asked. Can manage the emergency.
***/
{
Serial.printf("# moving to %g mm / %g steps\n",
mm,
mm_to_step(mm));
move_step_limit_switch(mm_to_step(mm));
}
void Motor :: move_step (int step)
/***
-Argument : Mouvement required in steps.
-Return : /
-Action : Move to the number of step.
***/
{
#if !CORE_MOCK
cli();
if (_mm_per_revolution > 0 && _steps_per_revolution > 0)
stepper.move(_clockwise_equals_forward? step: -step);
sei();
#endif
Serial.printf("# move to: %d step %g mm\n", step, step_to_mm(step));
}
void Motor :: move_mm (float mm)
/***
-Argument : Mouvement required in distance in mm.
-Return : /
-Action : Move the distance asked.
***/
{
Serial.printf("# moving to %g mm / %g steps\n",
mm,
mm_to_step(mm));
move_step(mm_to_step(mm));
}
//////////////////////
Malaurie Bernard
committed
bool Motor :: motor_is_running () //return true if the motor is currently running
/***
-Argument : /
-Return : A boolean to indicates if the motor is running.
-Action : /
***/
{
return stepper.isRunning();
Malaurie Bernard
committed
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
}
//POSITION
int Motor :: where_step ()
/***
-Argument : /
-Return : The current position in step.
-Action : /
***/
{
#if CORE_MOCK
return 0;
#else
return stepper.currentPosition() * (_clockwise_equals_forward? 1: -1);
#endif
}
int Motor :: where_mm ()
/***
-Argument : /
-Return : The current position in mm.
-Action : /
***/
{
#if CORE_MOCK
return 0;
#else
return step_to_mm(stepper.currentPosition() * (_clockwise_equals_forward? 1: -1));
#endif
}
void Motor :: reset_position()
/***
-Argument : /
-Return : /
-Action : Set the curent position as the zero.
***/
Malaurie Bernard
committed
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
#if !CORE_MOCK
stop();
cli();
stepper.setCurrentPosition(0);
sei();
#endif
}
//CONFIGURATION
void Motor :: show_configuration ()
/***
-Argument : /
-Return : /
-Action : Print the current motor configuration.
***/
{
Serial.printf("# %g mm/rev\n"
"# %d steps/rev\n"
"# %g steps/mm\n"
"# max speed: %g steps/s %g mm/s\n"
"# accel: %g steps/s/s %g mm/s/s\n",
_mm_per_revolution,
_steps_per_revolution,
_steps_per_revolution / _mm_per_revolution,
stepper.maxSpeed(),
step_to_mm(stepper.maxSpeed()),
stepper.acceleration(),
step_to_mm(stepper.acceleration())
);
}