Working version without LEDs

Dependencies:   mbed WS2812

Voici le dernier schéma de cablage (version du 08/02/2020)

https://os.mbed.com/media/uploads/max_ence/schemarobot_fev2020.pdf

Committer:
elab
Date:
Sat Feb 08 09:48:46 2020 +0000
Revision:
0:0e577ce96b2f
working version wihout LEDs

Who changed what in which revision?

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