-
Notifications
You must be signed in to change notification settings - Fork 1
/
me210_lewis.pdeOld
205 lines (155 loc) · 4.03 KB
/
me210_lewis.pdeOld
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
// Main program file
// Controls initialization, as well as the high-level FSM controlling the robot.
#include "defines.h"
#include "globalTimeout.h"
#include "sensors.h"
#include "motor.h"
#include "lineMotions.h"
#include "tokenManagement.h"
#include <Servo.h>
#include <Timers.h>
#define FWD_SPEED 210
#define SLOW_SPEED 115
#define TURN_SPEED 185
void setup() {
// initialize modules
globalTimeoutSetup();
motorSetup();
initializeServo();
pinMode(LED_PIN, OUTPUT);
// initialize serial
Serial.begin(57600);
Serial.println("Initialized");
//Find out which side we're on
char initialSide = findSide();
updateMotor();
//Head forward just for a little bit
// setMotion(255,0);
// delay(50);
int transSpeed = 200;
int rotSpeed = 5;
int val[3];
if (initialSide == 0) {
//Turn just a little bit
setMotion(0,-175);
for (int i = 0; i < 100; i++) {
updateMotor();
delay(1);
}
stopMotion();
for (int i = 0; i < 50; i++) {
updateMotor();
delay(1);
}
updateMotor();
//Idea #1: Do a gentle curve to get onto the "home" tape
setMotion(transSpeed,0);
//Keep going until we hit tape, then do line following routine
while(1) {
updateMotor();
readFrontSensors(val);
if(hasLine(val)) break;
}
do {
readFrontSensors(val);
}while(hasLine(val));
delay(30);
adjustMotion(0, -160);
do {
updateMotor();
readFrontSensors(val);
} while(!hasLine(val));
}
else {
setMotion(transSpeed,3);
//Keep going until we hit tape, then do line following routine
while(1) {
updateMotor();
readFrontSensors(val);
if(hasLine(val)) break;
}
do {
readFrontSensors(val);
}while(hasLine(val));
delay(30);
adjustMotion(0, 160);
do {
updateMotor();
readFrontSensors(val);
} while(!hasLine(val));
}
}
// function for determining side of board
// use blocking code for now
// returns 1 if right side
// need to recalibrate
char findSide() {
setMotion(0, SEESAW_HOME_TURN_SPD);
//Serial.println("Seesaw");
while(!readHomeBeacon()) {
updateMotor();
}
unsigned long startTime = millis();
while(!readFrontSeesaw()) {
updateMotor();
};
unsigned long endTime1 = millis();
while(!readHomeBeacon()) {
updateMotor();
}
unsigned long endTime2 = millis();
//Serial.println("Home");
//Serial.println(endTime);
stopMotion();
if (endTime1 - startTime < endTime2 - endTime1) {
digitalWrite(13, HIGH);
return 1;
}
else
return 0;
}
void loop() {
/*
for (int i = 0; i < 50; i++) {
updateServo();
delay(15);
}
*/
// test line following
startLineFollowing(FWD_SPEED);
while(followLine(FWD_SPEED) == LINE_FOLLOW_OK) updateMotor(); // follow line
adjustMotion(SLOW_SPEED,0);
while(!readSideSensor()) updateMotor(); // wait for turn sensor
stopMotion();
while(!motorDoneStop()) updateMotor();
//turn!
setMotion(0, -TURN_SPEED);
int val[3];
do {
readFrontSensors(val);
updateMotor();
}while(val[1] < LINE_SENSOR_MIN_THRES);
stopMotion();
while(!motorDoneStop()) updateMotor();
startLineFollowing(FWD_SPEED);
char token = 1;
while(followLine(FWD_SPEED) == LINE_FOLLOW_OK) {
if (readSideSeesaw() && !token) {
stopMotion();
while(!motorDoneStop()) updateMotor();
depositTokens();
for (int i = 0; i < 200; i++) {
updateMotor();
updateServo();
delay(15);
}
token = 1;
startLineFollowing(FWD_SPEED);
}
updateServo();
updateMotor();
}
stopMotion();
while(!motorDoneStop()) updateMotor();
while(1) updateMotor();
}