upm  1.7.1
Sensor/Actuator repository for libmraa (v2.0.0)
adafruitms1438.hpp
1 /*
2  * Author: Jon Trulson <jtrulson@ics.com>
3  * Copyright (c) 2015 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 
29 #include <string>
30 #include <mraa/i2c.h>
31 #include <mraa/gpio.h>
32 
33 #include "pca9685.hpp"
34 
35 #define ADAFRUITMS1438_I2C_BUS 0
36 #define ADAFRUITMS1438_DEFAULT_I2C_ADDR 0x60
37 
38 namespace upm {
39 
70  public:
71 
75  typedef enum {
76  DIR_NONE = 0x00,
77  DIR_CW = 0x01,
78  DIR_CCW = 0x02
79  } DIRECTION_T;
80 
84  typedef enum {
85  MOTOR_M1 = 0,
86  MOTOR_M2 = 1,
87  MOTOR_M3 = 2,
88  MOTOR_M4 = 3
89  } DCMOTORS_T;
90 
94  typedef enum {
95  STEPMOTOR_M12 = 0,
96  STEPMOTOR_M34 = 1
97  } STEPMOTORS_T;
98 
105  AdafruitMS1438(int bus, uint8_t address = ADAFRUITMS1438_DEFAULT_I2C_ADDR);
106 
110  ~AdafruitMS1438();
111 
118  uint32_t getMillis(STEPMOTORS_T motor);
119 
124  void initClock(STEPMOTORS_T motor);
125 
131  void setPWMPeriod(float hz);
132 
138  void enableMotor(DCMOTORS_T motor);
139 
145  void disableMotor(DCMOTORS_T motor);
146 
152  void enableStepper(STEPMOTORS_T motor);
153 
159  void disableStepper(STEPMOTORS_T motor);
160 
168  void setMotorSpeed(DCMOTORS_T motor, int speed);
169 
176  void setStepperSpeed(STEPMOTORS_T motor, int speed);
177 
184  void setMotorDirection(DCMOTORS_T motor, DIRECTION_T dir);
185 
193 
200  void stepConfig(STEPMOTORS_T motor, unsigned int stepsPerRev);
201 
208  void stepperSteps(STEPMOTORS_T motor, unsigned int steps);
209 
210  private:
211  /* Disable implicit copy and assignment operators */
212  AdafruitMS1438(const AdafruitMS1438&) = delete;
213  AdafruitMS1438 &operator=(const AdafruitMS1438&) = delete;
214 
215  // SWIG will generate a warning for these 'nested structs'; however,
216  // it can be ignored as these structs are never exposed.
217 
218  // struct to hold mappings of DC motors
219  typedef struct {
220  int pwm;
221  int in1;
222  int in2;
223  } DC_PINMAP_T;
224 
225  // struct to hold mappings of stepper motors
226  typedef struct {
227  int pwmA;
228  int in1A;
229  int in2A;
230  int pwmB;
231  int in1B;
232  int in2B;
233  } STEPPER_PINMAP_T;
234 
235  // struct to hold information about each stepper
236  typedef struct {
237  int stepsPerRev; // steps per revolution
238  int currentStep; // current step number
239  uint32_t stepDelay; // delay between steps
240  int stepDirection; // direction to step
241  struct timeval startTime; // starting time
242  } STEPPER_CONFIG_T;
243 
244  void setupPinMaps();
245  void stepperStep(STEPMOTORS_T motor);
246 
247  DC_PINMAP_T m_dcMotors[4];
248  STEPPER_PINMAP_T m_stepMotors[2];
249  STEPPER_CONFIG_T m_stepConfig[2];
250 
251  PCA9685 *m_pca9685;
252  };
253 }
254 
255 
void setStepperDirection(STEPMOTORS_T motor, DIRECTION_T dir)
Definition: adafruitms1438.cxx:194
void disableMotor(DCMOTORS_T motor)
Definition: adafruitms1438.cxx:128
uint32_t getMillis(STEPMOTORS_T motor)
Definition: adafruitms1438.cxx:69
void setStepperSpeed(STEPMOTORS_T motor, int speed)
Definition: adafruitms1438.cxx:163
void enableMotor(DCMOTORS_T motor)
Definition: adafruitms1438.cxx:123
void disableStepper(STEPMOTORS_T motor)
Definition: adafruitms1438.cxx:139
API for the AdafruitMS1438 Motor Shield.
Definition: adafruitms1438.hpp:69
void stepperSteps(STEPMOTORS_T motor, unsigned int steps)
Definition: adafruitms1438.cxx:281
AdafruitMS1438(int bus, uint8_t address=ADAFRUITMS1438_DEFAULT_I2C_ADDR)
Definition: adafruitms1438.cxx:36
C++ API wrapper for the bh1749 driver.
Definition: a110x.hpp:29
STEPMOTORS_T
Definition: adafruitms1438.hpp:94
API for the PCA9685 16-channel, 12-bit PWM LED Controller.
Definition: pca9685.hpp:66
void setMotorDirection(DCMOTORS_T motor, DIRECTION_T dir)
Definition: adafruitms1438.cxx:169
~AdafruitMS1438()
Definition: adafruitms1438.cxx:59
DCMOTORS_T
Definition: adafruitms1438.hpp:84
void setMotorSpeed(DCMOTORS_T motor, int speed)
Definition: adafruitms1438.cxx:145
void stepConfig(STEPMOTORS_T motor, unsigned int stepsPerRev)
Definition: adafruitms1438.cxx:210
void enableStepper(STEPMOTORS_T motor)
Definition: adafruitms1438.cxx:133
void setPWMPeriod(float hz)
Definition: adafruitms1438.cxx:115
void initClock(STEPMOTORS_T motor)
Definition: adafruitms1438.cxx:64
DIRECTION_T
Definition: adafruitms1438.hpp:75