-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathContinuous2Wheels.cpp
executable file
·328 lines (297 loc) · 9.94 KB
/
Continuous2Wheels.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
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
/*
UNIVERSIDADE ESTADUAL DO PIAUÍ - UESPI
Bac. em Ciências da Computação - CTU
AJ Alves.
zerokol.com
*/
/********************************************************************
* INCLUDES
********************************************************************/
#include "Continuous2Wheels.h"
/*
speed* must be higher or equal to 0 and lower or equal to 90
distance** is a value in centimeter
degree*** is a value positive or negative different of 0, the sigh determines the direction
degree**** is a value higher then 0
smooth***** percentage value
Ps:. Every time you use one of the motion methods the value of _speed and _direction are dynamically changed
*/
/********************************************************************
* IMPLEMENTATION OF PUBLIC METHODS
********************************************************************/
Continuous2Wheels::Continuous2Wheels(Servo* rightWhreel, Servo* leftWhreel,
double wheellRadius, double bendRadius) {
//attaching referred pins to associative wheels
_rightWheel = rightWhreel;
_leftWheel = leftWhreel;
//Setting the initial values
_wheelRadius = wheellRadius;
_bendRadius = bendRadius;
_rightWheel->write(STOPED);
_leftWheel->write(STOPED);
_speed = STOPED;
_direction = IDLE;
//Setting the resistance value
_resistence = RESISTENCE_DEFAULT;
//Setting the bend smooth
_bendSmooth = BEND_SMOOTH_DEFAULT;
}
Continuous2Wheels::Continuous2Wheels(Servo* rightWhreel, Servo* leftWhreel,
double wheellRadius, double bendRadius, double resistence) {
//attaching referred pins to associative wheels
_rightWheel = rightWhreel;
_leftWheel = leftWhreel;
//Setting the initial values
_wheelRadius = wheellRadius;
_bendRadius = bendRadius;
_rightWheel->write(STOPED);
_leftWheel->write(STOPED);
_speed = STOPED;
_direction = IDLE;
//Setting the resistance value
_resistence = resistence;
//Setting the bend smooth
_bendSmooth = BEND_SMOOTH_DEFAULT;
}
//Method to stop the machine
void Continuous2Wheels::stop() {
_direction = IDLE;
_rightWheel->write(STOPED);
_leftWheel->write(STOPED);
_speed = STOPED;
}
//Method to move the machine forward in a certain speed*
void Continuous2Wheels::forward(int speed) {
speed = speedFormater(speed);
_direction = FORWARD;
_rightWheel->write(STOPED + speed);
_leftWheel->write(STOPED - speed);
_speed = speed;
}
//Method to move the machine forward in a certain speed* to a distance*
void Continuous2Wheels::forward(int speed, double distance) {
speed = speedFormater(speed);
//get the walk value of wheel displacement
double displacement = getWalkDisplacement(distance);
//get the among of time to wait to reach the displacement
unsigned long waitValue = getWaitValue(displacement, speed);
//start the motion
_rightWheel->write(STOPED + speed);
_leftWheel->write(STOPED - speed);
//wait until to reach the target
delay(waitValue);
//stop the machine
stop();
}
//Method to move the machine backward in a certain speed*
void Continuous2Wheels::backward(int speed) {
speed = speedFormater(speed);
_direction = BACKWARD;
_rightWheel->write(STOPED - speed);
_leftWheel->write(STOPED + speed);
_speed = speed;
}
//Method to move the machine backward in a certain speed* to a distance*
void Continuous2Wheels::backward(int speed, double distance) {
speed = speedFormater(speed);
//get the walk value of wheel displacement
double displacement = getWalkDisplacement(distance);
//get the among of time to wait to reach the displacement
unsigned long waitValue = getWaitValue(displacement, speed);
//start the motion
_rightWheel->write(STOPED - speed);
_leftWheel->write(STOPED + speed);
//wait until to reach the target
delay(waitValue);
//stop the machine
stop();
}
//Method to make a bend with the passed degree*** value, the degree sigh will determine the bend side
/*
Bends does not stop the machine, it gets the current speed value and make a appropriate bend
*/
void Continuous2Wheels::bend(int degree) {
if (degree > 0) {
rigthBend(degree);
} else if (degree < 0) {
degree *= -1;
leftBend(degree);
}
}
//Method to make a right bend with the passed degree**** value
/*
Bends does not stop the machine, it gets the current speed value and make a appropriate bend
*/
void Continuous2Wheels::rigthBend(int degree) {
if (degree < 0) {
degree = 0;
}
//Get the raidians value from the degree
double radiansVal = degreeToRadian(degree);
//get the walk value of wheel displacement
double displacement = getCircleDisplacement(radiansVal, _bendRadius);
//get the among of time to wait to reach the displacement
unsigned long waitValue = getWaitValue(displacement, _speed);
//adjust the right wheel for a bend
_rightWheel->write(STOPED + (_speed * _bendSmooth));
//wait until to reach the target
delay(waitValue);
//restart the motion
_rightWheel->write(getWheelSpeed(_speed, RIGHTWHEEL));
}
//Method to make a left bend with the passed degree**** value
/*
Bends does not stop the machine, it gets the current speed value and make a appropriate bend
*/
void Continuous2Wheels::leftBend(int degree) {
if (degree < 0) {
degree = 0;
}
//Get the raidians value from the degree
double radiansVal = degreeToRadian(degree);
//get the walk value of wheel displacement
double displacement = getCircleDisplacement(radiansVal, _bendRadius);
//get the among of time to wait to reach the displacement
unsigned long waitValue = getWaitValue(displacement, _speed);
//adjust the left wheel for a bend
_leftWheel->write(STOPED - (_speed * _bendSmooth));
//wait until to reach the target
delay(waitValue);
//restart the motion
_leftWheel->write(getWheelSpeed(_speed, LEFTWHEEL));
}
//Method to spin the machine in its own axis, the sign of param will determine the spin direction
void Continuous2Wheels::spin(int degree) {
//Get the raidians value from the degree
double radiansVal = degreeToRadian(degree);
//get the walk value of wheel displacement now using the bend radius, it will make the machine spin in itself
double displacement = getCircleDisplacement(radiansVal, (_bendRadius / 2));
//by defaul the spin is made with a half maximum speed
int speed = STOPED / 2;
//get the among of time to wait to reach the displacement
unsigned long waitValue = getWaitValue(displacement, speed);
//from the sign of passed param choice the right direction
if (degree < 0) {
_rightWheel->write(speed);
_leftWheel->write(speed * -1);
} else {
_rightWheel->write(speed * -1);
_leftWheel->write(speed);
}
//wait until to reach the target
delay(waitValue);
//STOP THE MACHINE - FOR THE TIME BEING
stop();
}
//Method to spin the machine in its own axis, the sign of param will determine the spin direction and in a certain speed*
void Continuous2Wheels::spin(int degree, int speed) {
speed = speedFormater(speed);
//Get the raidians value from the degree
double radiansVal = degreeToRadian(degree);
//get the walk value of wheel displacement now using the bend radius, it will make the machine spin in itself
double displacement = getCircleDisplacement(radiansVal, (_bendRadius / 2));
//get the among of time to wait to reach the displacement
unsigned long waitValue = getWaitValue(displacement, speed);
//from the sign of passed param choice the right direction
if (degree < 0) {
_rightWheel->write(speed);
_leftWheel->write(speed * -1);
} else {
_rightWheel->write(speed * -1);
_leftWheel->write(speed);
}
//wait until to reach the target
delay(waitValue);
//STOP THE MACHINE - FOR THE TIME BEING
stop();
}
//Method to get the speed value
int Continuous2Wheels::getSpeed() {
return _speed;
}
//Method to get direction value
int Continuous2Wheels::getDirection() {
return _direction;
}
//Method to get the bend smooth value
float Continuous2Wheels::getBendSmooth() {
return _bendSmooth;
}
//Method to set the bend smooth***** value
void Continuous2Wheels::setBendSmooth(float smooth) {
if (smooth < 0.0) {
smooth = 0.0;
} else if (smooth > 1.0) {
smooth = 1.0;
}
_bendSmooth = smooth;
}
//Method to get the baud value of serial port communication
long Continuous2Wheels::getSerialPortBaud() {
return _serialPortBaud;
}
//Method to set the baud value of serial port communication
void Continuous2Wheels::setSerialPortBaud(long baud) {
_serialPortBaud = baud;
Serial.end();
Serial.begin(_serialPortBaud);
}
/********************************************************************
* IMPLEMENTATION OF PRIVATE METHODS
********************************************************************/
//Private method used to calculate the speed value to a specific wheel
int Continuous2Wheels::getWheelSpeed(int speed, int wheel) {
switch (_direction) {
default:
case IDLE:
return STOPED;
break;
case FORWARD:
if (wheel == RIGHTWHEEL) {
return STOPED + speed;
} else {
return STOPED - speed;
}
break;
case BACKWARD:
if (wheel == RIGHTWHEEL) {
return STOPED - speed;
} else {
return STOPED + speed;
}
break;
}
}
//Method to convert degrees to radians
double Continuous2Wheels::degreeToRadian(int degree) {
return (RAD * degree);
}
//Method to calculate the circle "for wheels" displacement
double Continuous2Wheels::getCircleDisplacement(double radiansV, double radius) {
return (radiansV * radius);
}
//Method to calculate the machine distance displacement
double Continuous2Wheels::getWalkDisplacement(double distance) {
unsigned int degree = (360 * distance) / circleLength(_wheelRadius);
double radiansV = degreeToRadian(degree);
return getCircleDisplacement(radiansV, _wheelRadius);
}
//Method to calculate the length for an circle
double Continuous2Wheels::circleLength(double radius) {
return 2 * PI * radius;
}
//Method to calculate among of time to wait until the displacement is walked
unsigned long Continuous2Wheels::getWaitValue(double displacement, int speed) {
return ((((displacement / speed) * MILI) * _resistence) * (1 + _bendSmooth));
}
//Method to format the speed to a valid speed value
int Continuous2Wheels::speedFormater(int speed) {
if (speed < 0) {
speed = 0;
} else if (speed > 90) {
speed = 90;
}
return speed;
}