Tarek Lule / MotorLib

Fork of MotorLib by CreaLab

Files at this revision

API Documentation at this revision

Comitter:
sepp_nepp
Date:
Wed Oct 31 14:24:17 2018 +0000
Parent:
12:fd881ff72048
Child:
14:f0667bfc2e98
Commit message:
First publishing of my version of Motor Lib

Changed in this revision

motor.cpp Show annotated file Show diff for this revision Revisions of this file
motor.h Show annotated file Show diff for this revision Revisions of this file
--- a/motor.cpp	Mon Oct 15 12:06:50 2018 +0000
+++ b/motor.cpp	Wed Oct 31 14:24:17 2018 +0000
@@ -1,248 +1,208 @@
 #include "motor.h"
 
-void Motor::initialization(PinName _MPh0, PinName _MPh1, PinName _MPh2, PinName _MPh3, uint32_t tickTime) {
-//pc_uart.printf("MOTOR INIT\n");
-    MPh0 = new DigitalOut(_MPh0);
-    MPh1 = new DigitalOut(_MPh1);
-    MPh2 = new DigitalOut(_MPh2);
-    MPh3 = new DigitalOut(_MPh3);
-        init = true;
-    MotorIndex = 0;
-    //
-    // Connect Interrupt routine in which the motor and all the state machine is performed
-    //
-    direction = CLOCKWISE;         // Default direction is clockwise
-    state = Motor_IDLE;    // Default state is IDLE
-    command = MOTOR_nop;       // Default command is NOP
-    MotorStepTime = tickTime;      // value in micro second for one step
-    MotorFullTurn = MOTOR_TICKS_FOR_A_TURN;       // Initial Calibration
-    NumSteps = MotorFullTurn;               // Default 1 turn
-    itOnStop = true;
-            tuneTimings.reset();
-         tuneTimings.start();
+void MotStatus::set(motorCommands Acmd, motorDir Adir, int32_t  ANSteps) {
+    cmd = Acmd;
+    dir = Adir;
+    NSteps  = NSteps;
+} MotStatus;
+
+Motor::Motor(PinName _MPh[4]) {
+    initialization( _MPh , MOTOR_STEP_TIME_DEFAULT_US);
 }
 
 Motor::Motor(PinName _MPh0, PinName _MPh1, PinName _MPh2, PinName _MPh3) {
-    initialization( _MPh0,  _MPh1,  _MPh2,  _MPh3, MOTOR_STEP_TIME_DEFAULT);
- 
+    PinName _MPh[4] = {_MPh0, _MPh1, _MPh2, _MPh3};
+    initialization( _MPh , MOTOR_STEP_TIME_DEFAULT_US);
 }
 
-
-Motor::Motor(PinName _MPh0, PinName _MPh1, PinName _MPh2, PinName _MPh3, uint32_t tickTime) {
- 
-    initialization( _MPh0,  _MPh1,  _MPh2,  _MPh3, tickTime);
- 
-}
-
-
-void Motor::removeMotorCallback() {
-    
-    _callback = NULL;
-    
+Motor::Motor(PinName _MPh0, PinName _MPh1, PinName _MPh2, PinName _MPh3, uint32_t aStepTime_us) {
+    PinName _MPh[4] = {_MPh0, _MPh1, _MPh2, _MPh3};
+    initialization( _MPh, aStepTime_us);
 }
 
-void Motor::setMotorCallback(void (*function)(void))
-{
-    setMotorCallback(function, true);
-}
-void Motor::setMotorCallback(void (*function)(void), bool onStop)
-{
-    _callback = function;
-    itOnStop = onStop;  
-}
-
+//void Motor::initialization(PinName _MPh0, PinName _MPh1, PinName _MPh2, PinName _MPh3, uint32_t aStepTime_us) {
+void Motor::initialization(PinName _MPh[4], uint32_t aStepTime_us) {
 
-uint32_t Motor::getCalibration()
-{
-    return MotorFullTurn;
-}
-
-void Motor::setCalibration(uint32_t nbTicksforFullTurn) {
-    MotorFullTurn = nbTicksforFullTurn;
-}
+    for (int ph=0; ph<4; ph++) { MPh[ph] = new DigitalOut(_MPh[ph]); } 
+    /*MPh[0] = new DigitalOut(_MPh0);
+    MPh[1] = new DigitalOut(_MPh1);
+    MPh[2] = new DigitalOut(_MPh2);
+    MPh[3] = new DigitalOut(_MPh3);*/
+    
+    MotorOFF();    // Default state is all phases are OFF
+    StepPhase = 0; // initial phase is Zero
+    
+    Status.set(MOTOR_nop, CLOCKWISE, 0);// Default command is NOP, clockwise direction, 0 steps
+    
+    TickIsAttached = false;
+    StepTime_us = aStepTime_us;// duration in micro second for one step
 
-void Motor::setDelayBtwTicks(uint32_t tickTime) {
-   if(MotorStepTime == tickTime) return;
-    MotorStepTime = tickTime;
-    if(MotorStepTime < MOTOR_STEP_TIME_MIN)  MotorStepTime = MOTOR_STEP_TIME_MIN;
-    // pc_uart.printf("Change ticktime to %d %d\n\r",tickTime, MotorStepTime);
-   if(!init) {
-        MotorSystemTick.detach();
-        MotorSystemTick.attach_us(callback(this, &Motor::ProcessMotorStateMachine), MotorStepTime);
-   }
+    Steps_FullTurn = MOTOR_STEPS_FOR_A_TURN;  // Default Calibration value
+    _callback = NULL; // No default Callback
+
+    // itOnStop = true;    
+    // tuneTimings.reset();
+    // tuneTimings.start();
 }
 
-void Motor::setSpeed(float sForOneTurn) {
-    MotorStepTime = 1000*sForOneTurn / MotorFullTurn;
-    if(MotorStepTime < MOTOR_STEP_TIME_MIN)  MotorStepTime = MOTOR_STEP_TIME_MIN;
-    if(!init) {
-        
-        MotorSystemTick.detach();
-       MotorSystemTick.attach_us(callback(this, &Motor::ProcessMotorStateMachine), MotorStepTime);
-    }
+//Attaching and removing Callbacks
+void Motor::removeMotorCallback() 
+{ _callback = NULL; }
+
+void Motor::setMotorCallback(void (*function)(void))
+{  _callback = function;  }
+
+uint32_t Motor::getStepsFullTurn()
+{    return Steps_FullTurn;  }
+
+void Motor::setStepsFullTurn(uint32_t StepsFullTurn) 
+{   Steps_FullTurn = StepsFullTurn; }
+
+void Motor::RunInfinite(motorDir direction) {
+    Status.set(MOTOR_run, direction, -1);
+    StartTick();
 }
 
-void Motor::Start() {
-        SetCommand(MOTOR_start);   
-};
-
-void Motor::Stop() {
-        SetCommand(MOTOR_stop);
-}
-
-
-void Motor::Pause() {
-        SetCommand(MOTOR_pause);
-}
-
-
-void Motor::Restart() {
-        SetCommand(MOTOR_restart);
+void Motor::RunSteps(motorDir direction, uint32_t steps) {
+    if (steps>0) 
+        { Status.set(MOTOR_run, direction, steps);
+        StartTick(); }
 }
 
-void Motor::SetZero() {
-        SetCommand(MOTOR_zero);
-}
+void Motor::Pause() 
+{   if (Status.cmd==MOTOR_run) Status.cmd = MOTOR_pause;  }
 
-void Motor::RunInfinite(MotorDir dir) {
-    SetDirection( dir);
-    NumSteps = -1;
-    SetCommand(MOTOR_start);
-}
+void Motor::Restart() 
+{   if (Status.cmd==MOTOR_pause) Status.cmd = MOTOR_run;  }
 
-void Motor::RunSteps(MotorDir dir, uint32_t steps) {
-    SetDirection( dir);
-    NumSteps = steps;
-    SetCommand(MOTOR_start);
-}
+void Motor::Stop() 
+{   Status.cmd = MOTOR_stop;  } 
+
+MotStatus Motor::getStatus() { return Status; }
 
-void Motor::RunDegrees(MotorDir dir, float degree) {
-    SetDirection( dir);
-    NumSteps = (int)(degree * (float)MotorFullTurn / (float)360.0);
-    SetCommand(MOTOR_start);
-}
-void Motor::SetDirection(MotorDir dir) {
-    direction=dir;
-}
-
-void Motor::SetCommand(MotorCommand cmd) {
-    if(init)   {
-        MotorSystemTick.attach_us(callback(this, &Motor::ProcessMotorStateMachine), MotorStepTime);
-        last=tuneTimings.read_us();
-        init=false;
-        }
-    command=cmd;
+void Motor::TestMotor()    // Just to check that it make a full turn back and forth
+{
+    int i;
+    MotorON();
+    for (i=0; i<Steps_FullTurn; i++) {
+        wait(0.005);
+        StepRight();
+        }   
+    wait(0.5);
+    for (i=0; i<Steps_FullTurn; i++) {
+        wait(0.005);
+        StepLeft();
+        }   
+    MotorOFF();
 }
 
-void Motor::StopMotor() // --- Stop Motor
-{
-        *MPh0 = 0;  *MPh1 = 0;  *MPh2 = 0;  *MPh3 = 0;
-        MotorIndex = 0;
-   
-}
-
-
-void    Motor::StartMotor()
-{
-        MotorIndex = 0;
-        *MPh0 = 1;  *MPh1 = 0;  *MPh2 = 0;   *MPh3 = 0;
-}
+/*******************************************************
+ **   Ticker / Timing procedures
+ *******************************************************/
 
-void    Motor::RightMotor()    // Move the Motor one step Right
-{
-    static const   int RPh0[8] =  {1, 1, 0, 0, 0, 0, 0, 1};
-    static const   int RPh1[8] = {0, 1, 1, 1, 0, 0, 0, 0};
-    static const   int RPh2[8] = {0, 0, 0, 1, 1, 1, 0, 0};
-    static const   int RPh3[8] = {0, 0, 0, 0, 0, 1, 1, 1};
-    *MPh0 = RPh0[MotorIndex];  *MPh1 = RPh1[MotorIndex];  *MPh2 = RPh2[MotorIndex];  *MPh3 = RPh3[MotorIndex];
-    if (MotorIndex<7) MotorIndex++;
-    else    MotorIndex = 0;
+void Motor::setRotationPeriodSec(float Seconds_Per_Turn) {
+    // rescale to usec and pass on to the next handler. 
+    setStepTime_us(1000 * Seconds_Per_Turn / Steps_FullTurn) ;
 }
-void    Motor::LeftMotor()    // Move the Motor one step Right
-{
-    static const   int LPh0[8] =  {1, 1, 0, 0, 0, 0, 0, 1};
-    static const   int LPh1[8] = {0, 1, 1, 1, 0, 0, 0, 0};
-    static const   int LPh2[8] = {0, 0, 0, 1, 1, 1, 0, 0};
-    static const   int LPh3[8] = {0, 0, 0, 0, 0, 1, 1, 1};
-   *MPh0 = LPh0[MotorIndex];  *MPh1 = LPh1[MotorIndex];  *MPh2 = LPh2[MotorIndex];  *MPh3 = LPh3[MotorIndex];
-    if (MotorIndex>0) MotorIndex--;
-    else    MotorIndex = 7;
+void Motor::setStepTime_us(uint32_t aStepTime_us) {
+    if(StepTime_us == aStepTime_us) return; // avoid futile activity
+    if (StepTime_us < MOTOR_STEP_TIME_MIN_US) // filter too small values
+       StepTime_us = MOTOR_STEP_TIME_MIN_US;
+       else StepTime_us = aStepTime_us; // or if OK then assign value
+    // if ticker already running recreate a new ticker;
+    if(TickIsAttached) { StopTick(); StartTick(); }
+ }
+
+void Motor::StartTick() {
+    if(!TickIsAttached)   {
+        // Connect Interrupt routine in which the motor and all the state machine is performed
+        MotorSysTick.attach_us(callback(this, &Motor::ProcessMotorStateMachine), StepTime_us);
+        // last=tuneTimings.read_us();
+        TickIsAttached=true;
+        }
 }
 
-void    Motor::RunMotor()     // Move the Motor in the user direction
-{
-        if (direction==CLOCKWISE) RightMotor();
-        else LeftMotor();
-}
-
-
 void Motor::ProcessMotorStateMachine()
-{        
+{
+    switch(Status.cmd) {
+        case MOTOR_run: {
+            switch(state) {
+                case Motor_OFF:
+                    MotorON(); // First only turn on the motor ..
+                    break;
+                case Motor_ZERO:
+                case Motor_ON:
+                        state = MOTOR_RUN;
+                case MOTOR_RUN:
+                    if (Status.NSteps==0)  // No more steps to go
+                        { Status.cmd = MOTOR_stop; 
+                          if (_callback) _callback.call(); } 
+                      else // More steps to go
+                        { StepOnce(); 
+                        Status.NSteps--;}
+                    break;
+                } // switch(state)
+            } // case MOTOR_run
+        case MOTOR_stop:
+            StopTick();
+            Status.cmd = MOTOR_nop; 
+            MotorOFF(); 
+            break;       
+        case MOTOR_nop:
+        case MOTOR_pause:
+        default: break;
+    } // switch(Status.cmd)
+}
 
-        if (state==Motor_IDLE) {
-            if (command == MOTOR_start) {
-                // Start the Motor
-                StartMotor();
-                state = Motor_RUN;
-                }
-            else if (command == MOTOR_zero) {
-                // Start zeroing the Motor
-                StartMotor();
-                state = Motor_ZERO;
-                }
-            command = MOTOR_nop;
-            }
-        else if (state==Motor_RUN) {
-            // Action always performed in that state
-             if (NumSteps>0 || NumSteps <0) {
-                RunMotor();
-                NumSteps--;
-                }
-            // Check next state
-            if (command == MOTOR_pause) {
-                state = Motor_PAUSE;
-                }
-            else if ((command == MOTOR_stop)||(NumSteps==0)) {
-                StopMotor();
-//                     if(state != Motor_IDLE ){//|| itOnStop == true) {
-//                        _callback.call();
-//                    }
-                state = Motor_IDLE;
-               }
-            command = MOTOR_nop;
-            }
-        else if (state==Motor_PAUSE) {
-            if (command == MOTOR_stop) {
-                StopMotor();
-                NumSteps=0;
-                state = Motor_IDLE;
-                }
-            else if (command == MOTOR_restart) {
-                state = Motor_RUN;
-                }
-            command = MOTOR_nop;
-            }
-        else if (state==Motor_ZERO) {
-            command = MOTOR_nop;
-            }
+void Motor::StopTick() {
+    if(TickIsAttached)   
+      { MotorSysTick.detach(); TickIsAttached=false; }
 }
 
-MotorStateList Motor::getState() {
-   return state;    
+/*******************************************************
+ **   all the low level direct motor HW access
+ *******************************************************/
+ 
+ /** Turn off all motor Phases, no more current flowing */
+void Motor::MotorOFF() 
+{   for (int ph=0; ph<4; ph++) *MPh[ph] = 0;  
+    state=Motor_OFF;
 }
 
-void Motor::TestMotor()    // Just to check that it make a full taurn back and forth
-{
-    int i;
-    StartMotor();
-    for (i=0; i<MotorFullTurn; i++) {
-        wait(0.005);
-        RightMotor();
-        }   
-    wait(0.5);
-    for (i=0; i<MotorFullTurn; i++) {
-        wait(0.005);
-        LeftMotor();
-        }   
-    StopMotor();
-}
\ No newline at end of file
+/** Turn on the motor Phase, In the last used phase, memorized in StepPhases 
+*  Equivalent to what previously the function "void Start();" did */
+void Motor::MotorON()
+{   SetPhases(); // attention, does not change StepPhase!
+    if (StepPhase==0)  state=Motor_ZERO;  
+        else state=Motor_ON;
+} 
+
+/** Motor phases turned on and put to Zero Position*/    
+void Motor::MotorZero() {
+    StepPhase = 0;  // sets the phases to 0
+    SetPhases(); 
+    state=Motor_ZERO;
+}
+
+void    Motor::StepOnce()     // Move the Motor in the used 'direction'
+{   if (Status.dir == CLOCKWISE) StepRight(); else StepLeft();
+}
+
+void    Motor::StepRight()    // Move the Motor one step Right
+{   if (StepPhase<7) StepPhase++;  else  StepPhase = 0;
+    SetPhases();
+}
+void    Motor::StepLeft()    // Move the Motor one step Right
+{   if (StepPhase>0) StepPhase--;  else  StepPhase = 7;
+    SetPhases();
+}
+
+static const int MotPh[4][8] = 
+    { {1, 1, 0, 0, 0, 0, 0, 1}, 
+      {0, 1, 1, 1, 0, 0, 0, 0}, 
+      {0, 0, 0, 1, 1, 1, 0, 0}, 
+      {0, 0, 0, 0, 0, 1, 1, 1}};
+      
+void    Motor::SetPhases()    // Engage Motor Phases according to StepPhase
+{ for (int ph=0; ph<4; ph++) { *MPh[ph] = MotPh[ph][StepPhase]; }   }
+
--- a/motor.h	Mon Oct 15 12:06:50 2018 +0000
+++ b/motor.h	Wed Oct 31 14:24:17 2018 +0000
@@ -1,102 +1,228 @@
 // -------------------- Motor ---------------------------
-#ifdef MOTOR_H
-#else
+#ifndef MOTOR_H
 #define MOTOR_H
 
 #include "mbed.h"
 
-#define MOTOR_STEP_TIME_MIN 700
-#define MOTOR_STEP_TIME_DEFAULT 5000
-#define MOTOR_TICKS_FOR_A_TURN 4096
+#define MOTOR_STEP_TIME_MIN_US 700      // was MOTOR_STEP_TIME_MIN
+#define MOTOR_STEP_TIME_DEFAULT_US 5000 // was MOTOR_STEP_TIME_DEFAULT
+#define MOTOR_STEPS_FOR_A_TURN 4096
 
-typedef enum MotorStateList {   // Define Motor States for the State Machine
-    Motor_IDLE = 0,
-    Motor_RUN,
-    Motor_PAUSE,
+/** Possible Motor States that the motor state machine can be in;
+    Motor_CALIB is deprecated, was removed from the states
+    Motor_OFF   replaces Motor_STOP, a state where all current is off
+    Motor_ON    replaces Motor_PAUSE, a state where phases are engaged but motor is not running
+    Motor_ZERO  is the motor at phase position 0, only reached at init and call of Zero command 
+    Motor_RUN   is deprecated, the motor running is simply represented 
+                by being in ON state, and the motor-command == MOTOR_run */
+typedef enum {  
+    Motor_OFF = 0,
+    Motor_ON,
     Motor_ZERO,
-    Motor_CALIB
-    } MotorState;
+    Motor_RUN
+    } motorStates;
     
-typedef enum MotorCommandList { // Define Motor State Machine Commands
+/** Possible Motor Commands that the state machine handles:
+    MOTOR_nop   is no active command to execute
+    MOTOR_run   run for requested direction, speed, duration
+    MOTOR_stop  stops immediately all, turns off motor
+    MOTOR_pause is the motor is paused from the state run
+    MOTOR_restart was replaced by MOTOR_run  
+    OFF and STOP commands do not go through the ticker command handler.
+        */
+typedef enum { // Define Motor State Machine Commands
     MOTOR_nop = 0,
-    MOTOR_start,
-    MOTOR_pause,
-    MOTOR_restart,
+    MOTOR_run,
     MOTOR_stop,
-    MOTOR_zero
-    } MotorCommand;
+    MOTOR_pause
+    } motorCommands;
 
-typedef enum MotorDirectionList { // Define Motor Clockwise or Anticlockwise
+typedef enum motorDir { // Define Motor Clockwise or Anticlockwise
     CLOCKWISE = 0,
     COUNTERCLOCKWISE
-    } MotorDir;
+    } motorDir;
     
+
+typedef struct {
+    /** Structure of Motor Status registers 
+    Used inside Motor Class. Can be passed back by Motor.getStatus() */
+    /** state = the general state that the moter state machine is in: 
+    * can be either of:     Motor_OFF = 0, Motor_ON, Motor_ZERO, or Motor_RUN  */
+    motorStates  state;
+    /** cmd = the current command being executed */
+    motorCommands cmd; 
+    /** dir = the direction that the motor is asked to run*/
+    motorDir      dir;
+    /** NSteps = the number of steps left for the motor to run 
+    NSteps=0 indicates all steps finsihed; NSteps<0 indicates to run "forever" */
+    int32_t       NSteps;
+    /** TickIsAttached = True while the Ticker is attached */
+    bool         TickIsAttached;
+    void set(motorCommands Acmd, motorDir Adir, int32_t  ANSteps);
+    } MotStatus;
+    
+/** Class of a Four Phase Stepper Motor 
+Handles single steps but also running a number of steps, or given amount angle. 
+
+Higher level function rely on ticker and a state-machine evaluating now incoming commands versus the motor state at every tick. 
+
+Lower level functions directly talk to the hard ware without ticker. 
+ */
 class Motor {
     
-    MotorState state;
-    MotorCommand command;
-    MotorDir direction;
-
-    
-    public:
- 
-    
-    Motor(PinName _MPh0, PinName _MPh1, PinName _MPh2, PinName _MPh3, uint32_t TickTime);
+  public:
+    /** Class Creator: receives the names of the 4 Digital Pins in an array of PinNames
+    *  the time in between two steps defaults here to MOTOR_STEP_TIME_DEFAULT_US=5000usec
+    *  Uses Ticker callback to handle step times. 
+    *  Call it for example like this:
+    * @code
+    *    PinName MotPhases[] = {PB_1, PB_15, PB_14, PB_13};
+    *    Motor MotorName(MotPhases);
+    * @endcode
+    */ 
+    Motor(PinName _MPh[4] );
+    /** Class Creator: receives the names of the 4 Digital Pins, 
+    *  Uses Ticker callback to handle step times. 
+    *  the time between two steps defaults here to MOTOR_STEP_TIME_DEFAULT_US=5000usec */
     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();
+    /** Class Creator: receives the names of the 4 Digital Pins, 
+    *  Uses Ticker callback to handle step times. 
+    *  aStepTime_us is the time in usec between two steps, thats used initially. */
+    Motor(PinName _MPh0, PinName _MPh1, PinName _MPh2, PinName _MPh3, uint32_t aStepTime_us);
+  private:
+    // void initialization(PinName _MPh0, PinName _MPh1, PinName _MPh2, PinName _MPh3, uint32_t aStepTime_us);
+    void initialization(PinName _MPh[4], uint32_t aStepTime_us);
+  public:
+    /** Attaching a basic Callback function, not member of a class
+     *  It is only called when a Run Command reaches it's target
+     *  It is not called when the Motor is stopped by calling Stop Function, or any other events.
+     * Attention: the attached Callback is called within a Ticker Callback. 
+     *    Your code you execute in the Callback should be short, must not use waits, or any long routines. 
+     *    Do not call any motor run commands in the callback, as it creates conflicting situations. 
+     *    Long Callback code may impair this and any other Ticker functions that are running in your application.
+     */
     void setMotorCallback(void (*mIT)());
-     void setMotorCallback(void (*mIT)(), bool itOnStop);
- 
-template<typename T>
-void setMotorCallback(T *object, void (T::*member)(void))
-{
-    _callback = callback(object,member);
-    itOnStop = true;
-}
- 
+    /** Attaching a Callback function, member of a class
+     *  It is only called when a Run Command reaches it's target
+     *  It is not called when the Motor is stopped by calling Stop Function 
+     * Attention: the attached Callback is called within a Ticker Callback. 
+     *    Your code you execute in the Callback should be short, must not use waits, or any long routines. 
+     *    Do not call any motor run commands in the callback, as it creates conflicting situations. 
+     *    Long Callback code may impair this and any other Ticker functions that are running in your application.
+     */
+    template<typename T>
+        void setMotorCallback(T *object, void (T::*member)(void))
+            {  _callback = callback(object,member);  }
+        
+    /** Removing the Callback function that may have been attached previously. */
     void removeMotorCallback();
+    
+    /** RunSteps Main Motor Running Function: 
+    * Runs motor for a number Nsteps>0 of steps; Nsteps<=0 will not be executed
+    * Given Direction can be: CLOCKWISE, or COUNTERCLOCKWISE 
+    * Call Pause() or Stop() to pause or end the motor running prematurely
+    * While running: Uses ticker; State = first Motor_ON then Motor_RUN; cmd=MOTOR_run
+    * At the end: calls the Callback, stops ticker; State = Motor_OFF */
+    void RunSteps    (motorDir direction, uint32_t Nsteps);
+    
+    /** RunDegrees = Main Motor Running Function: 
+    * Runs motor for a given angle>0 in degrees; Angles<=0 will not be executed
+    * Given Dicection can be: CLOCKWISE, or COUNTERCLOCKWISE 
+    * Call Pause() or Stop() to pause or end the motor running prematurely
+    * While running: Uses ticker; State = first Motor_ON then Motor_RUN; cmd=MOTOR_run
+    * At the end: calls the Callback, stops ticker; State = Motor_OFF; cmd=MOTOR_stop then MOTOR_nop */
+    void RunDegrees  (motorDir direction, float angle);
 
-    uint32_t getCalibration();    
-    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). 
+    /** RunInfinite = Main Motor Running Function: 
+    * Runs motor "unlimited", more precisely it runs 4Billion Steps. 
+    * Dicection can be: CLOCKWISE, or COUNTERCLOCKWISE 
+    * While running: Uses ticker; State = first Motor_ON then Motor_RUN; cmd=MOTOR_run
+    * Call Pause() or Stop() to pause or end the motor running*/
+    void RunInfinite (motorDir direction);
+    
+    /** Pause puts Motor into Pause state, stepping is suspended
+     * Only effective if Status.cmd=MOTOR_run
+     * Retains the number of steps that remain to be run. 
+     * While pausing: still uses ticker; State = Motor_RUN; cmd=MOTOR_pause
+     * Use Restart(); to continue */
+    void Pause();
+    
+    /** Restart continues with the paused stepping activity, 
+     * Only effective if Status.cmd=MOTOR_pause, otherwise no action 
+     * Status afterwards is same as afterRun commands.*/
+    void Restart();
     
-    void Start();
+    /** Stop completely ends any running/stepping activity, 
+    * Does not call the Callback function. Emits first cmd=MOTOR_stop then cmd=MOTOR_nop
+    * Aftewards: ticker is detached; State = Motor_OFF; */
     void Stop();
-    void Pause();
-    void Restart();
-    void SetZero();
+    /** getSTatus allows direct access to all motor status registers:
     
-    MotorStateList getState();
+    MotStatus getStatus();
+  public: // All the timing related parameters    
+    /** Get number of Steps per Full turn, 
+     *  Defaults to MOTOR_STEPS_FOR_A_TURN = 4096 
+     *  Needed to translate from degrees to number of steps 
+     *  Old Name was: getCalibration, but not good explicit name */
+    uint32_t getStepsFullTurn();    
+    /** Set number of Steps per Full turn, 
+     *  Defaults to MOTOR_STEPS_FOR_A_TURN = 4096 
+     *  Needed to translate from degrees to number of steps */
+    void setStepsFullTurn(uint32_t StepsFullTurn);
+    /** Set the time in microseconds between two motor steps 
+     *  was previously called setStepTime()  */
+    void setStepTime_us(uint32_t aStepTime_us); // in ms
+    /** Set the time in seconds to get one full turn, rotation of 360° 
+     * e.g. setRotationPeriodSec( 20.0 ); then motor will do 360° in 20 seconds;
+     * was previously called setSpeed()  */
+    void setRotationPeriodSec(float Seconds_Per_Turn) ;
     
-    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();
+  private: 
+    // all the  Ticker and Timing procedures, used to run the motor for a duration
+    void StartTick();
     void ProcessMotorStateMachine();
+    // The call back function pointer that is called when the Processor
+    // State Machine reaches its end. 
+    Callback<void()> _callback;
+    void StopTick();
+    MotStatus Status;       
+    //now part of Status: motorCommands command;
+    //now part of Status: motorDir direction;
+    //now part of Status: int32_t    NumSteps;    
+    //now part of Status: bool        TickIsAttached;
+    //now part of Status: motorStates state;
+    timestamp_t StepTime_us;  // Time in µs for one motor step
+    Ticker      MotorSysTick;    // System Timer for Motor
+    
+  public: // all the low level direct motor HW access, States are immediately reached
+    /** Turn off all motor Phases, no more current flowing 
+     *   Equivalent to what previously the function "void Stop();" did */
+    void MotorOFF();
+    /** Turn on the motor Phase, In the last used phase, memorized in StepPhases 
+     *  Equivalent to what previously the function "void Start();" did */
+    void MotorON();
+    /** Motor phases turned on and put to Zero Position*/    
+    void MotorZero();
+    void TestMotor();
+  private:
+    /** Motor advances one step rotating in direction of variable direction */
+    void StepOnce();
+    /** Motor advances one step rotating left */
+    void StepLeft();
+    /** Motor advances one step rotating right */
+    void StepRight();
+    /** Engage Motor Phases according to MotorIndex */
+    void SetPhases(); // Engage Motor Phases according to StepPhase
+
+    DigitalOut *MPh[4];       // Digital outputs, one per phase
+    int        StepPhase;     //  Motor Phase Variable, counts up and down with every step
+    uint32_t   Steps_FullTurn;// Number of step for a complete turn
+   
+  private: /* Deprecated, unused members of the class
+    void SetDirection(motorDir direction); // direction is set anyway by all the other commands
     Timer tuneTimings;
-uint32_t last;
-    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
-    int32_t    NumSteps;       // Number of Steps = NumWire * MotorFullTurn
-    Callback<void()> _callback;
-
-    bool itOnStop;
+    uint32_t last;
+    */ 
 };
 
 #endif
\ No newline at end of file