My Version of the Crealab MotorLib.
Fork of MotorLib by
Diff: motor.cpp
- Revision:
- 4:c009bcd5518c
- Parent:
- 3:01b4c058454d
- Child:
- 6:aec892eb1b49
diff -r 01b4c058454d -r c009bcd5518c motor.cpp --- a/motor.cpp Fri Jun 24 14:57:25 2016 +0000 +++ b/motor.cpp Tue Aug 23 11:33:46 2016 +0000 @@ -1,7 +1,7 @@ #include "motor.h" -Motor::Motor(PinName _MPh0, PinName _MPh1, PinName _MPh2, PinName _MPh3) { +Motor::Motor(PinName _MPh0, PinName _MPh1, PinName _MPh2, PinName _MPh3, uint32_t tickTime) { MPh0 = new DigitalOut(_MPh0); MPh1 = new DigitalOut(_MPh1); @@ -12,15 +12,37 @@ // // Connect Interrupt routine in which the motor and all the state machine is performed // - direction = DIRECTION_CLOCKWISE; // Default direction is clockwise + direction = CLOCKWISE; // Default direction is clockwise state = Motor_IDLE; // Default state is IDLE command = MOTOR_nop; // Default command is NOP - MotorStepTime = 30000; // value in micro second for one step + MotorStepTime = tickTime; // value in micro second for one step MotorFullTurn = 2140; // Initial Calibration NumSteps = 2000; // Default } +void Motor::Start() { + SetCommand(MOTOR_start); +}; + +void Motor::Stop() { + SetCommand(MOTOR_stop); +} + + +void Motor::Pause() { + SetCommand(MOTOR_pause); +} + + +void Motor::Restart() { + SetCommand(MOTOR_restart); +} + +void Motor::SetZero() { + SetCommand(MOTOR_zero); +} + void Motor::RunSteps(MotorDir dir, uint32_t steps) { SetDirection( dir); NumSteps = steps; @@ -29,7 +51,7 @@ void Motor::RunDegrees(MotorDir dir, float degree) { SetDirection( dir); - NumSteps = (degree * MotorFullTurn / 360.0); + NumSteps = (int)(degree * MotorFullTurn / 360.0); SetCommand(MOTOR_start); } void Motor::SetDirection(MotorDir dir) { @@ -46,6 +68,7 @@ { *MPh0 = 0; *MPh1 = 0; *MPh2 = 0; *MPh3 = 0; MotorIndex = 0; + } @@ -57,28 +80,28 @@ void Motor::RightMotor() // Move the Motor one step Right { - const int RPh0[4] = {0, 1, 0, 0}; - const int RPh1[4] = {0, 0, 1, 0}; - const int RPh2[4] = {0, 0, 0, 1}; - const int RPh3[4] = {1, 0, 0, 0}; + static const int RPh0[8] = {1, 1, 0, 0, 0, 0, 0, 1}; + static const int RPh1[8] = {0, 1, 1, 1, 0, 0, 0, 0}; + static const int RPh2[8] = {0, 0, 0, 1, 1, 1, 0, 0}; + static const int RPh3[8] = {0, 0, 0, 0, 0, 1, 1, 1}; *MPh0 = RPh0[MotorIndex]; *MPh1 = RPh1[MotorIndex]; *MPh2 = RPh2[MotorIndex]; *MPh3 = RPh3[MotorIndex]; - if (MotorIndex<3) MotorIndex++; + if (MotorIndex<7) MotorIndex++; else MotorIndex = 0; } void Motor::LeftMotor() // Move the Motor one step Right { - const int LPh0[4] = {0, 0, 1, 0}; - const int LPh1[4] = {0, 1, 0, 0}; - const int LPh2[4] = {1, 0, 0, 0}; - const int LPh3[4] = {0, 0, 0, 1}; - *MPh0 = LPh0[MotorIndex]; *MPh1 = LPh1[MotorIndex]; *MPh2 = LPh2[MotorIndex]; *MPh3 = LPh3[MotorIndex]; - if (MotorIndex<3) MotorIndex++; + static const int LPh0[8] = { 1, 0, 0, 0, 0, 0, 1, 1}; + static const int LPh1[8] = { 0, 0, 0, 0, 1, 1, 1, 0}; + static const int LPh2[8] = { 0, 0, 1, 1, 1, 0, 0, 0}; + static const int LPh3[8] = { 1, 1, 1, 0, 0, 0, 0, 0}; + *MPh0 = LPh0[MotorIndex]; *MPh1 = LPh1[MotorIndex]; *MPh2 = LPh2[MotorIndex]; *MPh3 = LPh3[MotorIndex]; + if (MotorIndex<7) MotorIndex++; else MotorIndex = 0; } void Motor::RunMotor() // Move the Motor in the user direction { - if (direction==DIRECTION_CLOCKWISE) RightMotor(); + if (direction==CLOCKWISE) RightMotor(); else LeftMotor(); }