upm  1.7.1
Sensor/Actuator repository for libmraa (v2.0.0)
grovemd.hpp
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 <stdint.h>
27 #include <sys/time.h>
28 #include <string>
29 #include <mraa/types.hpp>
30 #include <mraa/i2c.hpp>
31 
32 #define GROVEMD_I2C_BUS 0
33 #define GROVEMD_DEFAULT_I2C_ADDR 0x0f
34 
35 namespace upm {
84  class GroveMD {
85 
86  public:
87  // GroveMD registers
88  typedef enum { SET_SPEED = 0x82,
89  SET_PWM_FREQ = 0x84,
90  SET_DIRECTION = 0xaa,
91  SET_MOTOR_A = 0xa1, // not documented
92  SET_MOTOR_B = 0xa5, // not documented
93  STEPPER_ENABLE = 0x1a,
94  STEPPER_DISABLE = 0x1b,
95  STEPPER_NUM_STEPS = 0x1c
96  } REG_T;
97 
98  // legal directions for the stepper
99  typedef enum { STEP_DIR_CCW = 0x01,
100  STEP_DIR_CW = 0x00
101  } STEP_DIRECTION_T;
102 
103  // legal directions for individual DC motors
104  typedef enum { DIR_CCW = 0x02,
105  DIR_CW = 0x01
106  } DC_DIRECTION_T;
107 
108  // stepper modes
109  typedef enum { STEP_MODE1 = 0x00,
110  STEP_MODE2 = 0x01
111  } STEP_MODE_T;
112 
119  GroveMD(int bus=GROVEMD_I2C_BUS,
120  uint8_t address=GROVEMD_DEFAULT_I2C_ADDR);
121 
125  ~GroveMD();
126 
135  bool writePacket(REG_T reg, uint8_t data1, uint8_t data2);
136 
145  bool setMotorSpeeds(uint8_t speedA, uint8_t speedB);
146 
156  bool setPWMFrequencyPrescale(uint8_t freq=0x03);
157 
165  bool setMotorDirections(DC_DIRECTION_T dirA, DC_DIRECTION_T dirB);
166 
181  bool enableStepper(STEP_DIRECTION_T dir, uint8_t speed);
182 
188  bool disableStepper();
189 
201  bool setStepperSteps(unsigned int steps);
202 
212  void configStepper(unsigned int stepsPerRev, STEP_MODE_T mode=STEP_MODE1);
213 
214  protected:
215  mraa::I2c m_i2c;
216  uint8_t m_addr;
217 
218  private:
219  // steps per revolution
220  int m_stepsPerRev;
221  int m_currentStep;
222  uint32_t m_stepDelay;
223  uint32_t m_totalSteps;
224  STEP_MODE_T m_stepMode;
225 
230  void stepperStep();
231 
232  // step direction: - 1 = forward, -1 = backward
233  int m_stepDirection;
234 
235  // This is a NOOP value used to pad packets
236  static const uint8_t GROVEMD_NOOP = 0x01;
237  // our timer
238  struct timeval m_startTime;
239 
246  uint32_t getMillis();
247 
252  void initClock();
253 
254  };
255 }
256 
257 
bool enableStepper(STEP_DIRECTION_T dir, uint8_t speed)
Definition: grovemd.cxx:109
bool disableStepper()
Definition: grovemd.cxx:152
API for the Grove I2C Motor Driver.
Definition: grovemd.hpp:84
bool setMotorSpeeds(uint8_t speedA, uint8_t speedB)
Definition: grovemd.cxx:93
bool writePacket(REG_T reg, uint8_t data1, uint8_t data2)
Definition: grovemd.cxx:68
bool setPWMFrequencyPrescale(uint8_t freq=0x03)
Definition: grovemd.cxx:98
C++ API wrapper for the bh1749 driver.
Definition: a110x.hpp:29
bool setMotorDirections(DC_DIRECTION_T dirA, DC_DIRECTION_T dirB)
Definition: grovemd.cxx:103
void configStepper(unsigned int stepsPerRev, STEP_MODE_T mode=STEP_MODE1)
Definition: grovemd.cxx:215
~GroveMD()
Definition: grovemd.cxx:62
GroveMD(int bus=GROVEMD_I2C_BUS, uint8_t address=GROVEMD_DEFAULT_I2C_ADDR)
Definition: grovemd.cxx:36
bool setStepperSteps(unsigned int steps)
Definition: grovemd.cxx:162