-
Notifications
You must be signed in to change notification settings - Fork 1
/
mission9_safety_factor.ts
207 lines (160 loc) · 6.07 KB
/
mission9_safety_factor.ts
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
// Add your code here
function mission9_safety_factor() {
motors.largeB.reset();
motors.largeC.reset();
// Drive forward
motors.largeBC.steer(5, 33, 4.65, MoveUnit.Rotations);
// linefollow(10, 250);
motors.largeBC.tank(10, -10, .25, MoveUnit.Rotations);
runtowhite();
motors.largeBC.tank(0, 20, .5, MoveUnit.Rotations);
//linefollow();
linefollow_test(10, 0, 0, 0, -.2, .02, .66);
let basePower = 10;
// Reverse
motors.largeBC.tank(-basePower, -basePower, .45, MoveUnit.Rotations);
// Rotate right
motors.largeBC.tank(basePower, -basePower, .38, MoveUnit.Rotations);
// Forward
motors.largeBC.tank(basePower, basePower, .4, MoveUnit.Rotations);
// Rotate left
motors.largeBC.tank(-basePower, basePower, .25, MoveUnit.Rotations);
// Forward
motors.largeBC.tank(basePower, basePower, .25, MoveUnit.Rotations);
// Rotate right
motors.largeBC.tank(basePower, -basePower, .435, MoveUnit.Rotations);
// Forward
motors.largeBC.tank(basePower, basePower, .6, MoveUnit.Rotations);
// Rotate left
motors.largeBC.tank(-basePower, basePower, .375, MoveUnit.Rotations);
// Return to base
motors.largeBC.steer(0, -75, 7, MoveUnit.Rotations);
}
function test2(
basePower: number = 10
): void {
// Reset BC sensors
motors.largeB.reset();
motors.largeC.reset();
const WHITE_THRESHOLD = 90;
const Kp = -.1;
const Ki = .005;
const CENTER_READING = 60;
let speed1 = 40;
motors.largeBC.setBrake(false);
motors.largeBC.tank(speed1, speed1 - 2, 2, MoveUnit.Rotations);
motors.largeBC.stop();
// Steer Left Off the wall
motors.largeBC.steer(-100, 20, .6, MoveUnit.Rotations);
motors.largeBC.tank(speed1, speed1, 2.5, MoveUnit.Rotations);
// Drive till both sensors are white
let powerLeft = basePower;
let powerRight = basePower;
motors.largeBC.tank(powerLeft, powerRight);
while (true) {
let leftLightValue = 100 * sensors.color2.light(LightIntensityMode.Reflected) / LEFT_MAX;
let rightLightValue = 100 * sensors.color3.light(LightIntensityMode.Reflected) / RIGHT_MAX;
if (leftLightValue >= WHITE_THRESHOLD) {
powerLeft = 0;
}
if (rightLightValue >= WHITE_THRESHOLD) {
powerRight = 0;
}
motors.largeBC.tank(powerLeft, powerRight);
if (powerLeft == 0 && powerRight == 0) {
break;
}
pause(5);
if (exitFlag) {
exit();
return;
};
}
motors.largeBC.stop();
// steer right before line follow
motors.largeBC.steer(0, speed1, .5, MoveUnit.Rotations);
motors.largeBC.tank(basePower, -basePower, .25, MoveUnit.Rotations);
motors.largeBC.tank(1.5 * basePower, 1.5 * basePower * .25);
while (true) {
let leftLightValue = 100 * sensors.color2.light(LightIntensityMode.Reflected) / LEFT_MAX;
let rightLightValue = 100 * sensors.color3.light(LightIntensityMode.Reflected) / RIGHT_MAX;
if (rightLightValue >= WHITE_THRESHOLD) {
break
}
pause(5);
if (exitFlag) {
exit();
return;
};
}
motors.largeBC.steer(0, 10, .2, MoveUnit.Rotations);
motors.largeBC.stop();
let integralLeft = 0;
let integralRight = 0;
motors.largeBC.tank(basePower, basePower);
while (true) {
let leftLightValue = 100 * sensors.color2.light(LightIntensityMode.Reflected) / LEFT_MAX;
let rightLightValue = 100 * sensors.color3.light(LightIntensityMode.Reflected) / RIGHT_MAX;
if (rightLightValue >= WHITE_THRESHOLD && leftLightValue >= WHITE_THRESHOLD) {
break;
}
brick.showValue("right 0-100:", rightLightValue, 1)
brick.showValue("left 0-100:", leftLightValue, 2)
let errorLeft = (CENTER_READING - leftLightValue);
let errorRight = (CENTER_READING - rightLightValue);
integralLeft = .66 * integralLeft + errorLeft;
integralRight = .66 * integralRight + errorRight;
let leftAdjustment = Kp * errorLeft + Ki * integralLeft;
let rightAdjustment = Kp * errorRight + Ki * integralRight;
let leftPower = basePower + leftAdjustment;
let rightPower = basePower + rightAdjustment;
brick.showValue("rightAdj", rightAdjustment, 3)
brick.showValue("leftAdj", leftAdjustment, 4)
motors.largeBC.tank(leftPower, rightPower);
pause(5);
if (exitFlag) {
exit();
return;
};
}
motors.largeBC.setBrake(true);
motors.largeBC.stop();
pause(500)
// // Move forward to end
// let powerLeft = 3;
// let powerRight = 3;
// motors.largeBC.tank(powerLeft, powerRight);
// while (true) {
// let leftLightValue = 100 * sensors.color1.light(LightIntensityMode.Reflected) / LEFT_MAX;
// let rightLightValue = 100 * sensors.color2.light(LightIntensityMode.Reflected) / RIGHT_MAX;
// if (leftLightValue <= CENTER_READING + 10) {
// powerLeft = 0;
// }
// if (rightLightValue <= CENTER_READING + 10) {
// powerRight = 0;
// }
// motors.largeBC.tank(powerLeft, powerRight);
// if (powerLeft == 0 && powerRight == 0) {
// break;
// }
// pause(5)
// }
// motors.largeBC.stop();
// pause(500)
// Reverse
motors.largeBC.tank(-basePower, -basePower, .46, MoveUnit.Rotations);
// Rotate right
motors.largeBC.tank(basePower, -basePower, .38, MoveUnit.Rotations);
// Forward
motors.largeBC.tank(basePower, basePower, .4, MoveUnit.Rotations);
// Rotate left
motors.largeBC.tank(-basePower, basePower, .27, MoveUnit.Rotations);
// Forward
motors.largeBC.tank(basePower, basePower, .46, MoveUnit.Rotations);
// Rotate right
motors.largeBC.tank(basePower, -basePower, .47, MoveUnit.Rotations);
// Forward
motors.largeBC.tank(basePower, basePower, .6, MoveUnit.Rotations);
// Rotate left
motors.largeBC.tank(-basePower, basePower, .28, MoveUnit.Rotations);
}