My Version of the Crealab MotorLib.

Fork of MotorLib by CreaLab

Files at this revision

API Documentation at this revision

Comitter:
garphil
Date:
Mon Jul 17 14:02:22 2017 +0000
Parent:
8:4dd59b6f4e92
Child:
10:1df5a7a265e8
Commit message:
Added Callback + interface to set calibration or speed (by ticks or speed = seconds for 360?)

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 Jul 17 11:45:44 2017 +0000
+++ b/motor.cpp	Mon Jul 17 14:02:22 2017 +0000
@@ -1,8 +1,7 @@
 #include "motor.h"
 
+void Motor::initialization(PinName _MPh0, PinName _MPh1, PinName _MPh2, PinName _MPh3, uint32_t tickTime) {
 
-Motor::Motor(PinName _MPh0, PinName _MPh1, PinName _MPh2, PinName _MPh3, uint32_t tickTime) {
- 
     MPh0 = new DigitalOut(_MPh0);
     MPh1 = new DigitalOut(_MPh1);
     MPh2 = new DigitalOut(_MPh2);
@@ -16,11 +15,52 @@
     state = Motor_IDLE;    // Default state is IDLE
     command = MOTOR_nop;       // Default command is NOP
     MotorStepTime = tickTime;      // value in micro second for one step
-    MotorFullTurn = 2140;       // Initial Calibration
-    NumSteps = 2000;               // Default
+    MotorFullTurn = MOTOR_TICKS_FOR_A_TURN;       // Initial Calibration
+    NumSteps = MotorFullTurn;               // Default 1 turn
+    motorCallback = NULL;
+    itOnStop = true;
+}
+
+Motor::Motor(PinName _MPh0, PinName _MPh1, PinName _MPh2, PinName _MPh3) {
+    initialization( _MPh0,  _MPh1,  _MPh2,  _MPh3, MOTOR_STEP_TIME_DEFAULT);
+ 
+}
+
+
+Motor::Motor(PinName _MPh0, PinName _MPh1, PinName _MPh2, PinName _MPh3, uint32_t tickTime) {
+ 
+    initialization( _MPh0,  _MPh1,  _MPh2,  _MPh3, tickTime);
  
 }
 
+
+void Motor::removeMotorCallback() {
+    motorCallback = NULL;
+}
+
+void Motor::setMotorCallback(void (*mIT)()) {
+    motorCallback = mIT;
+    itOnStop = true;
+}
+void Motor::setMotorCallback(void (*mIT)(), bool onStop) {
+    motorCallback = mIT;
+}
+
+void Motor::setCalibration(uint32_t nbTicksforFullTurn) {
+    MotorFullTurn = nbTicksforFullTurn;
+}
+
+void Motor::setDelayBtwTicks(uint32_t tickTime) {
+    MotorStepTime = tickTime;
+    if(MotorStepTime < MOTOR_STEP_TIME_MIN)  MotorStepTime = MOTOR_STEP_TIME_MIN;
+ 
+}
+
+void Motor::setSpeed(float sForOneTurn) {
+    MotorStepTime = 1000*sForOneTurn / MotorFullTurn;
+    if(MotorStepTime < MOTOR_STEP_TIME_MIN)  MotorStepTime = MOTOR_STEP_TIME_MIN;
+}
+
 void Motor::Start() {
         SetCommand(MOTOR_start);   
 };
@@ -140,6 +180,9 @@
             else if ((command == MOTOR_stop)||(NumSteps==0)) {
                 StopMotor();
                 state = Motor_IDLE;
+                    if(command != MOTOR_stop || itOnStop == true) {
+                        if(motorCallback != NULL) motorCallback();
+                    }
                 }
             command = MOTOR_nop;
             }
--- a/motor.h	Mon Jul 17 11:45:44 2017 +0000
+++ b/motor.h	Mon Jul 17 14:02:22 2017 +0000
@@ -5,6 +5,10 @@
 
 #include "mbed.h"
 
+#define MOTOR_STEP_TIME_MIN 1
+#define MOTOR_STEP_TIME_DEFAULT 5000
+#define MOTOR_TICKS_FOR_A_TURN 4096
+
 typedef enum MotorStateList {   // Define Motor States for the State Machine
     Motor_IDLE = 0,
     Motor_RUN,
@@ -38,12 +42,21 @@
  
     
     Motor(PinName _MPh0, PinName _MPh1, PinName _MPh2, PinName _MPh3, uint32_t TickTime);
+    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 RunInfinite(MotorDir direction);
     void SetDirection(MotorDir dir);
     void TestMotor();
     void RunMotor();
+    void setMotorCallback(void (*mIT)());
+    void setMotorCallback(void (*mIT)(), bool onStop);
+    void removeMotorCallback();
+
+    
+    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). 
     
     void Start();
     void Stop();
@@ -54,6 +67,8 @@
     MotorStateList getState();
    
     private:
+    void initialization(PinName _MPh0, PinName _MPh1, PinName _MPh2, PinName _MPh3, uint32_t TickTime);
+ 
     void StopMotor();
     void StartMotor();
     void SetCommand(MotorCommand cmd);
@@ -71,7 +86,8 @@
     uint32_t    MotorFullTurn;  // Number of step for a complete turn
  //   uint32_t    NumWires;       // Number of Wires
     int32_t    NumSteps;       // Number of Steps = NumWire * MotorFullTurn
-
+    void (*motorCallback)();
+    bool itOnStop;
 };
 
 #endif
\ No newline at end of file