Browse Source

grovemd: Initial implementation

This module implements support for the Grove I2C Motor Driver.

The device *requires* a 100Khz I2C bus speed.  It will not work on
anything faster.

Signed-off-by: Jon Trulson <jtrulson@ics.com>
Signed-off-by: Zion Orent <zorent@ics.com>
Signed-off-by: Mihai Tudor Panu <mihai.tudor.panu@intel.com>
Jon Trulson 10 years ago
parent
commit
d380658b40

+ 3
- 0
examples/c++/CMakeLists.txt View File

@@ -97,6 +97,7 @@ add_executable (ina132-example ina132.cxx)
97 97
 add_executable (l298-example l298.cxx)
98 98
 add_executable (l298-stepper-example l298-stepper.cxx)
99 99
 add_executable (at42qt1070-example at42qt1070.cxx)
100
+add_executable (grovemd-example grovemd.cxx)
100 101
 
101 102
 include_directories (${PROJECT_SOURCE_DIR}/src/hmc5883l)
102 103
 include_directories (${PROJECT_SOURCE_DIR}/src/grove)
@@ -175,6 +176,7 @@ include_directories (${PROJECT_SOURCE_DIR}/src/grovegsr)
175 176
 include_directories (${PROJECT_SOURCE_DIR}/src/ina132)
176 177
 include_directories (${PROJECT_SOURCE_DIR}/src/l298)
177 178
 include_directories (${PROJECT_SOURCE_DIR}/src/at42qt1070)
179
+include_directories (${PROJECT_SOURCE_DIR}/src/grovemd)
178 180
 
179 181
 target_link_libraries (hmc5883l-example hmc5883l ${CMAKE_THREAD_LIBS_INIT})
180 182
 target_link_libraries (groveled-example grove ${CMAKE_THREAD_LIBS_INIT})
@@ -273,3 +275,4 @@ target_link_libraries (ina132-example ina132 ${CMAKE_THREAD_LIBS_INIT})
273 275
 target_link_libraries (l298-example l298 ${CMAKE_THREAD_LIBS_INIT})
274 276
 target_link_libraries (l298-stepper-example l298 ${CMAKE_THREAD_LIBS_INIT})
275 277
 target_link_libraries (at42qt1070-example at42qt1070 ${CMAKE_THREAD_LIBS_INIT})
278
+target_link_libraries (grovemd-example grovemd ${CMAKE_THREAD_LIBS_INIT})

+ 60
- 0
examples/c++/grovemd.cxx View File

@@ -0,0 +1,60 @@
1
+/*
2
+ * Author: Jon Trulson <jtrulson@ics.com>
3
+ * Copyright (c) 2014 Intel Corporation.
4
+ *
5
+ * Permission is hereby granted, free of charge, to any person obtaining
6
+ * a copy of this software and associated documentation files (the
7
+ * "Software"), to deal in the Software without restriction, including
8
+ * without limitation the rights to use, copy, modify, merge, publish,
9
+ * distribute, sublicense, and/or sell copies of the Software, and to
10
+ * permit persons to whom the Software is furnished to do so, subject to
11
+ * the following conditions:
12
+ *
13
+ * The above copyright notice and this permission notice shall be
14
+ * included in all copies or substantial portions of the Software.
15
+ *
16
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
23
+ */
24
+
25
+#include <unistd.h>
26
+#include <signal.h>
27
+#include <iostream>
28
+#include "grovemd.h"
29
+
30
+using namespace std;
31
+
32
+int main(int argc, char **argv)
33
+{
34
+  //! [Interesting]
35
+  // Instantiate an I2C Grove Motor Driver on I2C bus 0
36
+
37
+  upm::GroveMD *motors = new upm::GroveMD(GROVEMD_I2C_BUS, 
38
+                                          GROVEMD_DEFAULT_I2C_ADDR);
39
+
40
+  // set direction to CW and set speed to 50%
41
+  cout << "Spin M1 and M2 at half speed for 3 seconds" << endl;
42
+  motors->setDirection(upm::GroveMD::DIR_CW);
43
+  motors->setMotorSpeeds(127, 127);
44
+  
45
+  sleep(3);
46
+  // counter clockwise
47
+  cout << "Reversing M1 and M2 for 3 seconds" << endl;
48
+  motors->setDirection(upm::GroveMD::DIR_CCW);
49
+  sleep(3);
50
+
51
+  //! [Interesting]
52
+
53
+  cout << "Stopping motors" << endl;
54
+  motors->setMotorSpeeds(0, 0);
55
+
56
+  cout << "Exiting..." << endl;
57
+
58
+  delete motors;
59
+  return 0;
60
+}

+ 92
- 0
examples/javascript/grovemd.js View File

@@ -0,0 +1,92 @@
1
+/*jslint node:true, vars:true, bitwise:true, unparam:true */
2
+/*jshint unused:true */
3
+
4
+/*
5
+* Author: Zion Orent <zorent@ics.com>
6
+* Copyright (c) 2015 Intel Corporation.
7
+*
8
+* Permission is hereby granted, free of charge, to any person obtaining
9
+* a copy of this software and associated documentation files (the
10
+* "Software"), to deal in the Software without restriction, including
11
+* without limitation the rights to use, copy, modify, merge, publish,
12
+* distribute, sublicense, and/or sell copies of the Software, and to
13
+* permit persons to whom the Software is furnished to do so, subject to
14
+* the following conditions:
15
+*
16
+* The above copyright notice and this permission notice shall be
17
+* included in all copies or substantial portions of the Software.
18
+*
19
+* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
20
+* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
21
+* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
22
+* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
23
+* LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
24
+* OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
25
+* WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
26
+*/
27
+
28
+var groveMotorDriver_lib = require('jsupm_grovemd');
29
+
30
+function start()
31
+{
32
+	if (my_MotorDriver_obj)
33
+	{
34
+		// set direction to CW and set speed to 50%
35
+		console.log("Spin M1 and M2 at half speed for 3 seconds");
36
+		my_MotorDriver_obj.setDirection(groveMotorDriver_lib.GroveMD.DIR_CW);
37
+		my_MotorDriver_obj.setMotorSpeeds(127, 127);
38
+	}
39
+}
40
+
41
+function reverse()
42
+{
43
+	if (my_MotorDriver_obj)
44
+	{
45
+		// counter clockwise
46
+		console.log("Reversing M1 and M2 for 3 seconds");
47
+		my_MotorDriver_obj.setDirection(groveMotorDriver_lib.GroveMD.DIR_CCW);	
48
+	}
49
+}
50
+
51
+function end()
52
+{
53
+	if (my_MotorDriver_obj)
54
+	{
55
+		console.log("Stopping motors");
56
+		my_MotorDriver_obj.setMotorSpeeds(0, 0);
57
+	}
58
+	exit();
59
+}
60
+
61
+// When exiting: clear memory and print exit message
62
+function exit()
63
+{
64
+	if (my_MotorDriver_obj)
65
+	{
66
+		my_MotorDriver_obj = null;
67
+		groveMotorDriver_lib.cleanUp();
68
+	}
69
+	groveMotorDriver_lib = null;
70
+	console.log("Exiting");
71
+	process.exit(0);	
72
+}
73
+
74
+
75
+// Instantiate an I2C Grove Motor Driver on I2C bus 0
76
+var my_MotorDriver_obj = new groveMotorDriver_lib.GroveMD(
77
+	groveMotorDriver_lib.GROVEMD_I2C_BUS, 
78
+	groveMotorDriver_lib.GROVEMD_DEFAULT_I2C_ADDR);
79
+
80
+start();
81
+
82
+setTimeout(function()
83
+{
84
+	reverse();
85
+	setTimeout(end, 3000);
86
+}, 3000);
87
+
88
+
89
+process.on('SIGINT', function()
90
+{
91
+	exit();
92
+});

+ 5
- 0
src/grovemd/CMakeLists.txt View File

@@ -0,0 +1,5 @@
1
+set (libname "grovemd")
2
+set (libdescription "upm grove i2c motor driver module")
3
+set (module_src ${libname}.cxx)
4
+set (module_h ${libname}.h)
5
+upm_module_init()

+ 138
- 0
src/grovemd/grovemd.cxx View File

@@ -0,0 +1,138 @@
1
+/*
2
+ * Author: Jon Trulson <jtrulson@ics.com>
3
+ * Copyright (c) 2014 Intel Corporation.
4
+ *
5
+ * Permission is hereby granted, free of charge, to any person obtaining
6
+ * a copy of this software and associated documentation files (the
7
+ * "Software"), to deal in the Software without restriction, including
8
+ * without limitation the rights to use, copy, modify, merge, publish,
9
+ * distribute, sublicense, and/or sell copies of the Software, and to
10
+ * permit persons to whom the Software is furnished to do so, subject to
11
+ * the following conditions:
12
+ *
13
+ * The above copyright notice and this permission notice shall be
14
+ * included in all copies or substantial portions of the Software.
15
+ *
16
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
23
+ */
24
+
25
+#include <unistd.h>
26
+#include <iostream>
27
+#include <string>
28
+
29
+#include "grovemd.h"
30
+
31
+using namespace upm;
32
+using namespace std;
33
+
34
+
35
+GroveMD::GroveMD(int bus, uint8_t address)
36
+{
37
+  m_addr = address;
38
+
39
+  // setup our i2c link
40
+  if ( !(m_i2c = mraa_i2c_init(bus)) )
41
+    {
42
+      cerr << __FUNCTION__ << ": mraa_i2c_init() failed" << endl;
43
+      return;
44
+    }
45
+
46
+  // this board *requires* 100Khz i2c bus only
47
+  mraa_result_t rv;
48
+  if ( (rv = mraa_i2c_frequency(m_i2c, MRAA_I2C_STD)) != MRAA_SUCCESS )
49
+    {
50
+      cerr << "GroveMD: Could not set i2c frequency (MRAA_I2C_STD). " << endl;
51
+      mraa_result_print(rv);
52
+      return;
53
+    }
54
+
55
+  if (mraa_i2c_address(m_i2c, m_addr))
56
+    {
57
+      cerr << "GroveMD: Could not initialize i2c bus. " << endl;
58
+      return;
59
+    }
60
+}
61
+
62
+GroveMD::~GroveMD()
63
+{
64
+  setMotorSpeeds(0, 0);
65
+  mraa_i2c_stop(m_i2c);
66
+}
67
+
68
+bool GroveMD::writePacket(REG_T reg, uint8_t data1, uint8_t data2)
69
+{
70
+  uint8_t buf[3];
71
+
72
+  buf[0] = reg;
73
+  buf[1] = data1;
74
+  buf[2] = data2;
75
+
76
+  mraa_result_t rv;
77
+  if ( (rv = mraa_i2c_address(m_i2c, m_addr)) != MRAA_SUCCESS )
78
+    {
79
+      cerr << __FUNCTION__ << ": mraa_i2c_address() failed. " << endl;
80
+      mraa_result_print(rv);
81
+      return false;
82
+    }
83
+
84
+  // This sleep appears to be required.  Without it, writes randomly
85
+  // fail (no ACK received).  This happens most often on the SET_SPEED
86
+  // packet.  I am guessing that there is a timing problem and/or bug
87
+  // in the motor driver's firmware.
88
+
89
+  usleep(100);
90
+
91
+  if ( (rv = mraa_i2c_write(m_i2c, buf, 3)) != MRAA_SUCCESS )
92
+    {
93
+      cerr << __FUNCTION__ << ": mraa_i2c_write() failed:" << endl;
94
+      mraa_result_print(rv);
95
+      return false;
96
+    }
97
+
98
+  return true;
99
+}
100
+
101
+bool GroveMD::setMotorSpeeds(uint8_t speedA, uint8_t speedB)
102
+{
103
+  return writePacket(SET_SPEED, speedA, speedB);
104
+}
105
+
106
+bool GroveMD::setPWMFrequencyPrescale(uint8_t freq)
107
+{
108
+  return writePacket(SET_PWM_FREQ, freq, GROVEMD_NOOP);
109
+}
110
+
111
+bool GroveMD::setDirection(DIRECTION_T dir)
112
+{
113
+  return writePacket(SET_DIRECTION, dir, GROVEMD_NOOP);
114
+}
115
+
116
+bool GroveMD::enableStepper(DIRECTION_T dir, uint8_t speed)
117
+{
118
+  return writePacket(STEPPER_ENABLE, dir, speed);
119
+}
120
+
121
+bool GroveMD::disableStepper()
122
+{
123
+  return writePacket(STEPPER_DISABLE, GROVEMD_NOOP, GROVEMD_NOOP);
124
+}
125
+
126
+bool GroveMD::setStepperSteps(uint8_t steps)
127
+{
128
+  if (steps == 0)
129
+    {
130
+      // invalid
131
+      cerr << __FUNCTION__ << ": invalid number of steps.  "
132
+           << "Valid values are between 1 and 255." << endl;
133
+      return false;
134
+    }
135
+
136
+  return writePacket(STEPPER_NUM_STEPS, steps, GROVEMD_NOOP);
137
+}
138
+

+ 173
- 0
src/grovemd/grovemd.h View File

@@ -0,0 +1,173 @@
1
+/*
2
+ * Author: Jon Trulson <jtrulson@ics.com>
3
+ * Copyright (c) 2014 Intel Corporation.
4
+ *
5
+ * Permission is hereby granted, free of charge, to any person obtaining
6
+ * a copy of this software and associated documentation files (the
7
+ * "Software"), to deal in the Software without restriction, including
8
+ * without limitation the rights to use, copy, modify, merge, publish,
9
+ * distribute, sublicense, and/or sell copies of the Software, and to
10
+ * permit persons to whom the Software is furnished to do so, subject to
11
+ * the following conditions:
12
+ *
13
+ * The above copyright notice and this permission notice shall be
14
+ * included in all copies or substantial portions of the Software.
15
+ *
16
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
23
+ */
24
+#pragma once
25
+
26
+#include <string>
27
+#include <mraa/i2c.h>
28
+
29
+#define GROVEMD_I2C_BUS 0
30
+#define GROVEMD_DEFAULT_I2C_ADDR 0x0f
31
+
32
+// This is a NOOP value used to pad packets
33
+#define GROVEMD_NOOP 0x01
34
+
35
+namespace upm {
36
+  /**
37
+   * @brief UPM module for the Grove I2C Motor Driver
38
+   * @defgroup grovemd libupm-grovemd
39
+   * @ingroup seeed i2c motor
40
+   */
41
+
42
+  /**
43
+   * @sensor grovemd
44
+   * @library grovemd
45
+   * @name Grove I2C Motor Driver
46
+   * @category motor
47
+   * @manufacturer seeed
48
+   * @connection i2c
49
+   *
50
+   * @brief C++ API for the Grove I2C Motor Driver 
51
+   *
52
+   * This class implements support for the Grove I2C Motor Driver.
53
+   * This device can support a single 4-wire stepper motor, OR two
54
+   * 2-wire DC motors.  The device contains an Atmel ATmega8L
55
+   * microcontroller that manages an L298N H-bridge driver chip.
56
+   *
57
+   * This device supports an i2c bus speed of 100Khz only.
58
+   *
59
+   * The module does not provide any telemetry or status -- it only
60
+   * accepts I2C commands for its various operations.
61
+   *
62
+   * This module was tested with version 1.3 of the Grove I2C Motor
63
+   * Driver
64
+   *
65
+   * @ingroup i2c grove
66
+   * @snippet grovemd.cxx Interesting
67
+   */
68
+  class GroveMD {
69
+
70
+  public:
71
+    // GroveMD registers
72
+    typedef enum { SET_SPEED           = 0x82,
73
+                   SET_PWM_FREQ        = 0x84,
74
+                   SET_DIRECTION       = 0xaa,
75
+                   SET_MOTOR_A         = 0xa1, // not documented
76
+                   SET_MOTOR_B         = 0xa5, // not documented
77
+                   STEPPER_ENABLE      = 0x1a, 
78
+                   STEPPER_DISABLE     = 0x1b,
79
+                   STEPPER_NUM_STEPS   = 0x1c
80
+    } REG_T;
81
+
82
+    // legal directions
83
+    typedef enum { DIR_CCW    = 0x0a,
84
+                   DIR_CW     = 0x05
85
+    } DIRECTION_T;
86
+    
87
+    /**
88
+     * grovemd constructor
89
+     *
90
+     * @param bus i2c bus to use
91
+     * @param address i2c address to use
92
+     */
93
+    GroveMD(int bus=GROVEMD_I2C_BUS, 
94
+            uint8_t address=GROVEMD_DEFAULT_I2C_ADDR);
95
+
96
+    /**
97
+     * GroveMD Destructor
98
+     */
99
+    ~GroveMD();
100
+
101
+    /**
102
+     * Compose and write a 3-byte packet to the controller
103
+     *
104
+     * @param reg register location
105
+     * @param data1 first byte of data
106
+     * @param data2 second byte of data
107
+     * @return true if write successful
108
+     */
109
+    bool writePacket(REG_T reg, uint8_t data1, uint8_t data2);
110
+
111
+    /**
112
+     * For controlling DC motors, set the speeds of motors A & B.
113
+     * Valid values are 0-255.
114
+     *
115
+     * @param speedA speed of motor A
116
+     * @param speedB speed of motor B
117
+     * @return true if command successful
118
+     */
119
+    bool setMotorSpeeds(uint8_t speedA, uint8_t speedB);
120
+
121
+    /**
122
+     * For controlling DC motors, set the PWM frequency prescale
123
+     * factor.  Note this register is not ducumented other than to say
124
+     * that the default value is 0x03.  Presumably this is the timer
125
+     * pre-scale factor used on the ATMega MCU timer driving the PWM.
126
+     *
127
+     * @param freq PWM prescale frequency, default 0x03
128
+     * @return true if command successful
129
+     */
130
+    bool setPWMFrequencyPrescale(uint8_t freq=0x03);
131
+
132
+    /**
133
+     * For controlling DC motors, set the direction
134
+     *
135
+     * @param dir direction, CW or CCW
136
+     * @return true if command successful
137
+     */
138
+    bool setDirection(DIRECTION_T dir);
139
+
140
+    /**
141
+     * For controlling a stepper motor, set a direction, speed and
142
+     * then enable.
143
+     *
144
+     * @param dir direction, CW or CCW
145
+     * @param speed motor speed. Valid range is 1-255, higher is slower.
146
+     * @return true if command successful
147
+     */
148
+    bool enableStepper(DIRECTION_T dir, uint8_t speed);
149
+
150
+    /**
151
+     * For controlling a stepper motor, stop the stepper motor.
152
+     *
153
+     * @return true if command successful
154
+     */
155
+    bool disableStepper();
156
+
157
+    /**
158
+     * For controlling a stepper motor, specify the number of steps to
159
+     * execute.  Valid values are 1-255, 255 means to rotate continuously.
160
+     *
161
+     * @param steps number of steps to execute.  255 means rotate continously.
162
+     * @return true if command successful
163
+     */
164
+    bool setStepperSteps(uint8_t steps);
165
+
166
+
167
+  private:
168
+    mraa_i2c_context m_i2c;
169
+    uint8_t m_addr;
170
+  };
171
+}
172
+
173
+

+ 8
- 0
src/grovemd/jsupm_grovemd.i View File

@@ -0,0 +1,8 @@
1
+%module jsupm_grovemd
2
+%include "../upm.i"
3
+
4
+%{
5
+    #include "grovemd.h"
6
+%}
7
+
8
+%include "grovemd.h"

+ 13
- 0
src/grovemd/pyupm_grovemd.i View File

@@ -0,0 +1,13 @@
1
+%module pyupm_grovemd
2
+%include "../upm.i"
3
+
4
+%feature("autodoc", "3");
5
+
6
+#ifdef DOXYGEN
7
+%include "grovemd_doc.i"
8
+#endif
9
+
10
+%include "grovemd.h"
11
+%{
12
+    #include "grovemd.h"
13
+%}