Skip to content

Commit deedc69

Browse files
committed
Added board cleaning
1 parent 66471b5 commit deedc69

File tree

5 files changed

+243
-69
lines changed

5 files changed

+243
-69
lines changed

backend/src/errorHandler/error_handler.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,15 +19,15 @@ export async function initErrorHandler() {
1919
errors = JSON.parse(errorFileContent);
2020
} catch (e) {
2121

22-
await errorFile.write("[]");
22+
//await errorFile.write("[]");
2323
}
2424
}
2525

2626
export async function logEvent(error: ErrorDescription) {
2727
errors.push(error);
2828
console.log(`#${errors.length} | Level ${error.errorType} | ${error.description}`)
2929
//write to file
30-
await errorFile.write(JSON.stringify(errors))
30+
// await errorFile.write(JSON.stringify(errors))
3131

3232
if(error.errorType === ErrorType.FATAL) {
3333
GameManager.switchState(gameStates.ERROR, error);

backend/src/game/game_manager.ts

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import {playerSelection, PlayerSelectionAbortError, waitForTimeout, withTimeout} from "./game_utils.ts";
2-
import {moveToBlue, moveToColumn, moveToRed} from "../rv6l_client.ts";
2+
import {moveToBlue, moveToColumn, moveToRed, putBackToBlue, putBackToRed, removeFromField} from "../rv6l_client.ts";
33
import {type ErrorDescription, ErrorType, logEvent} from "../errorHandler/error_handler.ts";
44
import {applyGameMove, checkGameState, playAIMove, playMove, resetGame} from "./game.ts";
55
import {sendState, state} from "../state.ts";
@@ -221,16 +221,24 @@ const Error: GameState<void, void> = {
221221

222222
const CleanUp: GameState<boolean, void> = {
223223
stateName: "CLEAN_UP",
224-
expectedDuration: 1000 * 60 * 2,
224+
expectedDuration: 1000 * 60 * 10, //10 mins
225225
startTime: null,
226226
endTime: null,
227227
action: async (instantRestart: boolean) => {
228-
resetGame();
229-
if (!GameManager.isPhysicalBoardCleaned) {
230-
await withTimeout(new Promise<void>((resolve) => {
231-
setTimeout(resolve, 1000 * 3);
232-
}), CleanUp.expectedDuration!);
228+
229+
for (let i = 0; i < 6; i++) {
230+
if (state.board == null) break;
231+
for (let row = 0; row < (state.board![i] as number[]).length; row++) {
232+
const element = (state.board![i] as number[]).reverse()[row];
233+
await removeFromField(i, (state.board![i] as number[]).length - row - 1);
234+
if (element === 1) {
235+
await putBackToRed();
236+
} else if (element === 2) {
237+
await putBackToBlue();
238+
}
239+
}
233240
}
241+
resetGame();
234242

235243
if (instantRestart) {
236244
state.gameStartTime = Date.now();
@@ -281,7 +289,7 @@ const Sleep: GameState<void, void> = {
281289
expectedDuration: null,
282290
startTime: null,
283291
endTime: null,
284-
292+
285293
action: async () => {
286294
return {
287295
canContinue: false,

backend/src/internal_server.ts

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,10 @@ import {
1616
moveToRed,
1717
moveToRefPosition,
1818
RV6L_STATE,
19-
toggleGripper
19+
toggleGripper,
20+
removeFromField,
21+
putBackToBlue,
22+
putBackToRed
2023
} from "./rv6l_client.ts";
2124
import {type ErrorDescription, errors, ErrorType, logEvent} from "./errorHandler/error_handler.ts";
2225

@@ -161,6 +164,28 @@ export function initInternalServer() {
161164
} else if (data.command === "init_chip_palletizing") {
162165
await initChipPalletizing();
163166
sendStateToControlPanelClient!();
167+
}else if (data.command === "clean_board_at") {
168+
if (data.x == null || data.y == null) {
169+
logEvent({
170+
description: 'Invalid coordinates for cleanBoardAt command: ' + JSON.stringify({
171+
x: data.x,
172+
y: data.y
173+
}),
174+
errorType: ErrorType.WARNING,
175+
date: new Date().toString()
176+
});
177+
return;
178+
}
179+
180+
await removeFromField(data.x, data.y);
181+
sendStateToControlPanelClient!();
182+
183+
} else if(data.command === "put_back_blue") {
184+
await putBackToBlue();
185+
sendStateToControlPanelClient!();
186+
}else if(data.command === "put_back_red") {
187+
await putBackToRed();
188+
sendStateToControlPanelClient!();
164189
} else if (data.command === "move_to_ref_pos") {
165190
await moveToRefPosition();
166191
sendStateToControlPanelClient!();

backend/src/rv6l_client.ts

Lines changed: 74 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -101,12 +101,12 @@ export function initRV6LClient() {
101101
client.on('close', async function () {
102102
RV6L_STATE.rv6l_connected = false;
103103
logEvent({
104-
errorType: ErrorType.FATAL,
104+
errorType: ErrorType.WARNING,
105105
description: "RV6L connection closed unexpectedly. Reconnecting...",
106106
date: new Date().toString()
107107
})
108108
sendStateToControlPanelClient?.();
109-
await new Promise(resolve => setTimeout(resolve, 5000)); // Wait for 5 seconds before reconnecting
109+
await new Promise(resolve => setTimeout(resolve, 2000)); // Wait for 5 seconds before reconnecting
110110
logEvent({
111111
errorType: ErrorType.INFO,
112112
description: "Reconnecting to RV6L...",
@@ -128,7 +128,7 @@ export async function moveToBlue() {
128128
await wait(1000); // Simulate delay for mock
129129
} else {
130130
try {
131-
await writeVariableInProc("IMOVE", "1");
131+
await writeVariableInProc("I_Aktion", "11");
132132
await movementDone();
133133
} catch (error) {
134134
logEvent({
@@ -151,7 +151,7 @@ export async function moveToRed() {
151151
await wait(1000); // Simulate delay for mock
152152
} else {
153153
try {
154-
await writeVariableInProc("IMOVE", "2");
154+
await writeVariableInProc("I_Aktion", "21");
155155
await movementDone();
156156
} catch (error) {
157157
logEvent({
@@ -184,7 +184,8 @@ export async function moveToColumn(column: number) {
184184
await wait(1000); // Simulate delay for mock
185185
} else {
186186
try {
187-
await writeVariableInProc("IMOVE", (11 + column).toString());
187+
await writeVariableInProc("IX_Schacht", column.toString());
188+
await writeVariableInProc("I_Aktion", "31");
188189
await movementDone();
189190
} catch (error) {
190191
logEvent({
@@ -206,7 +207,9 @@ export async function initChipPalletizing() {
206207
await wait(1000); // Simulate delay for mock
207208
} else {
208209
try {
209-
await writeVariableInProc("IMOVE", "3");
210+
await writeVariableInProc("I_Aktion", "10");
211+
await movementDone()
212+
await writeVariableInProc("I_Aktion", "20");
210213
await movementDone()
211214
} catch (error) {
212215
logEvent({
@@ -228,7 +231,7 @@ export async function moveToRefPosition() {
228231
await wait(1000); // Simulate delay for mock
229232
} else {
230233
try {
231-
await writeVariableInProc("IMOVE", "4");
234+
await writeVariableInProc("I_Aktion", "90");
232235
await movementDone();
233236
} catch (error) {
234237
logEvent({
@@ -242,9 +245,71 @@ export async function moveToRefPosition() {
242245
stopAction("MoveToRefPosition");
243246
}
244247

248+
export async function removeFromField(x: number, y:number) {
249+
startAction("RemoveFromField");
250+
if( RV6L_STATE.mock) {
251+
await wait(1000); // Simulate delay for mock
252+
}else {
253+
try {
254+
await writeVariableInProc("IX_Feld", x.toString());
255+
await writeVariableInProc("IZ_Feld", y.toString());
256+
await writeVariableInProc("I_Aktion", "41");
257+
await movementDone();
258+
} catch (error) {
259+
logEvent({
260+
errorType: ErrorType.FATAL,
261+
description: "Couldn't complete remove from field at position X:" + x + " Y:" + y,
262+
date: new Date().toString()
263+
});
264+
}
265+
}
266+
267+
stopAction("RemoveFromField");
268+
}
269+
270+
export async function putBackToBlue() {
271+
startAction("PutBackToBlue");
272+
if( RV6L_STATE.mock) {
273+
await wait(1000); // Simulate delay for mock
274+
}else {
275+
try {
276+
await writeVariableInProc("I_Aktion", "12");
277+
await movementDone();
278+
} catch (error) {
279+
logEvent({
280+
errorType: ErrorType.FATAL,
281+
description: "Couldn't complete put back to blue",
282+
date: new Date().toString()
283+
});
284+
}
285+
}
286+
RV6L_STATE.blueChipsLeft++;
287+
stopAction("PutBackToBlue");
288+
}
289+
290+
export async function putBackToRed() {
291+
startAction("PutBackToRed");
292+
if( RV6L_STATE.mock) {
293+
await wait(1000); // Simulate delay for mock
294+
}else {
295+
try {
296+
await writeVariableInProc("I_Aktion", "22");
297+
await movementDone();
298+
} catch (error) {
299+
logEvent({
300+
errorType: ErrorType.FATAL,
301+
description: "Couldn't complete put back to red",
302+
date: new Date().toString()
303+
});
304+
}
305+
}
306+
RV6L_STATE.redChipsLeft++;
307+
stopAction("PutBackToRed");
308+
}
309+
245310
async function movementDone() {
246311
try {
247-
await waitForVariablePolling("IMOVE", "0");
312+
await waitForVariablePolling("I_Aktion", "0");
248313
} catch (error) {
249314
logEvent({
250315
errorType: ErrorType.FATAL,
@@ -305,7 +370,7 @@ async function waitForVariablePolling(variable: string, value: string) {
305370

306371
async function readVariableInProc(name: string): Promise<string> {
307372
let messageId = getNextMessageId();
308-
const getVariable = `<RSVCMD><clientStamp>${messageId}</clientStamp><symbolApi><readSymbolValue><name>IMOVE</name></readSymbolValue></symbolApi></RSVCMD>`
373+
const getVariable = `<RSVCMD><clientStamp>${messageId}</clientStamp><symbolApi><readSymbolValue><name>${name}</name></readSymbolValue></symbolApi></RSVCMD>`
309374
client.write(getVariable);
310375

311376
const result = await waitForMessage(messageId);

0 commit comments

Comments
 (0)