forked from polymathrobotics/caladan_examples
-
Notifications
You must be signed in to change notification settings - Fork 0
/
farmworld.py
206 lines (174 loc) · 6.5 KB
/
farmworld.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
#! /usr/bin/env python3
# Copyright (c) 2022-present, Polymath Robotics, Inc.
# Example farmworld 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.farmworld_config as farmworld_config # simple convenience, used to store config values
url = farmworld_config.api_url
token = farmworld_config.token
key = farmworld_config.key
scale = 400000.0 # Used to change drawing scale of the map
font = ("Courier New", 7)
latdiff = abs(farmworld_config.latlon_map[0][0] - farmworld_config.latlon_map[1][0])
londiff = abs(farmworld_config.latlon_map[0][1] - farmworld_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("Start Tilling"),
sg.Button("STOP", button_color=("white", "red")),
sg.Text("", key="-STATUS-", size=(45, None)),
],
[
sg.Button("Return to Equipment Shed"),
sg.Input("lat", size=10, key="lat"),
sg.Input("lon", size=10, key="lon"),
sg.Input("ori", size=10, key="ori"),
sg.Button("Send Goal"),
],
]
window = sg.Window(
"Polymath Robotics Caladan Farmworld Example",
layout,
icon="./images/icon.png",
finalize=True,
)
graph = window["graph"]
def latlon_to_pixelXY(lat, lon):
return (
-scale * (farmworld_config.latlon_map[0][1] - lon),
-scale * (farmworld_config.latlon_map[0][0] - lat),
)
def pixelXY_to_latlon(x, y):
return (
farmworld_config.latlon_map[0][0] + y / scale,
farmworld_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:
farmworld_config.stat = api.goal_status()
farmworld_config.odom = api.pose_with_odometry()
if "orientation" in farmworld_config.odom:
quat = farmworld_config.odom["orientation"]
farmworld_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(farmworld_config.odom)
time.sleep(1.2)
def tolerance_check(goal, tol):
if (
abs(farmworld_config.odom["position"]["latitude"] - goal[0]) < tol
and abs(farmworld_config.odom["position"]["longitude"] - goal[1]) < tol
):
return True
else:
return False
def tilling():
for goal in farmworld_config.tilling_goals:
send_goal(*goal)
while not tolerance_check(goal, 0.00003):
time.sleep(0.5)
def graph_clean():
graph.Erase()
graph.DrawImage(filename="./images/farmworld.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(farmworld_config.odom["odometry"]["linear.x"], 2)) + " m/s "
)
current_pose = (
"lat: "
+ str(round(farmworld_config.odom["position"]["latitude"], 6))
+ " lon: "
+ str(round(farmworld_config.odom["position"]["longitude"], 6))
+ " angle: "
+ str(round(farmworld_config.odom["orientation"], 3))
)
window["-POSE-"].update(current_pose)
window["-STATUS-"].update(farmworld_config.stat)
draw_pose = latlon_to_pixelXY(
farmworld_config.odom["position"]["latitude"],
farmworld_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(*farmworld_config.shed_goal)
if event == "Start Tilling":
graph_clean()
window.perform_long_operation(tilling, "-tilling DONE-")
if event == "STOP":
graph_clean()
api.cancel_prev_goal()
if event == "Send Goal":
graph_clean()
try:
lat, lon, ori = (
float(values["lat"]),
float(values["lon"]),
float(values["ori"]),
)
send_goal(lat, lon, ori)
except ValueError:
print(
"You need to provide latitude, longitude, and orientation values in the text boxes!"
)
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()