upm  0.2.0
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 
69  public:
70 
74  typedef enum {
75  DIR_NONE = 0x00,
76  DIR_CW = 0x01,
77  DIR_CCW = 0x02
78  } DIRECTION_T;
79 
83  typedef enum {
84  MOTOR_M1 = 0,
85  MOTOR_M2 = 1,
86  MOTOR_M3 = 2,
87  MOTOR_M4 = 3
88  } DCMOTORS_T;
89 
93  typedef enum {
94  STEPMOTOR_M12 = 0,
95  STEPMOTOR_M34 = 1
96  } STEPMOTORS_T;
97 
104  AdafruitMS1438(int bus, uint8_t address = ADAFRUITMS1438_DEFAULT_I2C_ADDR);
105 
109  ~AdafruitMS1438();
110 
117  uint32_t getMillis(STEPMOTORS_T motor);
118 
123  void initClock(STEPMOTORS_T motor);
124 
130  void setPWMPeriod(float hz);
131 
137  void enableMotor(DCMOTORS_T motor);
138 
144  void disableMotor(DCMOTORS_T motor);
145 
151  void enableStepper(STEPMOTORS_T motor);
152 
158  void disableStepper(STEPMOTORS_T motor);
159 
167  void setMotorSpeed(DCMOTORS_T motor, int speed);
168 
175  void setStepperSpeed(STEPMOTORS_T motor, int speed);
176 
183  void setMotorDirection(DCMOTORS_T motor, DIRECTION_T dir);
184 
192 
199  void stepConfig(STEPMOTORS_T motor, unsigned int stepsPerRev);
200 
207  void stepperSteps(STEPMOTORS_T motor, unsigned int steps);
208 
209  private:
210  // SWIG will generate warning for these 'nested structs', however
211  // those can be ignored as these structs are never exposed.
212 
213  // struct to hold mappings of the dc motors
214  typedef struct {
215  int pwm;
216  int in1;
217  int in2;
218  } DC_PINMAP_T;
219 
220  // struct to hold mappings of the stepper motors
221  typedef struct {
222  int pwmA;
223  int in1A;
224  int in2A;
225  int pwmB;
226  int in1B;
227  int in2B;
228  } STEPPER_PINMAP_T;
229 
230  // struct to hold some information about each stepper
231  typedef struct {
232  int stepsPerRev; // steps per revolution
233  int currentStep; // current step number
234  uint32_t stepDelay; // delay between steps
235  int stepDirection; // direction to step
236  struct timeval startTime; // starting time
237  } STEPPER_CONFIG_T;
238 
239  void setupPinMaps();
240  void stepperStep(STEPMOTORS_T motor);
241 
242  DC_PINMAP_T m_dcMotors[4];
243  STEPPER_PINMAP_T m_stepMotors[2];
244  STEPPER_CONFIG_T m_stepConfig[2];
245 
246  PCA9685 *m_pca9685;
247  };
248 }
249 
250 
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:68
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:74
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:93
DCMOTORS_T
Definition: adafruitms1438.h:83