upm  0.2.0
Sensor/Actuator repository for libmraa (v0.6.1)
l298.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 <mraa/gpio.h>
30 #include <mraa/pwm.h>
31 
32 // in milliseconds
33 #define L298_DEFAULT_PWM_PERIOD 4
34 
35 namespace upm {
63  class L298 {
64  public:
65 
69  typedef enum {
70  DIR_NONE = 0x00,
71  DIR_CW = 0x01,
72  DIR_CCW = 0x02
74 
82  L298(int pwm, int dir1, int dir2);
83 
94  L298(int stepsPerRev, int en, int i1, int i2, int i3, int i4);
95 
99  ~L298();
100 
107  uint32_t getMillis();
108 
113  void initClock();
114 
120  void setPeriodMS(int ms);
121 
127  void enable(bool enable);
128 
136  void setSpeed(int speed);
137 
143  void setDirection(L298_DIRECTION_T dir);
144 
150  void stepperSteps(unsigned int steps);
151 
152  private:
153  // DC motor mode enabled
154  bool m_motor;
155  // stepper mode enabled
156  bool m_stepper;
157 
158  struct timeval m_startTime;
159 
160  // DC motor
161  mraa_pwm_context m_pwm;
162  mraa_gpio_context m_dir1;
163  mraa_gpio_context m_dir2;
164 
165  // stepper (4-wire)
166  mraa_gpio_context m_stepEnable;
167  mraa_gpio_context m_stepI1;
168  mraa_gpio_context m_stepI2;
169  mraa_gpio_context m_stepI3;
170  mraa_gpio_context m_stepI4;
171 
172  // steps per revolution
173  int m_stepsPerRev;
174  int m_currentStep;
175  uint32_t m_stepDelay;
176 
181  void stepperStep();
182 
183  // step direction - 1 = forward, -1 = backward
184  int m_stepDirection;
185  };
186 }
187 
188 
uint32_t getMillis()
Definition: l298.cxx:136
void setPeriodMS(int ms)
Definition: l298.cxx:188
Definition: a110x.h:29
L298_DIRECTION_T
Definition: l298.h:69
~L298()
Definition: l298.cxx:165
void enable(bool enable)
Definition: l298.cxx:198
C++ API for the L298 Dual H-Bridge module.
Definition: l298.h:63
L298(int pwm, int dir1, int dir2)
Definition: l298.cxx:33
void stepperSteps(unsigned int steps)
Definition: l298.cxx:308
void setSpeed(int speed)
Definition: l298.cxx:211
void setDirection(L298_DIRECTION_T dir)
Definition: l298.cxx:235
void initClock()
Definition: l298.cxx:131