forked from AlexMaxseiner/FTC-Team4592-2012
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDefense(Safe).c
149 lines (147 loc) · 4.86 KB
/
Defense(Safe).c
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
#pragma config(Hubs, S3, HTMotor, none, none, none)
#pragma config(Sensor, S3, , sensorI2CMuxController)
#pragma config(Motor, mtr_S3_C1_1, leftDrive, tmotorTetrix, openLoop, reversed, encoder)
#pragma config(Motor, mtr_S3_C1_2, rightDrive, tmotorTetrix, openLoop, encoder)
//*!!Code automatically generated by 'ROBOTC' configuration wizard !!*//
#include "JoystickDriver.c" //Include file to "handle" the Bluetooth messages.
void forward(float distance);
void backward(float distance);
void initializeRobot();
task sudoMain();
task originalPath()
{
forward(400);
StopTask(sudoMain);
}
task defend() {
StopTask(sudoMain);
motor[leftDrive] = 0;
motor[rightDrive] = 0;
nMotorEncoder[leftDrive] = 0;
nMotorEncoder[rightDrive] = 0;
backward(10);
StartTask(sudoMain);
}
task sudoMain()
{
StopTask(defend);
const float CRASH_THRESHOLD = 0.75; // 70% - 80% works
ClearTimer(T1);
ClearTimer(T2);
ClearTimer(T3);
ClearTimer(T4);
nMotorEncoder[leftDrive] = 0;
nMotorEncoder[rightDrive] = 0;
StartTask(originalPath);
wait1Msec(1000);
while(true)
{
float averageMotorSpeed = abs((motor[leftDrive] + motor[rightDrive]) / 2.0);
float expectedValue = (-0.1801*(averageMotorSpeed*averageMotorSpeed)) + (43.294*(averageMotorSpeed));
float actualValue = (abs(nMotorEncoder[leftDrive]) + abs(nMotorEncoder[rightDrive])) / 2.0;
if(actualValue < (expectedValue * CRASH_THRESHOLD))
{
StopTask(originalPath);
StartTask(defend);
break;
}
nMotorEncoder[leftDrive] = 0;
nMotorEncoder[rightDrive] = 0;
wait1Msec(1000);
}
while(true) { }
}
task main() {
initializeRobot();
//waitForStart();
StartTask(sudoMain);
while(true){}
}
void forward(float distance) {//forward function to pass in a length in inches and then it goes that far
const float CHANGE = 2;
float totalTraveled = 0;
float encoderTarget = (169.92*distance) - 55.875; //calculate the encoder target
nMotorEncoder[rightDrive] = 0;
nMotorEncoder[leftDrive] = 0;
float leftEncoder;
float rightEncoder;
float leftPower = 50;
float rightPower = 50;
while(abs(totalTraveled) < abs(encoderTarget))
{
leftEncoder = nMotorEncoder[leftDrive];
rightEncoder = nMotorEncoder[rightDrive];
if(leftEncoder > rightEncoder)//changes based on which one has traveled farther
{
leftPower -= CHANGE;
rightPower += CHANGE;
}
else if(leftEncoder < rightEncoder)//same thing
{
leftPower += CHANGE;
rightPower -= CHANGE;
}
motor[leftDrive] = leftPower;
motor[rightDrive] = rightPower;
totalTraveled += (leftEncoder + rightEncoder)/ 2.0;
nMotorEncoder[rightDrive] = 0;
nMotorEncoder[leftDrive] = 0;
ClearTimer(T1);
while(time1[T1] < 200) {
leftEncoder = nMotorEncoder[leftDrive];
rightEncoder = nMotorEncoder[rightDrive];
if(!((abs(totalTraveled + (leftEncoder + rightEncoder)/ 2.0)) < abs(encoderTarget)))
{
totalTraveled += (nMotorEncoder[leftDrive] + nMotorEncoder[rightDrive])/ 2.0;
break;
}
}
}
}
void backward(float distance) {
const float CHANGE = 2;
float totalTraveled = 0;
float encoderTarget = (169.92*distance) - 55.875;
nMotorEncoder[rightDrive] = 0;
nMotorEncoder[leftDrive] = 0;
float leftEncoder;
float rightEncoder;
float leftPower = -50;
float rightPower = -50;
while(abs(totalTraveled) < abs(encoderTarget))
{
leftEncoder = abs(nMotorEncoder[leftDrive]);
rightEncoder = abs(nMotorEncoder[rightDrive]);
if(leftEncoder > rightEncoder)
{
leftPower -= CHANGE;
rightPower += CHANGE;
}
else if(leftEncoder < rightEncoder)
{
leftPower += CHANGE;
rightPower -= CHANGE;
}
motor[leftDrive] = leftPower;
motor[rightDrive] = rightPower;
totalTraveled += (leftEncoder + rightEncoder)/ 2.0;
nMotorEncoder[rightDrive] = 0;
nMotorEncoder[leftDrive] = 0;
ClearTimer(T1);
while(time1[T1] < 200) {
leftEncoder = nMotorEncoder[leftDrive];
rightEncoder = nMotorEncoder[rightDrive];
if(!((abs(totalTraveled + (leftEncoder + rightEncoder)/ 2.0)) < abs(encoderTarget)))
{
totalTraveled += (nMotorEncoder[leftDrive] + nMotorEncoder[rightDrive])/ 2.0;
break;
}
}
}
}
void initializeRobot()
{
// Place code here to sinitialize servos to starting positions.
// Sensors are automatically configured and setup by ROBOTC. They may need a brief time to stabilize.
return;
}