-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy paththreeCups_game.cpp
More file actions
451 lines (403 loc) · 11 KB
/
Copy paththreeCups_game.cpp
File metadata and controls
451 lines (403 loc) · 11 KB
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
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
#include "threeCups_game.h"
#include "game_utils.h"
#include <Arduino.h>
extern void changeConfig(String command);
extern String getPythonData(String command);
extern void parseCSV(const char *csv, int arr[], int &count);
extern bool sendServoCommand(int a1, int a2, int a3);
extern bool sendStepperCommand(const int cmds[]);
extern void printOnLCD(const String &msg);
// Define cup positions (left, middle, right)
#define LEFT_CUP 0
#define MIDDLE_CUP 1
#define RIGHT_CUP 2
#define NO_CUP -1
#define GRIP_CLOSED 90
#define GRIP_OPEN 130
#define DEFAULT_ANGLE_SHOULDER 80
const int retreatAngles[4] = {90, 90, 90, 90};
// Define game states
enum GameState
{
GAME_INIT,
WAITING_FOR_DETECTION,
PROCESSING_MULTIPLE_CUPS, // New state for handling multiple balls
GAME_OVER,
PICK_CUP,
ROBOT_RETREATING,
DROP_CUP
};
// States for the grabbing and releasing sequences
enum ArmMoveState
{
MOVE_IDLE,
PICK_LEFT, // Added for cup positions
DROP_LEFT, // Added for cup positions
PICK_MIDDLE, // Added for cup positions
DROP_MIDDLE, // Added for cup positions
PICK_RIGHT, // Added for cup positions
DROP_RIGHT, // Added for cup positions
MOVE_COMPLETE
};
// Cup position angles for the arm to point to
const int cupAngleData[3][4] = {
{138, 46, 126, 66}, // Left cup
{110, 58, 140, 72}, // Middle cup
{80, 46, 124, 70}, // Right cup
};
// State machine variables
static GameState currentState = GAME_INIT;
static ArmMoveState armState = MOVE_IDLE; // Current arm movement state
static unsigned long stateStartTime = 0;
static unsigned long lastActionTime = 0;
static int servoMoveIndex = 0;
static int currentMotor = 0;
static int targetAngle = 0;
static int overShootValue = 0;
static String ballCup[3]; // Cup contents - can be "null" or a string like "red"
static int moveAngles[4] = {0};
static bool gameEnded = false;
static int roundCount = 0;
static int currentCupIndex = 0; // Used for processing multiple cups
static String colors[4] = {"", "red", "blue", "yellow"};
static String cupsName[3] = {"Left Cup", "Middle Cup", "Right Cup"};
int dropped = 1;
void startCupsGame()
{
Serial.println("Starting Three Cups Game");
changeConfig("cups");
gameEnded = false;
// Initialize all cups to "null"
for (int i = 0; i < 3; i++)
{
ballCup[i] = "null";
}
roundCount = 1; // Only one round
currentCupIndex = 0;
currentState = GAME_INIT;
armState = MOVE_IDLE; // Initialize arm state
stateStartTime = millis();
printOnLCD("3 Cups Game Started");
}
bool cupsExecuteServoMove(ArmMotor motor, int angle)
{
if (sendServoCommand(motor, angle, 0))
{
return true;
}
// Serial.println("Servo command failed, will retry...");
return false;
}
// State machine servo move sequence setup
void setupCupsServoMoveSequence(int baseAngle, int shoulderAngle, int elbowAngle, int wristAngle, ArmMotor Currmotor, int angle)
{
servoMoveIndex = 0;
moveAngles[0] = baseAngle;
moveAngles[1] = shoulderAngle;
moveAngles[2] = elbowAngle;
moveAngles[3] = wristAngle;
currentMotor = Currmotor;
targetAngle = angle;
}
// Process one servo move step in the sequence
bool gripCups()
{
if (millis() - lastActionTime < 200)
{
return false; // Wait a little between commands
}
lastActionTime = millis();
bool success = cupsExecuteServoMove((ArmMotor)currentMotor, targetAngle);
if (success)
{
servoMoveIndex++;
switch (servoMoveIndex)
{
case 1:
currentMotor = ArmMotor::BASE;
targetAngle = moveAngles[0];
break;
case 2: // After base
currentMotor = ArmMotor::WRIST;
targetAngle = moveAngles[3];
break;
case 3: // After wrist
currentMotor = ArmMotor::ELBOW;
targetAngle = moveAngles[2];
break;
case 4: // After elbow
currentMotor = ArmMotor::SHOULDER;
targetAngle = moveAngles[1];
break;
case 5: // After shoulder
currentMotor = ArmMotor::GRIP;
targetAngle = GRIP_CLOSED;
break;
case 6:
currentMotor = ArmMotor::SHOULDER;
targetAngle = moveAngles[1] + 40;
break;
case 7: // After grip
return true; // Sequence complete
}
}
return false; // Sequence not complete yet
}
bool retreatArm()
{
if (millis() - lastActionTime < 200)
{
return false; // Wait a little between commands
}
lastActionTime = millis();
bool success = cupsExecuteServoMove((ArmMotor)currentMotor, targetAngle);
if (success)
{
servoMoveIndex++;
switch (servoMoveIndex)
{
case 1: // After shoulder
currentMotor = ArmMotor::BASE;
targetAngle = moveAngles[0];
break;
case 2: // After base
currentMotor = ArmMotor::WRIST;
targetAngle = moveAngles[3];
break;
case 3: // After wrist
currentMotor = ArmMotor::ELBOW;
targetAngle = moveAngles[2];
break;
case 4: // After elbow
currentMotor = ArmMotor::GRIP;
targetAngle = GRIP_OPEN;
break;
case 5: // After grip
return true; // Sequence complete
}
}
return false; // Sequence not complete yet
}
// Process one servo move step in the sequence
bool dropCups()
{
if (millis() - lastActionTime < 200)
{
return false; // Wait a little between commands
}
lastActionTime = millis();
bool success = cupsExecuteServoMove((ArmMotor)currentMotor, targetAngle);
if (success)
{
servoMoveIndex++;
switch (servoMoveIndex)
{
case 1: // After shoulder default
currentMotor = ArmMotor::GRIP;
targetAngle = GRIP_OPEN;
break;
case 2: // After grip
return true; // Sequence complete
}
}
return false; // Sequence not complete yet
}
void getAnglesForCup(int cupPosition, int angles[4])
{
for (int i = 0; i < 4; i++)
{
angles[i] = cupAngleData[cupPosition][i];
}
}
// Extract ball cup information from camera data
bool extractBallCup(int cameraData[], uint8_t count)
{
if (count != 3)
{
// Serial.println("Invalid camera data count");
return false;
}
// Set cup contents from camera data
bool anyBallFound = false;
for (int i = 0; i < 3; i++)
{
// Serial.println(cameraData[i]);
if (cameraData[i] != 0)
{
anyBallFound = true;
ballCup[i] = colors[cameraData[i]];
}
}
return anyBallFound; // Return true if at least one cup has a ball
}
void cupsGameLoop()
{
// Serial.println("CURRENT STATE : ");
// Serial.println(currentState);
// Serial.println("ARM STATE : ");
// Serial.println(armState);
if (gameEnded)
{
if (currentState != GAME_OVER)
{
currentState = GAME_OVER;
armState = MOVE_COMPLETE;
String resultMsg = "Game Over!";
printOnLCD(resultMsg);
}
return;
}
unsigned long currentTime = millis();
switch (currentState)
{
case GAME_INIT:
// Initialize game state
if (currentTime - stateStartTime > 500)
{
currentState = WAITING_FOR_DETECTION;
stateStartTime = currentTime;
printOnLCD("Waiting for cups...");
}
break;
case WAITING_FOR_DETECTION:
// Wait for camera to detect cups
if (currentTime - stateStartTime > 4000)
{
// Serial.println("Looking for ball position...");
String res = getPythonData("cupsResult");
if (res != "ERROR")
{
int cameraData[3];
int count;
parseCSV(res.c_str(), cameraData, count);
if (extractBallCup(cameraData, count))
{
// Serial.println("Balls detected in cups:");
// Reset the cup index counter for processing multiple cups
currentCupIndex = 0;
// Transition to the new state for handling multiple cups
currentState = PROCESSING_MULTIPLE_CUPS;
stateStartTime = currentTime;
printOnLCD("Balls detected");
}
else
{
// Couldn't identify any balls, retry
stateStartTime = currentTime + 1000;
printOnLCD("No balls detected");
}
}
else
{
// Serial.println("Camera failed");
stateStartTime = currentTime + 1000;
}
}
break;
case PROCESSING_MULTIPLE_CUPS:
// Find the next cup with a ball
while (currentCupIndex < 3 && ballCup[currentCupIndex] == "null")
{
currentCupIndex++;
}
// If we found a cup with a ball
if (currentCupIndex < 3)
{
// Get cup name for display
String cupName = cupsName[currentCupIndex];
// Set appropriate arm state based on cup position
Serial.print("Ball with color : ");
Serial.print(ballCup[currentCupIndex]);
Serial.print(" under the ");
Serial.println(cupsName[currentCupIndex]);
// Serial.print(" cup: ");
// Serial.println(ballCup[currentCupIndex]);
// Setup angles for pointing to this cup
getAnglesForCup(currentCupIndex, moveAngles);
setupCupsServoMoveSequence(
cupAngleData[currentCupIndex][0],
cupAngleData[currentCupIndex][1],
cupAngleData[currentCupIndex][2],
cupAngleData[currentCupIndex][3],
ArmMotor::GRIP,
GRIP_OPEN);
// Move to the pointing state
currentState = PICK_CUP;
stateStartTime = currentTime;
printOnLCD(ballCup[currentCupIndex] + " in " + cupName);
}
else
{
// No more cups with balls, game is complete
Serial.println("Game complete");
// Set arm state to complete
armState = MOVE_COMPLETE;
// Reset current index
currentCupIndex = 0;
// Always end the game after one round
gameEnded = true;
currentState = GAME_OVER;
}
break;
case PICK_CUP:
if (currentTime - stateStartTime > 1000)
{
if (gripCups())
{
setupCupsServoMoveSequence(
cupAngleData[currentCupIndex][0],
cupAngleData[currentCupIndex][1],
cupAngleData[currentCupIndex][2],
cupAngleData[currentCupIndex][3],
ArmMotor::SHOULDER,
cupAngleData[currentCupIndex][1] + 1);
currentState = DROP_CUP;
stateStartTime = currentTime;
}
}
break;
case DROP_CUP:
if (currentTime - stateStartTime > 5000)
{
if (dropCups())
{
setupCupsServoMoveSequence(
retreatAngles[0],
retreatAngles[1],
retreatAngles[2],
retreatAngles[3],
ArmMotor::SHOULDER,
DEFAULT_ANGLE_SHOULDER);
currentState = ROBOT_RETREATING;
stateStartTime = currentTime;
}
}
break;
case ROBOT_RETREATING:
// Execute retreating sequence
if (currentTime - stateStartTime > 1000)
{
if (retreatArm())
{
currentCupIndex++;
currentState = PROCESSING_MULTIPLE_CUPS;
stateStartTime = currentTime;
}
}
break;
case GAME_OVER:
// Game has ended
break;
}
}
void stopCupsGame()
{
Serial.println("Stopping Three Cups Game");
changeConfig("none");
gameEnded = true;
currentState = GAME_OVER;
armState = MOVE_IDLE; // Reset arm state
// Final result
String finalMessage = "Game completed";
printOnLCD(finalMessage);
}