-
Notifications
You must be signed in to change notification settings - Fork 0
/
ClassBot.cpp
363 lines (278 loc) · 7.53 KB
/
ClassBot.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
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
#include "ClassBot.h"
//
// initalize ClassBot
//
ClassBot::ClassBot(int l1, int l2, int l3, int l4, int r1, int r2, int r3, int r4, int servoPin)
{
leftMotor = new AccelStepper(AccelStepper::HALF4WIRE, l1, l2, l3, l4, true);
rightMotor = new AccelStepper(AccelStepper::HALF4WIRE, r1, r2, r3, r4, true);
pinMode(servoPin, OUTPUT);
penServo.attach(servoPin);
penUp();
leftMotor->setMaxSpeed(MAX_SPEED);
rightMotor->setMaxSpeed(MAX_SPEED);
motors.addStepper(*leftMotor);
motors.addStepper(*rightMotor);
}
//
// Set pen down
//
void ClassBot::penDown()
{
penServo.write(PEN_DOWN_ANGLE);
penIsUp = false;
delay(200);
}
//
// Pick pen up
//
void ClassBot::penUp()
{
penServo.write(PEN_UP_ANGLE);
penIsUp = true;
delay(200);
}
//
// Internal fuction to set steps to move
// left or right stepper motor with simple
// backlash compensation.
//
void ClassBot::setStep(int which, float dist)
{
boolean pos = dist > 0;
steps[which] = dist;
if (lastPos[which] != -1)
{
if (pos & !lastPos[which])
steps[which] += backlashComp;
if (!pos & lastPos[which])
steps[which] -= backlashComp;
}
lastPos[which] = pos;
}
//
// Move ClassBot forward "distance" millimeters
//
void ClassBot::moveForward(float distance)
{
int s = int(distance * stepsDist);
setStep(LEFT, -s);
setStep(RIGHT, s);
motors.moveTo(steps);
motors.runSpeedToPosition();
leftMotor->setCurrentPosition(0);
rightMotor->setCurrentPosition(0);
}
//
// Move ClassBot backward "distance" millimeters
//
void ClassBot::moveBackward(float distance)
{
int s = int(distance * stepsDist);
setStep(LEFT, s);
setStep(RIGHT, -s);
motors.moveTo(steps);
motors.runSpeedToPosition();
leftMotor->setCurrentPosition(0);
rightMotor->setCurrentPosition(0);
}
//
// Turn ClassBot left degrees
//
void ClassBot::turnLeft(float degrees)
{
float distance = wheelBPI * (degrees / 360.0);
int s = int(distance * stepsDist);
setStep(LEFT, s);
setStep(RIGHT, s);
motors.moveTo(steps);
motors.runSpeedToPosition();
leftMotor->setCurrentPosition(0);
rightMotor->setCurrentPosition(0);
}
//
// Turn ClassBot right degrees
//
void ClassBot::turnRight(float degrees)
{
float distance = wheelBPI * (degrees / 360.0);
int s = int(distance * stepsDist);
setStep(LEFT, -s);
setStep(RIGHT, -s);
motors.moveTo(steps);
motors.runSpeedToPosition();
leftMotor->setCurrentPosition(0);
rightMotor->setCurrentPosition(0);
}
// Calculate the bearing (angle to turn to) from where we are (a1, a2)
// to where we want to go (b1, b2)
double ClassBot::bearing(double a1, double a2, double b1, double b2)
{
double theta = atan2(b1 - a1, a2 - b2);
if (theta < 0.0)
theta += TWOPI;
return theta * RAD2DEG;
}
//
// turn to given heading
//
void ClassBot::turnTo(float heading) {
long dh;
if (botH != heading) {
dh = botH - heading;
if (dh > 180) {
dh = dh - 360;
}
if (dh < -180) {
dh = dh + 360;
}
if (dh < 0) {
turnRight(abs(dh));
}
if (dh > 0) {
turnLeft(dh);
}
botH = heading;
}
}
// We know where we are and we want to go to x, y
// find the direction we need to turn to and then
// calculate the distance from our location to the
// destination using the Pythagorean theorem.
void ClassBot::moveTo(long x, long y, boolean penIsUp, float scale)
{
double b;
double r;
long dx, dy, dh;
penUp();
if ((x != botX) || (y != botY)) // if we are not already where we want to go
{
b = bearing(botX, botY, x, y); // find the direction to where we want to go
dx = x - botX; // calcucate how far over do we have to go
dy = y - botY; // calculate how far down/up we have to go
r = sqrt((dx * dx) + (dy * dy)); // find the distance using the Pythagorean theorem
dh = botH - b; // find the difference between the direction we are facing
// and the direction we need to face
if (dh > 180)
dh = dh - 360;
if (dh < -180)
dh = dh + 360;
if (dh < 0) // and turn right
turnRight(abs(dh));
if (dh > 0) // or turn left
turnLeft(dh);
if (!penIsUp) // if the pen is not supposed to be up, set it down
penDown();
moveForward(r * scale); // move forward the distance
botX = x; // keep track of our new position
botY = y;
botH = b; // and our new heading
}
}
//
// Draw a character starting at the current location
// sized 'scale'
//
void ClassBot::drawChar(char ch, float scale)
{
char *vectors;
byte c, length, width, i, pup = true;
int x = botX, y = botY, vX, vY, xofs, yofs, left, right;
if ((ch > 31) && (ch < 127))
{
c = ch - 32;
vectors = (char *) pgm_read_word(&font[c]); // Get the address of the data for this glyph
length = pgm_read_byte(vectors++); // First byte contains the number of vectors for this glyph
left = pgm_read_byte(vectors++) - 'R'; // Position of the left side of glyph
right = pgm_read_byte(vectors++) - 'R'; // Position of the right side of the glyph
width = right - left; // Calculate the character with
xofs = -left; // Zero out the left side to midline
yofs = 16 - 7;
while (length--)
{
vX = pgm_read_byte(vectors++); // X coordinate of point
vY = pgm_read_byte(vectors++); // Y coordinate of point
if ((vX == ' ') && (vY == 'R')) // if the value is " R"
{
pup = true; // pick the pen up
continue; // this is not a move
}
vX -= 'R';
vY -= 'R';
moveTo(vX + xofs + x, yofs + vY + y, pup, scale); // otherwise move to the X, Y coordinate
if (pup) // if the pen was up, set the flag to down
pup = false;
}
}
moveTo(x + width, y, true, scale); // And move to the position for the next character
}
//
// Write "str" character string starting at the current location
// sized 'scale'
void ClassBot::writeText(char *str, float scale)
{
char *s = str;
while (*s)
drawChar(*s++, scale);
}
//
// Done, pick pen up and turn off steppers
//
void ClassBot::done()
{
penUp();
for (int i = 2; i < 10; i++)
digitalWrite(i, LOW);
}
//
// Set the current X location
//
void ClassBot::setX(long newX)
{
botX = newX;
}
//
// Set the current Y location
//
void ClassBot::setY(long newY)
{
botY = newY;
}
//
// Set the current heading in degrees
//
void ClassBot::setH(float degrees)
{
botH = degrees;
}
//
// Returns true if the pen is up
//
boolean ClassBot::getPen()
{
return penIsUp;
}
//
// Returns the current X location
//
long ClassBot::getX()
{
return botX;
}
//
// Returns the current Y location
//
long ClassBot::getY()
{
return botY;
}
//
// Returns the current heading in degrees
//
float ClassBot::getH()
{
return botH;
}
//
// EOF
//