forked from iuliandk/Brat
/
Autonomous 2-Axis Accelerometer.bas
463 lines (407 loc) · 14 KB
/
Autonomous 2-Axis Accelerometer.bas
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
;Program written for Bot Board II, Basic Atom Pro 28, IDE Ver. 8.0.1.7
;Written by Nathan Scherdin, modified by Jim and James Frye
;System variables
righthip con p10
rightknee con p8
rightankle con p7
lefthip con p6
leftknee con p5
leftankle con p4
NUMSERVOS con 6
aServoOffsets var sword(NUMSERVOS)
ServoTable bytetable RightHip,rightknee, rightankle,lefthip, leftknee, leftankle
TRUE con 1
FALSE con 0
;calibrate steps per degree.
stepsperdegree fcon 166.6
;You must calibrate the servos to "zero". Each robot will be different!
;When homed in and servos are at 0 degrees the robot should be standing
;straight with the AtomPro chip pointing backward. If you know the number
;of degrees the servo is off, you can calculate the value. 166.6 steps
;per degree. The values for our test robot were found by running the
;program bratosf.bas written by James Frye.
;Interrupt init
ENABLEHSERVO
;==============================================================================
; Complete initialization
;==============================================================================
aServoOffsets = rep 0\NUMSERVOS ; Use the rep so if size changes we should properly init
; try to retrieve the offsets from EEPROM:
; see how well my pointers work
gosub ReadServoOffsets
;Init positions
;Note, movement subroutine arguments are Rightankle,Rightknee,Righthip,Leftankle,Leftknee,Lefthip,speed
gosub movement [ 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0]
;low 1
;low 2
pause 1000
command var byte
xx var byte
ir var word
AMy var word
bat var word
detect var bit
behaviour var byte
filter var byte
temp var word(10)
;---------------------------------------;
;--------Command Quick Reference--------;
;---------------------------------------;
;- Command 1 = Walk Forward -;
;- Command 2 = Walk Backward -;
;- Command 3 = Long Stride Forward -;
;- Command 4 = Long Stride Backward -;
;- Command 5 = Kick -;
;- Command 6 = Headbutt -;
;- Command 7 = Get up from Front -;
;- Command 8 = Get up from Back -;
;- Command 9 = Rest Position -;
;- Command 0 = Home Position -;
;- Command 11= Turn Left -;
;---------------------------------------;
sound 9,[50\4400]
pause 50
sound 9,[50\3960]
pause 50
sound 9,[50\3400]
behaviour = 0
adin 17, AMy
if (AMy < 425) then; you're staring at the sky! get up!
command = 8
gosub move
endif
if (AMy > 550) then;you're flat on your face! get up!
command = 7
gosub move
endif
pause 2500
detect = 0
main
if (behaviour = 16) then
behaviour = 0
endif
behaviour = (behaviour +1) ;let's use this to trigger behaviour at times.
gosub readir
gosub readAMy
adin 16, bat
if (AMy < 425) then; you're staring at the sky! get up!
command = 8
gosub move
endif
if (AMy > 550) then;you're flat on your face! get up!
command = 7
gosub move
endif
if (bat < 270) then;battery is getting low
sound 9,[50\5000,25\4400,50\5000]
endif
if (ir > 250) then
detect = true
endif
sound 9,[100\(ir * 10)] ;beep in between steps, higher pitch for closer obstacles.
if detect then
low 14 ;turn the yellow LED on
command=2 ;back up one step
gosub move
command=11 ;turn left two steps
for xx=1 to 2
gosub move
next
detect = false
input 14 ;turn the yellow LED off
else
low 12 ;turn the red LED on
command=1
gosub move
input 12 ;turn the red LED off
endif
if (behaviour = 5) then
low 13 ;turn the green LED on
command = 9
gosub move
pause 2000
sound 9,[150\4400] ;little whistle while you rest
pause 50
sound 9,[50\4400]
pause 50
sound 9,[150\3960]
pause 50
sound 9,[50\3960]
pause 50
sound 9,[250\3400]
pause 2000
input 13 ;turn the green LED off
endif
goto main
move:
if(command = 1) then ; Walk Forward
gosub movement [ 7.0,-20.0,-20.0, -7.0, 20.0, 20.0, 500.0]
gosub movement [ -7.0,-20.0,-20.0, 7.0, 20.0, 20.0, 500.0]
gosub movement [ -7.0, 20.0, 20.0, 7.0,-20.0,-20.0, 500.0]
gosub movement [ 7.0, 20.0, 20.0, -7.0,-20.0,-20.0, 500.0]
elseif(command = 2) ; Walk Backwards
gosub movement [ -7.0,-20.0,-20.0, 7.0, 20.0, 20.0, 500.0]
gosub movement [ 7.0,-20.0,-20.0, -7.0, 20.0, 20.0, 500.0]
gosub movement [ 7.0, 20.0, 20.0, -7.0,-20.0,-20.0, 500.0]
gosub movement [ -7.0, 20.0, 20.0, 7.0,-20.0,-20.0, 500.0]
elseif(command = 3) ; Long Stride Forward
gosub movement [ 12.0,-45.0,-45.0,-12.0, 45.0, 45.0, 750.0]
gosub movement [-12.0,-45.0,-45.0, 12.0, 45.0, 45.0, 750.0]
gosub movement [-12.0, 45.0, 45.0, 12.0,-45.0,-45.0, 750.0]
gosub movement [ 12.0, 45.0, 45.0,-12.0,-45.0,-45.0, 750.0]
elseif(command = 4) ; Long Stride Backward
gosub movement [-12.0,-45.0,-45.0, 12.0, 45.0, 45.0, 750.0]
gosub movement [ 12.0,-45.0,-45.0,-12.0, 45.0, 45.0, 750.0]
gosub movement [ 12.0, 45.0, 45.0,-12.0,-45.0,-45.0, 750.0]
gosub movement [-12.0, 45.0, 45.0, 12.0,-45.0,-45.0, 750.0]
elseif(command = 5) ; Kick
gosub movement [ 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 500.0]
gosub movement [ 42.0, 0.0, 0.0,-14.0, 0.0, 0.0, 500.0]
gosub movement [ 0.0,-32.0, 41.0,-23.0, 0.0, 0.0, 500.0]
gosub movement [ 0.0, 24.0,-20.0,-23.0, 0.0, 0.0, 250.0]
gosub movement [ 0.0, 0.0, 0.0,-18.0, 0.0, 0.0, 500.0]
gosub movement [ 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 500.0]
sound 9,[50\3960]
pause 50
sound 9,[50\4400]
pause 50
sound 9,[50\3960]
elseif(command = 7) ; Get up from front
gosub movement [ 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 500.0]
gosub movement [ 0.0, 90.0, 45.0, 0.0, 90.0, 45.0, 500.0]
gosub movement [ 40.0, 90.0,-37.0, 0.0, 90.0, 45.0, 500.0]
gosub movement [ 0.0, 90.0,-65.0, 0.0, 90.0,-65.0, 500.0]
pause 500 ; Then Bow
gosub movement [ 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 750.0]
sound 9,[50\4400]
pause 50
sound 9,[50\4400]
pause 50
sound 9,[50\3960]
elseif(command = 8) ; Get up from back
gosub movement [ 0.0, 0.0, 0.0, 0.0, 0.0, 0.0,1000.0]
gosub movement [ 0.0,-90.0, 5.0, 0.0,-90.0, 5.0,1000.0]
gosub movement [ 0.0, 0.0, 0.0, 0.0, 0.0, 0.0,1000.0]
pause 1000
;gosub movement [ 0.0, 22.0,-80.0, 0.0, 22.0,-80.0,1000.0]
;gosub movement [ 0.0, 0.0, 0.0, 0.0, 0.0, 0.0,1000.0]
sound 9,[50\3960]
pause 50
sound 9,[50\3960]
pause 50
sound 9,[50\4400]
elseif(command = 0) ; Home Position
gosub movement [ 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 500.0]
sound 9,[50\4400]
pause 50
sound 9,[50\4400]
pause 50
sound 9,[50\4400]
elseif(command = 6) ; Headbutt
gosub movement [ 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 500.0]
gosub movement [ 0.0,-50.0,-90.0, 0.0,-50.0,-90.0, 500.0]
gosub movement [ 0.0, 32.0,-58.0, 0.0, 32.0,-58.0, 400.0]
pause 200
;gosub movement [ 0.0,-11.0, 7.0, 0.0,-11.0, 7.0, 500.0]
gosub movement [ 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 500.0]
sound 9,[50\4400]
pause 50
sound 9,[50\3960]
pause 50
sound 9,[50\3960]
elseif(command = 11) ; Turn
gosub movement [ 0.0,-35.0,-40.0, 0.0, 35.0, 37.0,500.0]
gosub movement [ 0.0, 35.0, 37.0, 0.0,-35.0,-40.0,500.0]
gosub movement [-14.0, 35.0, 37.0, 20.0,-35.0,-40.0,500.0]
gosub movement [-14.0, 35.0, 37.0, 20.0, 35.0, 37.0,500.0]
gosub movement [ 20.0, 35.0, 37.0,-14.0, 35.0, 37.0,500.0]
gosub movement [ 20.0,-35.0,-40.0,-14.0, 35.0, 37.0,500.0]
;gosub movement [ 0.0,-35.0,-70.0, 0.0, 35.0, 7.0,500.0]
;gosub movement [ 0.0, 35.0, 7.0, 0.0,-35.0,-70.0,500.0]
;gosub movement [-14.0, 35.0, 7.0, 20.0,-35.0,-70.0,500.0]
;gosub movement [-14.0, 35.0, 7.0, 20.0, 35.0, 7.0,500.0]
;gosub movement [ 20.0, 35.0, 7.0,-14.0, 35.0, 7.0,500.0]
;gosub movement [ 20.0,-35.0,-70.0,-14.0, 35.0, 7.0,500.0]
elseif(command = 9) ; Rest Position
gosub movement [ 0.0, 45.0, 45.0, 0.0, 45.0, 45.0, 500.0]
endif
return
;Should never need to edit anything below this line. Add user subroutines above this and below main.
lefthippos var float
leftkneepos var float
leftanklepos var float
righthippos var float
rightkneepos var float
rightanklepos var float
last_lefthippos var float
last_leftkneepos var float
last_leftanklepos var float
last_righthippos var float
last_rightkneepos var float
last_rightanklepos var float
lhspeed var float
lkspeed var float
laspeed var float
rhspeed var float
rkspeed var float
raspeed var float
speed var float
longestmove var float
; variables passed to gethservo
idle var byte
finished var byte
junk var word
movement [rightanklepos,rightkneepos,righthippos,leftanklepos,leftkneepos,lefthippos,speed]
if(speed<>0.0)then
gosub getlongest[lefthippos-last_lefthippos, |
leftkneepos-last_leftkneepos, |
leftanklepos-last_leftanklepos, |
righthippos-last_righthippos, |
rightkneepos-last_rightkneepos, |
rightanklepos-last_rightanklepos],longestmove
speed = ((longestmove*stepsperdegree)/(speed/20.0))
gosub getspeed[lefthippos,last_lefthippos,longestmove,speed],lhspeed
gosub getspeed[leftkneepos,last_leftkneepos,longestmove,speed],lkspeed
gosub getspeed[leftanklepos,last_leftanklepos,longestmove,speed],laspeed
gosub getspeed[righthippos,last_righthippos,longestmove,speed],rhspeed
gosub getspeed[rightkneepos,last_rightkneepos,longestmove,speed],rkspeed
gosub getspeed[rightanklepos,last_rightanklepos,longestmove,speed],raspeed
else
lhspeed=0.0;
lkspeed=0.0;
laspeed=0.0;
rhspeed=0.0;
rkspeed=0.0;
raspeed=0.0;
endif
hservo [lefthip\TOINT (-lefthippos*stepsperdegree) + aServoOffsets(3)\TOINT lhspeed, |
righthip\TOINT (righthippos*stepsperdegree) + aServoOffsets(0)\TOINT rhspeed, |
leftknee\TOINT (-leftkneepos*stepsperdegree) + aServoOffsets(4)\TOINT lkspeed, |
rightknee\TOINT (rightkneepos*stepsperdegree) + aServoOffsets(1)\TOINT rkspeed, |
leftankle\TOINT (-leftanklepos*stepsperdegree) + aServoOffsets(5)\TOINT laspeed, |
rightankle\TOINT (rightanklepos*stepsperdegree) + aServoOffsets(2)\TOINT raspeed]
;hservowait [lefthip,righthip,leftknee,rightknee,leftankle,rightankle]
; Simple loop to check the status of all of servos. This loop will
; terminate when all of the servos say that they are idle. I.E.
; they reached their new destination.
do
finished = hservoidle(lefthip) and hservoidle(righthip) and hservoidle(leftknee) and hservoidle(rightknee) and hservoidle (leftankle) and hservoidle(rightankle)
; ??? - we say not to edit, yet???
;add sensor handling code here
;adin 0,ir
;if (ir > 210) then
; detect = true
;endif
while (NOT finished)
last_lefthippos = lefthippos
last_leftkneepos = leftkneepos
last_leftanklepos = leftanklepos
last_righthippos = righthippos
last_rightkneepos = rightkneepos
last_rightanklepos = rightanklepos
return
one var float
two var float
three var float
four var float
five var float
six var float
getlongest[one,two,three,four,five,six]
if(one<0.0)then
one=-1.0*one
endif
if(two<0.0)then
two=-1.0*two
endif
if(three<0.0)then
three=-1.0*three
endif
if(four<0.0)then
four=-1.0*four
endif
if(five<0.0)then
five=-1.0*five
endif
if(six<0.0)then
six=-1.0*six
endif
if(one<two)then
one=two
endif
if(one<three)then
one=three
endif
if(one<four)then
one=four
endif
if(one<five)then
one=five
endif
if(one<six)then
one=six
endif
return one
newpos var float
oldpos var float
longest var float
maxval var float
getspeed[newpos,oldpos,longest,maxval]
if(newpos>oldpos)then
return ((newpos-oldpos)/longest)*maxval
endif
return ((oldpos-newpos)/longest)*maxval
readir
for filter = 0 to 9
adin 19, temp(filter)
next
ir = 0
for filter = 0 to 9
ir = ir + temp(filter)
next
ir = ir / 10
;sound 9,[10\4000]
;serout s_out,i38400,[dec ir ,13]
return
readAMy
;==============================================================================
; Subroutine: ReadServoOffsets
; Will read in the zero points that wer last saved for the different servos
; that are part of this robot.
;
;==============================================================================
pT var pointer ; try using a pointer variable
cSOffsets var byte ; number of
bCSIn var byte
bCSCalc var byte ; calculated checksum
b var byte ;
i var byte
ReadServoOffsets:
readdm 0, [cSOffsets, bCSIn]
;serout s_out, i9600, ["RSO: cnt:", dec cSOffsets, " CS in:", hex bcsIn];
if (cSOffsets > 0) and (cSOffsets <= NUMSERVOS) then ; offset data is bad so go to error location
; OK now lets read in the array of data
readdm 2, [str aServoOffsets\csOffsets*2]
;... calculate checksum...
bCSCalc = 0
for i = 0 to NUMSERVOS-1
bCSCalc = AServoOffsets(i).lowbyte + AServoOffsets(i).highbyte
; serout s_out, i9600, [" ", sdec aServoOffsets(i),":", hex aServoOffsets(i)]
next
; serout s_out, i9600, [ " CS Calc:", hex bCSCalc]
if bCSCalc <> bCSIn then
aServoOffsets = rep 0\NUMSERVOS
endif
endif
; serout s_out, i9600, [13]
return
for filter = 0 to 9
adin 17, temp(filter)
next
AMy = 0
for filter = 0 to 9
AMy = AMy + temp(filter)
next
AMy = AMy / 10
;sound 9,[10\4000]
;serout s_out,i38400,[dec ir ,13]
return