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@3:01b4c058454d, 2016-06-24 (annotated)
- Committer:
- garphil
- Date:
- Fri Jun 24 14:57:25 2016 +0000
- Revision:
- 3:01b4c058454d
- Parent:
- 2:c91c5ef00d25
- Child:
- 4:c009bcd5518c
Tested on Board.; Added Button (no uart for debug)
Who changed what in which revision?
| User | Revision | Line number | New 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 | 2:c91c5ef00d25 | 22 | DIRECTION_CLOCKWISE = 0, |
| garphil | 2:c91c5ef00d25 | 23 | DIRECTION_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 | 2:c91c5ef00d25 | 34 | |
| garphil | 0:bd05fd602a6e | 35 | Motor(PinName _MPh0, PinName _MPh1, PinName _MPh2, PinName _MPh3); |
| garphil | 3:01b4c058454d | 36 | void RunSteps(MotorDir direction, uint32_t steps); |
| garphil | 3:01b4c058454d | 37 | void RunDegrees(MotorDir direction, float steps); |
| garphil | 1:9519ac966b79 | 38 | void StopMotor(); |
| garphil | 2:c91c5ef00d25 | 39 | void StartMotor(); |
| garphil | 2:c91c5ef00d25 | 40 | void SetDirection(MotorDir dir); |
| garphil | 2:c91c5ef00d25 | 41 | void SetCommand(MotorCommand cmd); |
| garphil | 2:c91c5ef00d25 | 42 | void TestMotor(); |
| garphil | 1:9519ac966b79 | 43 | void RunMotor(); |
| garphil | 1:9519ac966b79 | 44 | void LeftMotor(); |
| garphil | 1:9519ac966b79 | 45 | void RightMotor(); |
| garphil | 1:9519ac966b79 | 46 | void ProcessMotorStateMachine(); |
| garphil | 0:bd05fd602a6e | 47 | |
| garphil | 2:c91c5ef00d25 | 48 | private: |
| garphil | 2:c91c5ef00d25 | 49 | |
| garphil | 0:bd05fd602a6e | 50 | DigitalOut *MPh0, *MPh1, *MPh2, *MPh3; |
| garphil | 0:bd05fd602a6e | 51 | |
| garphil | 0:bd05fd602a6e | 52 | int MotorIndex; // --- Motor Variable |
| garphil | 1:9519ac966b79 | 53 | |
| garphil | 3:01b4c058454d | 54 | bool init; |
| garphil | 0:bd05fd602a6e | 55 | Ticker MotorSystemTick; // System Callback for Motor |
| garphil | 0:bd05fd602a6e | 56 | timestamp_t MotorStepTime; // Time in µs for one motor step |
| garphil | 0:bd05fd602a6e | 57 | uint32_t MotorFullTurn; // Number of step for a complete turn |
| garphil | 2:c91c5ef00d25 | 58 | // uint32_t NumWires; // Number of Wires |
| garphil | 0:bd05fd602a6e | 59 | uint32_t NumSteps; // Number of Steps = NumWire * MotorFullTurn |
| garphil | 0:bd05fd602a6e | 60 | |
| garphil | 2:c91c5ef00d25 | 61 | }; |
