-
Notifications
You must be signed in to change notification settings - Fork 3
/
NavBot_v1.ino
486 lines (403 loc) · 10.1 KB
/
NavBot_v1.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
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
// Copyright (c) 2014, Solder Spot
// All rights reserved.
// See LICENSE.txt in root folder
// local includes
#include "Navigator.h"
#include "Pilot.h"
#include "NavBot.h"
//----------------------------------------
// Config Logic
//----------------------------------------
#define CFG_TEST_ENCODERS 0 // print encoder ticks as they change
#define CFG_TEST_MOTORS 0 // verify motor wiring
#define CFG_SQUARE_TEST 0
#define CFG_CALIBRATE_MOVE 1 // straight line movement
#define CFG_CALIBRATE_TURNS 0 // turning only test
#define CAL_DISTANCE 2 // meters to move for CALIBRATE_MOVE
#define CAL_TURNS 5 // num of turns for CALIBRATE_TURNS (+ right, - left)
#define SQUARE_SIZE 800 // size of square test sides in mm
//----------------------------------------
// Serial output config
//----------------------------------------
#define SERIAL_BAUD 9600
#define MOTOR_INFO 0 // print motor values
#define BUTTON_INFO 0 // print button state
#define NAV_INFO 0 // print nav data
#define TARGET_INFO 1 // print nav data at way points
#define MEM_REPORT 0 // print memory usage at startup
//----------------------------------------
// Your Paths
//----------------------------------------
int16_t my_path[] =
{
PTH_MOVE, 1000, PTH_TURN, 180,
PTH_MOVE, 1000, PTH_TURN, 180,
PTH_END
};
int16_t *run_sequence = my_path;
//----------------------------------------
// Include Bot Specific Code
//----------------------------------------
// the arduino ide does not seem to allow
// nested includes so you must first include
// any header files needed by the bot's code
// ZUMO_BOT
//#include <ZumoMotors.h>
//#include "ZumoBot.h"
// WALLIE_BOT
//#include <Wire.h>
//#include <SoftwareSerial.h>
//#include <PololuQik.h>
//#include "WallieBot.h"
// BLACK_BOT
#include <Wire.h>
#include <Adafruit_MotorShield.h>
#include "BlackBot.h"
// MY_BOT
//#inlcude <...your needed header files...>
//#include "MyBot.h"
//----------------------------------------
// Config
//----------------------------------------
bool pushToStart = true;
//----------------------------------------
// Data and Data Types
//----------------------------------------
// Bot states
enum State
{
INIT,
RUNNING,
WAITING,
NUM_STATES
};
State state = INIT;
//----------------------------------------
// Simple Path control
//----------------------------------------
enum PathState
{
PTH_WAITING,
PTH_STOPPING,
PTH_DONE
};
int16_t *pth_sequence = NULL;
PathState pth_state = PTH_DONE;
int16_t fullSquare[] =
{
PTH_MOVE, SQUARE_SIZE, PTH_TURN, 90,
PTH_MOVE, SQUARE_SIZE, PTH_TURN, 90,
PTH_MOVE, SQUARE_SIZE, PTH_TURN, 90,
PTH_MOVE, SQUARE_SIZE, PTH_TURN, 180,
PTH_MOVE, SQUARE_SIZE, PTH_TURN, -90,
PTH_MOVE, SQUARE_SIZE, PTH_TURN, -90,
PTH_MOVE, SQUARE_SIZE, PTH_TURN, -90,
PTH_MOVE, SQUARE_SIZE, PTH_TURN, 180,
PTH_END
};
int16_t cwSquare[] =
{
PTH_MOVE, SQUARE_SIZE, PTH_TURN, 90,
PTH_MOVE, SQUARE_SIZE, PTH_TURN, 90,
PTH_MOVE, SQUARE_SIZE, PTH_TURN, 90,
PTH_MOVE, SQUARE_SIZE, PTH_TURN, 90,
PTH_END
};
int16_t ccwSquare[] =
{
PTH_MOVE, SQUARE_SIZE, PTH_TURN, -90,
PTH_MOVE, SQUARE_SIZE, PTH_TURN, -90,
PTH_MOVE, SQUARE_SIZE, PTH_TURN, -90,
PTH_MOVE, SQUARE_SIZE, PTH_TURN, -90,
PTH_END
};
int16_t *squarePath = cwSquare;
//----------------------------------------
// Validate Config
//----------------------------------------
#if (CFG_TEST_ENCODERS+CFG_TEST_MOTORS+CFG_SQUARE_TEST+CFG_CALIBRATE_MOVE+CFG_CALIBRATE_TURNS) > 1
#error Only one CFG_XXX can be set to 1. The rest must be 0
#endif
//----------------------------------------
// setup
//----------------------------------------
void setup()
{
pinMode(BUTTON_PIN, INPUT);
digitalWrite(BUTTON_PIN, HIGH);
// set up navigation
navigator.InitEncoder( WHEEL_DIAMETER, WHEELBASE, TICKS_PER_REV );
navigator.SetDistanceScaler( DISTANCE_SCALER );
navigator.SetWheelbaseScaler( WHEELBASE_SCALER );
navigator.SetWheelRLScaler( WHEEL_RL_SCALER );
// set up pilot
pilot.SetNavigator( navigator );
pilot.SetTimeFunction( millis );
pilot.SetTicksHandler( ticks_handler );
pilot.SetMotorHandler( motor_handler );
#if USE_SERIAL
Serial.begin(SERIAL_BAUD);
Serial.println(F("NavBot V1!"));
#endif
init_bot();
#if MEM_REPORT
memReport();
Serial.print(F("Navigator: "));
Serial.print(sizeof(navigator));
Serial.print(F(" Pilot: "));
Serial.println(sizeof(pilot));
#endif
}
//----------------------------------------
// loop - Main logic
//----------------------------------------
void loop()
{
handleButtonPress();
if( pushToStart )
{
// don't do anything till the button is pressed
return;
}
// handle each state
switch(state)
{
case INIT:
{
pilot.Reset();
#if CFG_CALIBRATE_MOVE
//pilot.SetCalibrationMode( true );
pilot.MoveBy(nvMETERS(CAL_DISTANCE));
#elif CFG_CALIBRATE_TURNS
//pilot.SetCalibrationMode( true );
pilot.SpinBy(nvDEGREES(360*CAL_TURNS));
#elif CFG_TEST_MOTORS
while(1)
{
motor_handler( &pilot, 0, 256 );
delay(2000);
motor_handler( &pilot, 256, 0 );
delay(2000);
}
#else
#if CFG_SQUARE_TEST
//pilot.SetCalibrationMode( true );
init_path( squarePath );
#else
init_path( run_sequence );
#endif
#endif
state = RUNNING;
break;
}
case RUNNING :
{
#if CFG_CALIBRATE_MOVE || CFG_CALIBRATE_TURNS
if( pilot.IsDone())
{
#if TARGET_INFO
printNavInfo();
#endif
state = WAITING;
}
#else
update_path();
#endif
break;
}
case WAITING :
{
break;
}
}
//Serial.println(F("Pilot.Service()"));
pilot.Service();
#if NAV_INFO
outputNavInfo();
#endif
}
void stop()
{
pushToStart = true;
pilot.Stop();
while ( !pilot.IsDone() )
{
pilot.Service();
}
state = INIT;
}
//----------------------------------------
// button logic
//----------------------------------------
void handleButtonPress(void)
{
static char lastButton = HIGH;
static char count = 0;
char button = digitalRead( BUTTON_PIN );
if( (button != lastButton) )
{
if(++count > 2)
{
if( button == LOW )
{
#if BUTTON_INFO
Serial.println(F("Button Pressed"));
#endif
if( pushToStart )
{
pushToStart = false;
state = INIT;
}
else
{
stop();
}
}
lastButton = button;
count = 0;
}
}
else
{
count = 0;
}
#if CFG_TEST_ENCODERS
{
int16_t lft, rht;
static int16_t lTotal = 0;
static int16_t rTotal = 0;
static int16_t lastlTotal = 0;
static int16_t lastrTotal = 0;
bool ok = ticks_handler( &pilot, &lft, &rht);
lTotal += lft;
rTotal += rht;
if( lastlTotal != lTotal || lastrTotal != rTotal )
{
Serial.print(F("Encoders: lft = "));
Serial.print(lTotal);
Serial.print(F(" rht = "));
Serial.print(rTotal);
Serial.println(!ok ? F(" (error)") :F(""));
lastlTotal = lTotal;
lastrTotal = rTotal;
}
}
#endif
}
//----------------------------------------
// Simple Path control
//----------------------------------------
void init_path( int16_t *sequence )
{
pth_sequence = sequence;
pth_state = PTH_WAITING;
pth_next();
}
//----------------------------------------
//
//----------------------------------------
void update_path()
{
switch (pth_state)
{
case PTH_WAITING:
if( pilot.IsDone() && !navigator.InMotion())
{
#if TARGET_INFO
printNavInfo();
#endif
pth_next();
}
break;
default:
case PTH_DONE:
break;
}
}
//----------------------------------------
//
//----------------------------------------
void pth_next()
{
int16_t action;
if( !pth_sequence || pth_state == PTH_DONE )
{
return;
}
action = *pth_sequence++;
switch( action )
{
case PTH_MOVE:
pilot.MoveBy( *pth_sequence++);
pth_state = PTH_WAITING;
break;
case PTH_TURN:
pilot.TurnBy( nvDEGREES(*pth_sequence++));
pth_state = PTH_WAITING;
break;
default:
case PTH_END:
stop();
pth_state = PTH_DONE;
break;
}
}
//----------------------------------------
//
//----------------------------------------
#if NAV_INFO
void outputNavInfo()
{
static nvHeading lheading = nvDEGREES(SQUARE_SIZE);
static nvRate lspeed = nvMETERS(10);
static nvRate lturn = nvMETERS(10);
static nvDistance lx = nvMM(0);
static nvDistance ly = nvMM(0);
nvPose pose = navigator.Pose();
if( pose.heading != lheading
|| pose.position.x != lx
|| pose.position.y != ly
|| navigator.Speed() != lspeed
|| navigator.TurnRate() != lturn )
{
printNavInfo();
lturn = navigator.TurnRate();
lspeed = navigator.Speed();
lheading = pose.heading;
lx = pose.position.x;
ly = pose.position.y;
}
}
#endif
#if NAV_INFO || TARGET_INFO
void printNavInfo( void )
{
nvPose pose = navigator.Pose();
Serial.print(F("Nav - x:"));
Serial.print( pose.position.x );
Serial.print(F(" y: "));
Serial.print( pose.position.y );
Serial.print(F(" h: "));
Serial.print( pose.heading );
Serial.print(F(" v: "));
Serial.print( navigator.Speed() );
Serial.print(F(" t: "));
Serial.println( navigator.TurnRate() );
}
#endif
#if MEM_REPORT
void memReport( void )
{
int stack;
extern int __bss_start;
extern int __data_start;
extern int __heap_start;
extern void *__brkval;
Serial.print(F("SRAM: data - "));
Serial.print(((uint16_t)&__bss_start) - ((uint16_t)&__data_start));
Serial.print(F(" bss - "));
Serial.print(((uint16_t)&__heap_start) - ((uint16_t)&__bss_start));
Serial.print(F(" free - "));
Serial.println( ((uint16_t)&stack) - (__brkval!=0 ? ((uint16_t)__brkval) : ((uint16_t)&__heap_start)));
}
#endif