-
Notifications
You must be signed in to change notification settings - Fork 0
/
performance-test1.cpp
302 lines (251 loc) · 7.02 KB
/
performance-test1.cpp
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
#include <FEHLCD.h>
#include <FEHUtility.h>
#include <FEHIO.h>
#include <FEHServo.h>
#include <FEHMotor.h>
#include <FEHRPS.h>
#define OPTOTHRESH 2.0
#define ENCODERINCH 20
//declarations for encoders (right encoder (re) / left encoder (le) )
DigitalEncoder re(FEHIO::P3_5);
DigitalEncoder le(FEHIO::P0_0);
//declaration for motors (right motor (rm) / left motor (lm) )
FEHMotor rm(FEHMotor::Motor1,9.0);
FEHMotor lm(FEHMotor::Motor0,9.0);
//declaration for CdS cell
AnalogInputPin cds(FEHIO::P2_1);
//declaration for optosensors (left optosensor (lo). . .)
DigitalInputPin lo(FEHIO::P1_0);
DigitalInputPin mo(FEHIO::P1_2);
DigitalInputPin ro(FEHIO::P1_4);
//function to stop motors
void stopMotors() {
lm.SetPercent(0);
rm.SetPercent(0);
Sleep(500);
}
//move function
void move(int percent, int counts) {
//reset encoder
re.ResetCounts();
le.ResetCounts();
//set motors to percent
rm.SetPercent(percent);
lm.SetPercent(percent);
//While the average of the left and right encoder is less than counts,
//keep running motors
while((le.Counts() + re.Counts()) / 2. < counts);
stopMotors();
}
//turn Right function
void turnRight (double angle) {
//reset encoder
re.ResetCounts();
le.ResetCounts();
//encoder count for 90 degrees turn
double leftFull = 135;
double rightFull = 115;
//encoder count for specified angle parameter
double actualLeft = (leftFull/90.0) * angle;
double actualRight = (rightFull/90.0) * angle;
//turn servo for specified angle
rm.SetPercent(-30);
lm.SetPercent(30);
while(le.Counts() < actualLeft && re.Counts() < actualRight){}
stopMotors();
}
//turn Left function
void turnLeft (double angle) {
//reset encoder
re.ResetCounts();
le.ResetCounts();
//sleep duration for 90 degrees turn
double leftFull = 115;
double rightFull = 135;
//sleep duration for specified angle parameter
double actualLeft = (leftFull/90) * angle;
double actualRight = (rightFull/90) * angle;
//turn servo for specified angle
rm.SetPercent(30);
lm.SetPercent(-30);
while(le.Counts() < actualLeft && re.Counts() < actualRight){}
stopMotors();
}
//Detect Light
int detectLight() {
double lightLevel = cds.Value();
int value;
if(lightLevel > 2.8) {
value = 0;
} else if (lightLevel > 1.8) {
value = 2;
} else if (lightLevel > 1.1) {
value = 3;
} else {
value = 1;
}
return value;
}
//Algoritm for jukebox on preformance test 1, to be edited for future use
void jukebox(int color) {
//Backup to line up center
turnLeft(90.0);
turnLeft(45.0);
move(-30, ENCODERINCH * 3);
turnRight(45.0);
move(-30, ENCODERINCH * 2);
//If color = 1, run for red, otherwise run for blue
if (color == 1) {
LCD.SetFontColor(RED);
LCD.FillRectangle(0, 0, 359, 239);
turnRight(45.0);
move(50, ENCODERINCH * 2 + 6);
turnLeft(47.0);
move(30, ENCODERINCH * 7);
move(-30, ENCODERINCH * 4);
turnLeft(95.0);
move(50, ENCODERINCH * 9 + 4);
} else {
LCD.SetFontColor(BLUE);
LCD.FillRectangle(0, 0, 359, 239);
turnLeft(45.0);
move(50, ENCODERINCH * 2 + 2);
turnRight(40.0);
move(30, ENCODERINCH * 7);
move(-30, ENCODERINCH * 4);
turnLeft(95.0);
move(50, ENCODERINCH * 5 + 4);
}
}
//followLine function for specified time in seconds
void followLine (double time) {
//state variable
int state = 0;
//time measure
float timeNow;
timeNow = TimeNow();
while(TimeNow()-timeNow < time) {
//line at left
if (lo.Value() > OPTOTHRESH && ro.Value() < OPTOTHRESH && mo.Value() < OPTOTHRESH) {
lm.SetPercent(0);
rm.SetPercent(20);
state = 1;
Sleep(100);
}else if (ro.Value() > OPTOTHRESH && lo.Value() < OPTOTHRESH && mo.Value() < OPTOTHRESH) {
//line at right
lm.SetPercent(20);
rm.SetPercent(0);
state = 2;
Sleep(100);
}else if (mo.Value() > OPTOTHRESH && lo.Value() < OPTOTHRESH && ro.Value() < OPTOTHRESH) {
//on line
lm.SetPercent(20);
rm.SetPercent(20);
Sleep(100);
} else if (lo.Value() > OPTOTHRESH && ro.Value() > OPTOTHRESH && mo.Value() > OPTOTHRESH) {
//on line
lm.SetPercent(20);
rm.SetPercent(20);
Sleep(100);
} else if (lo.Value() > OPTOTHRESH && ro.Value() < OPTOTHRESH && mo.Value() > OPTOTHRESH){
//line slightly at right
lm.SetPercent(20);
rm.SetPercent(0);
state = 2;
Sleep(100);
} else if (lo.Value() < OPTOTHRESH && ro.Value() > OPTOTHRESH && mo.Value() > OPTOTHRESH) {
//line slightly at left
lm.SetPercent(0);
rm.SetPercent(20);
state = 1;
Sleep(100);
} else {
//nothing detected
if(state == 0) {
lm.SetPercent(20);
rm.SetPercent(20);
Sleep(100);
} else if(state == 1) {
lm.SetPercent(0);
rm.SetPercent(20);
Sleep(100);
} else if(state == 2) {
lm.SetPercent(20);
rm.SetPercent(0);
Sleep(100);
}
}
}//end while loop
stopMotors();
}// end method
void testCDS() {
double time = TimeNow();
while(TimeNow() - time < 500){
LCD.Clear();
LCD.Write(cds.Value());
Sleep(500);
}
}
int main(void)
{
// //touch screen position variables
// float x,y;
//initialize the screen
LCD.Clear(FEHLCD::Black);
LCD.SetFontColor(FEHLCD::White);
// //wait for screen to be pressed
// while(!LCD.Touch(&x,&y));
// //wait for screen to be unpressed
// while(LCD.Touch(&x,&y));
// testCDS();
//wait for start light
// while(detectLight() == 0) {
// }
while(cds.Value() > 2.0) {}
//move forward for 10 inches
move(40, ENCODERINCH*10);
//turn left 42 degrees
turnLeft(42.0);
//move until detect light
move(40, ENCODERINCH*8);
//Detect Color for Jukebox button
int color = detectLight();
//Oscilate until a light is found
int backup = 2;
while(color == 0) {
move(10, backup);
backup = backup * -1 + 2;
}
//Run jukebox algorithm
jukebox(color);
//turn left towards ramp
turnLeft(90.0);
//move up and down ramp
move(50, ENCODERINCH * 25);
move(-30, ENCODERINCH * 25);
// //move forward or backward depending on detected light color
// //turn left 90 degrees
// turnLeft(90.0);
// //move forward to push button
// move(40, ENCODERINCH*3);
// //back up
// move(-40, ENCODERINCH*4);
// //turn left 90 degrees
// turnLeft(90.0);
// //move forward 4 inches
// move(40, ENCODERINCH*5);
// //turn left 90 degrees
// turnLeft(90.0);
// //move forward 30 inches
// move(40, ENCODERINCH*30);
// //turn 180 degrees left
// turnLeft(180.0);
// //move forward 30 inches
// move(40, ENCODERINCH*30);
// //cds value test code
// while (true) {
// LCD.Clear();
// LCD.Write(cds.Value());
// Sleep(0.3);
// }
}