My Version of the Crealab MotorLib.
Fork of MotorLib by
Diff: motor.cpp
- Revision:
- 9:5983c10d5f8e
- Parent:
- 8:4dd59b6f4e92
- Child:
- 10:1df5a7a265e8
--- a/motor.cpp Mon Jul 17 11:45:44 2017 +0000 +++ b/motor.cpp Mon Jul 17 14:02:22 2017 +0000 @@ -1,8 +1,7 @@ #include "motor.h" +void Motor::initialization(PinName _MPh0, PinName _MPh1, PinName _MPh2, PinName _MPh3, uint32_t tickTime) { -Motor::Motor(PinName _MPh0, PinName _MPh1, PinName _MPh2, PinName _MPh3, uint32_t tickTime) { - MPh0 = new DigitalOut(_MPh0); MPh1 = new DigitalOut(_MPh1); MPh2 = new DigitalOut(_MPh2); @@ -16,11 +15,52 @@ state = Motor_IDLE; // Default state is IDLE command = MOTOR_nop; // Default command is NOP MotorStepTime = tickTime; // value in micro second for one step - MotorFullTurn = 2140; // Initial Calibration - NumSteps = 2000; // Default + MotorFullTurn = MOTOR_TICKS_FOR_A_TURN; // Initial Calibration + NumSteps = MotorFullTurn; // Default 1 turn + motorCallback = NULL; + itOnStop = true; +} + +Motor::Motor(PinName _MPh0, PinName _MPh1, PinName _MPh2, PinName _MPh3) { + initialization( _MPh0, _MPh1, _MPh2, _MPh3, MOTOR_STEP_TIME_DEFAULT); + +} + + +Motor::Motor(PinName _MPh0, PinName _MPh1, PinName _MPh2, PinName _MPh3, uint32_t tickTime) { + + initialization( _MPh0, _MPh1, _MPh2, _MPh3, tickTime); } + +void Motor::removeMotorCallback() { + motorCallback = NULL; +} + +void Motor::setMotorCallback(void (*mIT)()) { + motorCallback = mIT; + itOnStop = true; +} +void Motor::setMotorCallback(void (*mIT)(), bool onStop) { + motorCallback = mIT; +} + +void Motor::setCalibration(uint32_t nbTicksforFullTurn) { + MotorFullTurn = nbTicksforFullTurn; +} + +void Motor::setDelayBtwTicks(uint32_t tickTime) { + MotorStepTime = tickTime; + if(MotorStepTime < MOTOR_STEP_TIME_MIN) MotorStepTime = MOTOR_STEP_TIME_MIN; + +} + +void Motor::setSpeed(float sForOneTurn) { + MotorStepTime = 1000*sForOneTurn / MotorFullTurn; + if(MotorStepTime < MOTOR_STEP_TIME_MIN) MotorStepTime = MOTOR_STEP_TIME_MIN; +} + void Motor::Start() { SetCommand(MOTOR_start); }; @@ -140,6 +180,9 @@ else if ((command == MOTOR_stop)||(NumSteps==0)) { StopMotor(); state = Motor_IDLE; + if(command != MOTOR_stop || itOnStop == true) { + if(motorCallback != NULL) motorCallback(); + } } command = MOTOR_nop; }