Tarek Lule / MotorLib

Fork of MotorLib by CreaLab

Committer:
sepp_nepp
Date:
Thu Jan 03 23:15:42 2019 +0000
Revision:
22:d2c742bdae16
Parent:
21:588c5254437d
Child:
23:6060422cd6eb
Updates that makes the motor aware of the wheel diameter.

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 22:d2c742bdae16 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 */
sepp_nepp 22:d2c742bdae16 26 #define CLOCKWISE true /**< Constant for Clockwise direction */
sepp_nepp 22:d2c742bdae16 27 #define COUNTERCLOCKWISE false /**< Constant for Counter-Clockwise direction */
sepp_nepp 22:d2c742bdae16 28 #define MIN_SPEED_CM_SEC 0.1f /**< Constant for minimum speed in cm/sec */
garphil 9:5983c10d5f8e 29
sepp_nepp 22:d2c742bdae16 30 /** \enum motStates
sepp_nepp 19:2ac158fe414e 31 * \brief Possible States of Motor state machine
sepp_nepp 19:2ac158fe414e 32 *
sepp_nepp 19:2ac158fe414e 33 * Motor_CALIB is deprecated, was removed from the enum structure */
sepp_nepp 19:2ac158fe414e 34 typedef enum {
sepp_nepp 19:2ac158fe414e 35 Motor_OFF = 0, /**< All phase currents is off, replaces Motor_STOP. */
sepp_nepp 19:2ac158fe414e 36 Motor_ZERO, /**< Motor at phase position 0 and ON, only reached by call of Zero() procedure. */
sepp_nepp 19:2ac158fe414e 37 Motor_ON, /**< Phases are engaged, but Motor state machine stopped, replaces Motor_PAUSE. */
sepp_nepp 19:2ac158fe414e 38 Motor_RUN /**< Phases are engaged, and Motor state machine runs*/
sepp_nepp 22:d2c742bdae16 39 } motStates;
sepp_nepp 19:2ac158fe414e 40
sepp_nepp 22:d2c742bdae16 41 /** \enum motCmands
sepp_nepp 19:2ac158fe414e 42 * \brief Commands that are handled by the Motor state machine
sepp_nepp 22:d2c742bdae16 43 *
sepp_nepp 19:2ac158fe414e 44 * These Commands are issued asynchonously by call Motor class methods.
sepp_nepp 19:2ac158fe414e 45 * They are executed in the state machine called by the ticker handler.
sepp_nepp 22:d2c742bdae16 46 *
sepp_nepp 19:2ac158fe414e 47 * OFF and STOP commands do not go through the state machine.
sepp_nepp 22:d2c742bdae16 48 *
sepp_nepp 19:2ac158fe414e 49 * MOTOR_restart is equivalent to and replaced by MOTOR_run.
sepp_nepp 19:2ac158fe414e 50 */
sepp_nepp 19:2ac158fe414e 51 typedef enum {
sepp_nepp 19:2ac158fe414e 52 MOTOR_nop = 0, /**< No active command to execute. */
sepp_nepp 19:2ac158fe414e 53 MOTOR_run, /**< Run Motor until Nsteps are achieved. */
sepp_nepp 19:2ac158fe414e 54 MOTOR_stop, /**< Stop immediately all activity, turn off Motor. */
sepp_nepp 19:2ac158fe414e 55 MOTOR_pause /**< Motor is temporarily paused from the state run. */
sepp_nepp 22:d2c742bdae16 56 } motCmands;
sepp_nepp 13:4563244c4071 57
sepp_nepp 19:2ac158fe414e 58 /** \struct MotStatus
sepp_nepp 19:2ac158fe414e 59 * \brief Structure of Motor Status registers.
sepp_nepp 22:d2c742bdae16 60 *
sepp_nepp 19:2ac158fe414e 61 * Used by Motor Class to hold all Status 'Registers'.
sepp_nepp 22:d2c742bdae16 62 * The structure can be requested to get by Motor.getStatus().
sepp_nepp 22:d2c742bdae16 63 */
sepp_nepp 19:2ac158fe414e 64 typedef struct {
sepp_nepp 22:d2c742bdae16 65 motStates state; /**< General state that the motor state machine is in.*/
sepp_nepp 22:d2c742bdae16 66 motCmands cmd; /**< Command asked to be executed currently by the state machine.*/
sepp_nepp 22:d2c742bdae16 67 bool ClockWise; /**< Direction that the Motor is asked to run. True if Clockwise */
sepp_nepp 22:d2c742bdae16 68 int32_t NSteps; /**< Number of steps remain for the Motor to run.
sepp_nepp 22:d2c742bdae16 69 NSteps=0: all steps finished; NSteps<0: indicates to run "forever" */
sepp_nepp 22:d2c742bdae16 70 bool TickIsAttached;
sepp_nepp 22:d2c742bdae16 71 /**< Indicates if Ticker is attached.
sepp_nepp 22:d2c742bdae16 72 Ticker is automatically attached while Motor runs, or paused;
sepp_nepp 22:d2c742bdae16 73 detaches when finished a run, or stopped. */
sepp_nepp 22:d2c742bdae16 74 void set(motCmands aCmd, bool AClockWise, int32_t aNSteps);
sepp_nepp 22:d2c742bdae16 75 /**< Helper; set Command, Direction and NSteps in one call. */
sepp_nepp 19:2ac158fe414e 76 } MotStatus;
sepp_nepp 18:00e3d8c71a9c 77
sepp_nepp 19:2ac158fe414e 78 /** ATTENTION UNDER CONSTRUCTION, DO NOT YET USE.
sepp_nepp 19:2ac158fe414e 79 *
sepp_nepp 19:2ac158fe414e 80 *Class of a Four Phase Stepper Motor.
sepp_nepp 19:2ac158fe414e 81 *
sepp_nepp 19:2ac158fe414e 82 *Perform Runs for number of steps, or given amount angle, but also Low-Level steps.
sepp_nepp 19:2ac158fe414e 83 *
sepp_nepp 19:2ac158fe414e 84 *High-Level Run functions have 'Run' in their name.
sepp_nepp 19:2ac158fe414e 85 *They rely on tickers and return immediately after ticker is set up.
sepp_nepp 19:2ac158fe414e 86 *A state-machine evaluates commands versus the Motor state at every tick.
sepp_nepp 19:2ac158fe414e 87 *When End of Run is detected tickers stop, and Motor turns off.
sepp_nepp 19:2ac158fe414e 88 *
sepp_nepp 19:2ac158fe414e 89 *Callbacks can be attached to react to 'end of run' events.
sepp_nepp 19:2ac158fe414e 90 *Attention: the attached Callback is called within a Ticker Callback.
sepp_nepp 19:2ac158fe414e 91 * Your code you execute in the Callback should be short, must not use waits, or any long routines.
sepp_nepp 19:2ac158fe414e 92 * Do not call any Motor run commands in the callback, as it creates conflict situations.
sepp_nepp 19:2ac158fe414e 93 * Long Callback code may impair this and any other Ticker functions that run in your application.
sepp_nepp 19:2ac158fe414e 94 *
sepp_nepp 19:2ac158fe414e 95 *Low-Level functions directly talk to the hardware without ticker.
sepp_nepp 19:2ac158fe414e 96 *Use of Low-Level functions while tickers still run may lead to unexpected behavior.
sepp_nepp 22:d2c742bdae16 97 *
sepp_nepp 22:d2c742bdae16 98 * NB: all times are uint32_t, step numbers are int32_t
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 22:d2c742bdae16 107 * Pin names are used to create digital outputs: Pin0 = new DigitalOut(_MPh0)
sepp_nepp 19:2ac158fe414e 108 *
sepp_nepp 19:2ac158fe414e 109 @code
sepp_nepp 19:2ac158fe414e 110 PinName MotPhases[] = {PB_1, PB_15, PB_14, PB_13};
sepp_nepp 19:2ac158fe414e 111 Motor MotorName(MotPhases); // Call this creator for example like this:
sepp_nepp 19:2ac158fe414e 112 @endcode
sepp_nepp 19:2ac158fe414e 113 *
sepp_nepp 19:2ac158fe414e 114 * @param _MPh Array of Names of the 4 Digital Pins of type PinNames
sepp_nepp 19:2ac158fe414e 115 */
sepp_nepp 19:2ac158fe414e 116 Motor(PinName _MPh[4] );
sepp_nepp 19:2ac158fe414e 117
sepp_nepp 19:2ac158fe414e 118 /** Motor Class Creator
sepp_nepp 19:2ac158fe414e 119 *
sepp_nepp 19:2ac158fe414e 120 * Creates the class, initiallizes all fields, creates Phase Pins.
sepp_nepp 19:2ac158fe414e 121 * Time between two steps defaults here to MOTOR_STEP_TIME_DEFAULT_US=5000usec.
sepp_nepp 22:d2c742bdae16 122 * Pin names are used to create digital outputs: Pin0 = new DigitalOut(_MPh0)
sepp_nepp 19:2ac158fe414e 123 *
sepp_nepp 19:2ac158fe414e 124 @code
sepp_nepp 19:2ac158fe414e 125 // Call this creator for example like this:
sepp_nepp 19:2ac158fe414e 126 Motor MotorName(PB_1, PB_15, PB_14, PB_13);
sepp_nepp 19:2ac158fe414e 127 @endcode
sepp_nepp 19:2ac158fe414e 128 *
sepp_nepp 19:2ac158fe414e 129 * @param <_MPh0, _MPh1, _MPh2, _MPh3> List of Names of the 4 Digital Pins of type PinNames
sepp_nepp 19:2ac158fe414e 130 */
sepp_nepp 19:2ac158fe414e 131 Motor(PinName _MPh0, PinName _MPh1, PinName _MPh2, PinName _MPh3);
sepp_nepp 19:2ac158fe414e 132
sepp_nepp 19:2ac158fe414e 133 /** Motor Class Creator
sepp_nepp 19:2ac158fe414e 134 *
sepp_nepp 19:2ac158fe414e 135 * Creates the class, initiallizes all fields, creates Phase Pins.
sepp_nepp 19:2ac158fe414e 136 * Time between two steps is passed as parameter.
sepp_nepp 22:d2c742bdae16 137 * Pin names are used to create digital outputs: Pin0 = new DigitalOut(_MPh0)
sepp_nepp 19:2ac158fe414e 138 *
sepp_nepp 19:2ac158fe414e 139 @code
sepp_nepp 19:2ac158fe414e 140 // Call this creator for example like this:
sepp_nepp 19:2ac158fe414e 141 Motor MotorName(PB_1, PB_15, PB_14, PB_13);
sepp_nepp 19:2ac158fe414e 142 @endcode
sepp_nepp 19:2ac158fe414e 143 *
sepp_nepp 19:2ac158fe414e 144 * @param <_MPh0, _MPh1, _MPh2, _MPh3> List of Names of the 4 Digital Pins of type PinNames
sepp_nepp 22:d2c742bdae16 145 * @param <AStepTime_us> Lthe time in usec between two steps, thats used initially.
sepp_nepp 19:2ac158fe414e 146 */
sepp_nepp 22:d2c742bdae16 147 Motor(PinName _MPh0, PinName _MPh1, PinName _MPh2, PinName _MPh3, uint32_t AStepTime_us);
sepp_nepp 18:00e3d8c71a9c 148
sepp_nepp 19:2ac158fe414e 149 private:
sepp_nepp 22:d2c742bdae16 150 // deprecated: void initialization(PinName _MPh0, PinName _MPh1, PinName _MPh2, PinName _MPh3, uint32_t AStepTime_us);
sepp_nepp 22:d2c742bdae16 151 void initialization(PinName _MPh[4], uint32_t AStepTime_us);
sepp_nepp 16:d818c1a4dafb 152
sepp_nepp 19:2ac158fe414e 153 public:
sepp_nepp 19:2ac158fe414e 154 /** Attach a basic Callback function.
sepp_nepp 19:2ac158fe414e 155 *
sepp_nepp 19:2ac158fe414e 156 * Only called when a Run Command reaches it's requested end.
sepp_nepp 19:2ac158fe414e 157 * Not called when the Motor is stopped by a call of Stop Function, or any other events.
sepp_nepp 19:2ac158fe414e 158 * For use see precautions at Class description above.
sepp_nepp 19:2ac158fe414e 159 * Formerly called setMotorCallback()
sepp_nepp 19:2ac158fe414e 160 *
sepp_nepp 19:2ac158fe414e 161 @code
sepp_nepp 19:2ac158fe414e 162 // Simple callback function, state variable endMove can be polled elsewhere
sepp_nepp 19:2ac158fe414e 163 void CallBackFunction()
sepp_nepp 19:2ac158fe414e 164 { endMove=true; }
sepp_nepp 15:88fecbdd191c 165
sepp_nepp 19:2ac158fe414e 166 // main routine
sepp_nepp 19:2ac158fe414e 167 void main()
sepp_nepp 19:2ac158fe414e 168 { ...
sepp_nepp 19:2ac158fe414e 169 // Attach callback function:
sepp_nepp 19:2ac158fe414e 170 MotorInstance->callbackSet(CallBackFunction);
sepp_nepp 19:2ac158fe414e 171 ...
sepp_nepp 19:2ac158fe414e 172 while (true) {
sepp_nepp 19:2ac158fe414e 173 ....
sepp_nepp 19:2ac158fe414e 174 if (endMove) // poll the endMove flag
sepp_nepp 19:2ac158fe414e 175 { ... } // react to Movement End
sepp_nepp 19:2ac158fe414e 176
sepp_nepp 19:2ac158fe414e 177 ....
sepp_nepp 19:2ac158fe414e 178 }
sepp_nepp 19:2ac158fe414e 179 }
sepp_nepp 19:2ac158fe414e 180 @endcode
sepp_nepp 19:2ac158fe414e 181 * @param <*CBfunction> Callback function, must not be member of a class.
sepp_nepp 19:2ac158fe414e 182
sepp_nepp 19:2ac158fe414e 183 */
sepp_nepp 22:d2c742bdae16 184 void callbackSet(void (*CBfunction)(void)) {_callback = CBfunction;};
sepp_nepp 22:d2c742bdae16 185
sepp_nepp 19:2ac158fe414e 186
sepp_nepp 19:2ac158fe414e 187 /** Attach a Callback function, member of a class.
sepp_nepp 19:2ac158fe414e 188 * Only called when a Run Command reaches it's requested end.
sepp_nepp 19:2ac158fe414e 189 * Not called when the Motor is stopped by a call of Stop Function, or any other events.
sepp_nepp 19:2ac158fe414e 190 * For use see precautions at Class description above.
sepp_nepp 19:2ac158fe414e 191 * @param <*object> Class pointer which possesses callback member.
sepp_nepp 19:2ac158fe414e 192 * @param <*CBmember> Pointer to callback function, member of Class.
sepp_nepp 19:2ac158fe414e 193 *
sepp_nepp 19:2ac158fe414e 194 @code
sepp_nepp 19:2ac158fe414e 195 // Class Creator:
sepp_nepp 19:2ac158fe414e 196 AClass::AClass(Class Creation Parameters)
sepp_nepp 19:2ac158fe414e 197 { ...
sepp_nepp 19:2ac158fe414e 198 // Attach callback function:
sepp_nepp 19:2ac158fe414e 199 MotorInstance->setMotorCallback(this, &AClass::CallBackMemberFunction);
sepp_nepp 19:2ac158fe414e 200 ...
sepp_nepp 19:2ac158fe414e 201 }
sepp_nepp 19:2ac158fe414e 202
sepp_nepp 19:2ac158fe414e 203 // Simple callback function, state variable endMove can be polled by main thread
sepp_nepp 19:2ac158fe414e 204 void AClass::CallBackMemberFunction()
sepp_nepp 19:2ac158fe414e 205 { endMove=true; }
sepp_nepp 19:2ac158fe414e 206 @endcode
sepp_nepp 19:2ac158fe414e 207 */
sepp_nepp 20:492140a08f05 208 template<typename T>
sepp_nepp 19:2ac158fe414e 209 void callbackSet(T *object, void (T::*CBmember)(void)) {
sepp_nepp 19:2ac158fe414e 210 _callback = callback(object,CBmember);
sepp_nepp 19:2ac158fe414e 211 }
sepp_nepp 19:2ac158fe414e 212
sepp_nepp 19:2ac158fe414e 213 /** Remove the Callback function that may have been attached previously. */
sepp_nepp 22:d2c742bdae16 214 void callbackRemove() { _callback = NULL; };
sepp_nepp 22:d2c742bdae16 215
sepp_nepp 22:d2c742bdae16 216 public
sepp_nepp 22:d2c742bdae16 217 // *********************************************************************
sepp_nepp 22:d2c742bdae16 218 // all following functions are agnostic of centimeter-information
sepp_nepp 22:d2c742bdae16 219 // *********************************************************************
sepp_nepp 22:d2c742bdae16 220
sepp_nepp 22:d2c742bdae16 221 /** High Level: Run Motor for a given number of centimeters.
sepp_nepp 22:d2c742bdae16 222 *
sepp_nepp 22:d2c742bdae16 223 * Runs Motor for a given circumference in cimeters in given direction.
sepp_nepp 22:d2c742bdae16 224 * You must setup the perimeter and diameter with setDiam(Adiam_cm) in advance, otherwise no reaction.
sepp_nepp 22:d2c742bdae16 225 * Call Pause() or Stop() to pause or end the Motor run prematurely.
sepp_nepp 22:d2c742bdae16 226 * While run: Uses ticker; State: first Motor_ON then Motor_RUN; cmd=MOTOR_run.
sepp_nepp 22:d2c742bdae16 227 * At end: calls attached Callback, stops ticker; State: Motor_OFF; cmd=MOTOR_stop then MOTOR_nop.
sepp_nepp 22:d2c742bdae16 228 * @param[in] <AClockWise> Given Direction, true for CLOCKWISE, false for COUNTERCLOCKWISE.
sepp_nepp 22:d2c742bdae16 229 * @param[in] <centimeters> Circumference to rotate for, in cm, centimeters<0 are run in opposite direction.
sepp_nepp 22:d2c742bdae16 230 */
sepp_nepp 22:d2c742bdae16 231 void RunCentimeters (bool AClockWise, float centimeters);
sepp_nepp 22:d2c742bdae16 232
sepp_nepp 22:d2c742bdae16 233 /** High Level: Run Motor for a given number of centimeters in default direction.
sepp_nepp 22:d2c742bdae16 234 * Same as RunCentimeters(AClockWise,centimeters) but uses Default diretion.*/
sepp_nepp 22:d2c742bdae16 235 void RunCentimeters (float centimeters);
sepp_nepp 22:d2c742bdae16 236
sepp_nepp 22:d2c742bdae16 237 /** Additional geometric information: set the wheel diameter, also sets perimeter and degrees per cm.*/
sepp_nepp 22:d2c742bdae16 238 void setDiamCM( float Adiam_cm);
sepp_nepp 22:d2c742bdae16 239
sepp_nepp 22:d2c742bdae16 240 /** Set Motor speed in centimeter/sec, based on perimeter in cm */
sepp_nepp 22:d2c742bdae16 241 void setSpeed_cm_sec(float speed_cm_sec)
sepp_nepp 22:d2c742bdae16 242
sepp_nepp 22:d2c742bdae16 243 /** Helper: calculate the needed wheel angle from a turn angle and a turn_radius_cm */
sepp_nepp 22:d2c742bdae16 244 float calcAngle(float turn_angle_deg, float turn_radius_cm)
sepp_nepp 22:d2c742bdae16 245
sepp_nepp 22:d2c742bdae16 246 /** Calculated wheel Angle from calcAngle Helper function */
sepp_nepp 22:d2c742bdae16 247 float rotate_angle_deg;
sepp_nepp 22:d2c742bdae16 248
sepp_nepp 22:d2c742bdae16 249 /** Additional geometric information: wheel diameter in centimeter */
sepp_nepp 22:d2c742bdae16 250 float diam_cm;
sepp_nepp 22:d2c742bdae16 251
sepp_nepp 22:d2c742bdae16 252 /** Additional geometric information: wheel perimeter in centimeter */
sepp_nepp 22:d2c742bdae16 253 float perim_cm;
sepp_nepp 22:d2c742bdae16 254
sepp_nepp 22:d2c742bdae16 255 /** Additional geometric information: rotation angle in degrees per cm circumference */
sepp_nepp 22:d2c742bdae16 256 float degree_per_cm;
sepp_nepp 22:d2c742bdae16 257
sepp_nepp 22:d2c742bdae16 258 /** Default rotation direction that serves as local storage, but not as actual direction */
sepp_nepp 22:d2c742bdae16 259 bool defaultDirection;
sepp_nepp 22:d2c742bdae16 260
sepp_nepp 22:d2c742bdae16 261 /** State value that is used and managed by the class owner.
sepp_nepp 22:d2c742bdae16 262 * Used for example by Creabot library to indicate if this is the left or right motor.
sepp_nepp 22:d2c742bdae16 263 */
sepp_nepp 22:d2c742bdae16 264 char StateValue;
sepp_nepp 22:d2c742bdae16 265
sepp_nepp 22:d2c742bdae16 266 public
sepp_nepp 22:d2c742bdae16 267 // *****************************************************************
sepp_nepp 22:d2c742bdae16 268 // all following functions are agnostic of centimeter-information
sepp_nepp 22:d2c742bdae16 269 // *****************************************************************
sepp_nepp 22:d2c742bdae16 270
sepp_nepp 22:d2c742bdae16 271 /** High Level: Run Motor for a given angle.
sepp_nepp 22:d2c742bdae16 272 *
sepp_nepp 22:d2c742bdae16 273 * Runs Motor for a given angle in given direction.
sepp_nepp 22:d2c742bdae16 274 * Call Pause() or Stop() to pause or end the Motor run prematurely.
sepp_nepp 22:d2c742bdae16 275 * While run: Uses ticker; State: first Motor_ON then Motor_RUN; cmd=MOTOR_run.
sepp_nepp 22:d2c742bdae16 276 * At end: calls attached Callback, stops ticker; State: Motor_OFF; cmd=MOTOR_stop then MOTOR_nop.
sepp_nepp 22:d2c742bdae16 277 * @param[in] <AClockWise> Given Direction, true for CLOCKWISE, false for COUNTERCLOCKWISE.
sepp_nepp 22:d2c742bdae16 278 * @param[in] <angle_deg> Angle>0 to rotate for, in degrees, Angles<0 are run in opposite direction.
sepp_nepp 22:d2c742bdae16 279 */
sepp_nepp 22:d2c742bdae16 280 void RunDegrees (bool AClockWise, float angle_deg);
sepp_nepp 15:88fecbdd191c 281
sepp_nepp 19:2ac158fe414e 282 /** High Level: Run Motor for a number of Steps.
sepp_nepp 19:2ac158fe414e 283 *
sepp_nepp 19:2ac158fe414e 284 * During Run: Uses ticker; State: first Motor_ON then Motor_RUN; cmd=MOTOR_run.
sepp_nepp 19:2ac158fe414e 285 * Call Pause() or Stop() to pause or end the run prematurely.
sepp_nepp 19:2ac158fe414e 286 * At the end: calls the Callback, stops ticker; State: Motor_OFF.
sepp_nepp 22:d2c742bdae16 287 * @param[in] <AClockWise> Given Direction, true for CLOCKWISE, false for COUNTERCLOCKWISE.
sepp_nepp 22:d2c742bdae16 288 * @param[in] <Nsteps> Number of steps to run for; Nsteps<0 are run in opposite direction!
sepp_nepp 19:2ac158fe414e 289 */
sepp_nepp 22:d2c742bdae16 290 void RunSteps (bool AClockWise, int32_t Nsteps);
sepp_nepp 19:2ac158fe414e 291
sepp_nepp 19:2ac158fe414e 292 /** High Level: Run Motor "unlimited"
sepp_nepp 22:d2c742bdae16 293 *
sepp_nepp 19:2ac158fe414e 294 * Runs Motor with out limit in given direction, precisely runs 4Billion Steps.
sepp_nepp 19:2ac158fe414e 295 * While run: Uses ticker; State: first Motor_ON then Motor_RUN; cmd=MOTOR_run.
sepp_nepp 19:2ac158fe414e 296 * Call Pause() or Stop() to pause or end the Motor run.
sepp_nepp 22:d2c742bdae16 297 * @param[in] <AClockWise> Given Direction, true for CLOCKWISE, false for COUNTERCLOCKWISE.
sepp_nepp 19:2ac158fe414e 298 */
sepp_nepp 22:d2c742bdae16 299 void RunInfinite (bool AClockWise);
sepp_nepp 19:2ac158fe414e 300
sepp_nepp 19:2ac158fe414e 301 /** High Level: Pause a motor Run.
sepp_nepp 19:2ac158fe414e 302 * Put Motor into Pause state, Run is suspended, but only effective if Status.cmd=MOTOR_run.
sepp_nepp 19:2ac158fe414e 303 * Retains the number of steps that remain to be run if restarting run.
sepp_nepp 19:2ac158fe414e 304 * While paused: still uses ticker; State: Motor_RUN; cmd=MOTOR_pause.
sepp_nepp 19:2ac158fe414e 305 * Use RestartRun(); to continue. */
sepp_nepp 19:2ac158fe414e 306 void PauseRun();
sepp_nepp 15:88fecbdd191c 307
sepp_nepp 19:2ac158fe414e 308 /** High Level: Restart a Paused Run.
sepp_nepp 19:2ac158fe414e 309 * Restart the Run that was launched before calling PuaseRun.
sepp_nepp 19:2ac158fe414e 310 * Only effective if Status.cmd=MOTOR_pause, otherwise no re/action.
sepp_nepp 19:2ac158fe414e 311 * Status afterwards is same as afterRun commands. */
sepp_nepp 19:2ac158fe414e 312 void RestartRun();
sepp_nepp 19:2ac158fe414e 313
sepp_nepp 19:2ac158fe414e 314 /** High Level: End any Run.
sepp_nepp 19:2ac158fe414e 315 * Force stop of any ongoing run, but does not call the Callback function.
sepp_nepp 19:2ac158fe414e 316 * Only effective if Status.cmd=MOTOR_run, otherwise no re/action.
sepp_nepp 19:2ac158fe414e 317 * Emits first cmd=MOTOR_stop then cmd=MOTOR_nop.
sepp_nepp 19:2ac158fe414e 318 * Aftewards: ticker is detached; State: Motor_OFF; */
sepp_nepp 19:2ac158fe414e 319 void StopRun();
sepp_nepp 19:2ac158fe414e 320
sepp_nepp 19:2ac158fe414e 321 public: // All the ticker timing related parameters
sepp_nepp 16:d818c1a4dafb 322
sepp_nepp 19:2ac158fe414e 323 /** MidLevel: Get Motor status
sepp_nepp 19:2ac158fe414e 324 *
sepp_nepp 19:2ac158fe414e 325 * Gets the Status of the different internal mechanisms.
sepp_nepp 19:2ac158fe414e 326 * See documentation of MotStatus Structure.
sepp_nepp 19:2ac158fe414e 327 * @return MotStatus The structure of Motor status registers. */
sepp_nepp 19:2ac158fe414e 328 MotStatus getStatus();
sepp_nepp 19:2ac158fe414e 329
sepp_nepp 19:2ac158fe414e 330 /** MidLevel: Get number of Steps per Full turn
sepp_nepp 19:2ac158fe414e 331
sepp_nepp 19:2ac158fe414e 332 * Defaults to MOTOR_STEPS_FOR_A_TURN = 4096.
sepp_nepp 19:2ac158fe414e 333 * Used by RunDegrees() to translate from angle in degrees to number of steps.
sepp_nepp 19:2ac158fe414e 334 * Old Name was: getCalibration, but that was not a good explicit name.
sepp_nepp 22:d2c742bdae16 335 * @return int32_t The structure of Motor status registers. */
sepp_nepp 22:d2c742bdae16 336 int32_t getStepsFullTurn();
sepp_nepp 19:2ac158fe414e 337
sepp_nepp 19:2ac158fe414e 338 /** MidLevel: Set number of Steps per Full turn.
sepp_nepp 19:2ac158fe414e 339
sepp_nepp 19:2ac158fe414e 340 * Defaults is MOTOR_STEPS_FOR_A_TURN = 4096.
sepp_nepp 19:2ac158fe414e 341 * Used by RunDegrees() to translate from degrees to number of steps.
sepp_nepp 19:2ac158fe414e 342 * Old Name was: setCalibration, but not good explicit name.
sepp_nepp 19:2ac158fe414e 343 * @param <StepsFullTurn> Number of steps needed to complete a full motor turn
sepp_nepp 19:2ac158fe414e 344 */
sepp_nepp 22:d2c742bdae16 345 void setStepsFullTurn(int32_t StepsFullTurn);
sepp_nepp 15:88fecbdd191c 346
sepp_nepp 19:2ac158fe414e 347 /** Mid Level: Get the Motor step time.
sepp_nepp 19:2ac158fe414e 348
sepp_nepp 19:2ac158fe414e 349 * Step time is time between two Motor steps, and is given in microseconds
sepp_nepp 19:2ac158fe414e 350 * and is passed to the ticker as delay time.
sepp_nepp 19:2ac158fe414e 351 * So the larger the value the slower the motor speed.
sepp_nepp 19:2ac158fe414e 352 * Defaults to MOTOR_STEP_TIME_DEFAULT_US = 5000.
sepp_nepp 19:2ac158fe414e 353 * @return uint32_t The structure of Motor status registers.
sepp_nepp 19:2ac158fe414e 354 */
sepp_nepp 19:2ac158fe414e 355 uint32_t getStepTime_us;
sepp_nepp 19:2ac158fe414e 356
sepp_nepp 19:2ac158fe414e 357 /** Set the time in microseconds between two Motor steps.
sepp_nepp 19:2ac158fe414e 358 * Defaults to MOTOR_STEP_TIME_DEFAULT_US = 5000usec.
sepp_nepp 19:2ac158fe414e 359 * Filters values below Minimum Value = 700.
sepp_nepp 19:2ac158fe414e 360 * Passed to the ticker as delay time.
sepp_nepp 19:2ac158fe414e 361 * Can be called while ticker is running, and takes immediate effect.
sepp_nepp 19:2ac158fe414e 362 * Was previously called setStepTime(), but was not clear which units.
sepp_nepp 22:d2c742bdae16 363 * @param <AStepTime_us> the time in microseconds between two Motor steps
sepp_nepp 19:2ac158fe414e 364 */
sepp_nepp 22:d2c742bdae16 365 void setStepTime_us(uint32_t AStepTime_us);
sepp_nepp 15:88fecbdd191c 366
sepp_nepp 19:2ac158fe414e 367 /** Set the time in seconds to get one full turn, rotation of 360°.
sepp_nepp 19:2ac158fe414e 368 * was previously called setSpeed().
sepp_nepp 19:2ac158fe414e 369 * @param <Seconds_Per_Turn> Period of Rotation, e.g. if =20.0 then Motor will do 360° in 20 seconds.
sepp_nepp 19:2ac158fe414e 370 */
sepp_nepp 19:2ac158fe414e 371 void setRotationPeriodSec(float Seconds_Per_Turn) ;
sepp_nepp 19:2ac158fe414e 372
sepp_nepp 19:2ac158fe414e 373 private:
sepp_nepp 19:2ac158fe414e 374 // all the Ticker and Timing procedures, used to run the Motor for a duration
sepp_nepp 19:2ac158fe414e 375 void StartTick();
sepp_nepp 19:2ac158fe414e 376 void ProcessMotorStateMachine();
sepp_nepp 19:2ac158fe414e 377 // The call back function pointer that is called when the Processor
sepp_nepp 19:2ac158fe414e 378 // State Machine reaches its end.
sepp_nepp 19:2ac158fe414e 379 Callback<void()> _callback;
sepp_nepp 19:2ac158fe414e 380 void StopTick();
sepp_nepp 22:d2c742bdae16 381 MotStatus Status;
sepp_nepp 22:d2c742bdae16 382 timestamp_t StepTime_us; // Time in µs for one Motor step
sepp_nepp 22:d2c742bdae16 383 Ticker MotorSysTick; // System Timer for Motor
sepp_nepp 22:d2c742bdae16 384 int32_t Steps_FullTurn; // Number of step for a complete turn
sepp_nepp 19:2ac158fe414e 385
sepp_nepp 19:2ac158fe414e 386 public: // all the low level direct Motor HW access, States are immediately reached
sepp_nepp 16:d818c1a4dafb 387
sepp_nepp 19:2ac158fe414e 388 /** Low Level: Run one full turn clockwise then anticlockwise.
sepp_nepp 19:2ac158fe414e 389 * After: State: Motor_OFF.
sepp_nepp 19:2ac158fe414e 390 * Blocking function, returns back only after end of full movement.
sepp_nepp 19:2ac158fe414e 391 */
sepp_nepp 19:2ac158fe414e 392 void MotorTest();
sepp_nepp 15:88fecbdd191c 393
sepp_nepp 19:2ac158fe414e 394 /** Low Level: turn off all Motor Phases
sepp_nepp 19:2ac158fe414e 395 * No more current flows, reduces holding force.
sepp_nepp 19:2ac158fe414e 396 * After: State: Motor_OFF.
sepp_nepp 19:2ac158fe414e 397 * StepPhases memorizes the last used phase.
sepp_nepp 19:2ac158fe414e 398 * Equivalent what previously the function "void Stop();" did . */
sepp_nepp 19:2ac158fe414e 399 void MotorOFF();
sepp_nepp 19:2ac158fe414e 400
sepp_nepp 19:2ac158fe414e 401 /** Low Level: turn on the Motor Phases in the last used phase.
sepp_nepp 19:2ac158fe414e 402 * The last used phase is held in StepPhases.
sepp_nepp 19:2ac158fe414e 403 * After: State: Motor_ON, or Motor_ZERO if StepPhases==0
sepp_nepp 19:2ac158fe414e 404 * Equivalent to what previously the function "void Start();" did. */
sepp_nepp 19:2ac158fe414e 405 void MotorON();
sepp_nepp 19:2ac158fe414e 406
sepp_nepp 22:d2c742bdae16 407 /** Low Level: Advance Motor one step, rotates in direction of variable AClockWise. */
sepp_nepp 19:2ac158fe414e 408 void StepOnce();
sepp_nepp 19:2ac158fe414e 409
sepp_nepp 19:2ac158fe414e 410 /** Low Level: Advance Motor one step, rotates CounterClockwise. */
sepp_nepp 19:2ac158fe414e 411 void StepCCW();
sepp_nepp 19:2ac158fe414e 412
sepp_nepp 19:2ac158fe414e 413 /** Low Level: Advance Motor one step, rotates Clockwise. */
sepp_nepp 19:2ac158fe414e 414 void StepClkW();
sepp_nepp 19:2ac158fe414e 415
sepp_nepp 19:2ac158fe414e 416 /** Low Level: turn on the Motor Phases in Zero Position.
sepp_nepp 19:2ac158fe414e 417 * After: State: Motor_ZERO, StepPhases==0
sepp_nepp 19:2ac158fe414e 418 */
sepp_nepp 19:2ac158fe414e 419 void MotorZero();
sepp_nepp 19:2ac158fe414e 420
sepp_nepp 19:2ac158fe414e 421
sepp_nepp 19:2ac158fe414e 422 private:
sepp_nepp 16:d818c1a4dafb 423
sepp_nepp 19:2ac158fe414e 424 /** Low Level: Engage Motor Phases according to MotorIndex. */
sepp_nepp 19:2ac158fe414e 425 void SetPhases();
sepp_nepp 13:4563244c4071 426
sepp_nepp 19:2ac158fe414e 427 DigitalOut *MPh[4]; // Digital outputs, one per phase
sepp_nepp 19:2ac158fe414e 428 int StepPhase; // Motor Phase Variable, counts up and down with every step
sepp_nepp 19:2ac158fe414e 429 };
sepp_nepp 19:2ac158fe414e 430
sepp_nepp 19:2ac158fe414e 431 #endif