Tarek Lule / MotorLib

Fork of MotorLib by CreaLab

Committer:
sepp_nepp
Date:
Sun Dec 30 23:10:47 2018 +0000
Revision:
19:2ac158fe414e
Parent:
18:00e3d8c71a9c
Child:
20:492140a08f05
Release for Doxygen debug

Who changed what in which revision?

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