-
Notifications
You must be signed in to change notification settings - Fork 0
/
performance-test3.cpp
331 lines (293 loc) · 7.16 KB
/
performance-test3.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
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
#include <FEHLCD.h>
#include <FEHUtility.h>
#include <FEHIO.h>
#include <FEHServo.h>
#include <FEHMotor.h>
#include <FEHRPS.h>
#define OPTOTHRESH 2.0
#define ENCODERINCH 10
//declarations for encoders (right encoder (re) / left encoder (le) )
DigitalEncoder re(FEHIO::P3_5);
DigitalEncoder le(FEHIO::P3_0);
//declaration for motors (right motor (rm) / left motor (lm) )
FEHMotor rm(FEHMotor::Motor1,7.2);
FEHMotor lm(FEHMotor::Motor0,7.2);
//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);
//declares a servo on servo port 3
FEHServo arm_servo(FEHServo::Servo0);
//declares a servo on servo port 2
FEHServo ticket_servo(FEHServo::Servo7);
//declares a servo on servo port 5
FEHServo burger_servo(FEHServo::Servo5);
//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();
if (percent > 0) {
//set motors to percent
rm.SetPercent(percent + 3);
lm.SetPercent(percent);
} else if (percent < 0) {
rm.SetPercent(percent);
lm.SetPercent(percent + 3);
}
//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 = 120;
double rightFull = 110;
//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 = 95;
double rightFull = 105;
//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 > 3) {
value = 0;
} else if (lightLevel > 1.8) {
value = 2;
} else {
value = 1;
}
return value;
}
//Algorithm 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(10);
state = 2;
Sleep(100);
} else if (lo.Value() < OPTOTHRESH && ro.Value() > OPTOTHRESH && mo.Value() > OPTOTHRESH) {
//line slightly at left
lm.SetPercent(10);
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);
}
}
//Servo expand arm function//
//15 degrees is the ticket arm startig position
//150 degrees is the ticket arm before pushing ticket
//180 degrees is the ticket arm after pushing ticket
void ticket(int angle){
ticket_servo.SetDegree(angle);
Sleep(1.0);
}
//Second Servo vertical movment//
//180 degree is when slider is up
//135 is perpendicular to ground
//
void tray(int angle)
{
arm_servo.SetDegree(angle);
Sleep(200);
}
//Move arm Servo push ice cream lever//
void iceCream(int angle)
{
arm_servo.SetDegree(angle);
Sleep(200);
}
//Function to flip burger//
void BurgerFlip(int angle)
{
burger_servo.SetDegree(angle);
Sleep(200);
}
//function for wiggling towards ticket
void wiggle (double time)
{
while(TimeNow()-time < 5)
{
rm.SetPercent(20);
Sleep(0.1);
rm.SetPercent(0);
lm.SetPercent(10);
Sleep(0.1);
lm.SetPercent(0);
}
}
int main(void)
{
//Calibration code to set servo motors//
arm_servo.SetMin(725);
arm_servo.SetMax(2450);
ticket_servo.SetMin(747);
ticket_servo.SetMax(2424);
burger_servo.SetMin(515);
burger_servo.SetMax(2450);
tray(120);
ticket(10.0);
burger_servo.SetDegree(0);
//Wait until red light//
while(detectLight() == 0){}
//Move forward to mid ramp//
move(40, ENCODERINCH * 16 - 6);
//Turn left to face parallel to ramp to move backwards//
turnLeft(142.5);
//Move up the ramp slightly past kitchen//
move(-50, ENCODERINCH * 43);
//Turn to face burger//
turnLeft(90.0);
//Move forward
//Flip burger//
move(40, ENCODERINCH * 2);
BurgerFlip(110);
move(-20, ENCODERINCH);
Sleep(1.0);
BurgerFlip(180);
move(40, ENCODERINCH * 2 + 5);
BurgerFlip(90);
move(-40, ENCODERINCH * 3 + 5);
BurgerFlip(180);
//turnRight(30);
//Sleep(1.0);
//BurgerFlip(180);
//move(40, ENCODERINCH * 2);
//turnLeft(30);
//move(40, ENCODERINCH * 2 + 5);
//turnLeft(30);
//move(40, ENCODERINCH * 2);
//turnRight(30);
//BurgerFlip(90);
//move(-40, ENCODERINCH * 2);
//BurgerFlip(180);
move(-40, ENCODERINCH * 3);
//turn left
turnRight(220);
BurgerFlip(0);
Sleep(200);
move(20, ENCODERINCH);
//Flip arm tray up to flip ice cream//
iceCream(55);
}