Tarek Lule / MotorLib

Fork of MotorLib by CreaLab

Committer:
garphil
Date:
Thu Jul 27 07:25:47 2017 +0000
Revision:
10:1df5a7a265e8
Parent:
9:5983c10d5f8e
Child:
11:25d26c72a2f7
Changed constructor model

Who changed what in which revision?

UserRevisionLine numberNew contents of line
garphil 0:bd05fd602a6e 1 // -------------------- Motor ---------------------------
garphil 5:9886fd337a4b 2 #ifdef MOTOR_H
garphil 5:9886fd337a4b 3 #else
garphil 5:9886fd337a4b 4 #define MOTOR_H
garphil 5:9886fd337a4b 5
garphil 0:bd05fd602a6e 6 #include "mbed.h"
garphil 0:bd05fd602a6e 7
garphil 9:5983c10d5f8e 8 #define MOTOR_STEP_TIME_MIN 1
garphil 9:5983c10d5f8e 9 #define MOTOR_STEP_TIME_DEFAULT 5000
garphil 9:5983c10d5f8e 10 #define MOTOR_TICKS_FOR_A_TURN 4096
garphil 9:5983c10d5f8e 11
garphil 2:c91c5ef00d25 12 typedef enum MotorStateList { // Define Motor States for the State Machine
garphil 0:bd05fd602a6e 13 Motor_IDLE = 0,
garphil 0:bd05fd602a6e 14 Motor_RUN,
garphil 0:bd05fd602a6e 15 Motor_PAUSE,
garphil 0:bd05fd602a6e 16 Motor_ZERO,
garphil 0:bd05fd602a6e 17 Motor_CALIB
garphil 2:c91c5ef00d25 18 } MotorState;
garphil 2:c91c5ef00d25 19
garphil 2:c91c5ef00d25 20 typedef enum MotorCommandList { // Define Motor State Machine Commands
garphil 2:c91c5ef00d25 21 MOTOR_nop = 0,
garphil 2:c91c5ef00d25 22 MOTOR_start,
garphil 2:c91c5ef00d25 23 MOTOR_pause,
garphil 2:c91c5ef00d25 24 MOTOR_restart,
garphil 2:c91c5ef00d25 25 MOTOR_stop,
garphil 2:c91c5ef00d25 26 MOTOR_zero
garphil 2:c91c5ef00d25 27 } MotorCommand;
garphil 0:bd05fd602a6e 28
garphil 2:c91c5ef00d25 29 typedef enum MotorDirectionList { // Define Motor Clockwise or Anticlockwise
garphil 4:c009bcd5518c 30 CLOCKWISE = 0,
garphil 4:c009bcd5518c 31 COUNTERCLOCKWISE
garphil 0:bd05fd602a6e 32 } MotorDir;
garphil 0:bd05fd602a6e 33
garphil 0:bd05fd602a6e 34 class Motor {
garphil 0:bd05fd602a6e 35
garphil 2:c91c5ef00d25 36 MotorState state;
garphil 2:c91c5ef00d25 37 MotorCommand command;
garphil 2:c91c5ef00d25 38 MotorDir direction;
garphil 2:c91c5ef00d25 39
garphil 2:c91c5ef00d25 40
garphil 2:c91c5ef00d25 41 public:
garphil 4:c009bcd5518c 42
garphil 2:c91c5ef00d25 43
garphil 4:c009bcd5518c 44 Motor(PinName _MPh0, PinName _MPh1, PinName _MPh2, PinName _MPh3, uint32_t TickTime);
garphil 9:5983c10d5f8e 45 Motor(PinName _MPh0, PinName _MPh1, PinName _MPh2, PinName _MPh3);
garphil 3:01b4c058454d 46 void RunSteps(MotorDir direction, uint32_t steps);
garphil 3:01b4c058454d 47 void RunDegrees(MotorDir direction, float steps);
garphil 9:5983c10d5f8e 48 void RunInfinite(MotorDir direction);
garphil 4:c009bcd5518c 49 void SetDirection(MotorDir dir);
garphil 4:c009bcd5518c 50 void TestMotor();
garphil 4:c009bcd5518c 51 void RunMotor();
garphil 9:5983c10d5f8e 52 void setMotorCallback(void (*mIT)());
garphil 10:1df5a7a265e8 53 void setMotorCallback(void (*mIT)(), bool itOnStop);
garphil 10:1df5a7a265e8 54
garphil 10:1df5a7a265e8 55 template<typename T>
garphil 10:1df5a7a265e8 56 void setMotorCallback(T *object, void (T::*member)(void))
garphil 10:1df5a7a265e8 57 {
garphil 10:1df5a7a265e8 58 _callback = callback(object,member);
garphil 10:1df5a7a265e8 59 itOnStop = true;
garphil 10:1df5a7a265e8 60 }
garphil 10:1df5a7a265e8 61
garphil 9:5983c10d5f8e 62 void removeMotorCallback();
garphil 9:5983c10d5f8e 63
garphil 10:1df5a7a265e8 64 uint32_t getCalibration();
garphil 9:5983c10d5f8e 65 void setCalibration(uint32_t nbTicksforFullTurn);
garphil 9:5983c10d5f8e 66 void setDelayBtwTicks(uint32_t tickTime); // in ms
garphil 9:5983c10d5f8e 67 void setSpeed(float sForOneTurn) ;// nb of s to get rotation of 360° (if 20s, the motor will do 360° in 20 s).
garphil 4:c009bcd5518c 68
garphil 4:c009bcd5518c 69 void Start();
garphil 4:c009bcd5518c 70 void Stop();
garphil 4:c009bcd5518c 71 void Pause();
garphil 4:c009bcd5518c 72 void Restart();
garphil 4:c009bcd5518c 73 void SetZero();
arnophilippe 7:439458133bba 74
arnophilippe 7:439458133bba 75 MotorStateList getState();
garphil 10:1df5a7a265e8 76
garphil 4:c009bcd5518c 77 private:
garphil 9:5983c10d5f8e 78 void initialization(PinName _MPh0, PinName _MPh1, PinName _MPh2, PinName _MPh3, uint32_t TickTime);
garphil 9:5983c10d5f8e 79
garphil 1:9519ac966b79 80 void StopMotor();
garphil 2:c91c5ef00d25 81 void StartMotor();
garphil 2:c91c5ef00d25 82 void SetCommand(MotorCommand cmd);
garphil 1:9519ac966b79 83 void LeftMotor();
garphil 1:9519ac966b79 84 void RightMotor();
garphil 1:9519ac966b79 85 void ProcessMotorStateMachine();
garphil 10:1df5a7a265e8 86 Timer tuneTimings;
garphil 10:1df5a7a265e8 87 uint32_t last;
garphil 0:bd05fd602a6e 88 DigitalOut *MPh0, *MPh1, *MPh2, *MPh3;
garphil 0:bd05fd602a6e 89
garphil 0:bd05fd602a6e 90 int MotorIndex; // --- Motor Variable
garphil 1:9519ac966b79 91
garphil 3:01b4c058454d 92 bool init;
garphil 0:bd05fd602a6e 93 Ticker MotorSystemTick; // System Callback for Motor
garphil 0:bd05fd602a6e 94 timestamp_t MotorStepTime; // Time in µs for one motor step
garphil 0:bd05fd602a6e 95 uint32_t MotorFullTurn; // Number of step for a complete turn
garphil 2:c91c5ef00d25 96 // uint32_t NumWires; // Number of Wires
garphil 8:4dd59b6f4e92 97 int32_t NumSteps; // Number of Steps = NumWire * MotorFullTurn
garphil 10:1df5a7a265e8 98 Callback<void()> _callback;
garphil 10:1df5a7a265e8 99
garphil 9:5983c10d5f8e 100 void (*motorCallback)();
garphil 9:5983c10d5f8e 101 bool itOnStop;
garphil 10:1df5a7a265e8 102 bool toremove;
garphil 5:9886fd337a4b 103 };
garphil 5:9886fd337a4b 104
garphil 5:9886fd337a4b 105 #endif