-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAuto_SR
More file actions
1802 lines (1403 loc) · 37.8 KB
/
Copy pathAuto_SR
File metadata and controls
1802 lines (1403 loc) · 37.8 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
#pragma config(Hubs, S1, HTMotor, HTMotor, HTServo, none)
#pragma config(Sensor, S1, , sensorI2CMuxController)
#pragma config(Sensor, S2, GyroSensor, sensorI2CHiTechnicGyro)
#pragma config(Sensor, S3, IRSensor, sensorHiTechnicIRSeeker1200)
#pragma config(Motor, mtr_S1_C1_1, LeftMotor, tmotorTetrix, PIDControl)
#pragma config(Motor, mtr_S1_C1_2, RightMotor, tmotorTetrix, PIDControl, reversed)
#pragma config(Motor, mtr_S1_C2_1, GripperMotor, tmotorTetrix, openLoop)
#pragma config(Motor, mtr_S1_C2_2, LifterMotor, tmotorTetrix, openLoop, reversed, encoder)
#pragma config(Servo, srvo_S1_C3_1, LifterServo, tServoStandard)
#pragma config(Servo, srvo_S1_C3_2, GripperServo, tServoStandard)
#pragma config(Servo, srvo_S1_C3_3, ReleaseServo, tServoStandard)
#pragma config(Servo, srvo_S1_C3_4, GoalServo, tServoStandard)
#pragma config(Servo, srvo_S1_C3_5, HookServo, tServoStandard)
#pragma config(Servo, srvo_S1_C3_6, servo6, tServoNone)
//*!!Code automatically generated by 'ROBOTC' configuration wizard !!*//
/////////////////////////////////////////////////////////////////////////////////////////////////////
//
// Autonomous Mode Code Template
//
// This file contains a template for simplified creation of an autonomous program for an TETRIX robot
// competition.
//
// You need to customize two functions with code unique to your specific robot.
//
/////////////////////////////////////////////////////////////////////////////////////////////////////
#include "JoystickDriver.c" //Include file to "handle" the Bluetooth messages.
#define AUTONOMOUS_MODE_COMPILE 1
#define DEBUG_AUTONOMOUS_ROTATE 0
#define DEBUG_AUTONOMOUS_TRANSLATE 0
#define DEBUG_CALC_ANGLE 0
#include "hitechnic-irseeker-v2.h" //for IR seeker sensor
#define SERVO_STOP 134 //The power of the servo for it to be at a complete stop
#define SERVO_RUN 60 //The Power being put into the servos when moving
#define SAMPLE_PERIOD_MSEC 20
#define MIN_MOTOR_POWER_ROTATE 30
#define MIN_MOTOR_POWER_TRANSLATE 10//20
#define MIN_MOTOR_POWER_LIFT 45
#define WAY_POINT_1 9500 //8000 //in encoder counts
#define CHANGE_IN_MTR_PWR 5
#define T_CHANGE_IN_TIME 30 //30 //In Miliseconds
#define R_CHANGE_IN_TIME 10 //In Miliseconds
#define L_CHANGE_IN_TIME 30
#define R_BRAKING_RANGE 1
#define T_BRAKING_RANGE 5
#define L_BRAKING_RANGE 5
#define BRAKE_MTR_PWR -20 //-60
#define BRAKE_MTR_PWR_LIFT -10
#define BRAKE_TIME 60
#define ARRAY_INDEX 500
#define ROTATE 0
#define TRANSLATE 1
#define LIFT 2
#define ACCEL_STATE 1
#define A_CONST_MTR_PWR_STATE 2
#define DECEL_STATE 3
#define D_CONST_MTR_PWR_STATE 4
#define BRAKE_STATE 5
#define AT_TARGET_STATE 6
#define IDLE_STATE 7
#define T_DECEL_RANGE_FACTOR 200 //500
#define R_DECEL_RANGE_FACTOR 10
#define L_DECEL_RANGE_FACTOR 200
#define CM_ENCD_COUNTS 63
#define DIST_FROM_BUCKET 37
#define LNR_FULL_MTR_PWR 1
#define LNR_FOURTH_MTR_PWR 2
#define IR_RANGE_1 1370
#define IR_RANGE_2 1700
#define POSITION_1 2050
#define POSITION_2 1000
#define POSITION_3 1000
#define MAX_LIFTER_VALUE 14400
#define MIN_LIFTER_VALUE 95
#define TRANSLATE_MTR_INCREMENT 2
int TargetLifterPosition;
int MaxLifterMotorPower;
int GetLifterEncoderPosition();
int BaseMode;
int SecondsToDelay;
int Path;
int nButtonPresses = 0;
//byte Position;
//byte SelectedPosition;
//int SelectedPath;
int Position = 0;
float CurrentSample = 0;
float PreviousSample = 0;
byte MaxMotorPower = 0;
float TargetAngle = 0;
int TargetPosition = 0;
bool b_TrajectoryEnabled;
int TrajectoryMode;
int TrajectoryState;
float GyroRotation = 0;
int IRSensorValue;
float DegreesThisSample = 0;
float MeasuredAngle = 0;
int i = 0;
float Offset = 0;
#if DEBUG_AUTONOMOUS_ROTATE
int R_MtrPwrArray[ARRAY_INDEX];
float AngleArray[ARRAY_INDEX];
float RotationRateArray[ARRAY_INDEX];
int Index = 0;
#endif
#if DEBUG_AUTONOMOUS_TRANSLATE
int T_MtrPwrArray[ARRAY_INDEX];
int TrajectoryStateArray[ARRAY_INDEX];
int DTTPArray[ARRAY_INDEX];
int EncoderCountsArray[ARRAY_INDEX];
int T_Index = 0;
#endif
#if DEBUG_CALC_ANGLE
float DebugArray[ARRAY_INDEX];
float DebugArray2[ARRAY_INDEX];
int DebugArray3[ARRAY_INDEX];
int Index = 0;
#endif
int DirFactor;
int LeftMotorValue;
int Encoder_Array[ARRAY_INDEX];
int IR_Array[ARRAY_INDEX];
int IR_Index = 0;
void TranslateAbort();
void ChooseDelay();
void ChoosePath();
void CalibrateGyro();
void ResetGyro();
void ResetEncoders();
int GetEncoderPosition();
void Rotate(long SetTargetAngle, byte SetMaxMotorPower);
void Translate(int SetTargetPosition, byte SetMaxMotorPower);
void MoveSlides(int SetTargetPosition, byte SetMaxMotorPower);
void WaitForMoveToFinish();
void LiftHook();
void LowerGrabber();
void ScoreElements_Center();
void ScoreElements_Med();
task CalculateAngle()
{
while (true)
{
GyroRotation = SensorValue(GyroSensor);
CurrentSample = GyroRotation - Offset;
DegreesThisSample = (CurrentSample + PreviousSample)/2 * ((float)SAMPLE_PERIOD_MSEC/1000);
MeasuredAngle = MeasuredAngle + DegreesThisSample;
PreviousSample = CurrentSample;
#if DEBUG_CALC_ANGLE
if(Index < ARRAY_INDEX)
{
DebugArray[Index] = CurrentSample;
DebugArray2[Index] = MeasuredAngle;
DebugArray3[Index] = time1[T1];
Index++;
}
#endif
wait1Msec(SAMPLE_PERIOD_MSEC);
}
}
task TrajectoryGenerator()
{
int i;
float DistanceToTargetAngle = 0;
int DistanceToTargetPosition = 0;
int DistanceToTargetLifterPosition = 0;
float RotateDecelRange;
int TranslateDecelRange;
int LiftDecelRange;
while(true)
{
#if DEBUG_AUTONOMOUS_ROTATE
while(Index < 20)
{
if(Index < ARRAY_INDEX)
{
R_MtrPwrArray[Index] = motor[RightMotor];
AngleArray[Index] = MeasuredAngle;
RotationRateArray[Index] = CurrentSample;
Index++;
}
}
#endif
if(b_TrajectoryEnabled == true && TrajectoryMode == TRANSLATE)
{
///////////////////////////////////////////////
// AccelerateMode //
///////////////////////////////////////////////
TrajectoryState = ACCEL_STATE;
motor[RightMotor] = MIN_MOTOR_POWER_TRANSLATE*DirFactor;
motor[LeftMotor] = motor[RightMotor];
#if DEBUG_AUTONOMOUS_TRANSLATE
if(T_Index < ARRAY_INDEX)
{
T_MtrPwrArray[T_Index] = motor[RightMotor];
TrajectoryStateArray[T_Index] = TrajectoryState;
DTTPArray[T_Index] = DistanceToTargetPosition;
EncoderCountsArray[T_Index] = GetEncoderPosition();
T_Index++;
}
#endif
DistanceToTargetPosition = TargetPosition - GetEncoderPosition();
for(i=MIN_MOTOR_POWER_TRANSLATE;
i <= (MaxMotorPower - CHANGE_IN_MTR_PWR)
&& ((DistanceToTargetPosition*DirFactor) > ((TargetPosition/2)*DirFactor))
&& (b_TrajectoryEnabled == true);
i = i + CHANGE_IN_MTR_PWR)
{
motor[RightMotor] = i*DirFactor;
motor[LeftMotor] = motor[RightMotor];
wait1Msec(T_CHANGE_IN_TIME);
DistanceToTargetPosition = TargetPosition - GetEncoderPosition();
#if DEBUG_AUTONOMOUS_TRANSLATE
if(T_Index < ARRAY_INDEX)
{
T_MtrPwrArray[T_Index] = motor[RightMotor];
TrajectoryStateArray[T_Index] = TrajectoryState;
DTTPArray[T_Index] = DistanceToTargetPosition;
EncoderCountsArray[T_Index] = GetEncoderPosition();
T_Index++;
}
#endif
}
TranslateDecelRange = GetEncoderPosition() + (DirFactor*T_DECEL_RANGE_FACTOR);
///////////////////////////////////////////////
// Constant Motor Power After Acceleration //
///////////////////////////////////////////////
TrajectoryState = A_CONST_MTR_PWR_STATE;
while((DistanceToTargetPosition*DirFactor) > (TranslateDecelRange*DirFactor)
&& (b_TrajectoryEnabled == true))
{
motor[RightMotor] = i*DirFactor;
if(nMotorEncoder(LeftMotor) < nMotorEncoder(RightMotor))
{
motor[LeftMotor] = motor[RightMotor] + TRANSLATE_MTR_INCREMENT;
LeftMotorValue = motor[LeftMotor];
}
else if(nMotorEncoder(LeftMotor) > nMotorEncoder(RightMotor))
{
motor[LeftMotor] = motor[RightMotor] - TRANSLATE_MTR_INCREMENT;
LeftMotorValue = motor[LeftMotor];
}
else
{
motor[LeftMotor] = motor[RightMotor];
LeftMotorValue = motor[LeftMotor];
}
wait1Msec(T_CHANGE_IN_TIME);
DistanceToTargetPosition = TargetPosition - GetEncoderPosition();
#if DEBUG_AUTONOMOUS_TRANSLATE
if(T_Index < ARRAY_INDEX)
{
T_MtrPwrArray[T_Index] = motor[RightMotor];
TrajectoryStateArray[T_Index] = TrajectoryState;
DTTPArray[T_Index] = DistanceToTargetPosition;
EncoderCountsArray[T_Index] = GetEncoderPosition();
T_Index++;
}
#endif
}
///////////////////////////////////////////////
// Decelerate //
///////////////////////////////////////////////
TrajectoryState = DECEL_STATE;
for(;
(i > MIN_MOTOR_POWER_TRANSLATE) && ((DistanceToTargetPosition*DirFactor) > T_BRAKING_RANGE)
&& (b_TrajectoryEnabled == true);
i = i - CHANGE_IN_MTR_PWR
)
{
motor[RightMotor] = i*DirFactor;
motor[LeftMotor] = motor[RightMotor];
wait1Msec(T_CHANGE_IN_TIME);
DistanceToTargetPosition = TargetPosition - GetEncoderPosition();
#if DEBUG_AUTONOMOUS_TRANSLATE
if(T_Index < ARRAY_INDEX)
{
T_MtrPwrArray[T_Index] = motor[RightMotor];
TrajectoryStateArray[T_Index] = TrajectoryState;
DTTPArray[T_Index] = DistanceToTargetPosition;
EncoderCountsArray[T_Index] = GetEncoderPosition();
T_Index++;
}
#endif
}
///////////////////////////////////////////////
// Constant Motor Power After Deceleration //
///////////////////////////////////////////////
TrajectoryState = D_CONST_MTR_PWR_STATE;
while(((DistanceToTargetPosition*DirFactor) > T_BRAKING_RANGE)
&& (b_TrajectoryEnabled == true))
{
wait1Msec(T_CHANGE_IN_TIME);
DistanceToTargetPosition = TargetPosition - GetEncoderPosition();
#if DEBUG_AUTONOMOUS_TRANSLATE
if(T_Index < ARRAY_INDEX)
{
T_MtrPwrArray[T_Index] = motor[RightMotor];
TrajectoryStateArray[T_Index] = TrajectoryState;
DTTPArray[T_Index] = DistanceToTargetPosition;
EncoderCountsArray[T_Index] = GetEncoderPosition();
T_Index++;
}
#endif
}
///////////////////////////////////////////////
// Brake //
///////////////////////////////////////////////
TrajectoryState = BRAKE_STATE;
motor[RightMotor] = BRAKE_MTR_PWR*DirFactor;
motor[LeftMotor] = motor[RightMotor];
wait1Msec(BRAKE_TIME);
DistanceToTargetPosition = TargetPosition - GetEncoderPosition();
#if DEBUG_AUTONOMOUS_TRANSLATE
if(T_Index < ARRAY_INDEX)
{
T_MtrPwrArray[T_Index] = motor[RightMotor];
TrajectoryStateArray[T_Index] = TrajectoryState;
DTTPArray[T_Index] = DistanceToTargetPosition;
EncoderCountsArray[T_Index] = GetEncoderPosition();
T_Index++;
}
#endif
///////////////////////////////////////////////
// At Target Position //
///////////////////////////////////////////////
TrajectoryState = AT_TARGET_STATE;
motor[RightMotor] = 0;
motor[LeftMotor] = 0;
b_TrajectoryEnabled = false;
#if DEBUG_AUTONOMOUS_TRANSLATE
if(T_Index < ARRAY_INDEX)
{
T_MtrPwrArray[T_Index] = motor[RightMotor];
TrajectoryStateArray[T_Index] = TrajectoryState;
DTTPArray[T_Index] = DistanceToTargetPosition;
EncoderCountsArray[T_Index] = GetEncoderPosition();
T_Index++;
}
#endif
}
else if(b_TrajectoryEnabled == true && TrajectoryMode == ROTATE)
{
motor[LeftMotor] = MIN_MOTOR_POWER_ROTATE*DirFactor;
motor[RightMotor] = -motor[LeftMotor];
clearTimer(T1);
#if DEBUG_AUTONOMOUS_ROTATE
if(Index < ARRAY_INDEX)
{
R_MtrPwrArray[Index] = motor[RightMotor];
AngleArray[Index] = MeasuredAngle;
RotationRateArray[Index] = CurrentSample;
Index++;
}
#endif
DistanceToTargetAngle = TargetAngle - MeasuredAngle;
///////////////////////////////////////////////
// Accelerate //
///////////////////////////////////////////////
TrajectoryState = ACCEL_STATE;
for(i=MIN_MOTOR_POWER_ROTATE;
i <= (MaxMotorPower - CHANGE_IN_MTR_PWR)
&& ((DistanceToTargetAngle*DirFactor) > (TargetAngle/2*DirFactor))
&& (b_TrajectoryEnabled == true);
i = i + CHANGE_IN_MTR_PWR)
{
motor[LeftMotor] = i*DirFactor;
motor[RightMotor] = -motor[LeftMotor];
wait1Msec(R_CHANGE_IN_TIME);
DistanceToTargetAngle = TargetAngle - MeasuredAngle;
#if DEBUG_AUTONOMOUS_ROTATE
if(Index < ARRAY_INDEX)
{
R_MtrPwrArray[Index] = motor[RightMotor];
AngleArray[Index] = MeasuredAngle;
RotationRateArray[Index] = CurrentSample;
Index++;
}
#endif
}
RotateDecelRange = MeasuredAngle + (DirFactor*R_DECEL_RANGE_FACTOR);
///////////////////////////////////////////////
// Constant Motor Power After Acceleration //
///////////////////////////////////////////////
TrajectoryState = A_CONST_MTR_PWR_STATE;
while((DistanceToTargetAngle*DirFactor) > (RotateDecelRange*DirFactor)
&& (b_TrajectoryEnabled == true))
{
motor[LeftMotor] = i*DirFactor;
motor[RightMotor] = -motor[LeftMotor];
wait1Msec(R_CHANGE_IN_TIME);
DistanceToTargetAngle = TargetAngle - MeasuredAngle;
#if DEBUG_AUTONOMOUS_ROTATE
if(Index < ARRAY_INDEX)
{
R_MtrPwrArray[Index] = motor[RightMotor];
AngleArray[Index] = MeasuredAngle;
RotationRateArray[Index] = CurrentSample;
Index++;
}
#endif
}
///////////////////////////////////////////////
// Decelerate //
///////////////////////////////////////////////
TrajectoryState = DECEL_STATE;
for(;
(i > MIN_MOTOR_POWER_ROTATE)
&& ((DistanceToTargetAngle*DirFactor) > R_BRAKING_RANGE)
&& (b_TrajectoryEnabled == true);
i = i - CHANGE_IN_MTR_PWR)
{
motor[LeftMotor] = i*DirFactor;
motor[RightMotor] = -motor[LeftMotor];
wait1Msec(R_CHANGE_IN_TIME);
DistanceToTargetAngle = TargetAngle - MeasuredAngle;
#if DEBUG_AUTONOMOUS_ROTATE
if(Index < ARRAY_INDEX)
{
R_MtrPwrArray[Index] = motor[RightMotor];
AngleArray[Index] = MeasuredAngle;
RotationRateArray[Index] = CurrentSample;
Index++;
}
#endif
}
///////////////////////////////////////////////
// Constant Motor Power After Deceleration //
///////////////////////////////////////////////
TrajectoryState = D_CONST_MTR_PWR_STATE;
while((DistanceToTargetAngle*DirFactor) > R_BRAKING_RANGE
&& (b_TrajectoryEnabled == true))
{
wait1Msec(R_CHANGE_IN_TIME);
DistanceToTargetAngle = TargetAngle - MeasuredAngle;
#if DEBUG_AUTONOMOUS_ROTATE
if(Index < ARRAY_INDEX)
{
R_MtrPwrArray[Index] = motor[RightMotor];
AngleArray[Index] = MeasuredAngle;
RotationRateArray[Index] = CurrentSample;
Index++;
}
#endif
}
///////////////////////////////////////////////
// Brake //
///////////////////////////////////////////////
TrajectoryState = BRAKE_STATE;
motor[LeftMotor] = BRAKE_MTR_PWR*DirFactor;
motor[RightMotor] = -motor[LeftMotor];
wait1Msec(BRAKE_TIME);
DistanceToTargetAngle = TargetAngle - MeasuredAngle;
#if DEBUG_AUTONOMOUS_ROTATE
if(Index < ARRAY_INDEX)
{
R_MtrPwrArray[Index] = motor[RightMotor];
AngleArray[Index] = MeasuredAngle;
RotationRateArray[Index] = CurrentSample;
Index++;
}
#endif
///////////////////////////////////////////////
// At Target Position //
///////////////////////////////////////////////
TrajectoryState = AT_TARGET_STATE;
motor[RightMotor] = 0;
motor[LeftMotor] = 0;
b_TrajectoryEnabled = false;
#if DEBUG_AUTONOMOUS_ROTATE
if(Index < ARRAY_INDEX)
{
R_MtrPwrArray[Index] = motor[RightMotor];
AngleArray[Index] = MeasuredAngle;
RotationRateArray[Index] = CurrentSample;
Index++;
}
#endif
}
else if(b_TrajectoryEnabled == true && TrajectoryMode == LIFT)
{
///////////////////////////////////////////////
// AccelerateMode //
///////////////////////////////////////////////
TrajectoryState = ACCEL_STATE;
motor[LifterMotor] = MIN_MOTOR_POWER_LIFT*DirFactor;
DistanceToTargetLifterPosition = TargetLifterPosition - GetLifterEncoderPosition();
for(i=MIN_MOTOR_POWER_LIFT;
i <= (MaxMotorPower - CHANGE_IN_MTR_PWR)
&& ((DistanceToTargetLifterPosition*DirFactor) > ((TargetLifterPosition/2)*DirFactor))
&& (b_TrajectoryEnabled == true);
i = i + CHANGE_IN_MTR_PWR)
{
motor[LifterMotor] = i*DirFactor;
wait1Msec(L_CHANGE_IN_TIME);
DistanceToTargetLifterPosition = TargetLifterPosition - GetLifterEncoderPosition();
}
LiftDecelRange = GetLifterEncoderPosition() + (DirFactor*L_DECEL_RANGE_FACTOR);
///////////////////////////////////////////////
// Constant Motor Power After Acceleration //
///////////////////////////////////////////////
TrajectoryState = A_CONST_MTR_PWR_STATE;
while((DistanceToTargetLifterPosition*DirFactor) > (LiftDecelRange*DirFactor)
&& (b_TrajectoryEnabled == true))
{
if(DirFactor == 1)
{
motor[LifterMotor] = (i*DirFactor) + 90;
}
else
{
motor[LifterMotor] = i*DirFactor;
}
wait1Msec(L_CHANGE_IN_TIME);
DistanceToTargetLifterPosition = TargetLifterPosition - GetLifterEncoderPosition();
}
///////////////////////////////////////////////
// Decelerate //
///////////////////////////////////////////////
TrajectoryState = DECEL_STATE;
for(;
(i > MIN_MOTOR_POWER_LIFT) && ((DistanceToTargetLifterPosition*DirFactor) > L_BRAKING_RANGE)
&& (b_TrajectoryEnabled == true);
i = i - CHANGE_IN_MTR_PWR)
{
motor[LifterMotor] = i*DirFactor;
wait1Msec(L_CHANGE_IN_TIME);
DistanceToTargetLifterPosition = TargetLifterPosition - GetLifterEncoderPosition();
}
///////////////////////////////////////////////
// Constant Motor Power After Deceleration //
///////////////////////////////////////////////
TrajectoryState = D_CONST_MTR_PWR_STATE;
while(((DistanceToTargetLifterPosition*DirFactor) > L_BRAKING_RANGE)
&& (b_TrajectoryEnabled == true))
{
wait1Msec(L_CHANGE_IN_TIME);
DistanceToTargetLifterPosition = TargetLifterPosition - GetLifterEncoderPosition();
}
///////////////////////////////////////////////
// Brake //
///////////////////////////////////////////////
TrajectoryState = BRAKE_STATE;
motor[LifterMotor] = BRAKE_MTR_PWR_LIFT*DirFactor;
wait1Msec(BRAKE_TIME);
DistanceToTargetLifterPosition = TargetLifterPosition - GetLifterEncoderPosition();
///////////////////////////////////////////////
// At Target Position //
///////////////////////////////////////////////
TrajectoryState = AT_TARGET_STATE;
motor[LifterMotor] = 6;
b_TrajectoryEnabled = false;
}
else //b_TrajectoryEnabled == false
{
TrajectoryState = IDLE_STATE;
#if DEBUG_AUTONOMOUS_ROTATE
if(Index < ARRAY_INDEX)
{
R_MtrPwrArray[Index] = motor[RightMotor];
AngleArray[Index] = MeasuredAngle;
RotationRateArray[Index] = CurrentSample;
Index++;
}
#endif
#if DEBUG_AUTONOMOUS_TRANSLATE
if(T_Index < ARRAY_INDEX)
{
T_MtrPwrArray[T_Index] = motor[RightMotor];
TrajectoryStateArray[T_Index] = TrajectoryState;
DTTPArray[T_Index] = DistanceToTargetPosition;
EncoderCountsArray[T_Index] = GetEncoderPosition();
T_Index++;
}
#endif
wait1Msec(T_CHANGE_IN_TIME);
}
}
}
/////////////////////////////////////////////////////////////////////////////////////////////////////
//
// initializeRobot
//
// Prior to the start of autonomous mode, you may want to perform some initialization on your robot.
// Things that might be performed during initialization include:
// 1. Move motors and servos to a preset position.
// 2. Some sensor types take a short while to reach stable values during which time it is best that
// robot is not moving. For example, gyro sensor needs a few seconds to obtain the background
// "bias" value.
//
// In many cases, you may not have to add any code to this function and it will remain "empty".
//
/////////////////////////////////////////////////////////////////////////////////////////////////////
void initializeRobot()
{
ResetGyro();
CalibrateGyro();
nMotorEncoder(LifterMotor) = 0;
servo(LifterServo) = 33; //Starting Value
servo(GoalServo) = 255;
servo(GripperServo) = 142;
servo(ReleaseServo) = 159;
servo(HookServo) = 0;
motor[LeftMotor] = 0;
motor[RightMotor] = 0;
BaseMode = LNR_FULL_MTR_PWR;
ChooseDelay();
ChoosePath();
return;
}
/////////////////////////////////////////////////////////////////////////////////////////////////////
//
// Main Task
//
// The following is the main code for the autonomous robot operation. Customize as appropriate for
// your specific robot.
//
// The types of things you might do during the autonomous phase (for the 2008-9 FTC competition)
// are:
//
// 1. Have the robot follow a line on the game field until it reaches one of the puck storage
// areas.
// 2. Load pucks into the robot from the storage bin.
// 3. Stop the robot and wait for autonomous phase to end.
//
// This simple template does nothing except play a periodic tone every few seconds.
//
// At the end of the autonomous period, the FMS will autonmatically abort (stop) execution of the program.
//
/////////////////////////////////////////////////////////////////////////////////////////////////////
task main()
{
initializeRobot();
waitForStart(); // Wait for the beginning of autonomous phase.
clearTimer(T1);
clearDebugStream();
startTask(CalculateAngle, 255);
startTask(TrajectoryGenerator, 90);
wait1Msec(SecondsToDelay);
switch (Path)
{
case 1:
Translate(-2500, 40); //-4100
WaitForMoveToFinish();
wait1Msec(500);
Rotate(78, 30); //Turn Right
WaitForMoveToFinish();
wait1Msec(1000);
Translate(-2800, 10); //20
while(b_TrajectoryEnabled == true)
{
Position = nMotorEncoder(RightMotor);
IRSensorValue = SensorValue[IRSensor];
if(IR_Index < ARRAY_INDEX)
{
Encoder_Array[IR_Index] = nMotorEncoder[RightMotor];
IR_Array[IR_Index] = SensorValue[IRSensor];
IR_Index++;
}
if(IRSensorValue == 7 || IRSensorValue == 8)
{
TranslateAbort();
break;
}
else
{
wait1Msec(20);
}
}
WaitForMoveToFinish();
wait1Msec(500);
if(abs(Position) > 2600)
{
//Do nothing
}
if(abs(Position) <= IR_RANGE_1)
{
if(abs(Position) < 1050)
{
Translate(755, 30);
WaitForMoveToFinish();
}
else if(abs(Position) > 1600)
{
Translate(1455, 30);
WaitForMoveToFinish();
}
else if(abs(Position) > 1500)
{
Translate(1255, 30);
WaitForMoveToFinish();
}
else
{
Translate(730, 30);
WaitForMoveToFinish();
}
wait1Msec(200);
Rotate(-55, 30);//84
WaitForMoveToFinish();
wait1Msec(500);
LiftHook();
wait1Msec(500);
Translate(-1200, 30);
WaitForMoveToFinish();
Rotate(-25, 30);//84
WaitForMoveToFinish();
wait1Msec(500);
Translate(1700, 50);
WaitForMoveToFinish();
}
else if(abs(Position) <= IR_RANGE_2)
{
Translate(-500, 20);
WaitForMoveToFinish();
wait1Msec(500);
Rotate(-78, 30);
WaitForMoveToFinish();
wait1Msec(500);
Translate(-960, 30); //910
WaitForMoveToFinish();
LiftHook();
Rotate(-35, 20);//84
WaitForMoveToFinish();
wait1Msec(200);
Translate(-350, 20); //600
WaitForMoveToFinish();
Rotate(-25, 20);//84
WaitForMoveToFinish();
wait1Msec(200);
Translate(1300, 30);
WaitForMoveToFinish();
}
else //Position > IR_RANGE_2 (IR_RANGE_3)
{
Translate(-780, 30);
WaitForMoveToFinish();
wait1Msec(200);
Rotate(-79, 30); //Turn Left
WaitForMoveToFinish();
wait1Msec(200);
if(abs(Position) >= 2280)
{
Translate(-1500, 30);
WaitForMoveToFinish();
}
else if(abs(Position) >= 2260)
{
Translate(-1535, 30);
WaitForMoveToFinish();
}
else
{
Translate(-1560, 30);
WaitForMoveToFinish();
}
wait1Msec(200);
Rotate(-55, 30);
WaitForMoveToFinish();
LiftHook();
wait1Msec(200);
Translate(-950, 30);
WaitForMoveToFinish();
wait1Msec(200);
Rotate(-25, 30);
WaitForMoveToFinish();
wait1Msec(200);
Translate(1500, 40);
WaitForMoveToFinish();
}
break;
case 2:
//Score in Center Goal
Translate(-2500, 40); //-4100
WaitForMoveToFinish();
wait1Msec(500);
Rotate(79, 40); //Turn Right
WaitForMoveToFinish();
wait1Msec(1000);
Translate(-2800, 10); //20
while(b_TrajectoryEnabled == true)
{
Position = (nMotorEncoder(RightMotor)- 20);
IRSensorValue = SensorValue[IRSensor];
if(IR_Index < ARRAY_INDEX)
{
Encoder_Array[IR_Index] = nMotorEncoder[RightMotor];
IR_Array[IR_Index] = SensorValue[IRSensor];
IR_Index++;
}
if(IRSensorValue == 7 || IRSensorValue == 8)
{
TranslateAbort();
break;
}
else
{
wait1Msec(20);
}
}
WaitForMoveToFinish();
wait1Msec(200);
if(abs(Position) > 2700)
{
//Do nothing
}
else if(abs(Position) <= IR_RANGE_1)
{
if(abs(Position) > 1500)
{
Translate((POSITION_1 + 400), 30);
WaitForMoveToFinish();
}
else
{
Translate(POSITION_1, 30);
WaitForMoveToFinish();
}
wait1Msec(200);
Rotate(77, 40);
WaitForMoveToFinish();
wait1Msec(200);
Translate(1620, 40);
WaitForMoveToFinish();
wait1Msec(200);
Rotate(-77, 40);
WaitForMoveToFinish();
wait1Msec(200);
Translate(-250, 10);
WaitForMoveToFinish();
wait1Msec(200);
ScoreElements_Center();
wait1Msec(200);
Translate(250, 20);
WaitForMoveToFinish();
wait1Msec(200);
Rotate(-77, 40);
WaitForMoveToFinish();
wait1Msec(200);
Translate(1500, 40);
WaitForMoveToFinish();
wait1Msec(200);
Rotate(78, 40);
WaitForMoveToFinish();
wait1Msec(200);
/*if(abs(Position) > 1500)
{
Translate(-(POSITION_1 - 800), 30);
WaitForMoveToFinish();
}
else