Browse Source

stepmotor: made some small API changes for consistency and usability

Signed-off-by: Mihai Tudor Panu <mihai.tudor.panu@intel.com>
Mihai Tudor Panu 9 years ago
parent
commit
4dc679af70

+ 11
- 11
examples/c++/stepmotor.cxx View File

@@ -51,24 +51,24 @@ main(int argc, char **argv)
51 51
 
52 52
     while (doWork) {
53 53
         cout << "1 Revolution forward and back at 60 rpm" << endl;
54
-        sensor->setSpeed (60);
54
+        sensor->setSpeed(60);
55 55
         sensor->stepForward(200);
56
-        usleep (1000000);
57
-        sensor->stepBackwards(200);
58
-        usleep (1000000);
56
+        usleep(1000000);
57
+        sensor->stepBackward(200);
58
+        usleep(1000000);
59 59
 
60 60
         cout << "1 Revolution forward and back at 150 rpm" << endl;
61
-        sensor->setSpeed (150);
61
+        sensor->setSpeed(150);
62 62
         sensor->stepForward(200);
63
-        usleep (1000000);
64
-        sensor->stepBackwards(200);
65
-        usleep (1000000);
63
+        usleep(1000000);
64
+        sensor->stepBackward(200);
65
+        usleep(1000000);
66 66
 
67 67
         cout << "1 Revolution forward and back at 300 rpm" << endl;
68
-        sensor->setSpeed (300);
69
-        sensor->stepForward (200);
68
+        sensor->setSpeed(300);
69
+        sensor->stepForward(200);
70 70
         usleep (1000000);
71
-        sensor->stepBackwards (200);
71
+        sensor->stepBackward(200);
72 72
         usleep (1000000);
73 73
     }
74 74
 

+ 3
- 3
examples/java/StepMotorSample.java View File

@@ -34,21 +34,21 @@ public class StepMotorSample {
34 34
             sensor.setSpeed(60);
35 35
             sensor.stepForward(200);
36 36
             Thread.sleep(1000);
37
-            sensor.stepBackwards(200);
37
+            sensor.stepBackward(200);
38 38
             Thread.sleep(1000);
39 39
 
40 40
             System.out.println("One complete rotation forward and back at 150 rpm.");
41 41
             sensor.setSpeed(150);
42 42
             sensor.stepForward(200);
43 43
             Thread.sleep(1000);
44
-            sensor.stepBackwards(200);
44
+            sensor.stepBackward(200);
45 45
             Thread.sleep(1000);
46 46
 
47 47
             System.out.println("One complete rotation forward and back at 300 rpm.");
48 48
             sensor.setSpeed(300);
49 49
             sensor.stepForward(200);
50 50
             Thread.sleep(1000);
51
-            sensor.stepBackwards(200);
51
+            sensor.stepBackward(200);
52 52
             Thread.sleep(1000);
53 53
         }
54 54
         // ! [Interesting]

+ 3
- 3
examples/javascript/stepmotor.js View File

@@ -48,21 +48,21 @@ console.log("Rotating 1 revolution forward and back at 60 rpm.");
48 48
 stepper.setSpeed(60);
49 49
 stepper.stepForward(200);
50 50
 delay(1000);
51
-stepper.stepBackwards(200);
51
+stepper.stepBackward(200);
52 52
 delay(1000);
53 53
 
54 54
 console.log("Rotating 1 revolution forward and back at 150 rpm.");
55 55
 stepper.setSpeed(150);
56 56
 stepper.stepForward(200);
57 57
 delay(1000);
58
-stepper.stepBackwards(200);
58
+stepper.stepBackward(200);
59 59
 delay(1000);
60 60
 
61 61
 console.log("Rotating 1 revolution forward and back at 300 rpm.");
62 62
 stepper.setSpeed(300);
63 63
 stepper.stepForward(200);
64 64
 delay(1000);
65
-stepper.stepBackwards(200);
65
+stepper.stepBackward(200);
66 66
 delay(1000);
67 67
 
68 68
 stepper.quit();

+ 3
- 3
examples/python/stepmotor.py View File

@@ -46,19 +46,19 @@ print "Rotating 1 revolution forward and back at 60 rpm."
46 46
 stepper.setSpeed(60)
47 47
 stepper.stepForward(200)
48 48
 time.sleep(1)
49
-stepper.stepBackwards(200)
49
+stepper.stepBackward(200)
50 50
 time.sleep(1)
51 51
 
52 52
 print "Rotating 1 revolution forward and back at 150 rpm."
53 53
 stepper.setSpeed(150)
54 54
 stepper.stepForward(200)
55 55
 time.sleep(1)
56
-stepper.stepBackwards(200)
56
+stepper.stepBackward(200)
57 57
 time.sleep(1)
58 58
 
59 59
 print "Rotating 1 revolution forward and back at 300 rpm."
60 60
 stepper.setSpeed(300)
61 61
 stepper.stepForward(200)
62 62
 time.sleep(1)
63
-stepper.stepBackwards(200)
63
+stepper.stepBackward(200)
64 64
 time.sleep(1)

+ 20
- 17
src/stepmotor/stepmotor.cxx View File

@@ -34,10 +34,13 @@ using namespace upm;
34 34
 using namespace std;
35 35
 
36 36
 StepMotor::StepMotor (int dirPin, int stePin, int steps, int enPin)
37
-                    : m_dirPinCtx(dirPin), m_stePinCtx(stePin), m_enPinCtx(0), m_steps(steps) {
37
+                    : m_dirPinCtx(dirPin),
38
+                      m_stePinCtx(stePin),
39
+                      m_enPinCtx(0),
40
+                      m_steps(steps) {
38 41
     m_name = "StepMotor";
39 42
     setSpeed(60);
40
-    setStep(0);
43
+    setPosition(0);
41 44
 
42 45
     if (m_dirPinCtx.dir(mraa::DIR_OUT) != mraa::SUCCESS) {
43 46
         throw std::runtime_error(string(__FUNCTION__) +
@@ -95,7 +98,7 @@ StepMotor::setSpeed (int speed) {
95 98
 mraa::Result
96 99
 StepMotor::step (int ticks) {
97 100
     if (ticks < 0) {
98
-        return stepBackwards(abs(ticks));
101
+        return stepBackward(abs(ticks));
99 102
     } else {
100 103
         return stepForward(ticks);
101 104
     }
@@ -106,39 +109,39 @@ StepMotor::stepForward (int ticks) {
106 109
     dirForward();
107 110
     for (int i = 0; i < ticks; i++) {
108 111
         move();
109
-        if (++m_position >= m_steps) {
110
-            m_position = 0;
111
-        }
112
+        m_position++;
112 113
         delayus(m_delay - MINPULSE_US - OVERHEAD_US);
113 114
     }
114 115
     return mraa::SUCCESS;
115 116
 }
116 117
 
117 118
 mraa::Result
118
-StepMotor::stepBackwards (int ticks) {
119
-    dirBackwards();
119
+StepMotor::stepBackward (int ticks) {
120
+    dirBackward();
120 121
     for (int i = 0; i < ticks; i++) {
121 122
         move();
122
-        if (--m_position < 0) {
123
-            m_position = m_steps - 1;
124
-        }
123
+        m_position--;
125 124
         delayus(m_delay - MINPULSE_US - OVERHEAD_US);
126 125
     }
127 126
     return mraa::SUCCESS;
128 127
 }
129 128
 
130 129
 void
131
-StepMotor::setStep (int step) {
132
-    if (step <= m_steps) {
133
-        m_position = step;
134
-    }
130
+StepMotor::setPosition (int pos) {
131
+    m_position = pos;
135 132
 }
136 133
 
137 134
 int
138
-StepMotor::getStep () {
135
+StepMotor::getPosition () {
139 136
     return m_position;
140 137
 }
141 138
 
139
+int
140
+StepMotor::getStep () {
141
+    return m_position < 0 ? m_steps + m_position % m_steps :
142
+                            m_position % m_steps;
143
+}
144
+
142 145
 void
143 146
 StepMotor::move () {
144 147
     m_stePinCtx.write(1);
@@ -157,7 +160,7 @@ StepMotor::dirForward () {
157 160
 }
158 161
 
159 162
 mraa::Result
160
-StepMotor::dirBackwards () {
163
+StepMotor::dirBackward () {
161 164
     mraa::Result error = m_dirPinCtx.write(LOW);
162 165
     if (error != mraa::SUCCESS) {
163 166
         throw std::runtime_error(string(__FUNCTION__) +

+ 16
- 8
src/stepmotor/stepmotor.h View File

@@ -69,7 +69,7 @@ namespace upm {
69 69
  * and/or microstepping on stepper drivers that support such features.
70 70
  *
71 71
  * @image html stepmotor.jpg
72
- * <br><em>ECS1030 Sensor image provided by SparkFun* under
72
+ * <br><em>EasyDriver Sensor image provided by SparkFun* under
73 73
  * <a href=https://creativecommons.org/licenses/by-nc-sa/3.0/>
74 74
  * CC BY-NC-SA-3.0</a>.</em>
75 75
  *
@@ -128,20 +128,28 @@ class StepMotor {
128 128
          *
129 129
          * @param ticks Number of steps the motor moves
130 130
          */
131
-        mraa::Result stepBackwards (int ticks);
131
+        mraa::Result stepBackward (int ticks);
132 132
 
133 133
         /**
134
-         * Sets the current step. Useful if the motor is not at 0 when the
134
+         * Sets the current position. Useful if the motor is not at 0 when the
135 135
          * driver is initialized.
136 136
          *
137
-         * @param step Current shaft position
137
+         * @param step Current position
138 138
          */
139
-        void setStep (int step);
139
+        void setPosition (int pos);
140 140
 
141 141
         /**
142
-         * Gets the current step.
142
+         * Gets the current position. This is cumulative and the result of all
143
+         * the step commands sent to the motor.
143 144
          *
144
-         * @return Current shaft position.
145
+         * @return Stepper's position.
146
+         */
147
+        int getPosition ();
148
+
149
+        /**
150
+         * Gets the current step. This is relative to one revolution.
151
+         *
152
+         * @return Current step, ranges from 0 to number of steps per revolution.
145 153
          */
146 154
         int getStep ();
147 155
 
@@ -157,7 +165,7 @@ class StepMotor {
157 165
         int                 m_position;
158 166
 
159 167
         mraa::Result dirForward ();
160
-        mraa::Result dirBackwards ();
168
+        mraa::Result dirBackward ();
161 169
         void move ();
162 170
         void delayus (int us);
163 171
     };