coucou

Dependencies:   RoboClaw mbed

Fork of Robot2016_2-0 by ARES

Revision:
71:5590dbe8393a
Parent:
58:02dc8328975a
--- a/StepperMotor/Stepper.cpp	Wed May 04 21:51:00 2016 +0000
+++ b/StepperMotor/Stepper.cpp	Thu May 05 03:47:05 2016 +0000
@@ -10,15 +10,33 @@
     Step_Per_MM = step_per_mm;
 }
 
-void Stepper::step(int number, int dir, float speed)
+bool Stepper::step(int _number, int _dir, float _speed, bool _async)
 {
-    if (dir == HAUT) {
-        direction = 0;
-    } else if (dir == BAS) {
-        direction = 1;
+    if(done())
+    {
+        dir = _dir;
+        number = _number;
+        speed = _speed;
+        async = _async;
+        
+        if (dir == HAUT)
+            direction = 0;
+        else if (dir == BAS)
+            direction = 1;
+        
+        ticker.attach(this,&Stepper::tick,_speed);
+        if(!_async)
+            while(!done())
+                wait(speed);
+        
+        return true;
     }
     
-    //  Step...
+    return false;
+    
+    
+    
+    /*  Step...
     for(int i=0; i<number && !(minEndStop.read() == 0 && dir == BAS); i++)
     {
         stepPin = 1;
@@ -26,18 +44,39 @@
         stepPin = 0;
         wait_us(5);
         wait(speed);
+    }*/
+}
+
+bool Stepper::mm(int _number, int _dir, bool _async)
+{
+    return step(_number*Step_Per_MM, _dir, DELAY-0.001, _async);
+}
+
+bool Stepper::mm(float _distance, bool _async)
+{
+    return step(abs(_distance)*Step_Per_MM, (_distance>0?HAUT:BAS), DELAY-0.001, _async);
+}
+
+bool Stepper::done()
+{
+    return number == 0;
+}
+
+void Stepper::tick()
+{
+    stepPin = 1;
+    wait_us(5);
+    stepPin = 0;
+    
+    number--;
+    
+    if(number <= 0 || (minEndStop.read() == 0 && dir == BAS))
+    {
+        ticker.detach();
+        number = 0;
     }
 }
 
-void Stepper::mm(int number, int dir)
-{
-    step(number*Step_Per_MM, dir, DELAY-0.001);
-}
-
-void Stepper::mm(float distance)
-{
-    step(abs(distance*Step_Per_MM), (distance>0?HAUT:BAS), DELAY-0.001);
-}
 
 void Stepper::enable()
 {