upm  1.7.1
Sensor/Actuator repository for libmraa (v2.0.0)
l298.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 <mraa/gpio.h>
30 #include <mraa/pwm.h>
31 
32 // in milliseconds
33 #define L298_DEFAULT_PWM_PERIOD 4
34 
35 namespace upm {
69  class L298 {
70  public:
71 
75  typedef enum {
76  DIR_NONE = 0x00,
77  DIR_CW = 0x01,
78  DIR_CCW = 0x02
80 
88  L298(int pwm, int dir1, int dir2);
89 
100  L298(int stepsPerRev, int en, int i1, int i2, int i3, int i4);
101 
105  ~L298();
106 
113  uint32_t getMillis();
114 
119  void initClock();
120 
126  void setPeriodMS(int ms);
127 
133  void enable(bool enable);
134 
142  void setSpeed(int speed);
143 
149  void setDirection(L298_DIRECTION_T dir);
150 
156  void stepperSteps(unsigned int steps);
157 
158  private:
159  // DC motor mode enabled
160  bool m_motor;
161  // stepper mode enabled
162  bool m_stepper;
163 
164  struct timeval m_startTime;
165 
166  // DC motor
167  mraa_pwm_context m_pwm;
168  mraa_gpio_context m_dir1;
169  mraa_gpio_context m_dir2;
170 
171  // stepper (4-wire)
172  mraa_gpio_context m_stepEnable;
173  mraa_gpio_context m_stepI1;
174  mraa_gpio_context m_stepI2;
175  mraa_gpio_context m_stepI3;
176  mraa_gpio_context m_stepI4;
177 
178  // steps per revolution
179  int m_stepsPerRev;
180  int m_currentStep;
181  uint32_t m_stepDelay;
182 
187  void stepperStep();
188 
189  // step direction: - 1 = forward, -1 = backward
190  int m_stepDirection;
191  };
192 }
193 
194 
uint32_t getMillis()
Definition: l298.cxx:146
L298_DIRECTION_T
Definition: l298.hpp:75
void setPeriodMS(int ms)
Definition: l298.cxx:198
C++ API wrapper for the bh1749 driver.
Definition: a110x.hpp:29
~L298()
Definition: l298.cxx:175
void enable(bool enable)
Definition: l298.cxx:208
API for the L298 Dual H-Bridge Motor Driver.
Definition: l298.hpp:69
L298(int pwm, int dir1, int dir2)
Definition: l298.cxx:35
void stepperSteps(unsigned int steps)
Definition: l298.cxx:321
void setSpeed(int speed)
Definition: l298.cxx:221
void setDirection(L298_DIRECTION_T dir)
Definition: l298.cxx:248
void initClock()
Definition: l298.cxx:141