summaryrefslogtreecommitdiff
path: root/peripheral/libupm/src/l298/l298.cxx
blob: 00c08ec2533d49d25222e95f853c386f0ac912dd (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
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
/*
 * 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.
 */

#include <iostream>
#include <string>
#include <stdexcept>

#include "l298.h"

using namespace upm;
using namespace std;

// constructor for the DC motor(s) mode
L298::L298(int pwmA, int dir1, int dir2)
{
  // No stepper in this mode
  m_stepper = false;

  // disable until complete
  m_motor = false;

  if ( !(m_pwm = mraa_pwm_init(pwmA)) )
    {
      throw std::invalid_argument(std::string(__FUNCTION__) +
                                  ": mraa_pwm_init() failed, invalid pin?");
      return;
    }

  if ( !(m_dir1 = mraa_gpio_init(dir1)) )
    {
      throw std::invalid_argument(std::string(__FUNCTION__) +
                                  ": mraa_gpio_init(dir1) failed, invalid pin?");
      mraa_pwm_close(m_pwm);
      return;
    }
  mraa_gpio_dir(m_dir1, MRAA_GPIO_OUT);

  if ( !(m_dir2 = mraa_gpio_init(dir2)) )
    {
      throw std::invalid_argument(std::string(__FUNCTION__) +
                                  ": mraa_gpio_init(dir2) failed, invalid pin?");
      mraa_pwm_close(m_pwm);
      mraa_gpio_close(m_dir1);
      return;
    }
  mraa_gpio_dir(m_dir2, MRAA_GPIO_OUT);

  setPeriodMS(L298_DEFAULT_PWM_PERIOD);
  setDirection(DIR_NONE);
  setSpeed(0);
  m_motor = true;
}

// constructor for the stepper mode
L298::L298(int stepsPerRev, int en, int i1, int i2, int i3, int i4)
{
  // no DC motors in this mode
  m_motor = false;

  // disable until complete
  m_stepper = false;

  m_stepsPerRev = stepsPerRev;
  m_currentStep = 0;
  m_stepDelay = 0;
  m_stepDirection = 1;          // default is forward

  // init the gpio's we will need
  if ( !(m_stepEnable = mraa_gpio_init(en)) )
    {
      throw std::invalid_argument(std::string(__FUNCTION__) +
                                  ": mraa_gpio_init(en) failed, invalid pin?");
      return;
    }
  mraa_gpio_dir(m_stepEnable, MRAA_GPIO_OUT);

  if ( !(m_stepI1 = mraa_gpio_init(i1)) )
    {
      throw std::invalid_argument(std::string(__FUNCTION__) +
                                  ": mraa_gpio_init(i1) failed, invalid pin?");
      return;
    }
  mraa_gpio_dir(m_stepI1, MRAA_GPIO_OUT);

  if ( !(m_stepI2 = mraa_gpio_init(i2)) )
    {
      throw std::invalid_argument(std::string(__FUNCTION__) +
                                  ": mraa_gpio_init(i2) failed, invalid pin?");
      mraa_gpio_close(m_stepI1);
      return;
    }
  mraa_gpio_dir(m_stepI2, MRAA_GPIO_OUT);

  if ( !(m_stepI3 = mraa_gpio_init(i3)) )
    {
      throw std::invalid_argument(std::string(__FUNCTION__) +
                                  ": mraa_gpio_init(i3) failed, invalid pin?");
      mraa_gpio_close(m_stepI1);
      mraa_gpio_close(m_stepI2);
      return;
    }
  mraa_gpio_dir(m_stepI3, MRAA_GPIO_OUT);

  if ( !(m_stepI4 = mraa_gpio_init(i4)) )
    {
      throw std::invalid_argument(std::string(__FUNCTION__) +
                                  ": mraa_gpio_init(i4) failed, invalid pin?");
      mraa_gpio_close(m_stepI1);
      mraa_gpio_close(m_stepI2);
      mraa_gpio_close(m_stepI3);
      
      return;
    }
  mraa_gpio_dir(m_stepI4, MRAA_GPIO_OUT);

  m_stepper = true;
}


void L298::initClock()
{
  gettimeofday(&m_startTime, NULL);
}

uint32_t L298::getMillis()
{
  struct timeval elapsed, now;
  uint32_t elapse;

  // get current time
  gettimeofday(&now, NULL);

  // compute the delta since m_startTime
  if( (elapsed.tv_usec = now.tv_usec - m_startTime.tv_usec) < 0 )
    {
      elapsed.tv_usec += 1000000;
      elapsed.tv_sec = now.tv_sec - m_startTime.tv_sec - 1;
    }
  else
    {
      elapsed.tv_sec = now.tv_sec - m_startTime.tv_sec;
    }

  elapse = (uint32_t)((elapsed.tv_sec * 1000) + (elapsed.tv_usec / 1000));

  // never return 0
  if (elapse == 0)
    elapse = 1;

  return elapse;
}


L298::~L298()
{
  if (m_stepper)
    {
      enable(false);
      mraa_gpio_close(m_stepEnable);
      mraa_gpio_close(m_stepI1);
      mraa_gpio_close(m_stepI2);
      mraa_gpio_close(m_stepI3);
      mraa_gpio_close(m_stepI4);
    }

  if (m_motor)
    {
      setDirection(DIR_NONE);
      setSpeed(0);
      enable(false);
      mraa_pwm_close(m_pwm);
      mraa_gpio_close(m_dir1);
      mraa_gpio_close(m_dir2);
    }
}

void L298::setPeriodMS(int ms)
{
  if (m_motor)
    {
      if (mraa_pwm_period_ms(m_pwm, ms) != MRAA_SUCCESS)
        throw std::invalid_argument(std::string(__FUNCTION__) +
                                    ": mraa_pwm_period_ms() failed");
    }
}

void L298::enable(bool enable)
{
  if (m_motor)
    {
      mraa_pwm_enable(m_pwm, ((enable) ? 1 : 0));
    }

  if (m_stepper)
    {
      mraa_gpio_write(m_stepEnable, ((enable) ? 1 : 0));
    }
}

void L298::setSpeed(int speed)
{
  if (m_motor)
    {
      if (speed < 0)
        speed = 0;

      if (speed > 100)
        speed = 100;

      float percent = float(speed) / 100.0;

      if (m_motor)
        {
          mraa_pwm_write(m_pwm, percent);
        }
    }

  if (m_stepper)
    {
      m_stepDelay = 60 * 1000 / m_stepsPerRev / speed;
    }
}

void L298::setDirection(L298_DIRECTION_T dir)
{
  if (m_motor)
    {
      if (dir & 0x01)
        mraa_gpio_write(m_dir1, 1);
      else
        mraa_gpio_write(m_dir1, 0);

      if (dir & 0x02)
        mraa_gpio_write(m_dir2, 1);
      else
        mraa_gpio_write(m_dir2, 0);
    }

  if (m_stepper)
    {
      switch (dir)
        {
        case DIR_CW:
          m_stepDirection = 1;
          break;
        case DIR_CCW:
          m_stepDirection = -1;
          break;
        default:                // default to 1 if DIR_NONE specified
          m_stepDirection = 1;
          break;
        }
    }


}

void L298::stepperStep()
{
  int step = m_currentStep % 4;

  //   Step I0 I1 I2 I3
  //     1  1  0  1  0
  //     2  0  1  1  0
  //     3  0  1  0  1
  //     4  1  0  0  1

  switch (step)
    {
    case 0:    // 1010
      mraa_gpio_write(m_stepI1, 1);
      mraa_gpio_write(m_stepI2, 0);
      mraa_gpio_write(m_stepI3, 1);
      mraa_gpio_write(m_stepI4, 0);
      break;
    case 1:    // 0110
      mraa_gpio_write(m_stepI1, 0);
      mraa_gpio_write(m_stepI2, 1);
      mraa_gpio_write(m_stepI3, 1);
      mraa_gpio_write(m_stepI4, 0);
      break;
    case 2:    //0101
      mraa_gpio_write(m_stepI1, 0);
      mraa_gpio_write(m_stepI2, 1);
      mraa_gpio_write(m_stepI3, 0);
      mraa_gpio_write(m_stepI4, 1);
      break;
    case 3:    //1001
      mraa_gpio_write(m_stepI1, 1);
      mraa_gpio_write(m_stepI2, 0);
      mraa_gpio_write(m_stepI3, 0);
      mraa_gpio_write(m_stepI4, 1);
      break;
    }
}

void L298::stepperSteps(unsigned int steps)
{
  while (steps > 0)
    {
      if (getMillis() >= m_stepDelay)
        {
          // reset the clock
          initClock();

          m_currentStep += m_stepDirection;

          if (m_stepDirection == 1)
            {
              if (m_currentStep >= m_stepsPerRev)
                m_currentStep = 0;
            }
          else
            {
              if (m_currentStep <= 0)
                m_currentStep = m_stepsPerRev;
            }

          steps--;
          stepperStep();
          // cerr << "STEPNUM: " << m_currentStep << endl;
        }
    }
}