Tarek Lule / MotorLib

Fork of MotorLib by CreaLab

Committer:
garphil
Date:
Tue Aug 23 11:33:46 2016 +0000
Revision:
4:c009bcd5518c
Parent:
3:01b4c058454d
Child:
5:9886fd337a4b
Added Start Stop function directly... instead of setcommand(Motor_Start)...;

Who changed what in which revision?

UserRevisionLine numberNew contents of line
garphil 0:bd05fd602a6e 1 // -------------------- Motor ---------------------------
garphil 0:bd05fd602a6e 2 #include "mbed.h"
garphil 0:bd05fd602a6e 3
garphil 2:c91c5ef00d25 4 typedef enum MotorStateList { // Define Motor States for the State Machine
garphil 0:bd05fd602a6e 5 Motor_IDLE = 0,
garphil 0:bd05fd602a6e 6 Motor_RUN,
garphil 0:bd05fd602a6e 7 Motor_PAUSE,
garphil 0:bd05fd602a6e 8 Motor_ZERO,
garphil 0:bd05fd602a6e 9 Motor_CALIB
garphil 2:c91c5ef00d25 10 } MotorState;
garphil 2:c91c5ef00d25 11
garphil 2:c91c5ef00d25 12 typedef enum MotorCommandList { // Define Motor State Machine Commands
garphil 2:c91c5ef00d25 13 MOTOR_nop = 0,
garphil 2:c91c5ef00d25 14 MOTOR_start,
garphil 2:c91c5ef00d25 15 MOTOR_pause,
garphil 2:c91c5ef00d25 16 MOTOR_restart,
garphil 2:c91c5ef00d25 17 MOTOR_stop,
garphil 2:c91c5ef00d25 18 MOTOR_zero
garphil 2:c91c5ef00d25 19 } MotorCommand;
garphil 0:bd05fd602a6e 20
garphil 2:c91c5ef00d25 21 typedef enum MotorDirectionList { // Define Motor Clockwise or Anticlockwise
garphil 4:c009bcd5518c 22 CLOCKWISE = 0,
garphil 4:c009bcd5518c 23 COUNTERCLOCKWISE
garphil 0:bd05fd602a6e 24 } MotorDir;
garphil 0:bd05fd602a6e 25
garphil 0:bd05fd602a6e 26 class Motor {
garphil 0:bd05fd602a6e 27
garphil 2:c91c5ef00d25 28 MotorState state;
garphil 2:c91c5ef00d25 29 MotorCommand command;
garphil 2:c91c5ef00d25 30 MotorDir direction;
garphil 2:c91c5ef00d25 31
garphil 2:c91c5ef00d25 32
garphil 2:c91c5ef00d25 33 public:
garphil 4:c009bcd5518c 34
garphil 2:c91c5ef00d25 35
garphil 4:c009bcd5518c 36 Motor(PinName _MPh0, PinName _MPh1, PinName _MPh2, PinName _MPh3, uint32_t TickTime);
garphil 3:01b4c058454d 37 void RunSteps(MotorDir direction, uint32_t steps);
garphil 3:01b4c058454d 38 void RunDegrees(MotorDir direction, float steps);
garphil 4:c009bcd5518c 39 void SetDirection(MotorDir dir);
garphil 4:c009bcd5518c 40 void TestMotor();
garphil 4:c009bcd5518c 41 void RunMotor();
garphil 4:c009bcd5518c 42
garphil 4:c009bcd5518c 43 void Start();
garphil 4:c009bcd5518c 44 void Stop();
garphil 4:c009bcd5518c 45 void Pause();
garphil 4:c009bcd5518c 46 void Restart();
garphil 4:c009bcd5518c 47 void SetZero();
garphil 4:c009bcd5518c 48
garphil 4:c009bcd5518c 49 private:
garphil 1:9519ac966b79 50 void StopMotor();
garphil 2:c91c5ef00d25 51 void StartMotor();
garphil 2:c91c5ef00d25 52 void SetCommand(MotorCommand cmd);
garphil 1:9519ac966b79 53 void LeftMotor();
garphil 1:9519ac966b79 54 void RightMotor();
garphil 1:9519ac966b79 55 void ProcessMotorStateMachine();
garphil 4:c009bcd5518c 56
garphil 0:bd05fd602a6e 57 DigitalOut *MPh0, *MPh1, *MPh2, *MPh3;
garphil 0:bd05fd602a6e 58
garphil 0:bd05fd602a6e 59 int MotorIndex; // --- Motor Variable
garphil 1:9519ac966b79 60
garphil 3:01b4c058454d 61 bool init;
garphil 0:bd05fd602a6e 62 Ticker MotorSystemTick; // System Callback for Motor
garphil 0:bd05fd602a6e 63 timestamp_t MotorStepTime; // Time in µs for one motor step
garphil 0:bd05fd602a6e 64 uint32_t MotorFullTurn; // Number of step for a complete turn
garphil 2:c91c5ef00d25 65 // uint32_t NumWires; // Number of Wires
garphil 0:bd05fd602a6e 66 uint32_t NumSteps; // Number of Steps = NumWire * MotorFullTurn
garphil 0:bd05fd602a6e 67
garphil 2:c91c5ef00d25 68 };