-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathportworld.py
200 lines (167 loc) · 6.42 KB
/
portworld.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
#! /usr/bin/env python3
# Copyright (c) 2022-present, Polymath Robotics, Inc.
# Example portworld 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.portworld_config as portworld_config # simple convenience, used to store config values
url = portworld_config.api_url
token = portworld_config.token
key = portworld_config.key
scale = 400000.0 # Used to change drawing scale of the map
font = ("Courier New", 7)
latdiff = abs(portworld_config.latlon_map[0][0] - portworld_config.latlon_map[1][0])
londiff = abs(portworld_config.latlon_map[0][1] - portworld_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("STOP", button_color=("white", "red")),
sg.Text("", key="-STATUS-", size=(45, None)),
],
[
sg.Button("Return to Equipment Shed"),
sg.Button(portworld_config.button_a[0]),
sg.Button(portworld_config.button_b[0]),
sg.Button(portworld_config.button_c[0]),
],
[sg.Button(portworld_config.button_d[0]), sg.Button(portworld_config.button_e[0])],
]
window = sg.Window(
"Polymath Robotics Caladan Portworld Example",
layout,
icon="./images/icon.png",
finalize=True,
)
graph = window["graph"]
def latlon_to_pixelXY(lat, lon):
return (
-scale * (portworld_config.latlon_map[0][1] - lon),
-scale * (portworld_config.latlon_map[0][0] - lat),
)
def pixelXY_to_latlon(x, y):
return (
portworld_config.latlon_map[0][0] + y / scale,
portworld_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), 10, fill_color="red", 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:
portworld_config.stat = api.goal_status()
portworld_config.odom = api.pose_with_odometry()
if "orientation" in portworld_config.odom:
quat = portworld_config.odom["orientation"]
portworld_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(portworld_config.odom)
time.sleep(1.2)
def tolerance_check(goal, tol):
if (
abs(portworld_config.odom["position"]["latitude"] - goal[0]) < tol
and abs(portworld_config.odom["position"]["longitude"] - goal[1]) < tol
):
return True
else:
return False
def graph_clean():
graph.Erase()
graph.DrawImage(filename="./images/portworld.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(portworld_config.odom["odometry"]["linear.x"], 2)) + " m/s "
)
current_pose = (
"lat: "
+ str(round(portworld_config.odom["position"]["latitude"], 6))
+ " lon: "
+ str(round(portworld_config.odom["position"]["longitude"], 6))
+ " angle: "
+ str(round(portworld_config.odom["orientation"], 3))
)
window["-POSE-"].update(current_pose)
window["-STATUS-"].update(portworld_config.stat)
draw_pose = latlon_to_pixelXY(
portworld_config.odom["position"]["latitude"],
portworld_config.odom["position"]["longitude"],
)
position = graph.draw_circle(
draw_pose, 10, fill_color="white", line_color="black"
)
if event == "Return to Equipment Shed":
graph_clean()
send_goal(*portworld_config.shed_goal)
if event == "STOP":
graph_clean()
api.cancel_prev_goal()
if event == portworld_config.button_a[0]:
graph_clean()
send_goal(*portworld_config.button_a[1:])
if event == portworld_config.button_b[0]:
graph_clean()
send_goal(*portworld_config.button_b[1:])
if event == portworld_config.button_c[0]:
graph_clean()
send_goal(*portworld_config.button_c[1:])
if event == portworld_config.button_d[0]:
graph_clean()
send_goal(*portworld_config.button_d[1:])
if event == portworld_config.button_e[0]:
graph_clean()
send_goal(*portworld_config.button_e[1:])
if event == "graph":
graph_clean()
x, y = values["graph"]
lat, lon = pixelXY_to_latlon(x, y)
send_goal(
lat, lon, yaw=1.57
) # NOTE: Clicking on the map always sends orientation NORTH!
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()