This repository has been archived by the owner on Dec 18, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
MegaMotoHB.cpp
273 lines (247 loc) · 7.04 KB
/
MegaMotoHB.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
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
/********************************************//**
* \file MegaMotoHB.cpp
* \author Alexander Hogen
* \date 1/11/2017
* \version 0.1
* \brief Contains the definitions for all the
* member methods of the MegaMotoHB class.
*
*
* Released into the public domain.
*
***********************************************/
#include "MegaMotoHB.h"
/********************************************//**
* \brief Constructor to initialize with pin
* numbers for PWM A and PWM B and
* specify the motor wiring.
*
* Initializes the data members and sets up the
* Arduino pins appropariately to be able to talk
* to the MegaMoto properly.
*
* \param pin_pwm_a is a byte. Specify the pin
* used for MegaMoto PWM A here.
*
* \param pin_pwm_b is a byte. Specify the pin
* used for MegaMoto PWM B here.
*
***********************************************/
MegaMotoHB::MegaMotoHB(
unsigned char pin_pwm_a,
unsigned char pin_pwm_b):
MegaMotoBase(pin_pwm_a, pin_pwm_b),
pwm_duty(0),
current_dir(TMegaMotoHBDir::STOP)
{}
/********************************************//**
* \brief Constructor to initialize with pin
* numbers for PWM A, PWM B, and Enable.
*
* Initializes the data members and sets up the
* Arduino pins appropariately to be able to talk
* to the MegaMoto properly.
*
* \param pin_pwm_a is a byte. Specify the pin
* used for MegaMoto PWM A here.
*
* \param pin_pwm_b is a byte. Specify the pin
* used for MegaMoto PWM B here.
*
* \param pin_enable is a byte. Specify the pin
* used for the MegaMoto Enable pin here.
*
***********************************************/
MegaMotoHB::MegaMotoHB(
char pin_pwm_a,
char pin_pwm_b,
char pin_enable):
MegaMotoBase(pin_pwm_a, pin_pwm_b, pin_enable),
pwm_duty(0),
current_dir(TMegaMotoHBDir::STOP)
{}
/********************************************//**
* \brief Change the power output, either speed
* (power strength) or direction (polarity)
*
* Use this method to change the current output
* state of the MegaMoto. If the device being
* controlled is a motor, then this adjusts the
* direction (polarity) and speed (PWM duty cycle).
* However, for a light or some other device, you
* might call it changing the voltage direction
* and power/brightness.
*
* Calls StepPwmDuty() to gradually adjust the
* old PWM duty cycle to the new PWM duty cycle
* (i.e. If going from TMegaMotoHBDir::FWD to
* TMegaMotoHBDir::REV, this method will slow down
* in the FWD direction, and then speed up in the
* REV direction.)
*
* \param dir is the new motor direction, defined
* by TMegaMotoHBDir states.
*
* \param new_pwm_duty is the new PWM duty cycle or
* power output in the aforementioned
* direction. Valid values are 0 - 255, so
* it is a byte datatype.
*
***********************************************/
void MegaMotoHB::Power(const TMegaMotoHBDir dir, const unsigned char new_pwm_duty)
{
if ( dir == current_dir )
{
StepPwmDuty(new_pwm_duty);
}
else if ( dir == TMegaMotoHBDir::STOP )
{
// Slow to a stop
StepPwmDuty( 0 );
// Change current direction to the STOP case
current_dir = TMegaMotoHBDir::STOP;
// Turn off both A and B sides
analogWrite(pin_a, 0);
analogWrite(pin_b, 0);
}
// We are going to be changing directions (i.e. forward -> reverse)
else
{
// Slow down previous direction
StepPwmDuty( 0 );
// Change directions
current_dir = dir;
// Speed up in new direction
StepPwmDuty( new_pwm_duty );
}
}
/********************************************//**
* \brief Gradually change the PWM output from
* what it was to the new desired PWM
* duty cycle.
*
* Since motors and many other physical things
* don't like sudden changes, it is a good idea
* to fade between different speeds/power levels.
* See the definition of MEGA_MOTO_FEATHER_STEP
* for the actual fade speed.
*
* Called by the Power() method.
*
* \param pwm_duty_in is the new desired duty
* cycle. Acceptable values range from
* 0 - 255.
*
***********************************************/
void MegaMotoHB::StepPwmDuty(unsigned char pwm_duty_in)
{
bool increment = (pwm_duty_in > pwm_duty ? true : false);
if ( increment )
{
// Loop until current PWM matches the larger, target PWM
for ( ; pwm_duty < pwm_duty_in; pwm_duty++)
{
// Call the correct direction function
if ( current_dir == TMegaMotoHBDir::FWD )
{
// FORWARD
analogWrite(pin_a, pwm_duty);
//analogWrite(pin_b, 0);
}
else if ( current_dir == TMegaMotoHBDir::REV )
{
// REVERSE
//analogWrite(pin_a, 0);
analogWrite(pin_b, pwm_duty);
}
else
{
// Must have been an error.
// Set both current and desired PWMs to 0
// so that the for() loop exits.
pwm_duty = pwm_duty_in = 0;
}
// Wait for _ time before reducing another step
delay( pwm_step_delay_ms );
}
}
// We need to decrement to reach the new duty cycle
else
{
// Loop until current PWM matches the reduced, target PWM
for ( ; pwm_duty > pwm_duty_in; pwm_duty--)
{
// Call the correct direction function
if ( current_dir == TMegaMotoHBDir::FWD )
{
// FORWARD
analogWrite(pin_a, pwm_duty);
analogWrite(pin_b, 0);
}
else if ( current_dir == TMegaMotoHBDir::REV )
{
// REVERSE
analogWrite(pin_a, 0);
analogWrite(pin_b, pwm_duty);
}
else
{
// Must have been an error.
// Set both current and desired PWMs to 0
// so that the for() loop exits.
pwm_duty = pwm_duty_in = 0;
}
// Wait for _ time before reducing another step
delay( pwm_step_delay_ms );
}
}
}
/********************************************//**
* \brief "Gracefully" power down the motor/device
* and then (if applicable) disable the
* MegaMoto.
*
* If the Enable pin IS being used, you will need
* to call Enable() to resume operation after
* using Disable().
*
*
***********************************************/
void MegaMotoHB::Disable()
{
// Turn off the output device(s)
Power(STOP, 0);
// Disable the MegaMoto
if (use_enable_pin)
{
digitalWrite(pin_en, LOW);
}
}
/********************************************//**
* \brief Immediatly turn off the output
*
* Might be good to use for some safety-critical
* situation where slowing down the motor/device
* before turning it off is a *bad* idea.
*
***********************************************/
void MegaMotoHB::Kill()
{
// Enable the MegaMoto if it was off previously
if (use_enable_pin)
{
digitalWrite(pin_en, HIGH);
}
pwm_duty = 0;
// Turn off both PWM outputs
analogWrite(pin_a, pwm_duty);
analogWrite(pin_b, pwm_duty);
// Change current direction to the STOP case
current_dir = TMegaMotoHBDir::STOP;
// Don't disable the MegaMoto. Holding the output
// might allow some motors with internal brakes
// to keep their brakes on. This is very useful
// in a situation where we needed to kill the
// motor very quickly. Disableing the MegaMoto
// and letting the motor "free-run" could be bad!
}