forked from EDGE-tronics/Chess-Robot
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathInterface.py
More file actions
819 lines (726 loc) · 31.1 KB
/
Interface.py
File metadata and controls
819 lines (726 loc) · 31.1 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
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
import PySimpleGUI as sg
import ChessLogic as cl
import time
import os
import copy
import threading
import time
import cv2
import sys
import json
import VisionModule as vm
import platform
import ArmControl as ac
import lss_const as lssc
import pygame
import pathlib
try:
from picamera.array import PiRGBArray
from picamera import PiCamera
except:
pass
'''
styles:
BlueMono
GreenTan
LightGreen
Black
Dark
'''
# GLOBAL VARIABLES
CHESS_PATH = 'pieces_images' # path to the chess pieces
BLANK = 0 # piece names
PAWNW = 1
KNIGHTW = 2
BISHOPW = 3
ROOKW = 4
QUEENW = 5
KINGW = 6
PAWNB = 7
KNIGHTB = 8
BISHOPB = 9
ROOKB = 10
QUEENB = 11
KINGB = 12
blank = os.path.join(CHESS_PATH, 'blank.png')
bishopB = os.path.join(CHESS_PATH, 'nbishopb.png')
bishopW = os.path.join(CHESS_PATH, 'nbishopw.png')
pawnB = os.path.join(CHESS_PATH, 'npawnb.png')
pawnW = os.path.join(CHESS_PATH, 'npawnw.png')
knightB = os.path.join(CHESS_PATH, 'nknightb.png')
knightW = os.path.join(CHESS_PATH, 'nknightw.png')
rookB = os.path.join(CHESS_PATH, 'nrookb.png')
rookW = os.path.join(CHESS_PATH, 'nrookw.png')
queenB = os.path.join(CHESS_PATH, 'nqueenb.png')
queenW = os.path.join(CHESS_PATH, 'nqueenw.png')
kingB = os.path.join(CHESS_PATH, 'nkingb.png')
kingW = os.path.join(CHESS_PATH, 'nkingw.png')
images = {BISHOPB: bishopB, BISHOPW: bishopW, PAWNB: pawnB, PAWNW: pawnW, KNIGHTB: knightB, KNIGHTW: knightW,
ROOKB: rookB, ROOKW: rookW, KINGB: kingB, KINGW: kingW, QUEENB: queenB, QUEENW: queenW, BLANK: blank}
wclock = os.getcwd() + '/interface_images/wclock.png'
bclock = os.getcwd() + '/interface_images/bclock.png'
FENCODE = ""
colorTurn = True
graveyard = 'k0'
playerColor = True
playing = False
blackSquareColor = '#B58863'
whiteSquareColor = '#F0D9B5'
Debug = False
sequence = []
state = "stby"
newGameState = "config"
gameTime = 60.00
whiteSide = 0
route = os.getcwd() + '/'
homography = []
prevIMG = []
chessRoute = ""
detected = True
selectedCam = 0
skillLevel = 10 # Chess engine difficulty level
cap = cv2.VideoCapture()
rotMat = vm.np.zeros((2,2))
physicalParams = {"baseradius": 0.00,
"cbFrame": 0.00,
"sqSize": 0.00,
"cbHeight": 0.00,
"pieceHeight": 0.00}
def systemConfig():
global chessRoute
if platform.system() == 'Windows':
chessRoute = "games/stockfishX64.exe"
elif platform.system() == 'Linux':
chessRoute = "/usr/games/stockfish"
# GAME FUNCTIONS
def pcTurn(board,engine):
global sequence
global state
global homography
global cap
global selectedCam
command = ""
pcMove = engine.play(board, cl.chess.engine.Limit(time=1))
sequence = cl.sequenceGenerator(pcMove.move.uci(), board)
window.FindElement(key = "gameMessage").Update(sequence["type"])
if sequence["type"] == "White Queen Side Castling" or sequence["type"] == "Black Queen Side Castling":
command = "q_castling"
elif sequence["type"] == "White King Side Castling" or sequence["type"] == "Black King Side Castling":
command = "k_castling"
elif sequence["type"] == "Capture":
command = "capture"
elif sequence["type"] == "Passant":
command = "passant"
elif sequence["type"] == "Promotion":
command = "promotion"
if command:
speakThread = threading.Thread(target=speak, args=[command], daemon=True)
speakThread.start()
command = ""
ac.executeMove(sequence["seq"], physicalParams, playerColor, homography, cap, selectedCam)
board.push(pcMove.move)
updateBoard(sequence, board)
if board.is_checkmate():
window.FindElement(key = "robotMessage").Update("CHECKMATE!")
command = "checkmate"
elif board.is_check():
window.FindElement(key = "robotMessage").Update("CHECK!")
command = "check"
if command:
speakThread = threading.Thread(target=speak, args=[command], daemon=True)
speakThread.start()
state = "robotMove"
if board.is_game_over():
playing = False
state = "showGameResult"
def startEngine():
global engine
global state
global homography
engine = cl.chess.engine.SimpleEngine.popen_uci(chessRoute)
engine.configure({"Skill Level": skillLevel})
if playerColor == colorTurn:
state = "playerTurn"
else:
state = "pcTurn"
def playerTurn(board,squares):
result = cl.moveAnalysis(squares, board)
piece = ""
if result:
if result["type"] == "Promotion":
while not piece:
piece = coronationWindow()
result["move"] += piece
sequence = cl.sequenceGenerator(result["move"], board)
window.FindElement(key = "gameMessage").Update(sequence["type"])
board.push_uci(result["move"])
updateBoard(sequence,board)
return True
else:
return False
def startGame():
global gameTime
window.FindElement("newGame").Update(disabled=True)
window.FindElement("quit").Update(disabled=False)
window.FindElement(key = "wcount").Update(time.strftime("%H:%M:%S", time.gmtime(gameTime)))
window.FindElement(key = "bcount").Update(time.strftime("%H:%M:%S", time.gmtime(gameTime)))
window.FindElement(key = "clockButton").Update(image_filename=wclock)
window.FindElement(key = "robotMessage").Update("Good Luck!")
window.FindElement(key = "gameMessage").Update("--")
def quitGame():
window.FindElement("newGame").Update(disabled=False)
window.FindElement("quit").Update(disabled=True)
engine.quit()
# INTERFACE FUNCTIONS
def renderSquare(image, key, location):
if (location[0] + location[1]) % 2:
color = blackSquareColor
else:
color = whiteSquareColor
return sg.Button('', image_filename=image, size=(1, 1),
border_width=0, button_color=('white', color),
pad=(0, 0), key=key)
def redrawBoard(board):
columns = 'abcdefgh'
global playerColor
if playerColor:
sq = 63
for i in range(8):
window.FindElement(key = str(8-i)+"r").Update(" "+str(8-i))
window.FindElement(key = str(8-i)+"l").Update(str(8-i)+" ")
for j in range(8):
window.FindElement(key = columns[j]+"t").Update(columns[j])
window.FindElement(key = columns[j]+"b").Update(columns[j])
color = blackSquareColor if (i + 7-j) % 2 else whiteSquareColor
pieceNum = board.piece_type_at(sq)
if pieceNum:
if not board.color_at(sq):
pieceNum += 6
else:
pieceNum = 0
piece_image = images[pieceNum]
elem = window.FindElement(key=(i, 7-j))
elem.Update(button_color=('white', color),
image_filename=piece_image, )
sq -= 1
else:
sq = 0
for i in range(8):
window.FindElement(key = str(8-i)+"r").Update(" "+str(i+1))
window.FindElement(key = str(8-i)+"l").Update(str(i+1)+" ")
for j in range(8):
window.FindElement(key = columns[j]+"t").Update(columns[7-j])
window.FindElement(key = columns[j]+"b").Update(columns[7-j])
color = blackSquareColor if (i + 7-j) % 2 else whiteSquareColor
pieceNum = board.piece_type_at(sq)
if pieceNum:
if not board.color_at(sq):
pieceNum += 6
else:
pieceNum = 0
piece_image = images[pieceNum]
elem = window.FindElement(key=(i, 7-j))
elem.Update(button_color=('white', color),
image_filename=piece_image, )
sq += 1
def updateBoard(move, board):
global playerColor
global images
for cont in range(0,len(move["seq"]),4):
squareCleared = move["seq"][cont:cont+2]
squareOcupied = move["seq"][cont+2:cont+4]
scNum = cl.chess.SQUARE_NAMES.index(squareCleared)
y = cl.chess.square_file(scNum)
x = 7 - cl.chess.square_rank(scNum)
color = blackSquareColor if (x + y) % 2 else whiteSquareColor
if playerColor:
elem = window.FindElement(key=(x, y))
else:
elem = window.FindElement(key=(7-x, 7-y))
elem.Update(button_color=('white', color),
image_filename=blank, )
if squareOcupied != graveyard:
soNum = cl.chess.SQUARE_NAMES.index(squareOcupied)
pieceNum = board.piece_type_at(soNum)
if not board.color_at(soNum):
pieceNum += 6
y = cl.chess.square_file(soNum)
x = 7 - cl.chess.square_rank(soNum)
color = blackSquareColor if (x + y) % 2 else whiteSquareColor
if playerColor:
elem = window.FindElement(key=(x, y))
else:
elem = window.FindElement(key=(7-x, 7-y))
elem.Update(button_color=('white', color),
image_filename=images[pieceNum], )
def sideConfig(): # gameState: sideConfig
global newGameState
global state
global whiteSide
global prevIMG
global rotMat
i = 0
img = vm.drawQuadrants(prevIMG)
imgbytes = cv2.imencode('.png', img)[1].tobytes()
windowName = "Calibration"
initGame = [[sg.Text('Please select the "white" pieces side', justification='center', pad = (25,(5,15)), font='Any 15')],
[sg.Image(data=imgbytes, key='boardImg')],
[sg.Radio('1-2', group_id='grp', default = True,font='Any 14'), sg.Radio('2-3', group_id='grp',font='Any 14'),sg.Radio('4-3', group_id='grp',font='Any 14'), sg.Radio('1-4', group_id='grp',font='Any 14')],
[sg.Text('_'*30)],
[sg.Button("Back"), sg.Submit("Play")]]
newGameWindow = sg.Window(windowName, default_button_element_size=(12,1), auto_size_buttons=False, location = (100,50), icon='interface_images/robot_icon.ico').Layout(initGame)
while True:
button,value = newGameWindow.Read(timeout=100)
if button == "Play":
newGameState = "initGame"
while value[i] == False and i<4:
i+=1
whiteSide = i
if whiteSide == 0:
theta = 90
elif whiteSide == 1:
theta = 180
elif whiteSide == 2:
theta = -90
elif whiteSide == 3:
theta = 0
rotMat = vm.findRotation(theta)
prevIMG = vm.applyRotation(prevIMG,rotMat)
break
if button == "Back":
newGameState = "ocupiedBoard"
break
if button in (None, 'Exit'): # MAIN WINDOW
state = "stby"
newGameState = "config"
break
newGameWindow.close()
def ocupiedBoard(): # gameState: ocupiedBoard
global newGameState
global state
global selectedCam
global homography
global prevIMG
windowName = "Calibration"
initGame = [[sg.Text('Place the chess pieces and press Next', justification='center', pad = (25,(5,15)), font='Any 15')],
[sg.Image(filename='', key='boardVideo')],
[sg.Text('_'*30)],
[sg.Button("Back"), sg.Submit("Next")]]
newGameWindow = sg.Window(windowName, default_button_element_size=(12,1), auto_size_buttons=False, location = (100,50), icon='interface_images/robot_icon.ico').Layout(initGame)
while True:
button,value = newGameWindow.Read(timeout = 10)
if detected:
frame = takePIC()
prevIMG = vm.applyHomography(frame,homography)
imgbytes = cv2.imencode('.png', prevIMG)[1].tobytes()
newGameWindow.FindElement('boardVideo').Update(data=imgbytes)
if button == "Next":
newGameState = "sideConfig"
break
if button == "Back":
newGameState = "calibration"
break
if button in (None, 'Exit'): # MAIN WINDOW
state = "stby"
newGameState = "config"
break
newGameWindow.close()
def calibration(): # gameState: calibration
global newGameState
global state
global selectedCam
global homography
global detected
cbPattern = cv2.imread(route+'interface_images/cb_pattern.jpg', cv2.IMREAD_GRAYSCALE)
windowName = "Camera calibration"
initGame = [[sg.Text('Please adjust your camera and remove any chess piece', justification='center', pad = (25,(5,15)), font='Any 15', key = "calibrationBoard")],
[sg.Image(filename='', key='boardVideo')],
[sg.Text('_'*30)],
[sg.Button("Back"), sg.Submit("Next")]]
newGameWindow = sg.Window(windowName, default_button_element_size=(12,1), auto_size_buttons=False, location = (100,50), icon='interface_images/robot_icon.ico').Layout(initGame)
while True:
button,value = newGameWindow.Read(timeout = 10)
if detected:
frame = takePIC()
imgbytes = cv2.imencode('.png', frame)[1].tobytes()
newGameWindow.FindElement('boardVideo').Update(data=imgbytes)
homography = []
retIMG, homography = vm.findTransformation(frame,cbPattern)
if retIMG:
newGameWindow.FindElement('calibrationBoard').Update("Camera calibration successful. Please press Next")
else:
newGameWindow.FindElement('calibrationBoard').Update("Please adjust your camera and remove any chess piece")
if button == "Next" and retIMG:
newGameState = "ocupiedBoard"
break
if button == "Back":
if not selectedCam:
cap.close()
newGameState = "config"
break
if button in (None, 'Exit'): # MAIN WINDOW
state = "stby"
newGameState = "config"
break
newGameWindow.close()
def newGameWindow (): # gameState: config
global playerColor
global gameTime
global newGameState
global state
global detected
global cap
global selectedCam
global skillLevel
windowName = "Configuration"
frame_layout = [[sg.Radio('RPi Cam', group_id='grp', default = True, key = "rpicam"), sg.VerticalSeparator(pad=None), sg.Radio('USB0', group_id='grp', key = "usb0"), sg.Radio('USB1', group_id='grp', key = "usb1")]]
initGame = [[sg.Text('Game Parameters', justification='center', pad = (25,(5,15)), font='Any 15')],
[sg.CBox('Play as White', key='userWhite', default = playerColor)],
[sg.Spin([sz for sz in range(1, 300)], initial_value=10, font='Any 11',key='timeInput'),sg.Text('Game time (min)', pad=(0,0))],
[sg.Combo([sz for sz in range(1, 11)], default_value=10, key="enginelevel"),sg.Text('Engine skill level', pad=(0,0))],
[sg.Frame('Camera Selection', frame_layout, pad=(0, 10), title_color='white')],
[sg.Text('_'*30)],
[sg.Button("Exit"), sg.Submit("Next")]]
windowNewGame = sg.Window(windowName, default_button_element_size=(12,1), auto_size_buttons=False, icon='interface_images/robot_icon.ico').Layout(initGame)
while True:
button,value = windowNewGame.Read()
if button == "Next":
if value["rpicam"] == True:
selectedCam = 0
elif value["usb0"] == True:
selectedCam = 1
elif value["usb1"] == True:
selectedCam = 2
cap = initCam(selectedCam)
if detected:
newGameState = "calibration"
playerColor = value["userWhite"]
skillLevel = value["enginelevel"]*2
gameTime = float(value["timeInput"]*60)
break
if button in (None, 'Exit'): # MAIN WINDOW
state = "stby"
break
windowNewGame.close()
def coronationWindow (): # gameState: config
global playerColor
pieceSelected = ""
rook = rookB
knight = knightB
bishop = bishopB
queen = queenB
if playerColor:
rook = rookW
knight = knightW
bishop = bishopW
queen = queenW
windowName = "Promotion"
pieceSelection = [[sg.Text('Select the piece for promotion', justification='center', pad = (25,(5,15)), font='Any 15')],
[sg.Button('', image_filename=rook, size=(1, 1),
border_width=0, button_color=('white', "brown"),
pad=((40,0), 0), key="rook"),sg.Button('', image_filename=knight, size=(1, 1),
border_width=0, button_color=('white', "brown"),
pad=(0, 0), key="knight"),sg.Button('', image_filename=bishop, size=(1, 1),
border_width=0, button_color=('white', "brown"),
pad=(0, 0), key="bishop"),sg.Button('', image_filename=queen, size=(1, 1),
border_width=0, button_color=('white', "brown"),
pad=(0, 0), key="queen")]]
windowNewGame = sg.Window(windowName, default_button_element_size=(12,1), auto_size_buttons=False, icon='interface_images/robot_icon.ico').Layout(pieceSelection)
while True:
button,value = windowNewGame.Read()
if button == "rook":
pieceSelected = "r"
break
if button == "knight":
pieceSelected = "k"
break
if button == "bishop":
pieceSelected = "b"
break
if button == "queen":
pieceSelected = "q"
break
if button in (None, 'Exit'): # MAIN WINDOW
break
windowNewGame.close()
return pieceSelected
def takePIC():
global selectedCam
global cap
if selectedCam:
for i in range(5): # Clear images stored in buffer
cap.grab()
_ , frame = cap.read() # USB Cam
else:
cap.capture(rawCapture, format="bgr") # RPi Cam
frame = rawCapture.array
rawCapture.truncate(0) # Clear the stream in preparation for the next image
return frame
def quitGameWindow ():
global playing
global window
global cap
windowName = "Quit Game"
quitGame = [[sg.Text('Are you sure?',justification='center', size=(30, 1), font='Any 13')], [sg.Submit("Yes", size=(15, 1)),sg.Submit("No", size=(15, 1))]]
if not selectedCam:
cap.close()
if playing:
while True:
windowNewGame = sg.Window(windowName, default_button_element_size=(12,1), auto_size_buttons=False, icon='interface_images/robot_icon.ico').Layout(quitGame)
button,value = windowNewGame.Read()
if button == "Yes":
playing = False
break
if button in (None, 'Exit', "No"): # MAIN WINDOW
break
windowNewGame.close()
def mainBoardLayout():
# ------ Menu Definition ------ #
menu_def = [['&Configuration',["&Dimensions","E&xit"]],
['&Help', 'About'], ]
# ------ Layout ------ #
# sg.SetOptions(margins=(0,0))
sg.ChangeLookAndFeel('Dark')
# Main board display layout
board_layout = [[sg.T(' '*12)] + [sg.T('{}'.format(a), pad=((0,47),0), font='Any 13', key = a+'t') for a in 'abcdefgh']]
# Loop though board and create buttons with images
for i in range(8):
numberRow = 8-i
row = [sg.T(str(numberRow)+' ', font='Any 13', key = str(numberRow)+"l")]
for j in range(8):
row.append(renderSquare(blank, key=(i,j), location=(i,j)))
row.append(sg.T(' '+str(numberRow), font='Any 13', key = str(numberRow)+"r"))
board_layout.append(row)
# Add labels across bottom of board
board_layout.append([sg.T(' '*12)] + [sg.T('{}'.format(a), pad=((0,47),0), font='Any 13', key = a+'b') for a in 'abcdefgh'])
frame_layout_game = [
[sg.Button('---', size=(14, 2), border_width=0, font=('courier', 16), button_color=('black', "white"), pad=(4, 4), key="gameMessage")],
]
frame_layout_robot = [
[sg.Button('---', size=(14, 2), border_width=0, font=('courier', 16), button_color=('black', "white"), pad=(4, 4), key="robotMessage")],
]
board_controls = [[sg.RButton('New Game', key='newGame', size=(15, 2), pad=(0,(0,7)), font=('courier', 16))],
[sg.RButton('Quit', key='quit', size=(15, 2), pad=(0, 0), font=('courier', 16), disabled = True)],
[sg.Frame('GAME', frame_layout_game, pad=(0, 10), font='Any 12', title_color='white', key = "frameMessageGame")],
[sg.Frame('ROBOT', frame_layout_robot, pad=(0, (0,10)), font='Any 12', title_color='white', key = "frameMessageRobot")],
[sg.Button('White Time', size=(7, 2), border_width=0, font=('courier', 16), button_color=('black', whiteSquareColor), pad=(0, 0), key="wt"),sg.Button('Black Time', font=('courier', 16), size=(7, 2), border_width=0, button_color=('black', blackSquareColor), pad=((7,0), 0), key="bt")],
[sg.T("00:00:00",size=(9, 2), font=('courier', 13),key="wcount",pad = ((4,0),0)),sg.T("00:00:00",size=(9, 2), pad = (0,0), font=('courier', 13),key="bcount")],
[sg.Button('', image_filename=wclock, key='clockButton', pad = ((25,0),0))]]
layout = [[sg.Menu(menu_def, tearoff=False, key="manubar")],
[sg.Column(board_layout),sg.VerticalSeparator(pad=None),sg.Column(board_controls)]]
return layout
def initCam(selectedCam):
global detected
global rawCapture
button, value = window.Read(timeout=10)
if selectedCam: # USB Cam
cap = cv2.VideoCapture(selectedCam - 1)
cap.set(cv2.CAP_PROP_FRAME_WIDTH,640)
cap.set(cv2.CAP_PROP_FRAME_HEIGHT,480)
if not cap.isOpened():
detected = False
sg.popup_error('USB Video device not found')
else: # RPi Cam
cap = PiCamera()
if not cap:
detected = False
sg.popup_error('RPi camera module not found')
else:
cap.resolution = (640, 480)
rawCapture = PiRGBArray(cap, size=(640, 480))
return cap
def loadParams():
global physicalParams
if os.path.isfile('params.txt'):
json_file = open('params.txt')
physicalParams = json.load(json_file)
print(json_file)
else:
outfile = open('params.txt', 'w')
json.dump(physicalParams, outfile)
def phisicalConfig ():
global physicalParams
windowName = "Chessboard parameters"
robotParamLayout= [[sg.Text('Insert the physical dimensions in inches',justification='center', font='Any 14', pad=(10,10))],
[sg.Spin([sz/100 for sz in range(1, 1000)], initial_value=physicalParams["baseradius"], font='Any 11'),sg.Text('Base Radius', pad=(0,0))],
[sg.Spin([sz/100 for sz in range(1, 1000)], initial_value=physicalParams["cbFrame"], font='Any 11'),sg.Text('Chess Board Frame', pad=(0,0))],
[sg.Spin([sz/100 for sz in range(1, 1000)], initial_value=physicalParams["sqSize"], font='Any 11'),sg.Text('Square Size', pad=(0,0))],
[sg.Spin([sz/100 for sz in range(1, 1000)], initial_value=physicalParams["cbHeight"], font='Any 11'),sg.Text('Chess Board Height', pad=(0,0))],
[sg.Spin([sz/100 for sz in range(1, 1000)], initial_value=physicalParams["pieceHeight"], font='Any 11'),sg.Text('Tallest Piece Height', pad=(0,0))],
[sg.Text('_'*37)],
[sg.Submit("Save", size=(15, 1)),sg.Submit("Close", size=(15, 1))]]
while True:
robotParamWindow = sg.Window(windowName, default_button_element_size=(12,1), auto_size_buttons=False, icon='interface_images/robot_icon.ico').Layout(robotParamLayout)
button,value = robotParamWindow.Read()
if button == "Save":
physicalParams = {"baseradius": value[0],
"cbFrame":value[1],
"sqSize": value[2],
"cbHeight":value[3],
"pieceHeight": value[4]}
outfile = open('params.txt', 'w')
json.dump(physicalParams, outfile)
break
if button in (None, 'Close'): # MAIN WINDOW
break
robotParamWindow.close()
layout = mainBoardLayout()
window = sg.Window('ChessRobot', default_button_element_size=(12,1), auto_size_buttons=False, icon='interface_images/robot_icon.ico').Layout(layout)
def speak(command):
pygame.mixer.init()
filePath = str(pathlib.Path().absolute())+"/audio/"
pygame.mixer.music.load(filePath+command+".mp3")
pygame.mixer.music.play()
def main():
global playerColor
global state
global playing
global sequence
global newGameState
global detected
global physicalParams
global moveState
global prevIMG
global rotMat
global homography
global colorTurn
systemConfig()
loadParams()
interfaceMessage = ""
board = cl.chess.Board()
squares = []
whiteTime = 0
blackTime = 0
refTime = time.time()
board = cl.chess.Board()
while True :
button, value = window.Read(timeout=100)
if button in (None, 'Exit') or value["manubar"]=="Exit": # MAIN WINDOW
angles_rest = (0,-1150,450,1100,0)
_ = ac.LSSA_moveMotors(angles_rest)
ac.allMotors.limp()
ac.allMotors.setColorLED(lssc.LSS_LED_Black)
break
if value["manubar"]=="Dimensions":
if playing:
sg.popup("Please, first quit the game")
else:
phisicalConfig()
if button =="newGame":
if physicalParams["baseradius"] and physicalParams["cbFrame"] and physicalParams["sqSize"] and physicalParams["cbHeight"] and physicalParams["pieceHeight"]:
state = "startMenu"
else:
sg.popup_error('Please configure the chess board dimensions in the Configuration option of menu bar')
if button =="quit":
ac.allMotors.setColorLED(lssc.LSS_LED_Black)
quitGameWindow()
if not playing:
state = "showGameResult"
# PC messages
if playing:
if whiteTime <= 0:
playing = False
state = "showGameResult"
window.FindElement(key = "gameMessage").Update("Time Out\n"+"Black Wins")
elif blackTime <= 0:
playing = False
state = "showGameResult"
window.FindElement(key = "gameMessage").Update("Time Out\n"+ "White Wins")
if state == "stby": # stby
pass
elif state == "startMenu": # Start Menu
if newGameState == "config":
newGameWindow()
elif newGameState == "calibration":
calibration()
elif newGameState == "ocupiedBoard":
ocupiedBoard()
elif newGameState == "sideConfig":
sideConfig()
elif newGameState == "initGame":
playing = True
newGameState = "config"
board = cl.chess.Board()
if FENCODE:
board = cl.chess.Board(FENCODE)
colorTurn = board.turn
startGame()
whiteTime = gameTime
blackTime = gameTime
refTime = time.time()
startEngineThread = threading.Thread(target=startEngine, daemon=True)
startEngineThread.start()
speak("good_luck")
state = "stby"
redrawBoard(board)
elif state == "playerTurn": # Player Turn
if button == "clockButton":
currentIMG = takePIC()
curIMG = vm.applyHomography(currentIMG,homography)
curIMG = vm.applyRotation(curIMG,rotMat)
squares = vm.findMoves(prevIMG, curIMG)
if playerTurn(board, squares):
state = "pcTurn"
if board.is_game_over():
playing = False
state = "showGameResult"
else:
window.FindElement(key = "gameMessage").Update("Invalid move!")
speak("invalid_move")
state = "playerTurn"
elif state == "pcTurn": # PC turn
if board.turn:
window.FindElement(key = "clockButton").Update(image_filename=wclock)
else:
window.FindElement(key = "clockButton").Update(image_filename=bclock)
pcTurnThread = threading.Thread(target=pcTurn, args=(board,engine,), daemon=True)
pcTurnThread.start()
state = "stby" # Wait for the PC move, thread changes the state
elif state == "robotMove": # Robotic arm turn
previousIMG = takePIC()
prevIMG = vm.applyHomography(previousIMG,homography)
prevIMG = vm.applyRotation(prevIMG,rotMat)
state = "playerTurn"
window.FindElement(key = "robotMessage").Update("---")
if board.turn:
window.FindElement(key = "clockButton").Update(image_filename=wclock)
else:
window.FindElement(key = "clockButton").Update(image_filename=bclock)
elif state == "showGameResult":
gameResult = board.result()
if gameResult == "1-0":
window.FindElement(key = "gameMessage").Update("Game Over" + "\nWhite Wins")
if not playerColor: # If the player color is black -> robot color is white
ac.winLED(ac.allMotors)
else:
speak("goodbye")
elif gameResult == "0-1":
window.FindElement(key = "gameMessage").Update("Game Over" + "\nBlack Wins")
if playerColor: # If the player color is white -> robot color is black
ac.winLED(ac.allMotors)
else:
speak("goodbye")
elif gameResult == "1/2-1/2":
window.FindElement(key = "gameMessage").Update("Game Over" + "\nDraw")
else:
window.FindElement(key = "gameMessage").Update("Game Over")
window.FindElement(key = "robotMessage").Update("Goodbye")
quitGame()
state = "stby"
if playing:
dt = time.time() - refTime
if board.turn:
whiteTime = whiteTime - dt
if whiteTime < 0:
whiteTime = 0
refTime = time.time()
window.FindElement(key = "wcount").Update(time.strftime("%H:%M:%S", time.gmtime(whiteTime)))
else:
blackTime = blackTime - dt
if blackTime < 0:
blackTime = 0
refTime = time.time()
window.FindElement(key = "bcount").Update(time.strftime("%H:%M:%S", time.gmtime(blackTime)))
window.close()
if __name__ == "__main__":
main()