Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Fork of MotorLib by
motor.h
- Committer:
- garphil
- Date:
- 2017-07-17
- Revision:
- 9:5983c10d5f8e
- Parent:
- 8:4dd59b6f4e92
- Child:
- 10:1df5a7a265e8
File content as of revision 9:5983c10d5f8e:
// -------------------- Motor ---------------------------
#ifdef MOTOR_H
#else
#define MOTOR_H
#include "mbed.h"
#define MOTOR_STEP_TIME_MIN 1
#define MOTOR_STEP_TIME_DEFAULT 5000
#define MOTOR_TICKS_FOR_A_TURN 4096
typedef enum MotorStateList { // Define Motor States for the State Machine
Motor_IDLE = 0,
Motor_RUN,
Motor_PAUSE,
Motor_ZERO,
Motor_CALIB
} MotorState;
typedef enum MotorCommandList { // Define Motor State Machine Commands
MOTOR_nop = 0,
MOTOR_start,
MOTOR_pause,
MOTOR_restart,
MOTOR_stop,
MOTOR_zero
} MotorCommand;
typedef enum MotorDirectionList { // Define Motor Clockwise or Anticlockwise
CLOCKWISE = 0,
COUNTERCLOCKWISE
} MotorDir;
class Motor {
MotorState state;
MotorCommand command;
MotorDir direction;
public:
Motor(PinName _MPh0, PinName _MPh1, PinName _MPh2, PinName _MPh3, uint32_t TickTime);
Motor(PinName _MPh0, PinName _MPh1, PinName _MPh2, PinName _MPh3);
void RunSteps(MotorDir direction, uint32_t steps);
void RunDegrees(MotorDir direction, float steps);
void RunInfinite(MotorDir direction);
void SetDirection(MotorDir dir);
void TestMotor();
void RunMotor();
void setMotorCallback(void (*mIT)());
void setMotorCallback(void (*mIT)(), bool onStop);
void removeMotorCallback();
void setCalibration(uint32_t nbTicksforFullTurn);
void setDelayBtwTicks(uint32_t tickTime); // in ms
void setSpeed(float sForOneTurn) ;// nb of s to get rotation of 360° (if 20s, the motor will do 360° in 20 s).
void Start();
void Stop();
void Pause();
void Restart();
void SetZero();
MotorStateList getState();
private:
void initialization(PinName _MPh0, PinName _MPh1, PinName _MPh2, PinName _MPh3, uint32_t TickTime);
void StopMotor();
void StartMotor();
void SetCommand(MotorCommand cmd);
void LeftMotor();
void RightMotor();
void ProcessMotorStateMachine();
DigitalOut *MPh0, *MPh1, *MPh2, *MPh3;
int MotorIndex; // --- Motor Variable
bool init;
Ticker MotorSystemTick; // System Callback for Motor
timestamp_t MotorStepTime; // Time in µs for one motor step
uint32_t MotorFullTurn; // Number of step for a complete turn
// uint32_t NumWires; // Number of Wires
int32_t NumSteps; // Number of Steps = NumWire * MotorFullTurn
void (*motorCallback)();
bool itOnStop;
};
#endif
