|
@@ -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
|
+
|