-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathController.py
More file actions
193 lines (143 loc) · 4.88 KB
/
Controller.py
File metadata and controls
193 lines (143 loc) · 4.88 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
#!/usr/bin/python3
import socket, sys, struct, binascii
from enum import Enum
# =============================================== Constants
FORWARD = 0
BACKWARD = 1
class Commands(Enum):
MOVE_TO = 55
ROUTE_TO = 56
MOVE_TO_CHARGER = 57
MULTIPLE_TASKS = 58
MANUAL_MOVE = 59
CREATE_OBSTACLE = 60
REMOVE_OBSTACLE = 61
PROGRAM_EXIT = 62
ASK_POSITION = 130
directions = {
"dForward": 10,
"dBackward": 11,
"dLeft": 12,
"dRight": 13
}
commands_lengths = {
Commands.MOVE_TO: 9,
Commands.ROUTE_TO: 9,
Commands.MOVE_TO_CHARGER: 1,
Commands.MULTIPLE_TASKS: 1,
Commands.MANUAL_MOVE: 2,
Commands.CREATE_OBSTACLE: 8,
Commands.REMOVE_OBSTACLE: 8,
Commands.PROGRAM_EXIT: 8,
Commands.ASK_POSITION: 10,
-1: 0
}
class DirectionsCode(Enum):
D_FORWARD = 10
D_BACKWARD = 11
LEFT = 12
RIGHT = 13
# =============================================== Parameters
# This has to be moved in a file just for this
local_address = ("127.0.0.1", 44444)
ojabotti_adress = ("192.168.88.162", 22222)
address_in_use = local_address
# ======================================== Socket management
sock = None
class ConnectionRefusedError(BaseException):
pass
def create_connection():
_sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
try:
print("Connecting to {} port {}".format(*address_in_use))
_sock.connect(address_in_use)
print("Connected")
except ConnectionRefusedError:
print("Enable to connect, connexion refused")
return None
return _sock
def close_connection(sock):
if not sock:
print("Socket already closed or never openned")
return
print("Closing socket")
sock.close()
sock = None
print("Socket closed")
# ================================================ Commands
# Move to (x,y) in the specified direction
def move_to(x=0, y=0, direction=FORWARD):
if direction != FORWARD and direction != BACKWARD:
print("Direction {}".format(direction))
return
if not sock: return
values = (Commands.MOVE_TO, commands_lengths[Commands.MOVE_TO], x, y, direction)
# u_char u_short int int u_char
packed_data = struct.pack(">B H i i B", *values)
print(packed_data)
try:
print("Sending {!r}".format(binascii.hexlify(packed_data)))
sock.sendall(packed_data)
print("Sent")
except:
print("Oops...Smthg went wrong when sending data...")
# Move to (x,y) using the map to avoid obstacles
def route_to(x=0, y=0):
if not sock: return
values = (Commands.ROUTE_TO, commands_lengths[Commands.ROUTE_TO], x, y, 0)
packed_data = struct.pack(">B H i i B", *values)
print(packed_data)
try:
print("Sending {!r}".format(binascii.hexlify(packed_data)))
sock.sendall(packed_data)
print("Sent")
except:
print("Oops...Smthg went wrong when sending data...")
# Move to the charger already set
def move_to_charger():
if not sock: return
# 0 is for possible evolutions but it's meaningless
values = (Commands.MOVE_TO_CHARGER, commands_lengths[Commands.MOVE_TO_CHARGER], 0)
packed_data = struct.pack(">B H B", *values)
print(packed_data)
try:
print("Sending {!r}".format(binascii.hexlify(packed_data)))
sock.sendall(packed_data)
print("Sent")
except:
print("Oops...Smthg went wrong when sending data...")
# Manually moves the robot back and forth or rotates
def manual_move(direction = DirectionsCode.D_FORWARD):
if direction not in DirectionsCode:
print("Moving {}".format(direction))
return
if not sock: return
values = (Commands.MANUAL_MOVE, commands_lengths[Commands.MANUAL_MOVE], direction)
packed_data = struct.pack(">B H B", *values)
print(packed_data)
try:
print("Sending {!r}".format(binascii.hexlify(packed_data)))
sock.sendall(packed_data)
print("Sent")
except:
print("Oops...Smthg went wrong when sending data...")
# Create or remove an obstacle (create if create is True)
def manage_obstacle(x=0, y=0, create=True):
if not sock: return
c = Commands.REMOVE_OBSTACLE
if create:
c = Commands.CREATE_OBSTACLE
values = (c, commands_lengths[c], x, y)
packed_data = struct.pack(">B H i i B", *values)
print(packed_data)
try:
print("Sending {!r}".format(binascii.hexlify(packed_data)))
sock.sendall(packed_data)
print("Sent")
except:
print("Oops...Smthg went wrong when sending datas...")
# =============================================== Program
if __name__ == "__main__":
sock = create_connection()
move_to(0, 0)
close_connection(sock)