Kaynağa Gözat

grovemd: allow seperate directions for each DC motor

Signed-off-by: Jon Trulson <jtrulson@ics.com>
Signed-off-by: Mihai Tudor Panu <mihai.tudor.panu@intel.com>
Jon Trulson 10 yıl önce
ebeveyn
işleme
970d6a083f

+ 2
- 2
examples/c++/grovemd.cxx Dosyayı Görüntüle

@@ -39,13 +39,13 @@ int main(int argc, char **argv)
39 39
 
40 40
   // set direction to CW and set speed to 50%
41 41
   cout << "Spin M1 and M2 at half speed for 3 seconds" << endl;
42
-  motors->setDirection(upm::GroveMD::DIR_CW);
42
+  motors->setMotorDirections(upm::GroveMD::DIR_CW, upm::GroveMD::DIR_CW);
43 43
   motors->setMotorSpeeds(127, 127);
44 44
   
45 45
   sleep(3);
46 46
   // counter clockwise
47 47
   cout << "Reversing M1 and M2 for 3 seconds" << endl;
48
-  motors->setDirection(upm::GroveMD::DIR_CCW);
48
+  motors->setMotorDirections(upm::GroveMD::DIR_CCW, upm::GroveMD::DIR_CCW);
49 49
   sleep(3);
50 50
 
51 51
   //! [Interesting]

+ 4
- 2
examples/javascript/grovemd.js Dosyayı Görüntüle

@@ -33,7 +33,8 @@ function start()
33 33
 	{
34 34
 		// set direction to CW and set speed to 50%
35 35
 		console.log("Spin M1 and M2 at half speed for 3 seconds");
36
-		my_MotorDriver_obj.setDirection(groveMotorDriver_lib.GroveMD.DIR_CW);
36
+		my_MotorDriver_obj.setMotorDirections(groveMotorDriver_lib.GroveMD.DIR_CW,
37
+                                                     groveMotorDriver_lib.GroveMD.DIR_CW);
37 38
 		my_MotorDriver_obj.setMotorSpeeds(127, 127);
38 39
 	}
39 40
 }
@@ -44,7 +45,8 @@ function reverse()
44 45
 	{
45 46
 		// counter clockwise
46 47
 		console.log("Reversing M1 and M2 for 3 seconds");
47
-		my_MotorDriver_obj.setDirection(groveMotorDriver_lib.GroveMD.DIR_CCW);	
48
+		my_MotorDriver_obj.setMotorDirections(groveMotorDriver_lib.GroveMD.DIR_CCW,
49
+                                                     groveMotorDriver_lib.GroveMD.DIR_CCW);	
48 50
 	}
49 51
 }
50 52
 

+ 3
- 2
src/grovemd/grovemd.cxx Dosyayı Görüntüle

@@ -108,12 +108,13 @@ bool GroveMD::setPWMFrequencyPrescale(uint8_t freq)
108 108
   return writePacket(SET_PWM_FREQ, freq, GROVEMD_NOOP);
109 109
 }
110 110
 
111
-bool GroveMD::setDirection(DIRECTION_T dir)
111
+bool GroveMD::setMotorDirections(DC_DIRECTION_T dirA, DC_DIRECTION_T dirB)
112 112
 {
113
+  uint8_t dir = ((dirB & 0x03) << 2) | (dirA & 0x03);
113 114
   return writePacket(SET_DIRECTION, dir, GROVEMD_NOOP);
114 115
 }
115 116
 
116
-bool GroveMD::enableStepper(DIRECTION_T dir, uint8_t speed)
117
+bool GroveMD::enableStepper(STEP_DIRECTION_T dir, uint8_t speed)
117 118
 {
118 119
   return writePacket(STEPPER_ENABLE, dir, speed);
119 120
 }

+ 15
- 9
src/grovemd/grovemd.h Dosyayı Görüntüle

@@ -79,10 +79,15 @@ namespace upm {
79 79
                    STEPPER_NUM_STEPS   = 0x1c
80 80
     } REG_T;
81 81
 
82
-    // legal directions
83
-    typedef enum { DIR_CCW    = 0x0a,
84
-                   DIR_CW     = 0x05
85
-    } DIRECTION_T;
82
+    // legal directions for the stepper
83
+    typedef enum { STEP_DIR_CCW    = 0x0a,
84
+                   STEP_DIR_CW     = 0x05
85
+    } STEP_DIRECTION_T;
86
+    
87
+    // legal directions for individual DC motors
88
+    typedef enum { DIR_CCW    = 0x02,
89
+                   DIR_CW     = 0x01
90
+    } DC_DIRECTION_T;
86 91
     
87 92
     /**
88 93
      * grovemd constructor
@@ -130,22 +135,23 @@ namespace upm {
130 135
     bool setPWMFrequencyPrescale(uint8_t freq=0x03);
131 136
 
132 137
     /**
133
-     * For controlling DC motors, set the direction
138
+     * For controlling DC motors, set the directions of motors A & B
134 139
      *
135
-     * @param dir direction, CW or CCW
140
+     * @param dirA direction for motor A, DIR_CW or DIR_CCW
141
+     * @param dirB direction for motor B, DIR_CW or DIR_CCW
136 142
      * @return true if command successful
137 143
      */
138
-    bool setDirection(DIRECTION_T dir);
144
+    bool setMotorDirections(DC_DIRECTION_T dirA, DC_DIRECTION_T dirB);
139 145
 
140 146
     /**
141 147
      * For controlling a stepper motor, set a direction, speed and
142 148
      * then enable.
143 149
      *
144
-     * @param dir direction, CW or CCW
150
+     * @param dir direction, STEP_DIR_CW or STEP_DIR_CCW
145 151
      * @param speed motor speed. Valid range is 1-255, higher is slower.
146 152
      * @return true if command successful
147 153
      */
148
-    bool enableStepper(DIRECTION_T dir, uint8_t speed);
154
+    bool enableStepper(STEP_DIRECTION_T dir, uint8_t speed);
149 155
 
150 156
     /**
151 157
      * For controlling a stepper motor, stop the stepper motor.