-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDCMotor.cpp
91 lines (74 loc) · 3.5 KB
/
DCMotor.cpp
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
/* DCMotor
A small DC motor control class.
IMPORTANT NOTICE: This library is based on code from the SNAPI library, which is under The Multiplo
Pacifist License (MPL). This license is (or may be) not compatible with the GNU Lesser General Public
License (LGPL), so this notice constitutes an special written exception from Multiplo, to allow deployment
under the LGPL, in order to be compiled with the Arduino libraries and still hold compatibility between
both licenses.
But this does not affects the SNAPI original license in any way, and DOES NOT AUTHORIZES ANY THIRD PARTY
TO USE OR REDISTRIBUTE THE SNAPI LIBRARY UNDER THE LGPL. In other words: This exception ONLY AFFECTS the
library when compiled with Arduino libraries, if and only if, this license (the MPL) results in some
inconpatibility with the LGPL (so in this last case, then LGPL applies):
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.
Any way, we strongly believe in peace, so we would not like to see our work in any non-civil or military
project. But, due to the possible incompatibilty with the LGPL licenses, this may be only a recommendation
and an expression of our wishes. But, if some clever guy can determine that LGPL and MPL are compatible
licenses, then MPL applies here. You can contact us at: info_t1@multiplo.com.ar
*/
#include "DCMotor.h"
const int maxPWMValue = 255;
void DCMotor::setSpeed(const float value)
{
float tempSpeed = constrain(value, dcMotMinSpeed, dcMotMaxSpeed);
prevSpeed = speed;
speed = tempSpeed;
if (!clockwise)
tempSpeed = (-tempSpeed);
tempSpeed = (tempSpeed/dcMotMaxSpeed)*maxPWMValue;
if (tempSpeed > zeroZone) //Margin for floating point: between -0.5 and 0.5 is take as zero.
{
//Forward:
digitalWrite(d0_pin, HIGH); //First sets the direction.
digitalWrite(d1_pin, LOW);
analogWrite(enable_pin, (int)tempSpeed); //Then sets the PWM's duty cycle.
}
else if (tempSpeed < -zeroZone)
{
//Reverse:
digitalWrite(d0_pin, LOW);
digitalWrite(d1_pin, HIGH);
analogWrite(enable_pin, (int)(-tempSpeed));
}
else //(tempSpeed == 0)
{
//Free-running:
analogWrite(enable_pin, 0); //To avoid a possible glitch generated by the PWM timer
//after the next instruccion.
digitalWrite(enable_pin, LOW); //This line would be enough, but the following ensures
digitalWrite(d0_pin, LOW); //the full state of the H-bridge, so if someone writes
digitalWrite(d1_pin, LOW); //a HIGH to the enable_pin, the state will be "Braked".
//And in some H-bridge chips (such as the TB6612FGN)
//this is necessary.
}
braked = false;
}
void DCMotor::brake()
{
digitalWrite(d0_pin, HIGH);
digitalWrite(d1_pin, HIGH);
digitalWrite(enable_pin, HIGH); //This stops the analogWrite (PWM) for the enable_pin.
braked = true;
}
void DCMotor::setClockwise(const bool value)
{
clockwise = value;
if (!braked)
setSpeed(speed);
}