Tarek Lule / MotorLib

Fork of MotorLib by CreaLab

Revision:
16:d818c1a4dafb
Parent:
15:88fecbdd191c
Child:
17:86e5af6f7628
--- a/motor.cpp	Thu Nov 01 15:29:33 2018 +0000
+++ b/motor.cpp	Wed Nov 28 09:44:34 2018 +0000
@@ -1,9 +1,9 @@
 #include "motor.h"
 
 void MotStatus::set(motorCommands aCmd, motorDir aDir, int32_t  aNSteps) {
-    cmd = Acmd;
-    dir = Adir;
-    NSteps  = NSteps;
+    cmd = aCmd;
+    dir = aDir;
+    NSteps  = aNSteps;
 };
 
 Motor::Motor(PinName _MPh[4]) {
@@ -46,17 +46,11 @@
 }
 
 //Attaching and removing Callbacks
-void Motor::removeMotorCallback() 
-{ _callback = NULL; }
-
-void Motor::setMotorCallback(void (*function)(void))
-{  _callback = function;  }
+void Motor::callbackSet(void (*CBfunction)(void))
+{  _callback = CBfunction;  }
 
-uint32_t Motor::getStepsFullTurn()
-{    return Steps_FullTurn;  }
-
-void Motor::setStepsFullTurn(uint32_t StepsFullTurn) 
-{   Steps_FullTurn = StepsFullTurn; }
+void Motor::callbackRemove() 
+{ _callback = NULL; }
 
 void Motor::RunInfinite(motorDir direction) {
     Status.set(MOTOR_run, direction, -1);
@@ -68,37 +62,30 @@
         { Status.set(MOTOR_run, direction, steps);
         StartTick(); }
 }
+void Motor::RunDegrees(motorDir direction, float angle_deg) {
+    RunSteps(direction, (int)(angle_deg * (float)Steps_FullTurn / (float)360.0) );   
+}
 
-void Motor::Pause() 
+void Motor::PauseRun() 
 {   if (Status.cmd==MOTOR_run) Status.cmd = MOTOR_pause;  }
 
-void Motor::Restart() 
+void Motor::RestartRun() 
 {   if (Status.cmd==MOTOR_pause) Status.cmd = MOTOR_run;  }
 
-void Motor::Stop() 
+void Motor::StopRun() 
 {   Status.cmd = MOTOR_stop;  } 
 
 MotStatus Motor::getStatus() { return Status; }
 
-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();
-}
-
 /*******************************************************
  **   Ticker / Timing procedures
  *******************************************************/
+//Get, set the scaling
+uint32_t Motor::getStepsFullTurn()
+{    return Steps_FullTurn;  }
+
+void Motor::setStepsFullTurn(uint32_t StepsFullTurn) 
+{   Steps_FullTurn = StepsFullTurn; }
 
 void Motor::setRotationPeriodSec(float Seconds_Per_Turn) {
     // rescale to usec and pass on to the next handler. 
@@ -115,7 +102,7 @@
 
 void Motor::StartTick() {
     if(!Status.TickIsAttached)   {
-        // Connect Interrupt routine in which the motor and all the state machine is performed
+        // 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();
         Status.TickIsAttached=true;
@@ -128,7 +115,7 @@
         case MOTOR_run: {
             switch(Status.state) {
                 case Motor_OFF:
-                    MotorON(); // First only turn on the motor ..
+                    MotorON(); // First only turn on the Motor ..
                     break;
                 case Motor_ZERO:
                 case Motor_ON:
@@ -160,16 +147,31 @@
 }
 
 /*******************************************************
- **   all the low level direct motor HW access
+ **   all the low level direct Motor HW access
  *******************************************************/
- 
- /** Turn off all motor Phases, no more current flowing */
+ void Motor::MotorTest()    // 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);
+        StepClkW();
+        }   
+    wait(0.5);
+    for (i=0; i<Steps_FullTurn; i++) {
+        wait(0.005);
+        StepCCW();
+        }   
+    MotorOFF();
+}
+
+ /** Turn off all Motor Phases, no more current flowing */
 void Motor::MotorOFF() 
 {   for (int ph=0; ph<4; ph++) *MPh[ph] = 0;  
     Status.state=Motor_OFF;
 }
 
-/** Turn on the motor Phase, In the last used phase, memorized in StepPhases 
+/** 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!
@@ -185,14 +187,14 @@
 }
 
 void    Motor::StepOnce()     // Move the Motor in the used 'direction'
-{   if (Status.dir == CLOCKWISE) StepRight(); else StepLeft();
+{   if (Status.dir == CLOCKWISE) StepClkW(); else StepCCW();
 }
 
-void    Motor::StepRight()    // Move the Motor one step Right
+void    Motor::StepClkW()    // Move the Motor one step Clockwise
 {   if (StepPhase<7) StepPhase++;  else  StepPhase = 0;
     SetPhases();
 }
-void    Motor::StepLeft()    // Move the Motor one step Right
+void    Motor::StepCCW()    // Move the Motor one step Clockwise
 {   if (StepPhase>0) StepPhase--;  else  StepPhase = 7;
     SetPhases();
 }