-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
50 lines (40 loc) · 1.63 KB
/
main.py
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
#!/usr/bin/env python
import argparse
from lego.EV3_Send import send_to_lego
from measurement_test.black_board import find_obj, find_board, take_pic, get_img
from measurement_test import black_board
HOST = '10.42.0.93' # The remote host
PORT = 50007 # The same port as used by the server
def main():
# construct the argument parse and parse the arguments
ap = argparse.ArgumentParser()
ap.add_argument("-i", "--image", help="path to the image file")
ap.add_argument("-H", "--host", help="host to connect", default=HOST)
ap.add_argument("-p", "--port", help="port to connect", default=PORT)
args = vars(ap.parse_args())
host = args.get('host')
port = args.get('port')
image_name = args.get('image')
if image_name:
image = get_img(image_name)
else:
image = take_pic()
heigth_coefficient, width_coefficient = find_board(image=image)
for color in black_board.COLORS.keys():
print(f"Finding color: {color}")
x, y = find_obj(image, color, heigth_coefficient, width_coefficient)
print(f"Color found: {color} | X:{x}, Y:{y}")
print(f"Sending lego to {x}, {y} for color {color}")
try:
response = send_to_lego(host=host, port=port, color=color, x=x, y=y)
print(f"Lego response: {response}")
except ConnectionRefusedError:
print(f"Can not connect to {host}:{port}")
print("*" * 100)
else:
print("Closing connection")
response = send_to_lego(
host, port, color=None, x=None, y=None, end=True)
print(f"Lego response: {response}")
if __name__ == "__main__":
main()