-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
1060 lines (868 loc) · 30.4 KB
/
main.cpp
File metadata and controls
1060 lines (868 loc) · 30.4 KB
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
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
*
* FEH ROBOT SP24 TEAM C2
* Team Members:
* - Griffin Bohm (bohm.26@buckeyemail.osu.edu)
* - Brian Jia (jia.659@buckeyemail.osu.edu)
* - Sam Patterson (patterson.1368@buckeyemail.osu.edu)
* - Drew Straub (straub.135@buckeyemail.osu.edu)
*
* Code by Brian Jia (jia.659@buckeyemail.osu.edu)
*
* Unless otherwise specified, all lengths in this code are in inches,
* and all angles are in radians.
*
* IF YOU ARE NOT A MEMBER OF FEH ROBOT SP24 TEAM C2 (THE TEAM), DO NOT USE THIS CODE.
* THE TEAM HOLDS EXCLUSIVE COPYRIGHT OVER THIS CODE AND USING IT FOR YOUR OWN
* ROBOT CONSTITUTES COPYRIGHT INFRINGEMENT AND ACADEMIC MISCONDUCT.
*
*/
#include "CMSIS/MK60D10.h"
#include <FastLCD.h>
#include <FEHLCD.h>
#include <FEHMotor.h>
#include <FEHServo.h>
#include <FEHIO.h>
#include <FEHRCS.h>
#include <cmath>
#include <typeinfo>
#include <FEHBattery.h>
extern "C" {
#include "FreeRTOS.h"
#include "task.h"
#include "queue.h"
#include "timers.h"
}
using namespace std;
/*
* Namespace forward declarations and uses.
*/
namespace util {};
namespace robot_control {};
namespace tasks {};
using namespace util;
using namespace robot_control;
using namespace tasks;
/*
* Proteus constants.
*/
int SystemCoreClock = 110 * 1000000; // yeah we overclocked the proteus
constexpr int LCD_WIDTH = 320;
constexpr int LCD_HEIGHT = 240;
#define Sleep vTaskDelay
/*
* Code that won't be modified often during testing is stuffed into the "util" namespace.
*/
namespace util {
constexpr float rad(float deg) {
return (float) (deg * (M_PI / 180));
}
constexpr float deg(float rad) {
return (float) (rad * (180 / M_PI));
}
[[noreturn]]
void poweroff() {
GPIOD_PDOR &= ~GPIO_PDOR_PDO(GPIO_PIN(13));
while (true) {}
}
template<int m>
struct Vec {
float vec[m];
Vec add(Vec &b) const {
Vec<m> sum{};
for (int i = 0; i < m; i++) {
sum.vec[i] = vec[i] + b.vec[i];
}
return sum;
}
float dist(Vec<m> &b) const {
static_assert(m >= 2);
return sqrt(pow(b.vec[0] - vec[0], 2) + pow(b.vec[1] - vec[1], 2));
}
};
struct CycTimer {
uint32_t value{};
uint32_t last_lap_cyc{};
void begin() {
// Enable debugging functions including cycle counter
// 24th bit is TRCENA
DEMCR |= (0b1 << 24);
// Enable the cycle counter itself
// first bit is CYCCNTENA
DWT_CTRL |= 0b1;
value = DWT_CYCCNT;
}
uint32_t lap() {
uint32_t current_cyc = DWT_CYCCNT;
uint32_t lap_cyc = current_cyc - value;
value = current_cyc;
last_lap_cyc = lap_cyc;
return lap_cyc;
}
};
enum {
Clear = 0,
White,
Gray,
Red,
Yellow,
};
struct PIController {
float sample_time;
float error{};
float kP, kI;
float I{}, max_I;
float control_effort{};
explicit PIController(float sample_rate, float kP, float kI, float max_I) :
sample_time(1 / sample_rate), kP(kP), kI(kI), max_I(max_I) {}
float process(float setpoint, float process_variable) {
error = setpoint - process_variable;
I += error * sample_time;
I = fmaxf(-max_I, fminf(max_I, I));
control_effort =
kP * error +
kI * I;
return control_effort;
}
void reset() {
I = 0;
}
};
}
/*
* Robot control constants.
*/
constexpr int CONTROL_LOOP_HZ = 100;
constexpr float IGWAN_COUNTS_PER_REV = 636;
constexpr float TRACK_WIDTH = 8.40;
constexpr float WHEEL_DIA = 2.5;
constexpr float INCHES_PER_REV = WHEEL_DIA * M_PI;
constexpr float INCHES_PER_COUNT = (float) (INCHES_PER_REV / IGWAN_COUNTS_PER_REV);
float DRIVE_MOTOR_MAX_VOLTAGE = 11; // TODO: igwans are actually rated at 12
/*
* Robot control configuration.
*/
constexpr float DRIVE_MIN_PERCENT = 7.5;
constexpr float TURN_MIN_PERCENT = 7.5;
constexpr float STOPPED_I = 10;
constexpr float STOPPED_I_ACCUMULATE = 3;
constexpr float STOPPED_I_HIGHPASS = 0.999;
constexpr float START_LIGHT_THRESHOLD_VOLTAGE = 1;
constexpr int WAIT_FOR_LIGHT_CONFIDENT_MS = 50;
constexpr int TICKET_LIGHT_AVERAGING_MS = 50;
constexpr int SWITCH_CONFIDENT_TICKS = 10;
constexpr Vec<2> INITIAL_POS{0, 0};
constexpr float INITIAL_ANGLE = rad(-45);
/*
* Diagnostics/visualization configuration.
*/
constexpr int DIAGNOSTICS_HZ = 10;
constexpr float FORCE_START_HOLD_SEC = 0.5;
constexpr float FORCE_START_TOTAL_SEC = 1;
/*
* Port configuration.
*/
constexpr auto DRIVE_MOTOR_L_PORT = FEHMotor::Motor0;
constexpr auto DRIVE_MOTOR_R_PORT = FEHMotor::Motor1;
constexpr auto CDS_CELL_RED_PIN = FEHIO::FEHIOPin::P1_0;
constexpr auto CDS_CELL_BLUE_PIN = FEHIO::FEHIOPin::P1_1;
constexpr auto L_BUMP_SWITCH_PIN = FEHIO::FEHIOPin::P1_5;
constexpr auto R_BUMP_SWITCH_PIN = FEHIO::FEHIOPin::P1_6;
constexpr auto BUTTON_BUMP_SWITCH_PIN = FEHIO::FEHIOPin::P1_7;
constexpr auto ENCODER_L_PIN_0 = FEHIO::FEHIOPin::P3_0;
constexpr auto ENCODER_L_PIN_1 = FEHIO::FEHIOPin::P3_1;
constexpr auto ENCODER_R_PIN_0 = FEHIO::FEHIOPin::P3_2;
constexpr auto ENCODER_R_PIN_1 = FEHIO::FEHIOPin::P3_3;
constexpr float DRIVE_INCHES_PER_COUNT_L = INCHES_PER_COUNT;
constexpr float DRIVE_INCHES_PER_COUNT_R = INCHES_PER_COUNT;
constexpr auto FUEL_SERVO_PORT = FEHServo::FEHServoPort::Servo0;
constexpr auto DUMPTRUCK_SERVO_PORT = FEHServo::FEHServoPort::Servo1;
constexpr auto PASSPORT_SERVO_PORT = FEHServo::FEHServoPort::Servo2;
constexpr auto FUEL_SERVO_MIN = 650;
constexpr auto FUEL_SERVO_MAX = 2400;
constexpr auto DUMPTRUCK_SERVO_MIN = 500;
constexpr auto DUMPTRUCK_SERVO_MAX = 2500;
constexpr auto PASSPORT_SERVO_MIN = 500;
constexpr auto PASSPORT_SERVO_MAX = 2500;
/*
* Main robot control code.
*/
namespace robot_control {
enum TicketLightColor : int {
TICKET_LIGHT_NONE,
TICKET_LIGHT_RED,
TICKET_LIGHT_BLUE
};
struct Robot {
/*
* Because my modified DigitalEncoder code obtains a pointer to itself using the “this” keyword so that the port
* IRQs can update the object state, we MUST avoid using a copy of the object and use the original object
* that we instantiated. I couldn't get this working properly with C++ move semantics, so I'm instantiating the
* DigitalEncoder objects inside the Drivetrain constructor instead. It's working now.
*/
FEHMotor ml{DRIVE_MOTOR_L_PORT, DRIVE_MOTOR_MAX_VOLTAGE};
FEHMotor mr{DRIVE_MOTOR_R_PORT, DRIVE_MOTOR_MAX_VOLTAGE};
DigitalEncoder el{ENCODER_L_PIN_0, ENCODER_L_PIN_1};
DigitalEncoder er{ENCODER_R_PIN_0, ENCODER_R_PIN_1};
DigitalInputPin button_bump_switch{BUTTON_BUMP_SWITCH_PIN};
DigitalInputPin l_bump_switch{L_BUMP_SWITCH_PIN};
DigitalInputPin r_bump_switch{R_BUMP_SWITCH_PIN};
AnalogInputPin cds_red{CDS_CELL_RED_PIN};
AnalogInputPin cds_blue{CDS_CELL_BLUE_PIN};
FEHServo fuel_servo{FUEL_SERVO_PORT};
FEHServo dumptruck_servo{DUMPTRUCK_SERVO_PORT};
FEHServo passport_servo{PASSPORT_SERVO_PORT};
// Start Light / Ticket Light variables
TicketLightColor ticket_light_color = TICKET_LIGHT_NONE;
volatile bool force_start{};
float target_speed = 50;
float drive_slew_rate = 200; // Percent per m/s
float turn_slew_rate = 400; // Percent per radian/s
/* START DEBUG VARIABLES */
volatile float cds_red_value{};
volatile float cds_blue_value{};
float d_battery_voltage{};
float d_pct_l{};
float d_pct_r{};
float d_diff{};
int32_t total_counts_l{}, total_counts_r{};
float R{};
/* END DEBUG VARIABLES */
// Position is in inches
Vec<2> pos{};
// Angle in radians
float angle{};
float target_angle{};
int32_t counts_l{}, counts_r{};
int32_t task_counts_l{}, task_counts_r{};
Robot() {
pos = INITIAL_POS;
angle = INITIAL_ANGLE;
target_angle = INITIAL_ANGLE;
fuel_servo.SetMax(FUEL_SERVO_MAX);
fuel_servo.SetMin(FUEL_SERVO_MIN);
dumptruck_servo.SetMax(DUMPTRUCK_SERVO_MAX);
dumptruck_servo.SetMin(DUMPTRUCK_SERVO_MIN);
passport_servo.SetMax(PASSPORT_SERVO_MAX);
passport_servo.SetMin(PASSPORT_SERVO_MIN);
}
void process_odometry() {
counts_l = el.Counts();
counts_r = -er.Counts();
total_counts_l += counts_l;
total_counts_r += counts_r;
task_counts_l += counts_l;
task_counts_r += counts_r;
el.ResetCounts();
er.ResetCounts();
float arclength_l = DRIVE_INCHES_PER_COUNT_L * (float) counts_l;
float arclength_r = DRIVE_INCHES_PER_COUNT_R * (float) counts_r;
float arclength_inner;
if (abs(arclength_l) > abs(arclength_r)) {
arclength_inner = arclength_r;
} else {
arclength_inner = arclength_l;
}
float dAngle = (arclength_l - arclength_r) / TRACK_WIDTH;
if (dAngle != 0) {
float radius_inner = fabsf(arclength_inner / dAngle);
// Let R be the distance from the arc center to the point between the wheels
R = radius_inner + TRACK_WIDTH / 2;
float dx = R * (cos(angle + dAngle) - cos(angle));
float dy = -R * (sin(angle + dAngle) - sin(angle));
if (abs(arclength_l) > abs(arclength_r)) {
dx *= -1;
dy *= -1;
}
pos.vec[0] += dx;
pos.vec[1] += dy;
angle += dAngle;
} else {
pos.vec[0] += arclength_l * sin(angle);
pos.vec[1] += arclength_l * cos(angle);
}
}
void motor_power(float pct_l, float pct_r) {
// A lower battery voltage will result in a HIGHER power supplied to compensate the voltage drop.
d_battery_voltage = Battery.Voltage();
float voltage_compensation = 11.7f / d_battery_voltage;
pct_l *= voltage_compensation;
pct_r *= voltage_compensation;
pct_l *= 9.0f / DRIVE_MOTOR_MAX_VOLTAGE;
pct_r *= 9.0f / DRIVE_MOTOR_MAX_VOLTAGE;
d_pct_l = pct_l;
d_pct_r = pct_r;
ml.SetPercent(pct_l);
mr.SetPercent(pct_r);
}
static float slew(float rate, float min, float max, float dist_from_start, float dist_to_end) {
float slewed_start = sqrtf(rate * fabs(dist_from_start));
float slewed_end = sqrtf(rate * fabs(dist_to_end));
return fmin(max, fmin(slewed_start, slewed_end) + min);
}
struct unstuck {
uint32_t last_encoder_l_tick_at = 0;
uint32_t last_encoder_r_tick_at = 0;
uint32_t tick_count = 0;
float stopped_i = 0;
float process(Robot &r, float turn_l_mul, float turn_r_mul) {
if (r.counts_l != 0) {
last_encoder_l_tick_at = tick_count;
}
if (r.counts_r != 0) {
last_encoder_r_tick_at = tick_count;
}
if (turn_l_mul > 0.2) {
if (last_encoder_l_tick_at + 25 < tick_count) {
stopped_i += STOPPED_I_ACCUMULATE / CONTROL_LOOP_HZ;
}
}
if (turn_r_mul > 0.2) {
if (last_encoder_r_tick_at + 25 < tick_count) {
stopped_i += STOPPED_I_ACCUMULATE / CONTROL_LOOP_HZ;
}
}
tick_count++;
stopped_i *= STOPPED_I_HIGHPASS;
return stopped_i * STOPPED_I;
}
};
void execute_straight(float inches, bool until_switch, int timeout_ms) {
int switch_pressed_ticks = 0;
Vec<2> pos0 = pos;
unstuck unstuck;
PIController angle_controller(CONTROL_LOOP_HZ, 200, 200, 100);
TickType_t time = xTaskGetTickCount();
TickType_t start_time = xTaskGetTickCount();
while (true) {
if (timeout_ms > 0) {
if (xTaskGetTickCount() - start_time > timeout_ms) {
motor_power(0, 0);
return;
}
}
if (until_switch) {
// if (!l_bump_switch.Value() && !r_bump_switch.Value()) {
// TODO: Fix r_bump_switch
if (!r_bump_switch.Value()) {
switch_pressed_ticks++;
} else if (!button_bump_switch.Value()) {
switch_pressed_ticks++;
} else {
switch_pressed_ticks = 0;
}
if (switch_pressed_ticks >= SWITCH_CONFIDENT_TICKS) {
motor_power(0, 0);
return;
}
}
float control_effort = angle_controller.process(target_angle, angle);
float dist = pos.dist(pos0);
float dist_remain = fabs(inches) - fabs(dist);
float power_pct = slew(
drive_slew_rate,
DRIVE_MIN_PERCENT,
target_speed,
dist,
dist_remain
);
power_pct += unstuck.process(*this, 1, 1);
if (inches < 0) {
motor_power(-power_pct + control_effort, -power_pct - control_effort);
} else {
motor_power(power_pct + control_effort, power_pct - control_effort);
}
if (fabs(dist) > fabsf(inches)) {
motor_power(0, 0);
return;
}
vTaskDelayUntil(&time, 1000 / CONTROL_LOOP_HZ);
}
}
void execute_turn(float degree, float turn_l_mul, float turn_r_mul) {
target_angle = (float) rad(degree);
float turn_start_angle = angle;
bool turning_right = target_angle > angle;
task_counts_l = 0;
task_counts_r = 0;
unstuck unstuck;
PIController pi(CONTROL_LOOP_HZ, 0.5, 1, 30);
TickType_t time = xTaskGetTickCount();
while (true) {
float angle_turned_so_far = fabs(angle - turn_start_angle);
float angle_remain = fabs(target_angle - angle);
float power_pct = slew(
turn_slew_rate,
TURN_MIN_PERCENT,
target_speed,
angle_turned_so_far,
angle_remain
);
power_pct += unstuck.process(*this, turn_l_mul, turn_r_mul);
// make sure wheel ratios are correct using PI controller
float l_effort, r_effort;
// if regular (i.e. not pivot) turn
if (turn_l_mul != 0 && turn_r_mul != 0) {
// diff > 0 - left wheel is ahead of right
// diff < 0 - right wheel is ahead of left
float diff = abs((float)task_counts_l / turn_l_mul) - abs((float)task_counts_r / turn_r_mul);
float effort = pi.process(0, diff);
// when left is ahead, diff > 0, and so PI produces a negative effort
l_effort = effort;
r_effort = -effort;
} else {
if (turn_l_mul == 0) {
l_effort = pi.process(0, (float)task_counts_l);
r_effort = 0;
} else {
l_effort = 0;
r_effort = pi.process(0, (float)task_counts_r);
}
}
l_effort = 0;
r_effort = 0;
float new_pct_l, new_pct_r;
if (turning_right) {
new_pct_l = power_pct + l_effort;
new_pct_r = -power_pct + r_effort;
if (angle >= target_angle) {
motor_power(0, 0);
return;
}
} else {
new_pct_l = -power_pct + l_effort;
new_pct_r = power_pct + r_effort;
if (angle <= target_angle) {
motor_power(0, 0);
return;
}
}
new_pct_l *= turn_l_mul;
new_pct_r *= turn_r_mul;
motor_power(new_pct_l, new_pct_r);
vTaskDelayUntil(&time, 1000 / CONTROL_LOOP_HZ);
}
}
} robot;
}
/*
* Task running/handling code.
*/
namespace tasks {
void WaitForStartLight() {
robot.ml.Stop();
robot.mr.Stop();
// wait some time until we're confident that the light has started
int ms_been_confident_for = 0;
while (ms_been_confident_for < WAIT_FOR_LIGHT_CONFIDENT_MS) {
robot.cds_red_value = robot.cds_red.Value();
robot.cds_blue_value = robot.cds_blue.Value();
// robot control loop will update cds_red_value
if (robot.cds_red_value < START_LIGHT_THRESHOLD_VOLTAGE) {
ms_been_confident_for++;
} else {
ms_been_confident_for = 0;
}
if (robot.force_start) {
break;
}
Sleep(1);
}
}
void CaptureTicketLight() {
robot.ml.Stop();
robot.mr.Stop();
float cds_red_value_avg = 0;
float cds_blue_value_avg = 0;
for (int i = 0; i < TICKET_LIGHT_AVERAGING_MS; i++) {
robot.cds_red_value = robot.cds_red.Value();
robot.cds_blue_value = robot.cds_blue.Value();
cds_red_value_avg += robot.cds_red_value;
cds_blue_value_avg += robot.cds_blue_value;
Sleep(1);
}
cds_red_value_avg /= TICKET_LIGHT_AVERAGING_MS;
cds_blue_value_avg /= TICKET_LIGHT_AVERAGING_MS;
cds_red_value_avg *= 1.15; // Correction factor to make red and blue equal.
if (cds_red_value_avg < cds_blue_value_avg) {
// CDS cell red receiving more light...
robot.ticket_light_color = TICKET_LIGHT_RED;
} else {
robot.ticket_light_color = TICKET_LIGHT_BLUE;
}
}
void CaptureTicketLightDelayed_task(const int *ms) {
Sleep(*ms);
CaptureTicketLight();
}
void CaptureTicketLightDelayed(int ms) {
static int ms_static = ms;
const int STACK_SIZE = 1024;
static StaticTask_t tcb;
static StackType_t stack[STACK_SIZE];
xTaskCreateStatic(
TaskFunction_t(CaptureTicketLightDelayed_task),
"TicketLightDelayed",
STACK_SIZE,
&ms,
5,
stack,
&tcb
);
}
void Straight(float inches) {
robot.execute_straight(inches, false, 0);
}
void StraightTimeout(float inches, int timeout_ms) {
robot.execute_straight(inches, false, timeout_ms);
}
void StraightUntilSwitch(float inches) {
robot.execute_straight(inches, true, 0);
}
void StraightUntilSwitchTimeout(float inches, int timeout_ms) {
robot.execute_straight(inches, true, timeout_ms);
}
void ResetFacing(float degree) {
robot.angle = rad(degree);
}
void Speed(float percent) {
robot.target_speed = percent;
}
void TurnSlewRate(float rate) {
robot.turn_slew_rate = rate;
}
void DriveSlewRate(float rate) {
robot.drive_slew_rate = rate;
}
void Turn(float degree) {
robot.execute_turn(degree, 1, 1);
}
void Pivot(float degree, float turn_wheel_bias) {
float turn_l_mul = 1, turn_r_mul = 1;
if (turn_wheel_bias < 0) {
turn_r_mul = 1 - fabs(turn_wheel_bias);
turn_l_mul = fabs(turn_wheel_bias) + 1;
} else if (turn_wheel_bias > 0) {
turn_l_mul = 1 - fabs(turn_wheel_bias);
turn_r_mul = fabs(turn_wheel_bias) + 1;
}
robot.execute_turn(degree, turn_l_mul, turn_r_mul);
}
void Arc(float degree, float turn_l_mul, float turn_r_mul) {
robot.execute_turn(degree, turn_l_mul, turn_r_mul);
}
void PivotLeft(float degree) {
// right wheel turning only = pivoting on left
robot.execute_turn(degree, 0, 2);
}
void PivotRight(float degree) {
// left wheel turning only = pivoting on right
robot.execute_turn(degree, 2, 0);
}
void FwdSquareWiggle() {
robot.motor_power(100, 0);
Sleep(50);
robot.motor_power(0, 100);
Sleep(50);
robot.motor_power(100, 100);
Sleep(300);
robot.motor_power(0, 0);
}
void FwdSquarePush() {
robot.motor_power(100, 100);
Sleep(500);
robot.motor_power(0, 0);
}
void FuelServo(float degree) {
robot.fuel_servo.SetDegree(degree);
}
void DumptruckServo(float degree) {
robot.dumptruck_servo.SetDegree(degree);
}
void PassportServo(float degree) {
robot.passport_servo.SetDegree(degree);
}
}
/*
* Visualization/debugging code.
*/
namespace visualization {
template<typename T>
void log(const char *label, T value) {
FastLCD::Write(label);
FastLCD::Write(": ");
FastLCD::WriteLine(value);
}
void print_diagnostics() {
static bool prev_touching = false;
static bool display_testing_screen = true;
static float holding_sec = 0;
int x, y;
bool touching = LCD.Touch(&x, &y);
if (touching && !prev_touching) {
if (x < LCD_WIDTH / 2) {
display_testing_screen = !display_testing_screen;
}
}
prev_touching = touching;
switch (robot.ticket_light_color) {
case TICKET_LIGHT_RED:
FastLCD::SetPaletteColor(Clear, RED);
break;
case TICKET_LIGHT_BLUE:
FastLCD::SetPaletteColor(Clear, BLUE);
break;
case TICKET_LIGHT_NONE:
FastLCD::SetPaletteColor(Clear, BLACK);
break;
}
FastLCD::Clear();
FastLCD::SetFontPaletteIndex(White);
log("Time (s)", (int)(xTaskGetTickCount() / 1000));
log("CDS Red", robot.cds_red_value);
log("CDS Blue", robot.cds_blue_value);
log("FuelLever", RCS.GetCorrectLever());
log("Angle", deg(robot.angle));
log("TargetAngle", deg(robot.target_angle));
log("Battery%", (int)round((robot.d_battery_voltage / 11.7) * 100));
log("MotorL%", robot.d_pct_l);
log("MotorR%", robot.d_pct_r);
log("Diff", robot.d_diff);
log("LBump", robot.l_bump_switch.Value());
log("RBump", robot.r_bump_switch.Value());
log("FBump", robot.button_bump_switch.Value());
if (holding_sec < FORCE_START_HOLD_SEC) {
if (touching && x >= LCD_WIDTH / 2) {
holding_sec += 1.0 / DIAGNOSTICS_HZ;
} else {
holding_sec = 0;
}
} else {
holding_sec += 1.0 / DIAGNOSTICS_HZ;
if (holding_sec > FORCE_START_TOTAL_SEC) {
robot.force_start = true;
holding_sec = 0;
}
}
if (holding_sec > 0) {
float progress = holding_sec / FORCE_START_TOTAL_SEC;
auto progress_bar_width = (int) (progress * LCD_WIDTH);
FastLCD::SetFontPaletteIndex(3);
FastLCD::FillRectangle(0, 0, progress_bar_width, 32);
}
FastLCD::DrawScreen();
}
}
void robot_path_task() {
const int TS = 3000;
const int DS = 1000;
FuelServo(180);
DumptruckServo(180);
PassportServo(83);
DriveSlewRate(DS);
TurnSlewRate(TS);
Speed(90);
/*
* PATH START!
*/
// Wait for start light to turn on.
WaitForStartLight();
// Start button
DriveSlewRate(1000); // fast acceleration for start button
StraightTimeout(-2, 1000);
DriveSlewRate(DS); // a bit slower for getting to levers
// Forward
Straight(19.5);
DriveSlewRate(DS); // regular acceleration for rest of course
// Turn left toward fuel levers
Turn(-90);
// Levers are 3.5 inches apart
float leverMinus = float(2 - RCS.GetCorrectLever()) * 3.5f;
// Go toward fuel levers
Straight(8 - leverMinus); // This will vary based on which fuel lever to use.
// Turn arm toward fuel levers
Turn(0);
Straight(-4.5); // Back into position for the fuel lever flipping
// Fuel lever flip sequence
FuelServo(70);
Sleep(250);
Straight(3);
FuelServo(135);
Sleep(4500);
FuelServo(45);
Straight(-3);
FuelServo(90);
Sleep(250);
Straight(3);
FuelServo(45);
FuelServo(180);
// Turn right
Turn(90);
// Square with the left wall
StraightUntilSwitch(-(8.0f + leverMinus) - 2);
ResetFacing(90);
// Face up ramp
Straight(4);
Turn(0);
// Drive up ramp
DriveSlewRate(DS * 2); // get the acceleration out of the way before we hit the ramp
Straight(26);
DriveSlewRate(DS);
// Go to luggage drop
TurnSlewRate(TS * 0.75); // This turn is crazy fucking fast let's slow it down
Pivot(180, -0.72);
TurnSlewRate(TS);
StraightUntilSwitchTimeout(9, 1000);
FwdSquareWiggle();
ResetFacing(180);
// Drop the luggage
DumptruckServo(70);
Sleep(200);
DumptruckServo(180);
// Go to ticket light
Straight(-15);
Turn(135);
// Square with ticket light wall
StraightUntilSwitch(-11);
StraightUntilSwitch(-2);
ResetFacing(135);
Straight(2);
CaptureTicketLight();
if (robot.ticket_light_color == TICKET_LIGHT_BLUE) {
Straight(6);
Turn(90);
Straight(9);
PivotRight(0);
StraightUntilSwitchTimeout(8.5, 1000);
// Pivot to get into position for the center button
PivotRight(-37.5);
PivotLeft(0);
} else {
// TODO: I decreased this by 0.5 on Friday
Straight(9.25);
TurnSlewRate(TS * 0.5); // This turn is crazy fucking fast let's slow it down
Pivot(0, 0.72); // TODO: Decreased from 0.82 to 0.72 because it was too far right of white button
TurnSlewRate(TS);
StraightUntilSwitchTimeout(4, 1000);
// Pivot to get into position for the center button
PivotLeft(37.5);
PivotRight(0);
}
StraightTimeout(4, 2000);
// Dumptruck in place
DumptruckServo(10);
// TODO: I increased this by 100 on Friday
Sleep(600);
DumptruckServo(55);
Sleep(600);
DumptruckServo(10);
Sleep(600);
DumptruckServo(55);
// Get into place for passport
Straight(-2);
PassportServo(160);
PivotRight(-35);
DumptruckServo(0);
PivotLeft(0);
PivotRight(28);
StraightTimeout(4, 1000);
// Passport arm up
PassportServo(53);
Sleep(500);
PassportServo(83);
// use the dumptruck to hit the passport down
PivotRight(30);
PivotLeft(120);
Turn(180);
// Go toward ramp to go to end button
Straight(6);
DumptruckServo(180);
PivotLeft(90);
// Square with right side wall and turn
StraightUntilSwitchTimeout(11, 1200);
FwdSquarePush();
ResetFacing(90);
Straight(-1);
Turn(180);
// Go down right side ramp and hit the end button
Speed(100);
Straight(50);
}
[[noreturn]]
void odometry_task() {
static TickType_t time = 0;
while (true) {
robot.process_odometry();
vTaskDelayUntil(&time, 1000 / CONTROL_LOOP_HZ);
}
}
[[noreturn]]
void diagnostics_task() {
static TickType_t time = 0;
while (true) {
visualization::print_diagnostics();
vTaskDelayUntil(&time, 1000 / DIAGNOSTICS_HZ);
}
}
extern "C" void vApplicationGetIdleTaskMemory(StaticTask_t **ppxIdleTaskTCBBuffer,
StackType_t **ppxIdleTaskStackBuffer,
uint32_t *puxIdleTaskStackSize) {
static StaticTask_t xIdleTaskTCB;
static StackType_t uxIdleTaskStack[configMINIMAL_STACK_SIZE];
*ppxIdleTaskTCBBuffer = &xIdleTaskTCB;
*ppxIdleTaskStackBuffer = uxIdleTaskStack;
*puxIdleTaskStackSize = configMINIMAL_STACK_SIZE;
}
extern "C" void vApplicationGetTimerTaskMemory(StaticTask_t **ppxTimerTaskTCBBuffer,
StackType_t **ppxTimerTaskStackBuffer,
uint32_t *puxTimerTaskStackSize) {