upm  0.5.1
Sensor/Actuator repository for libmraa (v0.9.1)
 All Data Structures Files Functions Variables Enumerations Enumerator Macros Groups Pages
grovemd.h
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 {
82  class GroveMD {
83 
84  public:
85  // GroveMD registers
86  typedef enum { SET_SPEED = 0x82,
87  SET_PWM_FREQ = 0x84,
88  SET_DIRECTION = 0xaa,
89  SET_MOTOR_A = 0xa1, // not documented
90  SET_MOTOR_B = 0xa5, // not documented
91  STEPPER_ENABLE = 0x1a,
92  STEPPER_DISABLE = 0x1b,
93  STEPPER_NUM_STEPS = 0x1c
94  } REG_T;
95 
96  // legal directions for the stepper
97  typedef enum { STEP_DIR_CCW = 0x01,
98  STEP_DIR_CW = 0x00
99  } STEP_DIRECTION_T;
100 
101  // legal directions for individual DC motors
102  typedef enum { DIR_CCW = 0x02,
103  DIR_CW = 0x01
104  } DC_DIRECTION_T;
105 
106  // stepper modes
107  typedef enum { STEP_MODE1 = 0x00,
108  STEP_MODE2 = 0x01
109  } STEP_MODE_T;
110 
117  GroveMD(int bus=GROVEMD_I2C_BUS,
118  uint8_t address=GROVEMD_DEFAULT_I2C_ADDR);
119 
123  ~GroveMD();
124 
133  bool writePacket(REG_T reg, uint8_t data1, uint8_t data2);
134 
143  bool setMotorSpeeds(uint8_t speedA, uint8_t speedB);
144 
154  bool setPWMFrequencyPrescale(uint8_t freq=0x03);
155 
163  bool setMotorDirections(DC_DIRECTION_T dirA, DC_DIRECTION_T dirB);
164 
179  bool enableStepper(STEP_DIRECTION_T dir, uint8_t speed);
180 
186  bool disableStepper();
187 
199  bool setStepperSteps(unsigned int steps);
200 
210  void configStepper(unsigned int stepsPerRev, STEP_MODE_T mode=STEP_MODE1);
211 
212  protected:
213  mraa::I2c m_i2c;
214  uint8_t m_addr;
215 
216  private:
217  // steps per revolution
218  int m_stepsPerRev;
219  int m_currentStep;
220  uint32_t m_stepDelay;
221  uint32_t m_totalSteps;
222  STEP_MODE_T m_stepMode;
223 
228  void stepperStep();
229 
230  // step direction: - 1 = forward, -1 = backward
231  int m_stepDirection;
232 
233  // This is a NOOP value used to pad packets
234  static const uint8_t GROVEMD_NOOP = 0x01;
235  // our timer
236  struct timeval m_startTime;
237 
244  uint32_t getMillis();
245 
250  void initClock();
251 
252  };
253 }
254 
255 
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.h:82
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
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