-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathmineworld.py
239 lines (200 loc) · 7.65 KB
/
mineworld.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
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
#! /usr/bin/env python3
# Copyright (c) 2022-present, Polymath Robotics, Inc.
# Example mineworld UI for using the Caladan API in Python
# Designed as a simple teaching example, not feature complete or fully robust.
import time # used for sleep
import math # used for some basic trigonometry
import PySimpleGUI as sg # used for the Gui
import caladan_api # example API library
import config.mineworld_config as mineworld_config # simple convenience, used to store config values
url = mineworld_config.api_url
token = mineworld_config.token
key = mineworld_config.key
scale = 400000.0 # Used to change drawing scale of the map
font = ("Courier New", 7)
latdiff = abs(mineworld_config.latlon_map[0][0] - mineworld_config.latlon_map[1][0])
londiff = abs(mineworld_config.latlon_map[0][1] - mineworld_config.latlon_map[1][1])
api = caladan_api.SimpleAPI("", "", "") # Init to avoid flake8 issues
sg.theme("DarkGrey10")
layout = [
[sg.Text("Please provide Token and Key below", key="-PROMPT-")],
[sg.Input(token)],
[sg.Input(key)],
[sg.Button("Connect"), sg.Text("", key="-VEL-"), sg.Text("", key="-POSE-")],
[
sg.Graph(
canvas_size=(londiff * scale, latdiff * scale),
graph_bottom_left=(0, 0),
graph_top_right=(londiff * scale, latdiff * scale),
background_color="#1C1E23",
enable_events=True,
key="graph",
)
],
[
sg.Button("Return to Equipment Shed"),
sg.Button("STOP", button_color=("white", "red")),
sg.Text("", key="-STATUS-", size=(45, None)),
],
[
sg.Button(mineworld_config.button_a[0]),
sg.Button(mineworld_config.button_b[0]),
sg.Button(mineworld_config.button_c[0]),
],
[
sg.Button(mineworld_config.button_d[0]),
sg.Button(mineworld_config.button_e[0]),
sg.Button(mineworld_config.button_f[0]),
sg.Button(mineworld_config.button_g[0]),
],
]
window = sg.Window(
"Polymath Robotics Caladan mineworld Example",
layout,
icon="./images/icon.png",
finalize=True,
)
graph = window["graph"]
def latlon_to_pixelXY(lat, lon):
return (
-scale * (mineworld_config.latlon_map[0][1] - lon),
-scale * (mineworld_config.latlon_map[0][0] - lat),
)
def pixelXY_to_latlon(x, y):
return (
mineworld_config.latlon_map[0][0] + y / scale,
mineworld_config.latlon_map[0][1] + x / scale,
)
def send_goal(lat, lon, yaw):
x, y = latlon_to_pixelXY(lat, lon)
graph.draw_circle((x, y), 3, fill_color="#1C1E23", line_color="red")
graph.DrawText(
text=(round(lat, 5), round(lon, 5)),
location=(x, y - 7),
font=font,
color="white",
)
api.send_gps_goal(lat, lon, yaw)
def update_loop():
while True:
mineworld_config.stat = api.goal_status()
mineworld_config.odom = api.pose_with_odometry()
if "orientation" in mineworld_config.odom:
quat = mineworld_config.odom["orientation"]
mineworld_config.odom["orientation"] = math.atan2(
2.0 * (quat["w"] * quat["z"]), 1.0 - 2.0 * (quat["z"] * quat["z"])
)
window.write_event_value("-UPDATE-", "updated")
else:
print(mineworld_config.odom)
time.sleep(1.2)
def tolerance_check(goal, tol):
if (
abs(mineworld_config.odom["position"]["latitude"] - goal[0]) < tol
and abs(mineworld_config.odom["position"]["longitude"] - goal[1]) < tol
):
return True
else:
return False
def bulldoze():
while True: # Cancelled by the STOP button, as this cancels any goal
graph_clean()
for goal in mineworld_config.bulldoze:
send_goal(*goal)
while not tolerance_check(goal, 0.00003):
time.sleep(0.5)
def dump():
while True: # Cancelled by the STOP button, as this cancels any goal
graph_clean()
for goal in mineworld_config.dump:
send_goal(*goal)
while not tolerance_check(goal, 0.00003):
time.sleep(0.5)
def graph_clean():
graph.Erase()
graph.DrawImage(filename="./images/mineworld.png", location=(0, latdiff * scale))
graph_clean()
while True: # Main UI Loop
event, values = window.read()
if event == sg.WIN_CLOSED: # Handle closing window
break
# Only update and accept other commands if already connected
if window["Connect"].get_text() == "Connected":
window["-VEL-"].update(
str(round(mineworld_config.odom["odometry"]["linear.x"], 2)) + " m/s "
)
current_pose = (
"lat: "
+ str(round(mineworld_config.odom["position"]["latitude"], 6))
+ " lon: "
+ str(round(mineworld_config.odom["position"]["longitude"], 6))
+ " angle: "
+ str(round(mineworld_config.odom["orientation"], 3))
)
window["-POSE-"].update(current_pose)
window["-STATUS-"].update(mineworld_config.stat)
draw_pose = latlon_to_pixelXY(
mineworld_config.odom["position"]["latitude"],
mineworld_config.odom["position"]["longitude"],
)
position = graph.draw_circle(
draw_pose, 3, fill_color="#1C1E23", line_color="white"
)
if event == "Return to Equipment Shed":
graph_clean()
send_goal(*mineworld_config.shed_goal)
if event == "STOP":
graph_clean()
api.cancel_prev_goal()
if event == mineworld_config.button_a[0]:
graph_clean()
send_goal(*mineworld_config.button_a[1:])
if event == mineworld_config.button_b[0]:
graph_clean()
send_goal(*mineworld_config.button_b[1:])
if event == mineworld_config.button_c[0]:
graph_clean()
send_goal(*mineworld_config.button_c[1:])
if event == mineworld_config.button_d[0]:
graph_clean()
window.perform_long_operation(bulldoze, "-bulldoze DONE-")
if event == mineworld_config.button_e[0]:
graph_clean()
window.perform_long_operation(dump, "-dump DONE-")
if event == mineworld_config.button_f[0]:
graph_clean()
send_goal(
mineworld_config.odom["position"]["latitude"],
mineworld_config.odom["position"]["longitude"],
mineworld_config.button_f[1],
)
if event == mineworld_config.button_g[0]:
graph_clean()
send_goal(
mineworld_config.odom["position"]["latitude"],
mineworld_config.odom["position"]["longitude"],
mineworld_config.button_g[1],
)
if event == "graph":
graph_clean()
x, y = values["graph"]
lat, lon = pixelXY_to_latlon(x, y)
send_goal(
lat, lon, yaw=0
) # NOTE: Clicking on the map always sends orientation 0!
if event == "Connect": # First time clicking the connect button, check we get UUID
api = caladan_api.SimpleAPI(url, values[1], values[0])
uuid_response = api.get_uuid()
if "uuid" in uuid_response:
window["-PROMPT-"].update(
"Successfully connected to vehicle " + uuid_response["uuid"]
)
window["Connect"].update(button_color=("black", "green"))
window.perform_long_operation(update_loop, "-OPERATION DONE-")
window["Connect"].update("Connected")
graph_clean()
else:
window["-PROMPT-"].update(
uuid_response
) # otherwise, print returned error message
window.close()