This repository has been archived by the owner on Aug 13, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
robot3_2.bsx
681 lines (552 loc) · 25.3 KB
/
robot3_2.bsx
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
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
' {$PBASIC 2.5}
' {$STAMP BS2sx}
'=============================================================================================================
'========== CONDITIONAL COMPILATION ==========================================================================
'=============================================================================================================
' To prevent code from being compiled simply comment out the #DEFINE line
' Comment out line to use pot & joystick values for proto robot
#DEFINE COMPETITION_ROBOT
'=============================================================================================================
'========== ARCTAN LOOKUP TABLE ==============================================================================
'=============================================================================================================
'=============================================================================================================
'========== DECLARE VARIABLES ================================================================================
'=============================================================================================================
' Below is a list of declared input and output variables. Comment or un-comment
' the variables as needed. Declare any additional variables required in
' your main program loop. Note that you may only use 26 total variables.
'---------- Misc. MUST BE THE FIRST VARIABLES IN ALL BANKS ---------------------------------------------------
'------PERSISTENT VARIABLES -- VALID IN ALL BANKS ------------------------------------------------------------
'NOTE:In order for these variables to be valid in ALL banks, they must be in the exact same spot in each bank.
' To do this, just always keep these as the first variables in each bank.
'packet_num VAR byte
'delta_t VAR byte
'---------- Aliases for the Pbasic Mode Byte (nvr_PB_mode) -------------------------------------------------------
'-------------------------------------------------------------------------------------------------------------
' Bit 7 of the nvr_PB_mode byte (aliased as comp_mode below) indicates the status
' of the Competition Control, either Enabled or Disabled. This indicates the
' starting and stopping of rounds at the competitions.
' Comp_mode is indicated by a solid "Disabled" LED on the Operator Interface.
' Comp_mode = 1 for Enabled, 0 for Disabled.
'
' Bit 6 of the nvr_PB_mode byte (aliased as auton_mode below) indicates the status
' of the Autonomous Mode, either Autonomous or Normal. This indicates when
' the robot must run on its own programming. When in Autonomous Mode, all
' OI analog inputs are set to 127 and all OI switch inputs are set to 0 (zero).
' Auton_mode is indicated by a blinking "Disabled" LED on the Operator Interface.
' Auton_mode = 1 for Autonomous, 0 for Normal.
'
' Autonomous Mode can be turned ON by setting the RC to Team 0 (zero).
'
' Bit 5 of the nvr_PB_mode byte (aliased as user_display_mode below) indicates when
' the user selects the "User Mode" on the OI. nvr_PB_mode.bit5 is set to 1 in "User Mode".
' When the user selects channel, team number, or voltage, nvr_PB_mode.bit5 is set to 0
' When in "User Mode", the eight Robot Feedback LED are turned OFF.
' Note: "User Mode" is identified by the letter u in the left digit (for 4 digit OI's)
' Note: "User Mode" is identified by decimal places on the right two digits (for 3 digit OI's)
nvr_PB_mode VAR byte
nvr_PB_mode__comp_mode VAR nvr_PB_mode.bit7
nvr_PB_mode__auton_mode VAR nvr_PB_mode.bit6
nvr_PB_mode__user_display_mode VAR nvr_PB_mode.bit5
nvr_manybits VAR byte
nvr_manybits__twitchdone VAR nvr_manybits.bit0
nvr_manybits__crab_broken VAR nvr_manybits.bit1
nvr_manybits__first_loop VAR nvr_manybits.bit2
nvr_manybits__first_auto_loop VAR nvr_manybits.bit3
nvr_manybits__prev_loop_ctr VAR nvr_manybits.bit4
nvr_manybits__prev_loop_dir VAR nvr_manybits.bit5
nvr_manybits__fwing_brake VAR nvr_manybits.bit6
nvr_manybits__bwing_brake VAR nvr_manybits.bit7
nvr_2_3_waypoint_x VAR byte
nvr_2_3_waypoint_y VAR byte
nvr_2_3_waypoint_speed VAR byte
nvr_2_3_auton_bits VAR byte
nvr_2_3_auton_bits__auton_stop VAR nvr_2_3_auton_bits.bit0
'THIS MUST MATCH WITH robot2/3/4 ALSO
waypoint VAR byte
waypoint__side VAR waypoint.BIT7 'Top bit indicates side of the field
waypoint__prog_side VAR waypoint.HIGHNIB 'Program number combined with side is the program to run
waypoint__idx VAR waypoint.LOWNIB 'Bottom 4 bits are the current waypoint index we are at
WAYPOINT_PROG_ONLY_MASK CON %0111 'ANDing prog_side with this will return prog only (mask off "side"...bit7)
'---------- Operator Interface (OI) - Analog Inputs ----------------------------------------------------------
'-------------------------------------------------------------------------------------------------------------
'---------- Operator Interface - Digital Inputs --------------------------------------------------------------
'-------------------------------------------------------------------------------------------------------------
oi_swA VAR byte 'OI Digital Switch Inputs 1 thru 8
oi_swB VAR byte 'OI Digital Switch Inputs 9 thru 16
'---------- Robot Controller - Digital Inputs ----------------------------------------------------------------
'-------------------------------------------------------------------------------------------------------------
rc_swA VAR byte 'RC Digital Inputs 1 thru 8
'rc_swB VAR byte 'RC Digital Inputs 9 thru 16
'---------- Robot Controller - Digital Outputs ---------------------------------------------------------------
'-------------------------------------------------------------------------------------------------------------
relayA VAR byte
relayB VAR byte
'---------- Temporary Variables ------------------------------------------------------------------------------
'-------------------------------------------------------------------------------------------------------------
tmp1 VAR byte
tmp2 VAR byte
tmp3 VAR byte
tmp4 VAR byte
tmp5 VAR byte
tmp6 VAR byte
tmp7 VAR byte
tmp8 VAR byte
'Keep track of the the user_mode bit (RC request to see user data instead of LEDs)
tmp1_manybits2__force_auto VAR tmp1.bit0 'User requested robot to run auto regardless of auton bit from OI
tmp1_manybits2__prev_user_mode VAR tmp1.bit1 'Previous loop value of the PBMode.user_mode
tmp1_manybits2__many2 VAR tmp1.bit2
tmp1_manybits2__many3 VAR tmp1.bit3
tmp1_manybits2__loop_cnt VAR tmp1.HIGHNIB 'Keep loop count...incrememnted each loop
'=============================================================================================================
'========== DEFINE ALIASES ===================================================================================
'=============================================================================================================
' Aliases are variables which are sub-divisions of variables defined
' above. Aliases don't require any additional RAM.
'---------- Aliases for each OI switch input -----------------------------------------------------------------
'-------------------------------------------------------------------------------------------------------------
' Below are aliases for the digital inputs located on the Operator Interface.
' Ports 1 & 3 have their inputs duplicated in ports 4 & 2 respectively. The
' inputs from ports 1 & 3 may be disabled via the 'Disable' dip switch
' located on the Operator Interface. See Users Manual for details.
fwing_up_sw VAR oi_swA.bit0 'p1_sw_trig
fwing_dn_sw VAR oi_swA.bit1 'p1_sw_top
bwing_up_sw VAR oi_swA.bit2 'p1_sw_aux1
bwing_dn_sw VAR oi_swA.bit3 'p1_sw_aux2
stacker_up_sw VAR oi_swA.bit4 'p3_sw_trig
stacker_dn_sw VAR oi_swA.bit5 'p3_sw_top
scraper_sw VAR oi_swA.bit6 'p3_sw_aux1
crab_ovr_sw VAR oi_swA.bit7 'p3_sw_aux2
p2_sw_trig VAR oi_swB.bit0 'p2_sw_trig
p2_sw_top VAR oi_swB.bit1 'p2_sw_top
p2_sw_aux1 VAR oi_swB.bit2 'p2_sw_aux1
p2_sw_aux2 VAR oi_swB.bit3 'p2_sw_aux2
crab_trig VAR oi_swB.bit4 'p4_sw_trig
crab_top VAR oi_swB.bit5 'p4_sw_top
drive_trig VAR oi_swB.bit6 'p4_sw_aux1
drive_top VAR oi_swB.bit7 'p4_sw_aux2
'---------- Aliases for each RC switch input -----------------------------------------------------------------
'-------------------------------------------------------------------------------------------------------------
' Below are aliases for the digital inputs located on the Robot Controller.
rc_sw1 VAR rc_swA.bit0
rc_sw2 VAR rc_swA.bit1
rc_sw3 VAR rc_swA.bit2
rc_sw4 VAR rc_swA.bit3
rc_sw5 VAR rc_swA.bit4
rc_sw6 VAR rc_swA.bit5
rc_sw7 VAR rc_swA.bit6
rc_sw8 VAR rc_swA.bit7
'---------- Aliases for each RC Relay outputs ----------------------------------------------------------------
'-------------------------------------------------------------------------------------------------------------
' Below are aliases for the relay outputs located on the Robot Controller.
relay1_fwd VAR relayA.bit0
relay1_rev VAR relayA.bit1
relay2_fwd VAR relayA.bit2
relay2_rev VAR relayA.bit3
relay3_fwd VAR relayA.bit4
relay3_rev VAR relayA.bit5
relay4_fwd VAR relayA.bit6
relay4_rev VAR relayA.bit7
relay5_fwd VAR relayB.bit0
relay5_rev VAR relayB.bit1
relay6_fwd VAR relayB.bit2
relay6_rev VAR relayB.bit3
relay7_fwd VAR relayB.bit4
relay7_rev VAR relayB.bit5
relay8_fwd VAR relayB.bit6
relay8_rev VAR relayB.bit7
'=============================================================================
'SHARED Constants (these are used by more than one bank and MUST AGREE in each)
TRUE CON 1
FALSE CON 0
'=============================================================================
'Constants for Scratchpad ram locations [0-62] (63 reserved)
'(these constants are shared and must agree in all banks)
'(I just reserved these in the order they come in the serin stream)
s_oi_swA CON 0
s_oi_swB CON 1
s_rc_swA CON 2
s_drive_x CON 3
s_drive_y CON 4
s_crab_x CON 5
s_crab_y CON 6
s_fwing_oi CON 7
s_bwing_oi CON 8
s_stacker_oi CON 9
s_manybits2 CON 10 'additional status bits
's_packet_num CON 11
s_lcrab_pot CON 12 'left crab pot
s_rcrab_pot CON 13 'right crab pot
s_fwing_pot CON 15 'front wing pot
s_bwing_pot CON 16 'back wing pot
's_p3_y CON 17
s_stacker_pot CON 18 'stacker pot
's_sensor6 CON 19
's_p2_wheel CON 20
's_p1_wheel CON 21
's_sensor7 CON 22
'(...end of possible serin inputs. continue now, with outputs and stored user variables.)
s_stacker_tgt CON 25
's_waypoint_sel CON 26
's_waypoint_flgs CON 27 'flags associated with waypoint
s_curr_pos_x CON 32
s_curr_pos_y CON 33
s_curr_orient CON 34 'current compass value (or theta) of the robot
s_target_orient CON 35 'target compass value of the robot
s_fwing_wp_tgt CON 36
s_bwing_wp_tgt CON 37
s_waypoint CON 38
s_target_heading CON 39
s_relayA CON 40
s_relayB CON 41
s_fwing_tgt CON 42
s_bwing_tgt CON 43
s_rcrab_target CON 44
s_lcrab_target CON 45
s_right_crab CON 46 'crab steering motor speed
s_left_crab CON 47
s_rcrab_init CON 48
s_lcrab_init CON 49
s_lf_cur CON 50 'current speed of drive wheels
s_rf_cur CON 51
s_lb_cur CON 52
s_rb_cur CON 53
s_lf_wheel CON 54 'stored value for left_wheel
s_rf_wheel CON 55
s_lb_wheel CON 56
s_rb_wheel CON 57
s_front_wing CON 58 'current speed of wings
s_back_wing CON 59
s_stacker CON 60 'current speed of stacker
s_bat_volt CON 62
s_banknumber CON 63 'Read-Only
' ...insert any other storage names up to 62 that are useful...
' ____ ___ _ _ ____ _____ _ _ _ _____ ____
' / ___/ _ \| \ | / ___|_ _|/ \ | \ | |_ _/ ___|
' | | | | | | \| \___ \ | | / _ \ | \| | | | \___ \
' | |__| |_| | |\ |___) || |/ ___ \| |\ | | | ___) |
' \____\___/|_| \_|____/ |_/_/ \_\_| \_| |_| |____/
'
'Bank Constants (only used in this bank)
ANALOG_TO_BUTTON_HIGH CON 200 'threshold for analog buttons
DRIVE_DEADZONE CON 5
DRIVE_ACCEL_RATE CON 13
DRIVE_SCL CON 50 'turbo drive speed reduction (percent) [e.g. 50]
DRIVE_CON CON 64 '128 * (1- reduction percent) [e.g. 64]
TWO_AXIS_DEADZONE CON 50
CRAB_MODE_DEADZONE CON 20
CRAB_MANU_SPEED CON 50
CRAB_FAST CON 126 'must be < 127
CRAB_MED CON 80
CRAB_SLOW CON 17
CRAB_POSITION_NEAR CON 2
CRAB_POSITION_CLOSE CON 15
CRAB_POSITION_FAR CON 40
CRAB_RIGHT90 CON 18
CRAB_LEFT90 CON 213
AUTO_DRIVE_FWD CON 0
AUTO_DRIVE_REV CON 1
CC_OUTBAUD CON 240
CC_INBAUD CON 240
#IF COMPETITION_ROBOT #THEN
'=================CHANGE THESE IN BANK3 ALSO!!!!!!!!!!!!!!!!!!!!!
'=================Competition ROBOT
POT_RCRAB_RIGHT_180 CON 11 'Right Pot reading for 0 brads (180 degrees to the right)
POT_RCRAB_LEFT_180 CON 244 'Right Pot reading for 255 brads (180 degrees to the left)
'=================END CHANGE BANK3 WARNING!!!!!!!!!!!!!!!!!!!!!!!
#ELSE
'=================CHANGE THESE IN BANK3 ALSO!!!!!!!!!!!!!!!!!!!!!
'=================Proto ROBOT===============
POT_RCRAB_RIGHT_180 CON 12 'Right Pot reading for 0 brads (180 degrees to the right)
POT_RCRAB_LEFT_180 CON 243 'Right Pot reading for 255 brads (180 degrees to the left)
'=================END CHANGE BANK3 WARNING!!!!!!!!!!!!!!!!!!!!!!!
#ENDIF
'=================CHANGE THESE IN BANK3 ALSO!!!!!!!!!!!!!!!!!!!!!
'Waypoint option flags
AUTO_FLAGS_DRIVE_FWD CON $80 'Force the robot to drive forward to get to this waypoint
AUTO_FLAGS_DRIVE_REV CON $40 'Force the robot to drive in reverse to get to this waypoint
AUTO_FLAGS_HOLD_POSITION CON $20 'Robot will move on to another waypoint and will instead hold this position
'=================END CHANGE BANK3 WARNING!!!!!!!!!!!!!!!!!!!!!!!
BRADS_TO_OPTIMIZE_CRAB CON 96 'If our crab target is farther away than this many brads then we will actually pick the
' opposite 180 degree target and drive backwards. This should not be lower than
' 63 brads (90 degrees) but can be larger so we can take advantage of momentum so that
' if we are half way between the two target (63 brads from target) we continue to the
' same target, but if we are 96 (130 degrees) from target use the opposite target
'=============================================================================================================
'========== MAIN CODE ========================================================================================
'=============================================================================================================
'===========ONLY RUNS IN AUTO MODE=========
Gosub TargetAngleSpeedController
nvr_manybits__first_auto_loop = 0
Run 4
'============================================
'========= PotsToBrads Subroutine ===========
'== Inputs: tmp1 (in pot ticks)
'== Outputs: tmp1 (in brads)
'== Modifys: tmp1...duh?
'== Purpose: convert from pot ticks to binary degrees
'============================================
PotsToBradsScaling:
'Cap the pot values at their max and min so the equation below will work correctly.
tmp1 = tmp1 MIN POT_RCRAB_RIGHT_180
tmp1 = tmp1 MAX POT_RCRAB_LEFT_180
'scale so ticks go to full 0-255 binary degree range
if (tmp1 >= 127) then
tmp1 = 127 + ((tmp1 - 127) * 128 / (POT_RCRAB_LEFT_180 - 127))
else
tmp1 = 127 - ((127 - tmp1) * 127 / (127 - POT_RCRAB_RIGHT_180))
endif
return 'from PotsToBrads
'===========================================================
'========= TargetAngleSpeedController Subroutine ===========
'== Inputs:
'== Outputs:
'== Modifys:
'== Purpose:
'===========================================================
TargetAngleSpeedController:
GET s_curr_pos_x, tmp3
GET s_curr_pos_y, tmp4
if nvr_2_3_auton_bits__auton_stop = 1 then
'stop wheels
PUT s_lf_wheel, 127
PUT s_rf_wheel, 127
PUT s_lb_wheel, 127
PUT s_rb_wheel, 127
'debug "--auton_stop--" 'MICH_DEBUG
goto TargetControllerDone
endif
'debug "TargX=", dec nvr_2_3_waypoint_x, "CurrX=", dec tmp3, "TargY=", dec nvr_2_3_waypoint_y, "CurrY=", dec tmp4 'MICH_DEBUG
if ((nvr_2_3_waypoint_y >= tmp4) AND (nvr_2_3_waypoint_x >= tmp3)) then
'quadrant 1
'tmp5 = arctan((yT-yC) / (xT-xC))
'tmp5 = arctan((nvr_2_3_waypoint_y-tmp4) / (nvr_2_3_waypoint_x-tmp3))
tmp1 = nvr_2_3_waypoint_y - tmp4
tmp2 = nvr_2_3_waypoint_x - tmp3
Gosub ArcTan
else
if ((nvr_2_3_waypoint_y >= tmp4) AND (tmp3 >= nvr_2_3_waypoint_x)) then
'quadrant 2
'tmp5 = arctan((yT-yC) / (xC-xT))
'tmp5 = arctan((nvr_2_3_waypoint_y-tmp4) / (tmp3-nvr_2_3_waypoint_x))
tmp1 = nvr_2_3_waypoint_y - tmp4
tmp2 = tmp3 - nvr_2_3_waypoint_x
Gosub ArcTan
tmp5 = 127 - tmp5
else
if ((tmp4 >= nvr_2_3_waypoint_y) AND (tmp3 >= nvr_2_3_waypoint_x)) then
'quadrant 3
'tmp5 = arctan((yC-yT) / (xC-xT))
'tmp5 = arctan((tmp4-nvr_2_3_waypoint_y) / (tmp3-nvr_2_3_waypoint_x))
tmp1 = tmp4 - nvr_2_3_waypoint_y
tmp2 = tmp3 - nvr_2_3_waypoint_x
Gosub ArcTan
tmp5 = 127 + tmp5
else
if ((tmp4 >= nvr_2_3_waypoint_y) AND (nvr_2_3_waypoint_x >= tmp3)) then
'quadrant 4
'tmp5 = arctan((yC-yT) / (xT-xC))
'tmp5 = arctan((tmp4-nvr_2_3_waypoint_y) / (nvr_2_3_waypoint_x-tmp3))
tmp1 = tmp4 - nvr_2_3_waypoint_y
tmp2 = nvr_2_3_waypoint_x - tmp3
Gosub ArcTan
tmp5 = 256 - tmp5
endif
endif
endif
endif
PUT s_target_heading, tmp5
Gosub CrabPositionController 'Convert the heading into a crab target
tmp1 = AUTO_DRIVE_FWD 'Default to drive forward
Gosub OptimizeCrabTurn 'Figure out which of the 180 apart targets are better
'Returns AUTO_DRIVE_FWD/AUTO_DRIVE_REV into tmp1
'set wheel speed based on tmp1
if(tmp1 = AUTO_DRIVE_FWD) then
' debug " FWD", dec nvr_2_3_waypoint_speed 'MICH_DEBUG
PUT s_lf_wheel, 127 + nvr_2_3_waypoint_speed
PUT s_rf_wheel, 127 + nvr_2_3_waypoint_speed
PUT s_lb_wheel, 127 + nvr_2_3_waypoint_speed
PUT s_rb_wheel, 127 + nvr_2_3_waypoint_speed
else
' debug " REV", dec nvr_2_3_waypoint_speed 'MICH_DEBUG
PUT s_lf_wheel, 127 - nvr_2_3_waypoint_speed
PUT s_rf_wheel, 127 - nvr_2_3_waypoint_speed
PUT s_lb_wheel, 127 - nvr_2_3_waypoint_speed
PUT s_rb_wheel, 127 - nvr_2_3_waypoint_speed
endif
TargetControllerDone:
return 'from TargetAngleSpeedController
'=======================================
'========= HypDistance Subroutine ======
'== Inputs: tmp1 - opposite
'== tmp2 - angle (in binary degrees-brads)
'== Outputs: tmp5 - hypotenuse
'== Modifys: tmp5
'== Purpose: return the length of the hypotenuse
'== of the triangle with opp/angle passed in
'=======================================
HypDistance:
'SIN = opp/hyp, so hyp = opp/SIN
tmp5 = tmp1 / SIN(tmp2)
return 'from HypDistance
'=======================================================
'========= CrabPositionController Subroutine ===========
'== Inputs: s_crab_heading
'== s_current_orient
'== Outputs: s_rcrab_target
'== s_lcrab_target
'== Modifys: tmp1, tmp2, tmp3
'== Purpose:
'=======================================================
CrabPositionController:
if (nvr_2_3_auton_bits__auton_stop = 1) then
'point crabs straight ahead
PUT s_rcrab_target, 127
PUT s_lcrab_target, 127
else
'alphaH
GET s_target_heading, tmp1
'thetaC
GET s_curr_orient, tmp2
' debug "PosCtrl=", dec tmp1, ",", dec tmp2
'if (alphaH > thetaC) then
' crabaim = 127 + (alphaH - thetaC)
'else
' crabaim = 127 - (thetaC - alphaH)
'endif
if (tmp1 > tmp2) then
tmp3 = 127 + (tmp1 - tmp2)
else
tmp3 = 127 - (tmp2 - tmp1)
endif
'output is in brads, must scale to pot values later
PUT s_rcrab_target, tmp3
PUT s_lcrab_target, tmp3
' debug "->", dec tmp3
endif
'debug " c tgt: ", DEC tmp3, CR
return 'from CrabPositionController
'============================================
'====== OptimizeCrabTurn Subroutine =========
'== Inputs: s_crab_target Crab target (in brads)
'== s_rcrab_pot Current crab location (in pot ticks)
'== Outputs: s_crab_taget Updated with optimized turn if needed
'== tmp1 Set to AUTO_DRIVE_FWD or AUTO_DRIVE_REV for drive direction
'== Modifys: tmp5, tmp1
'== Purpose: If we need to turn the wheels more than 180 degrees, we can
'== optimize this by turning less and driving backwards!
'============================================
OptimizeCrabTurn:
'GET s_waypoint_flgs, tmp1
'if((tmp1 & AUTO_FLAGS_DRIVE_FWD) = AUTO_FLAGS_DRIVE_FWD) then
' 'Flags indicate we should go forward only...no optimize
' tmp1 = AUTO_DRIVE_FWD
'else
' if((tmp1 & AUTO_FLAGS_DRIVE_REV) = AUTO_FLAGS_DRIVE_REV) then
' 'Flags indicate we should go reverse only...no optimize
' tmp1 = AUTO_DRIVE_REV
'
' 'Turn the wheels to the 180 degree point opposite to our current target
' GET s_rcrab_target, tmp5
'
' if tmp5 >= 127 then
' tmp5 = tmp5 - 127
' else
' tmp5 = tmp5 + 127
' endif
'
' PUT s_rcrab_target, tmp5
' PUT s_lcrab_target, tmp5
' else
'Flags don't specify a direction, so pick the most optimal direction based
' on our current position
GET s_rcrab_pot, tmp1
Gosub PotsToBradsScaling 'Convert tmp1 from pot values to brads
GET s_rcrab_target, tmp5
'debug "Optmize=", dec tmp5, "w", dec tmp1
'Check if tmp5(target) > tmp1(current pot value in brads)
if (tmp5 > tmp1) AND ((tmp5 - tmp1) > BRADS_TO_OPTIMIZE_CRAB) then
'The wheels have to move to the left to get to the target and
' the wheels have to turn more than ninety degrees(or the constant)
'Then the wheels turn the smaller angle and drive backwards
if tmp5 >= 127 then
tmp5 = tmp5 - 127
else
tmp5 = tmp5 + 127
endif
tmp1 = AUTO_DRIVE_REV
else
if (tmp5 < tmp1) AND ((tmp1 - tmp5) > BRADS_TO_OPTIMIZE_CRAB) then
'The wheels have to move to the right to get to the target and
'The wheels have to turn more than ninety degrees(or the constant)
'Then the wheels turn the smaller angle and drive backwards
if tmp5 >= 127 then
tmp5 = tmp5 - 127
else
tmp5 = tmp5 + 127
endif
tmp1 = AUTO_DRIVE_REV
else
'leave the turning the same and drive forwards
tmp1 = AUTO_DRIVE_FWD
endif
endif
PUT s_rcrab_target, tmp5
PUT s_lcrab_target, tmp5
' endif
'endif
'debug " Tgt=", dec tmp5
return 'from OptimizeCrabTurn
'=======================================
'========= ArcTan Subroutine ===========
'== Inputs: tmp1 - opposite
'== tmp2 - adjacent
'== Outputs: tmp5 - angle (in brads)
'== Modifys: tmp1, tmp2, tmp4, tmp5
'== Purpose: return the arctan of an opposite/adjecent pair
'== Assumes calling function takes care of quadrant
'=======================================
ArcTan:
'tmp1 is the passed in "opposite", and tmp2 is the passed in "adjacent"
if ((tmp1 = 0) and (tmp2 = 0)) then
'Passed in opp and adj are both zero...should not happen but keep from
' a divide by zero and just return angle of zero
tmp5 = 0
else
'We always want our opposite to be smaller than our adjacent so we only have
' to deal with 45 degrees or smaller. If the passed in opposite is larger,
' we simply reverse them and calucalute the other angle prior to output.
if (tmp1 < tmp2) then
'Passed in opp is smaller than adj...perfect!
'tmp4 is the swap_angle flag
tmp4 = 0
else
'Passed in opp is larger than adj...swap it and set the
' swap_angle flag so we will calculate the exact angle prior to output
tmp5 = tmp1
tmp1 = tmp2
tmp2 = tmp5
tmp4 = 1 'swap_angle flag = true
endif
'This is a TAN lookup table that will convert (opp/adj)*64 into an angle from 0-32 brads(45 degrees).
' Since the angle must be less than 45 degrees, the opposite MUST be less than the adj side.
' NOTE: (a<<6) is the same as (a*64), but the shifting is much faster in hardware.
LOOKUP ((tmp1<<6)/tmp2), [0, 1, 1, 2, 3, 3, 4, 4, 5, 6, 6, 7, 8, 8, 9, 9, 10,11,11,12,
12,13,13,14,15,15,16,16,17,17, 18,18,19,19,20,20,21,21,22,22,
23,23,24,24,25,25,25,26,26,27, 27,27,28,28,29,29,29,30,30,30,
31,31,31,32,32], tmp5
'------------ OLD ANGLE TABLE --------------------------------------------------------------------
'LOOKUP (100*tmp1/tmp2), [0 ,0 ,1 ,2 ,2 ,3 ,4 ,4 ,5 ,5, 6 ,6 ,7 ,7 ,8 ,9 ,9 ,10,10,11,
' 11,12,12,13,14,14,15,15,16,16, 17,17,18,18,19,19,20,20,21,21,
' 22,22,23,23,24,24,25,25,26,26, 27,27,27,28,29,29,29,30,30,31,
' 31,31,32,32,33,33,33,34,34,35, 35,35,36,36,36,37,37,38,38,38,
' 39,39,39,40,40,40,41,41,41,42, 42,42,43,43,43,44,44,44,44,45,45], tmp5
'-------------------------------------------------------------------------------------------------
'If we swapped the opposite and adjacent then we have to convert to the other
' angle in the triangle. This is simple, just subtract from 90 degrees (64 brads).
if (tmp4 = 1) then
'swap_angle flag was set...calculate the other angle in the triangle.
tmp5 = 64 - tmp5
endif
endif
return 'from ArcTan