Tarek Lule / MotorLib

Fork of MotorLib by CreaLab

Committer:
sepp_nepp
Date:
Wed Nov 28 09:44:34 2018 +0000
Revision:
16:d818c1a4dafb
Parent:
15:88fecbdd191c
Child:
17:86e5af6f7628
Release with major documentation added

Who changed what in which revision?

UserRevisionLine numberNew contents of line
sepp_nepp 15:88fecbdd191c 1 /**
sepp_nepp 15:88fecbdd191c 2 * @file motor.h
sepp_nepp 16:d818c1a4dafb 3 * @brief File contains Crealab Motor Library.
sepp_nepp 15:88fecbdd191c 4
sepp_nepp 15:88fecbdd191c 5 * motor.h contains the class Motor, and related enums and structs.
sepp_nepp 16:d818c1a4dafb 6 * Includes only "mbed.h".
sepp_nepp 16:d818c1a4dafb 7
sepp_nepp 16:d818c1a4dafb 8 * Rotation directions are now consistently called Clockwise, and Counterclockwise (CCW),
sepp_nepp 16:d818c1a4dafb 9 * instead of mix them with Left and Right.
sepp_nepp 16:d818c1a4dafb 10 * Doxygens Tags are preceeded by either a backslash @\ or by an at symbol @@.
sepp_nepp 15:88fecbdd191c 11
sepp_nepp 15:88fecbdd191c 12 * @author Tarek Lule, Francois Druilhe, et al.
sepp_nepp 15:88fecbdd191c 13 * @date 01. Nov. 2018.
sepp_nepp 15:88fecbdd191c 14 * @see https://os.mbed.com/users/sepp_nepp/code/MotorLib/ */
sepp_nepp 15:88fecbdd191c 15
sepp_nepp 15:88fecbdd191c 16 // -------------------- Motor ---------------------------
sepp_nepp 16:d818c1a4dafb 17
sepp_nepp 13:4563244c4071 18 #ifndef MOTOR_H
garphil 5:9886fd337a4b 19 #define MOTOR_H
garphil 5:9886fd337a4b 20
garphil 0:bd05fd602a6e 21 #include "mbed.h"
garphil 0:bd05fd602a6e 22
sepp_nepp 16:d818c1a4dafb 23 #define MOTOR_STEP_TIME_MIN_US 700 /**< Shortest Time between two motor steps = 0.7ms, was MOTOR_STEP_TIME_MIN*/
sepp_nepp 16:d818c1a4dafb 24 #define MOTOR_STEP_TIME_DEFAULT_US 5000 /**< Default Time between two motor steps = 5ms, was MOTOR_STEP_TIME_DEFAULT*/
sepp_nepp 16:d818c1a4dafb 25 #define MOTOR_STEPS_FOR_A_TURN 4096 /**< Default number of motor steps to complete a turn = 4096 steps */
garphil 9:5983c10d5f8e 26
sepp_nepp 15:88fecbdd191c 27 /** \enum motorStates
sepp_nepp 16:d818c1a4dafb 28 * \brief Motor States of Motor state machine
sepp_nepp 15:88fecbdd191c 29 *
sepp_nepp 15:88fecbdd191c 30 * Motor_CALIB is deprecated, was removed from the enum structure */
sepp_nepp 15:88fecbdd191c 31 typedef enum {
sepp_nepp 15:88fecbdd191c 32 Motor_OFF = 0, /**< All phase currents is off, replaces Motor_STOP. */
sepp_nepp 15:88fecbdd191c 33 Motor_ZERO, /**< Motor at phase position 0 and ON, only reached by call of Zero() command. */
sepp_nepp 16:d818c1a4dafb 34 Motor_ON, /**< Phases are engaged, but Motor does not run, replaces Motor_PAUSE. */
sepp_nepp 16:d818c1a4dafb 35 Motor_RUN /**< Phases are engaged, and Motor state machine runs*/
sepp_nepp 15:88fecbdd191c 36 } motorStates;
sepp_nepp 15:88fecbdd191c 37
sepp_nepp 15:88fecbdd191c 38 /** \enum motorCommands
sepp_nepp 15:88fecbdd191c 39 * \brief Commands that are handled by the Motor state machine
sepp_nepp 16:d818c1a4dafb 40
sepp_nepp 16:d818c1a4dafb 41 * These Commands are issued asynchonously by call Motor class methods.
sepp_nepp 15:88fecbdd191c 42 * They are executed in the state machine called by the ticker handler.
sepp_nepp 15:88fecbdd191c 43
sepp_nepp 15:88fecbdd191c 44 * OFF and STOP commands do not go through the state machine.
sepp_nepp 15:88fecbdd191c 45
sepp_nepp 15:88fecbdd191c 46 * MOTOR_restart is equivalent to and replaced by MOTOR_run.
sepp_nepp 15:88fecbdd191c 47 */
sepp_nepp 14:f0667bfc2e98 48 typedef enum { // Define Motor State Machine Commands
sepp_nepp 15:88fecbdd191c 49 MOTOR_nop = 0, /**< No active command to execute. */
sepp_nepp 15:88fecbdd191c 50 MOTOR_run, /**< Run Motor until Nsteps are achieved. */
sepp_nepp 15:88fecbdd191c 51 MOTOR_stop, /**< Stop immediately all activity, turn off Motor. */
sepp_nepp 15:88fecbdd191c 52 MOTOR_pause /**< Motor is temporarily paused from the state run. */
sepp_nepp 15:88fecbdd191c 53 } motorCommands;
garphil 0:bd05fd602a6e 54
sepp_nepp 15:88fecbdd191c 55 /** \enum motorDir
sepp_nepp 15:88fecbdd191c 56 * \brief Define Motor direction to be Clockwise or Anticlockwise
sepp_nepp 15:88fecbdd191c 57 */
sepp_nepp 15:88fecbdd191c 58 typedef enum motorDir {
sepp_nepp 15:88fecbdd191c 59 CLOCKWISE = 0, /**< Turn Motor in clockwise direction. */
sepp_nepp 15:88fecbdd191c 60 COUNTERCLOCKWISE /**< Turn Motor in anti-clockwise direction. */
sepp_nepp 15:88fecbdd191c 61 } motorDir;
sepp_nepp 13:4563244c4071 62
sepp_nepp 15:88fecbdd191c 63 /** \struct MotStatus
sepp_nepp 16:d818c1a4dafb 64 * \brief Structure of Motor Status registers.
sepp_nepp 16:d818c1a4dafb 65
sepp_nepp 16:d818c1a4dafb 66 * Used by Motor Class to hold all Status 'Registers'.
sepp_nepp 15:88fecbdd191c 67 * The structure can be requested to get by Motor.getStatus(). */
sepp_nepp 13:4563244c4071 68 typedef struct {
sepp_nepp 15:88fecbdd191c 69 motorStates state; /**< General state that the moter state machine is in.*/
sepp_nepp 15:88fecbdd191c 70 motorCommands cmd; /**< Command asked to be executed currently by the state machine.*/
sepp_nepp 16:d818c1a4dafb 71 motorDir dir; /**< Direction that the Motor is asked to run.*/
sepp_nepp 16:d818c1a4dafb 72 int32_t NSteps;/**< Number of steps remain for the Motor to run.
sepp_nepp 15:88fecbdd191c 73 NSteps=0: all steps finsihed; NSteps<0: indicates to run "forever" */
sepp_nepp 15:88fecbdd191c 74 bool TickIsAttached; /**< Indicates if Ticker is attached.
sepp_nepp 16:d818c1a4dafb 75 Ticker is automatically attached while Motor runs, or paused;
sepp_nepp 15:88fecbdd191c 76 detaches when finished a run, or stopped. */
sepp_nepp 16:d818c1a4dafb 77 void set(motorCommands aCmd, motorDir aDir, int32_t aNSteps); /**< Helper; set Command, Direction and NSteps in one call. */
sepp_nepp 15:88fecbdd191c 78 } MotStatus;
sepp_nepp 15:88fecbdd191c 79
sepp_nepp 16:d818c1a4dafb 80 /** ATTENTION UNDER CONSTRUCTION, DO NOT YET USE.
sepp_nepp 16:d818c1a4dafb 81 *
sepp_nepp 16:d818c1a4dafb 82 *Class of a Four Phase Stepper Motor.
sepp_nepp 14:f0667bfc2e98 83 *
sepp_nepp 16:d818c1a4dafb 84 *Perform Runs for number of steps, or given amount angle, but also Low-Level steps.
sepp_nepp 16:d818c1a4dafb 85 *
sepp_nepp 16:d818c1a4dafb 86 *High-Level Run functions have 'Run' in their name.
sepp_nepp 16:d818c1a4dafb 87 *They rely on tickers and return immediately after ticker is set up.
sepp_nepp 16:d818c1a4dafb 88 *A state-machine evaluates commands versus the Motor state at every tick.
sepp_nepp 16:d818c1a4dafb 89 *When End of Run is detected tickers stop, and Motor turns off.
sepp_nepp 15:88fecbdd191c 90 *
sepp_nepp 15:88fecbdd191c 91 *Callbacks can be attached to react to 'end of run' events.
sepp_nepp 16:d818c1a4dafb 92 *Attention: the attached Callback is called within a Ticker Callback.
sepp_nepp 16:d818c1a4dafb 93 * Your code you execute in the Callback should be short, must not use waits, or any long routines.
sepp_nepp 16:d818c1a4dafb 94 * Do not call any Motor run commands in the callback, as it creates conflict situations.
sepp_nepp 16:d818c1a4dafb 95 * Long Callback code may impair this and any other Ticker functions that run in your application.
sepp_nepp 14:f0667bfc2e98 96 *
sepp_nepp 16:d818c1a4dafb 97 *Low-Level functions directly talk to the hardware without ticker.
sepp_nepp 16:d818c1a4dafb 98 *Use of Low-Level functions while tickers still run may lead to unexpected behavior.
sepp_nepp 14:f0667bfc2e98 99 */
sepp_nepp 15:88fecbdd191c 100 class Motor
sepp_nepp 15:88fecbdd191c 101 {
sepp_nepp 15:88fecbdd191c 102 public:
sepp_nepp 16:d818c1a4dafb 103 /** Motor Class Creator
sepp_nepp 16:d818c1a4dafb 104 *
sepp_nepp 16:d818c1a4dafb 105 * Creates the class, initiallizes all fields, creates Phase Pins.
sepp_nepp 16:d818c1a4dafb 106 * Time between two steps defaults here to MOTOR_STEP_TIME_DEFAULT_US=5000usec.
sepp_nepp 16:d818c1a4dafb 107 *
sepp_nepp 15:88fecbdd191c 108 * @code
sepp_nepp 15:88fecbdd191c 109 * PinName MotPhases[] = {PB_1, PB_15, PB_14, PB_13};
sepp_nepp 16:d818c1a4dafb 110 * Motor MotorName(MotPhases); // Call this creator for example like this:
sepp_nepp 16:d818c1a4dafb 111 * @endcode
sepp_nepp 16:d818c1a4dafb 112 *
sepp_nepp 16:d818c1a4dafb 113 * @param _MPh[4] Array of Names of the 4 Digital Pins of type PinNames */
sepp_nepp 13:4563244c4071 114 Motor(PinName _MPh[4] );
sepp_nepp 16:d818c1a4dafb 115
sepp_nepp 16:d818c1a4dafb 116 /** Motor Class Creator
sepp_nepp 16:d818c1a4dafb 117 *
sepp_nepp 16:d818c1a4dafb 118 * Creates the class, initiallizes all fields, creates Phase Pins.
sepp_nepp 16:d818c1a4dafb 119 * Time between two steps defaults here to MOTOR_STEP_TIME_DEFAULT_US=5000usec.
sepp_nepp 16:d818c1a4dafb 120 *
sepp_nepp 16:d818c1a4dafb 121 * @param <PB_1, PB_15, PB_14, PB_13> List of Names of the 4 Digital Pins of type PinNames
sepp_nepp 16:d818c1a4dafb 122 *
sepp_nepp 15:88fecbdd191c 123 * @code
sepp_nepp 16:d818c1a4dafb 124 * // Call this creator for example like this:
sepp_nepp 15:88fecbdd191c 125 * Motor MotorName(PB_1, PB_15, PB_14, PB_13);
sepp_nepp 15:88fecbdd191c 126 * @endcode */
garphil 9:5983c10d5f8e 127 Motor(PinName _MPh0, PinName _MPh1, PinName _MPh2, PinName _MPh3);
sepp_nepp 16:d818c1a4dafb 128
sepp_nepp 16:d818c1a4dafb 129 /** Motor Class Creator
sepp_nepp 16:d818c1a4dafb 130 *
sepp_nepp 16:d818c1a4dafb 131 * Creates the class, initiallizes all fields, creates Phase Pins.
sepp_nepp 16:d818c1a4dafb 132 * Time between two steps is passed as parameter.
sepp_nepp 16:d818c1a4dafb 133 *
sepp_nepp 16:d818c1a4dafb 134 * @code
sepp_nepp 16:d818c1a4dafb 135 * // Call this creator for example like this:
sepp_nepp 16:d818c1a4dafb 136 * Motor MotorName(PB_1, PB_15, PB_14, PB_13);
sepp_nepp 16:d818c1a4dafb 137 * @endcode
sepp_nepp 16:d818c1a4dafb 138 *
sepp_nepp 16:d818c1a4dafb 139 * @param <PB_1, PB_15, PB_14, PB_13> List of Names of the 4 Digital Pins of type PinNames
sepp_nepp 16:d818c1a4dafb 140 * @param <aStepTime_us> Lthe time in usec between two steps, thats used initially. */
sepp_nepp 13:4563244c4071 141 Motor(PinName _MPh0, PinName _MPh1, PinName _MPh2, PinName _MPh3, uint32_t aStepTime_us);
sepp_nepp 16:d818c1a4dafb 142
sepp_nepp 15:88fecbdd191c 143 private:
sepp_nepp 16:d818c1a4dafb 144 // deprecated: void initialization(PinName _MPh0, PinName _MPh1, PinName _MPh2, PinName _MPh3, uint32_t aStepTime_us);
sepp_nepp 13:4563244c4071 145 void initialization(PinName _MPh[4], uint32_t aStepTime_us);
sepp_nepp 16:d818c1a4dafb 146
sepp_nepp 15:88fecbdd191c 147 public:
sepp_nepp 15:88fecbdd191c 148 /** Attach a basic Callback function.
sepp_nepp 16:d818c1a4dafb 149 *
sepp_nepp 16:d818c1a4dafb 150 * Only called when a Run Command reaches it's requested end.
sepp_nepp 16:d818c1a4dafb 151 * Not called when the Motor is stopped by a call of Stop Function, or any other events.
sepp_nepp 16:d818c1a4dafb 152 * For use see precautions at Class description above.
sepp_nepp 16:d818c1a4dafb 153 * Formerly called setMotorCallback()
sepp_nepp 16:d818c1a4dafb 154 @code
sepp_nepp 16:d818c1a4dafb 155 // Simple callback function, state variable endMove can be polled elsewhere
sepp_nepp 16:d818c1a4dafb 156 void CallBackFunction()
sepp_nepp 16:d818c1a4dafb 157 { endMove=true; }
sepp_nepp 15:88fecbdd191c 158
sepp_nepp 16:d818c1a4dafb 159 // main routine
sepp_nepp 16:d818c1a4dafb 160 void main()
sepp_nepp 16:d818c1a4dafb 161 { ...
sepp_nepp 16:d818c1a4dafb 162 // Attach callback function:
sepp_nepp 16:d818c1a4dafb 163 MotorInstance->callbackSet(CallBackFunction);
sepp_nepp 16:d818c1a4dafb 164 ...
sepp_nepp 16:d818c1a4dafb 165 while (true) {
sepp_nepp 16:d818c1a4dafb 166 ....
sepp_nepp 16:d818c1a4dafb 167 if (endMove) // poll the endMove flag
sepp_nepp 16:d818c1a4dafb 168 { ... } // react to Movement End
sepp_nepp 16:d818c1a4dafb 169
sepp_nepp 16:d818c1a4dafb 170 ....
sepp_nepp 16:d818c1a4dafb 171 }
sepp_nepp 16:d818c1a4dafb 172 }
sepp_nepp 16:d818c1a4dafb 173 @endcode
sepp_nepp 16:d818c1a4dafb 174 * @param <*CBfunction> Callback function, must not be member of a class.
sepp_nepp 16:d818c1a4dafb 175
sepp_nepp 16:d818c1a4dafb 176 */
sepp_nepp 16:d818c1a4dafb 177 void callbackSet(void (*CBfunction)());
sepp_nepp 16:d818c1a4dafb 178
sepp_nepp 16:d818c1a4dafb 179 /** Attach a Callback function, member of a class.
sepp_nepp 16:d818c1a4dafb 180 * Only called when a Run Command reaches it's requested end.
sepp_nepp 16:d818c1a4dafb 181 * Not called when the Motor is stopped by a call of Stop Function, or any other events.
sepp_nepp 16:d818c1a4dafb 182 * For use see precautions at Class description above.
sepp_nepp 16:d818c1a4dafb 183 * @param <T *object> Class pointer who possesses callback member.
sepp_nepp 16:d818c1a4dafb 184 * @param <T::*CBmember> Pointer to callback function, member of Class.
sepp_nepp 16:d818c1a4dafb 185 *
sepp_nepp 15:88fecbdd191c 186 @code
sepp_nepp 15:88fecbdd191c 187 // Class Creator:
sepp_nepp 15:88fecbdd191c 188 AClass::AClass(Class Creation Parameters)
sepp_nepp 15:88fecbdd191c 189 { ...
sepp_nepp 15:88fecbdd191c 190 // Attach callback function:
sepp_nepp 15:88fecbdd191c 191 MotorInstance->setMotorCallback(this, &AClass::CallBackMemberFunction);
sepp_nepp 16:d818c1a4dafb 192 ...
sepp_nepp 15:88fecbdd191c 193 }
sepp_nepp 15:88fecbdd191c 194
sepp_nepp 16:d818c1a4dafb 195 // Simple callback function, state variable endMove can be polled by main thread
sepp_nepp 15:88fecbdd191c 196 void AClass::CallBackMemberFunction()
sepp_nepp 16:d818c1a4dafb 197 { endMove=true; }
sepp_nepp 15:88fecbdd191c 198 @endcode
sepp_nepp 15:88fecbdd191c 199 */
sepp_nepp 13:4563244c4071 200 template<typename T>
sepp_nepp 16:d818c1a4dafb 201 void callbackSet(T *object, void (T::*CBmember)(void)) {
sepp_nepp 16:d818c1a4dafb 202 _callback = callback(object,CBmember);
sepp_nepp 15:88fecbdd191c 203 }
sepp_nepp 15:88fecbdd191c 204
sepp_nepp 16:d818c1a4dafb 205 /** Remove the Callback function that may have been attached previously. */
sepp_nepp 16:d818c1a4dafb 206 void callbackRemove();
sepp_nepp 15:88fecbdd191c 207
sepp_nepp 16:d818c1a4dafb 208 /** High Level: Run Motor for a number of Steps.
sepp_nepp 16:d818c1a4dafb 209 *
sepp_nepp 16:d818c1a4dafb 210 * During Run: Uses ticker; State: first Motor_ON then Motor_RUN; cmd=MOTOR_run.
sepp_nepp 16:d818c1a4dafb 211 * Call Pause() or Stop() to pause or end the run prematurely.
sepp_nepp 16:d818c1a4dafb 212 * At the end: calls the Callback, stops ticker; State: Motor_OFF.
sepp_nepp 16:d818c1a4dafb 213 * @param[in] <Direction> Given Direction, can be: CLOCKWISE, or COUNTERCLOCKWISE.
sepp_nepp 16:d818c1a4dafb 214 * @param[in] <Nsteps> Number of steps to run for; must be >0 ; Nsteps<=0 will not be executed.
sepp_nepp 16:d818c1a4dafb 215 */
sepp_nepp 16:d818c1a4dafb 216 void RunSteps (motorDir direction, uint32_t Nsteps);
sepp_nepp 15:88fecbdd191c 217
sepp_nepp 16:d818c1a4dafb 218 /** High Level: Run Motor for a given angle
sepp_nepp 16:d818c1a4dafb 219
sepp_nepp 16:d818c1a4dafb 220 * Runs Motor for a given angle in given direction.
sepp_nepp 16:d818c1a4dafb 221 * Call Pause() or Stop() to pause or end the Motor run prematurely.
sepp_nepp 16:d818c1a4dafb 222 * While run: Uses ticker; State: first Motor_ON then Motor_RUN; cmd=MOTOR_run.
sepp_nepp 16:d818c1a4dafb 223 * At end: calls attached Callback, stops ticker; State: Motor_OFF; cmd=MOTOR_stop then MOTOR_nop.
sepp_nepp 16:d818c1a4dafb 224 * @param[in] <Direction> Given Direction, can be: CLOCKWISE, or COUNTERCLOCKWISE.
sepp_nepp 16:d818c1a4dafb 225 * @param[in] <angle_deg> Angle>0 to rotate for, in degrees, Angles<=0 are not executed.
sepp_nepp 16:d818c1a4dafb 226 */
sepp_nepp 16:d818c1a4dafb 227 void RunDegrees (motorDir direction, float angle_deg);
garphil 9:5983c10d5f8e 228
sepp_nepp 16:d818c1a4dafb 229 /** High Level: Run Motor "unlimited"
sepp_nepp 16:d818c1a4dafb 230
sepp_nepp 16:d818c1a4dafb 231 * Runs Motor with out limit in given direction, precisely runs 4Billion Steps.
sepp_nepp 16:d818c1a4dafb 232 * While run: Uses ticker; State: first Motor_ON then Motor_RUN; cmd=MOTOR_run.
sepp_nepp 16:d818c1a4dafb 233 * Call Pause() or Stop() to pause or end the Motor run.
sepp_nepp 16:d818c1a4dafb 234 * @param[in] <Direction> Given Direction, can be: CLOCKWISE, or COUNTERCLOCKWISE.
sepp_nepp 16:d818c1a4dafb 235 */
sepp_nepp 13:4563244c4071 236 void RunInfinite (motorDir direction);
sepp_nepp 15:88fecbdd191c 237
sepp_nepp 16:d818c1a4dafb 238 /** High Level: Pause a motor Run.
sepp_nepp 16:d818c1a4dafb 239 * Put Motor into Pause state, Run is suspended, but only effective if Status.cmd=MOTOR_run.
sepp_nepp 16:d818c1a4dafb 240 * Retains the number of steps that remain to be run if restarting run.
sepp_nepp 16:d818c1a4dafb 241 * While paused: still uses ticker; State: Motor_RUN; cmd=MOTOR_pause.
sepp_nepp 16:d818c1a4dafb 242 * Use RestartRun(); to continue. */
sepp_nepp 16:d818c1a4dafb 243 void PauseRun();
sepp_nepp 15:88fecbdd191c 244
sepp_nepp 16:d818c1a4dafb 245 /** High Level: Restart a Paused Run.
sepp_nepp 16:d818c1a4dafb 246 * Restart the Run that was launched before calling PuaseRun.
sepp_nepp 16:d818c1a4dafb 247 * Only effective if Status.cmd=MOTOR_pause, otherwise no re/action.
sepp_nepp 14:f0667bfc2e98 248 * Status afterwards is same as afterRun commands. */
sepp_nepp 16:d818c1a4dafb 249 void RestartRun();
sepp_nepp 15:88fecbdd191c 250
sepp_nepp 16:d818c1a4dafb 251 /** High Level: End any Run.
sepp_nepp 16:d818c1a4dafb 252 * Force stop of any ongoing run, but does not call the Callback function.
sepp_nepp 16:d818c1a4dafb 253 * Only effective if Status.cmd=MOTOR_run, otherwise no re/action.
sepp_nepp 14:f0667bfc2e98 254 * Emits first cmd=MOTOR_stop then cmd=MOTOR_nop.
sepp_nepp 16:d818c1a4dafb 255 * Aftewards: ticker is detached; State: Motor_OFF; */
sepp_nepp 16:d818c1a4dafb 256 void StopRun();
sepp_nepp 16:d818c1a4dafb 257
sepp_nepp 16:d818c1a4dafb 258 public: // All the ticker timing related parameters
sepp_nepp 16:d818c1a4dafb 259
sepp_nepp 16:d818c1a4dafb 260 /** MidLevel: Get Motor status
sepp_nepp 16:d818c1a4dafb 261
sepp_nepp 16:d818c1a4dafb 262 * Gets the Status of the different internal mechanisms.
sepp_nepp 16:d818c1a4dafb 263 * See documentation of MotStatus Structure.
sepp_nepp 16:d818c1a4dafb 264 * @return <MotStatus> The structure of Motor status registers. */
sepp_nepp 13:4563244c4071 265 MotStatus getStatus();
sepp_nepp 16:d818c1a4dafb 266
sepp_nepp 16:d818c1a4dafb 267 /** MidLevel: Get number of Steps per Full turn
sepp_nepp 16:d818c1a4dafb 268
sepp_nepp 16:d818c1a4dafb 269 * Defaults to MOTOR_STEPS_FOR_A_TURN = 4096.
sepp_nepp 16:d818c1a4dafb 270 * Used by RunDegrees() to translate from angle in degrees to number of steps.
sepp_nepp 16:d818c1a4dafb 271 * Old Name was: getCalibration, but that was not a good explicit name.
sepp_nepp 16:d818c1a4dafb 272 * @return <MotStatus> The structure of Motor status registers. */
sepp_nepp 15:88fecbdd191c 273 uint32_t getStepsFullTurn();
sepp_nepp 15:88fecbdd191c 274
sepp_nepp 16:d818c1a4dafb 275 /** MidLevel: Set number of Steps per Full turn.
sepp_nepp 16:d818c1a4dafb 276
sepp_nepp 16:d818c1a4dafb 277 * Defaults is MOTOR_STEPS_FOR_A_TURN = 4096.
sepp_nepp 16:d818c1a4dafb 278 * Used by RunDegrees() to translate from degrees to number of steps.
sepp_nepp 16:d818c1a4dafb 279 * Old Name was: setCalibration, but not good explicit name.
sepp_nepp 16:d818c1a4dafb 280 * @param <StepsFullTurn> Number of steps needed to complete a full motor turn
sepp_nepp 16:d818c1a4dafb 281 */
sepp_nepp 13:4563244c4071 282 void setStepsFullTurn(uint32_t StepsFullTurn);
sepp_nepp 15:88fecbdd191c 283
sepp_nepp 16:d818c1a4dafb 284 /** Mid Level: Get the Motor step time.
sepp_nepp 16:d818c1a4dafb 285
sepp_nepp 16:d818c1a4dafb 286 * Step time is time between two Motor steps, and is given in microseconds
sepp_nepp 16:d818c1a4dafb 287 * and is passed to the ticker as delay time.
sepp_nepp 16:d818c1a4dafb 288 * So the larger the value the slower the motor speed.
sepp_nepp 16:d818c1a4dafb 289 * Defaults to MOTOR_STEP_TIME_DEFAULT_US = 5000.
sepp_nepp 16:d818c1a4dafb 290 * @return <uint32_t> The structure of Motor status registers.
sepp_nepp 16:d818c1a4dafb 291 */
sepp_nepp 16:d818c1a4dafb 292 uint32_t getStepTime_us;
sepp_nepp 16:d818c1a4dafb 293
sepp_nepp 16:d818c1a4dafb 294 /** Set the time in microseconds between two Motor steps.
sepp_nepp 16:d818c1a4dafb 295 * Defaults to MOTOR_STEP_TIME_DEFAULT_US = 5000usec.
sepp_nepp 16:d818c1a4dafb 296 * Filters values below Minimum Value = 700.
sepp_nepp 16:d818c1a4dafb 297 * Passed to the ticker as delay time.
sepp_nepp 16:d818c1a4dafb 298 * Can be called while ticker is running, and takes immediate effect.
sepp_nepp 16:d818c1a4dafb 299 * Was previously called setStepTime(), but was not clear which units.
sepp_nepp 16:d818c1a4dafb 300 * @param <aStepTime_us> the time in microseconds between two Motor steps
sepp_nepp 16:d818c1a4dafb 301 */
sepp_nepp 15:88fecbdd191c 302 void setStepTime_us(uint32_t aStepTime_us);
sepp_nepp 15:88fecbdd191c 303
sepp_nepp 15:88fecbdd191c 304 /** Set the time in seconds to get one full turn, rotation of 360°.
sepp_nepp 16:d818c1a4dafb 305 * was previously called setSpeed().
sepp_nepp 16:d818c1a4dafb 306 * @param <Seconds_Per_Turn> Period of Rotation, e.g. if =20.0 then Motor will do 360° in 20 seconds.
sepp_nepp 16:d818c1a4dafb 307 */
sepp_nepp 13:4563244c4071 308 void setRotationPeriodSec(float Seconds_Per_Turn) ;
sepp_nepp 15:88fecbdd191c 309
sepp_nepp 15:88fecbdd191c 310 private:
sepp_nepp 16:d818c1a4dafb 311 // all the Ticker and Timing procedures, used to run the Motor for a duration
sepp_nepp 13:4563244c4071 312 void StartTick();
garphil 1:9519ac966b79 313 void ProcessMotorStateMachine();
sepp_nepp 13:4563244c4071 314 // The call back function pointer that is called when the Processor
sepp_nepp 15:88fecbdd191c 315 // State Machine reaches its end.
sepp_nepp 13:4563244c4071 316 Callback<void()> _callback;
sepp_nepp 13:4563244c4071 317 void StopTick();
sepp_nepp 15:88fecbdd191c 318 MotStatus Status;
sepp_nepp 16:d818c1a4dafb 319 timestamp_t StepTime_us; // Time in µs for one Motor step
sepp_nepp 15:88fecbdd191c 320 Ticker MotorSysTick; // System Timer for Motor
sepp_nepp 16:d818c1a4dafb 321 uint32_t Steps_FullTurn;// Number of step for a complete turn
sepp_nepp 16:d818c1a4dafb 322
sepp_nepp 16:d818c1a4dafb 323 public: // all the low level direct Motor HW access, States are immediately reached
sepp_nepp 16:d818c1a4dafb 324
sepp_nepp 16:d818c1a4dafb 325 /** Low Level: Run one full turn clockwise then anticlockwise.
sepp_nepp 16:d818c1a4dafb 326 * After: State: Motor_OFF.
sepp_nepp 16:d818c1a4dafb 327 * Blocking function, returns back only after end of full movement.
sepp_nepp 16:d818c1a4dafb 328 */
sepp_nepp 16:d818c1a4dafb 329 void MotorTest();
sepp_nepp 15:88fecbdd191c 330
sepp_nepp 16:d818c1a4dafb 331 /** Low Level: turn off all Motor Phases
sepp_nepp 16:d818c1a4dafb 332 * No more current flows, reduces holding force.
sepp_nepp 16:d818c1a4dafb 333 * After: State: Motor_OFF.
sepp_nepp 16:d818c1a4dafb 334 * StepPhases memorizes the last used phase.
sepp_nepp 16:d818c1a4dafb 335 * Equivalent what previously the function "void Stop();" did . */
sepp_nepp 13:4563244c4071 336 void MotorOFF();
sepp_nepp 16:d818c1a4dafb 337
sepp_nepp 16:d818c1a4dafb 338 /** Low Level: turn on the Motor Phases in the last used phase.
sepp_nepp 16:d818c1a4dafb 339 * The last used phase is held in StepPhases.
sepp_nepp 16:d818c1a4dafb 340 * After: State: Motor_ON, or Motor_ZERO if StepPhases==0
sepp_nepp 16:d818c1a4dafb 341 * Equivalent to what previously the function "void Start();" did. */
sepp_nepp 13:4563244c4071 342 void MotorON();
sepp_nepp 16:d818c1a4dafb 343
sepp_nepp 16:d818c1a4dafb 344 /** Low Level: Advance Motor one step, rotates in direction of variable direction. */
sepp_nepp 13:4563244c4071 345 void StepOnce();
sepp_nepp 16:d818c1a4dafb 346
sepp_nepp 16:d818c1a4dafb 347 /** Low Level: Advance Motor one step, rotates CounterClockwise. */
sepp_nepp 16:d818c1a4dafb 348 void StepCCW();
sepp_nepp 16:d818c1a4dafb 349
sepp_nepp 16:d818c1a4dafb 350 /** Low Level: Advance Motor one step, rotates Clockwise. */
sepp_nepp 16:d818c1a4dafb 351 void StepClkW();
sepp_nepp 16:d818c1a4dafb 352
sepp_nepp 16:d818c1a4dafb 353 /** Low Level: turn on the Motor Phases in Zero Position.
sepp_nepp 16:d818c1a4dafb 354 * After: State: Motor_ZERO, StepPhases==0
sepp_nepp 16:d818c1a4dafb 355 */
sepp_nepp 16:d818c1a4dafb 356 void MotorZero();
sepp_nepp 16:d818c1a4dafb 357
sepp_nepp 16:d818c1a4dafb 358
sepp_nepp 16:d818c1a4dafb 359 private:
sepp_nepp 16:d818c1a4dafb 360
sepp_nepp 16:d818c1a4dafb 361 /** Low Level: Engage Motor Phases according to MotorIndex. */
sepp_nepp 16:d818c1a4dafb 362 void SetPhases();
sepp_nepp 13:4563244c4071 363
sepp_nepp 13:4563244c4071 364 DigitalOut *MPh[4]; // Digital outputs, one per phase
sepp_nepp 16:d818c1a4dafb 365 int StepPhase; // Motor Phase Variable, counts up and down with every step
garphil 5:9886fd337a4b 366 };
garphil 5:9886fd337a4b 367
sepp_nepp 16:d818c1a4dafb 368 #endif