eLab Team / Mbed 2 deprecated myRobot

Dependencies:   mbed WS2812

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers motor.h Source File

motor.h

00001 // -------------------- Motor ---------------------------
00002 #ifdef MOTOR_H
00003 #else
00004 #define MOTOR_H
00005 
00006 #include "mbed.h"
00007 
00008 #define MOTOR_STEP_TIME_MIN 700
00009 #define MOTOR_STEP_TIME_DEFAULT 5000
00010 #define MOTOR_TICKS_FOR_A_TURN 4096
00011 
00012 typedef enum MotorStateList {   // Define Motor States for the State Machine
00013     Motor_IDLE = 0,
00014     Motor_RUN,
00015     Motor_PAUSE,
00016     Motor_ZERO,
00017     Motor_CALIB
00018     } MotorState;
00019     
00020 typedef enum MotorCommandList { // Define Motor State Machine Commands
00021     MOTOR_nop = 0,
00022     MOTOR_start,
00023     MOTOR_pause,
00024     MOTOR_restart,
00025     MOTOR_stop,
00026     MOTOR_zero
00027     } MotorCommand;
00028 
00029 typedef enum MotorDirectionList { // Define Motor Clockwise or Anticlockwise
00030     CLOCKWISE = 0,
00031     COUNTERCLOCKWISE
00032     } MotorDir;
00033     
00034 class Motor {
00035     
00036     MotorState state;
00037     MotorCommand command;
00038     MotorDir direction;
00039 
00040     
00041     public:
00042  
00043     
00044     Motor(PinName _MPh0, PinName _MPh1, PinName _MPh2, PinName _MPh3, uint32_t TickTime);
00045     Motor(PinName _MPh0, PinName _MPh1, PinName _MPh2, PinName _MPh3);
00046     void RunSteps(MotorDir direction, uint32_t steps);
00047     void RunDegrees(MotorDir direction, float steps);
00048     void RunInfinite(MotorDir direction);
00049     void SetDirection(MotorDir dir);
00050     void TestMotor();
00051     void RunMotor();
00052     void setMotorCallback(void (*mIT)());
00053      void setMotorCallback(void (*mIT)(), bool itOnStop);
00054  
00055 template<typename T>
00056 void setMotorCallback(T *object, void (T::*member)(void))
00057 {
00058     _callback = callback(object,member);
00059     itOnStop = true;
00060 }
00061  
00062     void removeMotorCallback();
00063 
00064     uint32_t getCalibration();    
00065     void setCalibration(uint32_t nbTicksforFullTurn);
00066     void setDelayBtwTicks(uint32_t tickTime); // in ms
00067     void setSpeed(float sForOneTurn) ;// nb of s to get rotation of 360° (if 20s, the motor will do 360° in 20 s). 
00068     
00069     void Start();
00070     void Stop();
00071     void Pause();
00072     void Restart();
00073     void SetZero();
00074     
00075     MotorStateList getState();
00076     
00077     private:
00078     void initialization(PinName _MPh0, PinName _MPh1, PinName _MPh2, PinName _MPh3, uint32_t TickTime);
00079  
00080     void StopMotor();
00081     void StartMotor();
00082     void SetCommand(MotorCommand cmd);
00083     void LeftMotor();
00084     void RightMotor();
00085     void ProcessMotorStateMachine();
00086     Timer tuneTimings;
00087 uint32_t last;
00088     DigitalOut *MPh0, *MPh1, *MPh2, *MPh3;
00089     
00090     int MotorIndex;    // --- Motor Variable
00091    
00092     bool init;
00093     Ticker  MotorSystemTick;    // System Callback for Motor
00094     timestamp_t MotorStepTime;  // Time in µs for one motor step
00095     uint32_t    MotorFullTurn;  // Number of step for a complete turn
00096     int32_t    NumSteps;       // Number of Steps = NumWire * MotorFullTurn
00097     Callback<void()> _callback;
00098 
00099     bool itOnStop;
00100 };
00101 
00102 #endif