summaryrefslogtreecommitdiff
path: root/peripheral/libupm/src/adafruitms1438/adafruitms1438.h
blob: 1f00ac93f69c836211738fddfa32a1e233a06ec5 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
/*
 * Author: Jon Trulson <jtrulson@ics.com>
 * Copyright (c) 2015 Intel Corporation.
 *
 * Permission is hereby granted, free of charge, to any person obtaining
 * a copy of this software and associated documentation files (the
 * "Software"), to deal in the Software without restriction, including
 * without limitation the rights to use, copy, modify, merge, publish,
 * distribute, sublicense, and/or sell copies of the Software, and to
 * permit persons to whom the Software is furnished to do so, subject to
 * the following conditions:
 *
 * The above copyright notice and this permission notice shall be
 * included in all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
 * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
 * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
 * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
 */
#pragma once

#include <stdint.h>
#include <sys/time.h>

#include <string>
#include <mraa/i2c.h>
#include <mraa/gpio.h>

#include "pca9685.h"

#define ADAFRUITMS1438_I2C_BUS 0
#define ADAFRUITMS1438_DEFAULT_I2C_ADDR 0x60

namespace upm {
  
  /**
   * @brief Adafruit Motor Shield 1438 library
   * @defgroup adafruitms1438 libupm-adafruitms1438
   * @ingroup adafruit i2c motor
   */

  /**
   * @library adafruitms1438
   * @sensor adafruitms1438
   * @comname Adafruit Motor Shield
   * @type motor
   * @man adafruit
   * @web http://www.adafruit.com/products/1438
   * @con i2c
   *
   * @brief API for the AdafruitMS1438 Motor Shield
   *
   * This class implements support for the stepper and DC motors that
   * can be connected to this Motor Shield.
   * Note: the two servo connections are not controlled by the PCA9685
   * controller (or this class). They are connected directly to digital
   * PWM pins 9 and 10 on the Arduino* breakout board.
   *
   * @image html adafruitms1438.jpg
   * An example using a DC motor connected to M3
   * @snippet adafruitms1438.cxx Interesting
   * An example using a stepper motor connected to M1 & M2
   * @snippet adafruitms1438-stepper.cxx Interesting
   */
  class AdafruitMS1438 {
  public:

    /**
     * Enum to specify the direction of a motor
     */
    typedef enum {
      DIR_NONE = 0x00,
      DIR_CW   = 0x01,
      DIR_CCW  = 0x02
    } DIRECTION_T;

    /**
     * Enum to specify a DC motor
     */
    typedef enum {
      MOTOR_M1 = 0,
      MOTOR_M2 = 1,
      MOTOR_M3 = 2,
      MOTOR_M4 = 3
    } DCMOTORS_T;

    /**
     * Enum to specify a stepper motor
     */
    typedef enum {
      STEPMOTOR_M12 = 0,
      STEPMOTOR_M34 = 1
    } STEPMOTORS_T;

    /**
     * AdafruitMS1438 constructor
     *
     * @param bus I2C bus to use
     * @param address Address for this sensor
     */
    AdafruitMS1438(int bus, uint8_t address = ADAFRUITMS1438_DEFAULT_I2C_ADDR);

    /**
     * AdafruitMS1438 destructor
     */
    ~AdafruitMS1438();

    /**
     * Returns the number of milliseconds elapsed since initClock(...)
     * was last called.
     *
     * @return Elapsed milliseconds
     */
    uint32_t getMillis(STEPMOTORS_T motor);

    /**
     * Resets the clock
     *
     */
    void initClock(STEPMOTORS_T motor);

    /**
     * Sets the PWM period.  Note: this applies to all PWM channels.
     *
     * @param hz Sets the PWM period 
     */
    void setPWMPeriod(float hz);

    /**
     * Enables PWM output for a motor
     *
     * @param motor DC motor to enable
     */
    void enableMotor(DCMOTORS_T motor);

    /**
     * Disables PWM output for a motor
     *
     * @param motor DC motor to disable
     */
    void disableMotor(DCMOTORS_T motor);

    /**
     * Enables output for a stepper motor
     *
     * @param motor Stepper motor to enable
     */
    void enableStepper(STEPMOTORS_T motor);

    /**
     * Disable output for a stepper motor
     *
     * @param motor Stepper motor to disable
     */
    void disableStepper(STEPMOTORS_T motor);

    /**
     * set the speed of a DC motor.  Values can range from 0 (off) to
     * 100 (full speed).
     *
     * @param motor the DC motor to configure
     * @param speed speed to set the motor to
     */
    void setMotorSpeed(DCMOTORS_T motor, int speed);

    /**
     * Sets the speed of a stepper in revolutions per minute (RPM)
     *
     * @param motor DC motor to configure
     * @param speed Speed to set the motor to
     */
    void setStepperSpeed(STEPMOTORS_T motor, int speed);

    /**
     * Sets the direction of a DC motor, clockwise or counterclockwise
     *
     * @param motor DC motor to configure
     * @param dir Direction to set the motor in
     */
    void setMotorDirection(DCMOTORS_T motor, DIRECTION_T dir);
 
    /**
     * Sets the direction of a stepper motor, clockwise or counterclockwise
     *
     * @param motor Stepper motor to configure
     * @param dir Direction to set the motor in
     */
    void setStepperDirection(STEPMOTORS_T motor, DIRECTION_T dir);
 
    /**
     * Sets a stepper motor configuration
     *
     * @param motor Stepper motor to configure
     * @param stepsPerRev Number of steps to complete a full revolution
     */
    void stepConfig(STEPMOTORS_T motor, unsigned int stepsPerRev);

    /**
     * Steps a stepper motor a specified number of steps
     *
     * @param motor Stepper motor to step
     * @param steps Number of steps to move the stepper motor
     */
    void stepperSteps(STEPMOTORS_T motor, unsigned int steps);

  private:
    // SWIG will generate a warning for these 'nested structs'; however,
    // it can be ignored as these structs are never exposed.

    // struct to hold mappings of DC motors
    typedef struct {
      int pwm;
      int in1;
      int in2;
    } DC_PINMAP_T;

    // struct to hold mappings of stepper motors
    typedef struct {
      int pwmA;
      int in1A;
      int in2A;
      int pwmB;
      int in1B;
      int in2B;
    } STEPPER_PINMAP_T;

    // struct to hold information about each stepper
    typedef struct {
      int stepsPerRev;          // steps per revolution
      int currentStep;          // current step number
      uint32_t stepDelay;       // delay between steps
      int stepDirection;        // direction to step
      struct timeval startTime; // starting time
    } STEPPER_CONFIG_T;

    void setupPinMaps();
    void stepperStep(STEPMOTORS_T motor);

    DC_PINMAP_T m_dcMotors[4];
    STEPPER_PINMAP_T m_stepMotors[2];
    STEPPER_CONFIG_T m_stepConfig[2];

    PCA9685 *m_pca9685;
  };
}