upm  0.1.9
Sensor/Actuator repository for libmraa (v0.6.1)
adafruitms1438.h
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.h"
34 
35 #define ADAFRUITMS1438_I2C_BUS 0
36 #define ADAFRUITMS1438_DEFAULT_I2C_ADDR 0x60
37 
38 namespace upm {
39 
63  public:
64 
68  typedef enum {
69  DIR_NONE = 0x00,
70  DIR_CW = 0x01,
71  DIR_CCW = 0x02
72  } DIRECTION_T;
73 
77  typedef enum {
78  MOTOR_M1 = 0,
79  MOTOR_M2 = 1,
80  MOTOR_M3 = 2,
81  MOTOR_M4 = 3
82  } DCMOTORS_T;
83 
87  typedef enum {
88  STEPMOTOR_M12 = 0,
89  STEPMOTOR_M34 = 1
90  } STEPMOTORS_T;
91 
98  AdafruitMS1438(int bus, uint8_t address = ADAFRUITMS1438_DEFAULT_I2C_ADDR);
99 
103  ~AdafruitMS1438();
104 
111  uint32_t getMillis(STEPMOTORS_T motor);
112 
117  void initClock(STEPMOTORS_T motor);
118 
124  void setPWMPeriod(float hz);
125 
131  void enableMotor(DCMOTORS_T motor);
132 
138  void disableMotor(DCMOTORS_T motor);
139 
145  void enableStepper(STEPMOTORS_T motor);
146 
152  void disableStepper(STEPMOTORS_T motor);
153 
161  void setMotorSpeed(DCMOTORS_T motor, int speed);
162 
169  void setStepperSpeed(STEPMOTORS_T motor, int speed);
170 
177  void setMotorDirection(DCMOTORS_T motor, DIRECTION_T dir);
178 
186 
193  void stepConfig(STEPMOTORS_T motor, unsigned int stepsPerRev);
194 
201  void stepperSteps(STEPMOTORS_T motor, unsigned int steps);
202 
203  private:
204  // SWIG will generate warning for these 'nested structs', however
205  // those can be ignored as these structs are never exposed.
206 
207  // struct to hold mappings of the dc motors
208  typedef struct {
209  int pwm;
210  int in1;
211  int in2;
212  } DC_PINMAP_T;
213 
214  // struct to hold mappings of the stepper motors
215  typedef struct {
216  int pwmA;
217  int in1A;
218  int in2A;
219  int pwmB;
220  int in1B;
221  int in2B;
222  } STEPPER_PINMAP_T;
223 
224  // struct to hold some information about each stepper
225  typedef struct {
226  int stepsPerRev; // steps per revolution
227  int currentStep; // current step number
228  uint32_t stepDelay; // delay between steps
229  int stepDirection; // direction to step
230  struct timeval startTime; // starting time
231  } STEPPER_CONFIG_T;
232 
233  void setupPinMaps();
234  void stepperStep(STEPMOTORS_T motor);
235 
236  DC_PINMAP_T m_dcMotors[4];
237  STEPPER_PINMAP_T m_stepMotors[2];
238  STEPPER_CONFIG_T m_stepConfig[2];
239 
240  PCA9685 *m_pca9685;
241  };
242 }
243 
244 
void setStepperDirection(STEPMOTORS_T motor, DIRECTION_T dir)
Definition: adafruitms1438.cxx:189
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:158
void enableMotor(DCMOTORS_T motor)
Definition: adafruitms1438.cxx:123
void disableStepper(STEPMOTORS_T motor)
Definition: adafruitms1438.cxx:139
C++ API for the ADAFRUITMS1438 motor shield.
Definition: adafruitms1438.h:62
void stepperSteps(STEPMOTORS_T motor, unsigned int steps)
Definition: adafruitms1438.cxx:276
AdafruitMS1438(int bus, uint8_t address=ADAFRUITMS1438_DEFAULT_I2C_ADDR)
Definition: adafruitms1438.cxx:36
Definition: a110x.h:29
void setMotorDirection(DCMOTORS_T motor, DIRECTION_T dir)
Definition: adafruitms1438.cxx:164
~AdafruitMS1438()
Definition: adafruitms1438.cxx:59
void setMotorSpeed(DCMOTORS_T motor, int speed)
Definition: adafruitms1438.cxx:145
DIRECTION_T
Definition: adafruitms1438.h:68
void stepConfig(STEPMOTORS_T motor, unsigned int stepsPerRev)
Definition: adafruitms1438.cxx:205
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
STEPMOTORS_T
Definition: adafruitms1438.h:87
DCMOTORS_T
Definition: adafruitms1438.h:77