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
 
51
 
52
     while (doWork) {
52
     while (doWork) {
53
         cout << "1 Revolution forward and back at 60 rpm" << endl;
53
         cout << "1 Revolution forward and back at 60 rpm" << endl;
54
-        sensor->setSpeed (60);
54
+        sensor->setSpeed(60);
55
         sensor->stepForward(200);
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
         cout << "1 Revolution forward and back at 150 rpm" << endl;
60
         cout << "1 Revolution forward and back at 150 rpm" << endl;
61
-        sensor->setSpeed (150);
61
+        sensor->setSpeed(150);
62
         sensor->stepForward(200);
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
         cout << "1 Revolution forward and back at 300 rpm" << endl;
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
         usleep (1000000);
70
         usleep (1000000);
71
-        sensor->stepBackwards (200);
71
+        sensor->stepBackward(200);
72
         usleep (1000000);
72
         usleep (1000000);
73
     }
73
     }
74
 
74
 

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

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

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

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

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

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

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

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

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

69
  * and/or microstepping on stepper drivers that support such features.
69
  * and/or microstepping on stepper drivers that support such features.
70
  *
70
  *
71
  * @image html stepmotor.jpg
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
  * <a href=https://creativecommons.org/licenses/by-nc-sa/3.0/>
73
  * <a href=https://creativecommons.org/licenses/by-nc-sa/3.0/>
74
  * CC BY-NC-SA-3.0</a>.</em>
74
  * CC BY-NC-SA-3.0</a>.</em>
75
  *
75
  *
128
          *
128
          *
129
          * @param ticks Number of steps the motor moves
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
          * driver is initialized.
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
         int getStep ();
154
         int getStep ();
147
 
155
 
157
         int                 m_position;
165
         int                 m_position;
158
 
166
 
159
         mraa::Result dirForward ();
167
         mraa::Result dirForward ();
160
-        mraa::Result dirBackwards ();
168
+        mraa::Result dirBackward ();
161
         void move ();
169
         void move ();
162
         void delayus (int us);
170
         void delayus (int us);
163
     };
171
     };