-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscooter.ino
317 lines (287 loc) · 6.03 KB
/
scooter.ino
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
/*
* Scooter Robot
* An Artificial Intelligence Project (CAP5636)
* by Kyle Burnett and Brian Woods
*/
#include <Servo.h>
const int pingPin = 7;
const int armServoPin = 9;
const int handServoPin = 10;
Servo armServo, handServo;
const float epsilon = 0.05,
alpha = 0.2,
discount = 0.9;
float values[8][8][4];
long pos;
int lastAction;
/*
index | armValue | handValue
------+------------+-----------
0 | 21 | 93
1 | 26 | 98
2 | 31 | 103
3 | 36 | 108
4 | 41 | 113
5 | 46 | 118
6 | 51 | 123
7 | 56 | 128
-------------------------------
*/
int armPos = 3, handPos = 3;
void setup()
{
// Initialize servos
armServo.attach(armServoPin);
delay(100);
moveArm();
handServo.attach(handServoPin);
delay(100);
moveHand();
// Initialize values array
for (int i = 0; i < 8; i++)
{
for (int j = 0; j < 8; j++)
{
for (int k = 0; k < 4; k++)
{
values[i][j][k] = 0;
}
}
}
// Initialize position
pos = getPos();
}
void loop()
{
performAction();
float reward = getReward();
updateValues(reward);
}
void moveArm()
{
int value = armPos * 5 + 21;
armServo.write(value);
}
void moveHand()
{
int value = handPos * 5 + 93;
handServo.write(value);
}
/*
Loops through the list of actions for the current arm and hand positions
and returns the best action according to the results stored in the q-value
array.
*/
int getBestAction()
{
int best = 0;
float bestValue = -1.0;
for (int i = 0; i < 4; ++i)
{
if (values[armPos][handPos][i] > bestValue)
{
bestValue = values[armPos][handPos][i];
best = i;
}
}
return best;
}
/*
Legal moves are defined as indexed values 0 through 3 as follows
index | meaning
------+----------------------
0 | arm up; hand up
1 | arm up; hand down
2 | arm down; hand up
3 | arm down; hand down
-----------------------------
getMovesLegality() returns a 4-element boolean array where each element
corresponds to the legality of the i-th move
*/
boolean * getMovesLegality()
{
boolean actions[4] = {true, true, true, true};
// If we are at the last arm postion, can't go up
if (armPos == 7)
{
actions[0] = false;
actions[1] = false;
}
// If we are at the last hand position, can't go up
if (handPos == 7)
{
actions[0] = false;
actions[2] = false;
}
// If we are at the first arm position, can't go down
if (armPos == 0)
{
actions[2] = false;
actions[3] = false;
}
// If we are at the last hand position, can't go up
if (handPos == 0)
{
actions[1] = false;
actions[3] = false;
}
// If we are at arm position 1, can't go down unless hand is at 1
if (armPos == 1 && handPos != 1)
{
actions[2] = false;
actions[3] = false;
}
return actions;
}
/*
Legal moves are defined as indexed values 0 through 3 as follows
index | meaning
------+----------------------
0 | arm up; hand up
1 | arm up; hand down
2 | arm down; hand up
3 | arm down; hand down
-----------------------------
doAction() takes an action and changes the armPos and handPos indices
accordingly.
*/
void doAction(int action)
{
if (action == 0 || action == 1)
{
armPos++;
}
else
{
armPos--;
}
if (action == 0 || action == 2)
{
handPos++;
}
else
{
handPos--;
}
}
void performAction()
{
// Get legal actions
// With probability epsilon, choose action randomly
// Otherwise, act according to best action
randomSeed(analogRead(0));
long coinflip = random(100);
if (coinflip < (epsilon*100))
{
// Random Legal Action
boolean check = false;
boolean * legalActions = getMovesLegality();
while(!check)
{
int randLegal = random(5);
if ( legalActions[randLegal] )
{
check = true;
doAction(randLegal);
lastAction = randLegal;
}
}
}
else
{
// Use Best Action Policy to get best legal movement schemeand execute it
int action = getBestAction();
doAction(action);
lastAction = action;
}
moveArm();
moveHand();
}
float getReward()
{
// Retrieve reward values depending on the change in
// position to either closer or farther from the goal.
long newPos = getPos();
// Closer to goal.
if (newPos < pos)
{
pos = newPos;
return 1.0;
}
// Farther from goal
else if (newPos > pos)
{
pos = newPos;
return -1.0;
}
// No position change
else
{
pos = newPos;
return 0.0;
}
}
float getQValue(int action)
{
int nextArm, nextHand;
if (action == 0 || action == 1)
{
nextArm = armPos + 1;
}
else
{
nextArm = armPos - 1;
}
if (action == 0 || action == 2)
{
nextHand = handPos + 1;
}
else
{
nextHand = handPos - 1;
}
return values[nextArm][nextHand][action];
}
void updateValues(float reward)
{
boolean * legalActions = getMovesLegality();
float maxValue = -1.0;
for (int i = 0; i < 4; ++i)
{
if (legalActions[i])
{
float value = getQValue(i);
if (value > maxValue)
{
maxValue = value;
}
}
}
// sample = R(s,a,s') + gamma * max_a'{Q(s',a')}
float sample = reward + discount * maxValue;
// Q(s,a) <- (1-alpha) * Q(s,a) + alpha * sample
float prevValue = values[armPos][handPos][lastAction];
values[armPos][handPos][lastAction] = (1 - alpha) * prevValue + alpha * sample;
}
long getPos()
{
long duration;
// Trigger PING
pinMode(pingPin, OUTPUT);
digitalWrite(pingPin, LOW);
delayMicroseconds(2);
digitalWrite(pingPin, HIGH);
delayMicroseconds(5);
digitalWrite(pingPin, LOW);
// Read signal
pinMode(pingPin, INPUT);
duration = pulseIn(pingPin, HIGH);
return microsecondsToCentimeters(duration);
}
long microsecondsToCentimeters(long microseconds)
{
// The speed of sound is 340 m/s or 29 microseconds per centimeter.
// The ping travels out and back, so to find the distance of the
// object we take half of the distance travelled.
return microseconds / 29 / 2;
}