-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathBrownMaze2013.ino
349 lines (292 loc) · 6.73 KB
/
BrownMaze2013.ino
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
#include <FreeRam.h>
#include <PID.h>
#include <Encoder.h>
#include <Motor.h>
#include <Robot.h>
#include <Arduino.h>
#include <PinChangeInt.h>
#include <WallSensor.h>
#include <MazeNode.h>
#include <Maze.h>
#include <Brain.h>
/** Robot Pin Definitions **/
// Left Motor
#define PIN_LA 8
#define PIN_LB 7
#define PIN_LN 6
// Right Motor
#define PIN_RA 3
#define PIN_RB 4
#define PIN_RN 5
// Left Encoder
#define PIN_ELA 11
#define PIN_ELB 12
// Right Encoder
#define PIN_ERA 9
#define PIN_ERB 10
// IR Sensors
#define PIN_IRL A1
#define PIN_IRF A2
#define PIN_IRR A3
/** Robot Dimenion Definitions (cm or cm/s) **/
#define WHEEL_RADIUS 1.708278f
#define WHEEL_BASE 8.84//8.5f 8.84
#define MAX_SPEED 60.0f
#define COUNT_PER_REV 1200
#define TIMEOUT 10000
/** PID Constants **/
#define TANK_I_GAIN 0.14
#define POS_P_GAIN 0.03 // 0.023
#define ANGLE_P_GAIN 0.25 // 0.25
#define ANGLE_I_GAIN 0.00
#define ANGLE_D_GAIN 0.00
#define DANGLE_P_GAIN 0.0 // 0.15
#define DANGLE_D_GAIN 0.0 // 1.7
// length of a maze tile (18cm) 22.0
#define TILE_LENGTH 18.5f
// ms delay for waits
#define INSTRUCTION_DELAY 400
Robot* r;
WallSensor* ws;
Brain b;
void setup() {
delay(1000);
// stop switch pin
pinMode(13, INPUT_PULLUP);
ws = new WallSensor(PIN_IRF, PIN_IRR, PIN_IRL);
r = new Robot(
new Motor(PIN_LA, PIN_LB, PIN_LN, false),
new Motor(PIN_RA, PIN_RB, PIN_RN, false),
new Encoder(PIN_ELA, PIN_ELB, WHEEL_RADIUS, COUNT_PER_REV, TIMEOUT, true),
new Encoder(PIN_ERA, PIN_ERB, WHEEL_RADIUS, COUNT_PER_REV, TIMEOUT, false),
WHEEL_BASE,
MAX_SPEED
);
PCattachInterrupt(PIN_ELA, isrLA, CHANGE);
PCattachInterrupt(PIN_ELB, isrLB, CHANGE);
PCattachInterrupt(PIN_ERA, isrRA, CHANGE);
PCattachInterrupt(PIN_ERB, isrRB, CHANGE);
Serial.begin(9600);
r->begin(
new PID(0.0, TANK_I_GAIN, 0.0), // Left I Controller
new PID(0.0, TANK_I_GAIN, 0.0), // Right I Controller
new PID(POS_P_GAIN, 0.0, 0.0, 0.3), // Speed P Controller
new PID(DANGLE_P_GAIN, 0.0, DANGLE_D_GAIN), // Drive Angle PD Controller
new PID(ANGLE_P_GAIN, ANGLE_I_GAIN, ANGLE_D_GAIN) // Angle PID Controller
);
Serial.println("starting maze");
delay(1000);
doMaze(r, ws);
Serial.println("done maze");
//Serial.print("Free Ram = ");
//Serial.print(freeRam());
//Serial.println(" Bytes");
}
void loop() {
// stop switch
// maybe this can be built in to update()??
while (digitalRead(13)) {
r->reset();
delay(1000);
r->reset();
}
/*
r->changeSetX(2*TILE_LENGTH);
r->waitForNext(INSTRUCTION_DELAY);
r->changeSetAngle(PI);
r->waitForNext(INSTRUCTION_DELAY);
r->changeSetX(-2*TILE_LENGTH);
r->waitForNext(INSTRUCTION_DELAY);
r->changeSetAngle(PI);
r->waitForNext(INSTRUCTION_DELAY);
*/
/*
//--------------------------------------------
// drive in a maze and avoid walls
//--------------------------------------------
int direction = r->getDirection();
int* walls = ws->getWalls(direction);
r->changeSetAngle(0.0);
if (walls[direction]) {
if (!walls[(direction + 1) % 4]) {
turnRight();
}
else if (!walls[(direction + 3) % 4]) {
turnLeft();
}
else {
turnAround();
}
}
moveForward(TILE_LENGTH);
//--------------------------------------------
*/
// drive a small portion of the maze
//doLoop();
// back and forth 5 tiles
//doStraight(5);
//doStraight(2);
// move in a square
//doSquare(TILE_LENGTH);
// repeadedly move forward
//moveForward(TILE_LENGTH);
// repeadetly turn
//turnRight();
//turnLeft();
// back and forth
//moveForward(TILE_LENGTH);
//turnAround();
//r->update();
//printData();
}
/** Encoder Interrupts **/
void isrLA() {
r->getLeftEncoder()->encoderEvent(true);
}
void isrLB() {
r->getLeftEncoder()->encoderEvent(false);
}
void isrRA() {
r->getRightEncoder()->encoderEvent(true);
}
void isrRB() {
r->getRightEncoder()->encoderEvent(false);
}
void printData() {
Serial.print("dir: ");
Serial.print(r->getDirection());
Serial.print("; Angle: ");
Serial.print(r->getAngle()*180.0/PI);
Serial.print("; X: ");
Serial.print(r->getX());
Serial.print("; Y: ");
Serial.println(r->getY());
/*
Serial.print("Left: ");
Serial.print(r->getLeftSpeed());
Serial.print("; Right: ");
Serial.println(r->getRightSpeed());
*/
}
//--------------------------------------------
// Basic directional / relative movement
void moveForward(float distance) {
//fixAngle();
int direction = r->getDirection();
//r->setSetAngle(r->getAngleToTarget(distance));
//r->waitForNext(INSTRUCTION_DELAY);
if (direction == 0) {
r->changeSetX(distance);
}
else if (direction == 1) {
r->changeSetY(distance);
}
else if (direction == 2) {
r->changeSetX(-distance);
}
else if (direction == 3) {
r->changeSetY(-distance);
}
else {
r->wait(100000);
}
r->waitForNext(INSTRUCTION_DELAY);
}
void turnRight() {
r->changeSetAngle(PI/2);
r->waitForNext(INSTRUCTION_DELAY);
}
void turnLeft() {
r->changeSetAngle(-PI/2);
r->waitForNext(INSTRUCTION_DELAY);
}
void turnAround() {
turnRight();
turnRight();
}
void fixAngle() {
r->changeSetAngle(0);
r->waitForNext(INSTRUCTION_DELAY);
}
//--------------------------------------------
// Movement Patterns
void doSquare(float size) {
for (int i = 0; i < 4; i++) {
moveForward(TILE_LENGTH);
turnRight();
//turnLeft();
}
}
void doLoop() {
// forward 3
moveForward(TILE_LENGTH);
moveForward(TILE_LENGTH);
moveForward(TILE_LENGTH);
// right 1
turnRight();
// forward 1
moveForward(TILE_LENGTH);
// right 1
turnRight();
// forward 2
moveForward(TILE_LENGTH);
moveForward(TILE_LENGTH);
// left 1
turnLeft();
// forward 3
moveForward(TILE_LENGTH);
moveForward(TILE_LENGTH);
moveForward(TILE_LENGTH);
// left 1
turnLeft();
// forward 3
moveForward(TILE_LENGTH);
moveForward(TILE_LENGTH);
moveForward(TILE_LENGTH);
// left 1
turnLeft();
// forward 2
moveForward(TILE_LENGTH);
moveForward(TILE_LENGTH);
// left 1
// forward 1 - issue?!
//r->changeSetX(-TILE_LENGTH);
//r->waitForNext(INSTRUCTION_DELAY);
moveForward(TILE_LENGTH);
// right 2
turnAround();
// forward 1
moveForward(TILE_LENGTH);
// left 1
turnLeft();
// forward 1
moveForward(TILE_LENGTH);
// right 1
turnRight();
// forward 1
moveForward(TILE_LENGTH);
// left 1
turnLeft();
// forward 1
moveForward(TILE_LENGTH);
// left 1
turnLeft();
// forward 5
moveForward(TILE_LENGTH);
moveForward(TILE_LENGTH);
moveForward(TILE_LENGTH);
moveForward(TILE_LENGTH);
moveForward(TILE_LENGTH);
// right 2
turnAround();
}
void doStraight(int count) {
for (int i = 0; i < count; i++) {
//fixAngle();
moveForward(TILE_LENGTH);
}
//fixAngle();
turnRight();
//fixAngle();
turnRight();
}