Skip to content

Commit 2ef9f95

Browse files
committed
Lake day changes. Solved issue with urllib segfaulting when not run in main thread (add env variable no_proxy='*'). Moved backend communication out from eng_display. backend.py launches a new thread for each graphql mutation. This shouldn't be a problem unless the internet is terrible.
1 parent aa50591 commit 2ef9f95

File tree

4 files changed

+45
-193
lines changed

4 files changed

+45
-193
lines changed

backend.py

Lines changed: 13 additions & 73 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
1-
from sgqlc.endpoint.http import HTTPEndpoint
2-
3-
import config
1+
import math
42
from pygeodesy.sphericalNvector import LatLon
3+
from sgqlc.endpoint.http import HTTPEndpoint
54
from statistics import mean
5+
import threading
6+
67
from algorithms.helpers.node import Node
7-
import math
8+
import config
89

910

1011
class Backend:
@@ -13,14 +14,17 @@ def __init__(self):
1314
self.base_2_node: Node = None
1415
self.endpoint = HTTPEndpoint(config.BACKEND_URL)
1516

17+
# Uncomment following line to disable backend updates
18+
# self.endpoint = lambda x: None
19+
1620
def clear_nodes(self):
1721
mutation = \
1822
"""
1923
mutation {
2024
clearNodes
2125
}
2226
"""
23-
self.endpoint(mutation)
27+
threading.Thread(target=lambda: self.endpoint(mutation)).start()
2428

2529
def update_node(self, node):
2630
if not node.is_resolved():
@@ -44,10 +48,10 @@ def update_node(self, node):
4448
}
4549
"""
4650

47-
if node.id == "1":
51+
if node.id == "0":
4852
self.base_1_node = node
4953
lat, lon = config.BASE_1_GPS
50-
elif node.id == "0":
54+
elif node.id == "1":
5155
self.base_2_node = node
5256
# lat, lon = config.BASE_2_GPS # could do this, but can also be sanity check for GPS translator
5357
lat, lon = self.translate_node_to_gps_coords(node)
@@ -61,71 +65,7 @@ def update_node(self, node):
6165
'lat': lat,
6266
'lon': lon
6367
}
64-
self.endpoint(mutation)
65-
66-
def update_node_heading(self, n_id, heading, source="TELEMETRY"):
67-
mutation = \
68-
"""
69-
mutation {
70-
updateNode(id: "%(id)s", node: {
71-
pose: {
72-
orientation: {
73-
heading: %(heading).2f
74-
source: %(source)s
75-
}
76-
}
77-
}) {
78-
id
79-
}
80-
}
81-
"""
82-
83-
mutation = mutation % {
84-
'id': n_id,
85-
'heading': heading,
86-
'source': source
87-
}
88-
self.endpoint(mutation)
89-
90-
def update_node_temp(self, n_id, temp):
91-
mutation = \
92-
"""
93-
mutation {
94-
updateNode(id: "%(id)s", node: {
95-
telemetry: {
96-
temp: %(temp).2f
97-
}
98-
}) {
99-
id
100-
}
101-
}
102-
"""
103-
104-
mutation = mutation % {
105-
'id': n_id,
106-
'temp': temp
107-
}
108-
self.endpoint(mutation)
109-
110-
def update_node_batt(self, n_id, batt):
111-
mutation = \
112-
"""
113-
mutation {
114-
updateNode(id: "%(id)s", node: {
115-
telemetry: {
116-
batt: %(batt).2f
117-
}
118-
}) {
119-
id
120-
}
121-
}
122-
"""
123-
124-
mutation = mutation % {
125-
'id': n_id,
126-
'batt': batt
127-
}
128-
self.endpoint(mutation)
68+
threading.Thread(target=lambda: self.endpoint(mutation)).start()
12969

13070
def update_node_telemetry(self, n_id, temp, batt, heading, source="TELEMETRY"):
13171
mutation = \
@@ -155,7 +95,7 @@ def update_node_telemetry(self, n_id, temp, batt, heading, source="TELEMETRY"):
15595
'heading': heading,
15696
'source': source
15797
}
158-
self.endpoint(mutation)
98+
threading.Thread(target=lambda: self.endpoint(mutation)).start()
15999

160100
def translate_node_to_gps_coords(self, node):
161101
if not self.base_1_node or not self.base_2_node:

config.py

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,5 +17,9 @@
1717

1818
BACKEND_URL = "https://web.mnslac.xtriage.com/graphql"
1919

20-
BASE_1_GPS = 34.217852, -83.952365
21-
BASE_2_GPS = 34.217921, -83.950185
20+
###############
21+
# Site config #
22+
###############
23+
24+
BASE_1_GPS = 34.21797, -83.95238
25+
BASE_2_GPS = 34.21763, -83.95173

eng_display.py

Lines changed: 10 additions & 78 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66
import tkinter as tk
77
from tkinter import font
88
from tkinter import *
9-
from canvasvg import *
109
from multiprocessing import Process, Pipe
1110

1211
from main import Main
@@ -76,52 +75,6 @@ def move_to(self, x, y):
7675
# "cmd": "clear_screen"
7776
# "args": None
7877
# }
79-
#
80-
# {
81-
# "cmd": "backend_clear_nodes"
82-
# "args": None
83-
# }
84-
#
85-
# {
86-
# "cmd": "backend_update_node"
87-
# "args": Node
88-
# }
89-
#
90-
# {
91-
# "cmd": "backend_update_node_heading"
92-
# "args": {
93-
# "id": str,
94-
# "heading": float,
95-
# "source": TELEMETRY or POSITION
96-
# }
97-
# }
98-
#
99-
# {
100-
# "cmd": "backend_update_node_temp"
101-
# "args": {
102-
# "id": str,
103-
# "temp": float,
104-
# }
105-
# }
106-
#
107-
# {
108-
# "cmd": "backend_update_node_batt"
109-
# "args": {
110-
# "id": str,
111-
# "batt": float,
112-
# }
113-
# }
114-
#
115-
# {
116-
# "cmd": "backend_update_node_telemetry"
117-
# "args": {
118-
# "id": str,
119-
# "temp": float,
120-
# "batt": float,
121-
# "heading": float,
122-
# "source": TELEMETRY or POSITION
123-
# }
124-
# }
12578

12679
class EngDisplay:
12780
def __init__(self, src=None):
@@ -183,7 +136,7 @@ def create_eng_display(self):
183136
self.window.protocol("WM_DELETE_WINDOW", self.close_callback)
184137

185138
self.create_circle(100, 100, 100)
186-
if not self.updateFrame():
139+
if not self.update_frame():
187140
return
188141

189142
self.main_loop()
@@ -199,13 +152,13 @@ def main_loop(self):
199152
last_update = 0
200153
while True:
201154
if frame_end is True:
202-
if not self.updateFrame():
155+
if not self.update_frame():
203156
return
204157
last_update = time.time()
205158
# self.clear_canvas()
206159
frame_end = False
207160
elif time.time() - last_update > 0.01666666667:
208-
if not self.updateFrame():
161+
if not self.update_frame():
209162
return
210163
last_update = time.time()
211164
while receiving is True:
@@ -226,28 +179,6 @@ def main_loop(self):
226179
self.draw_circle(msg['args'])
227180
elif msg['cmd'] == "connect_points":
228181
self.connect_points(msg['args'])
229-
elif msg['cmd'] == "backend_clear_nodes":
230-
self.backend.clear_nodes()
231-
elif msg['cmd'] == "backend_update_node":
232-
self.backend.update_node(msg['args'])
233-
elif msg['cmd'] == "backend_update_node_heading":
234-
try:
235-
self.backend.update_node_heading(msg['args']['id'], msg['args']['heading'],
236-
msg['args']['source'])
237-
except KeyError:
238-
self.backend.update_node_heading(msg['args']['id'], msg['args']['heading'])
239-
elif msg['cmd'] == "backend_update_node_temp":
240-
self.backend.update_node_temp(msg['args']['id'], msg['args']['temp'])
241-
elif msg['cmd'] == "backend_update_node_batt":
242-
self.backend.update_node_batt(msg['args']['id'], msg['args']['batt'])
243-
elif msg['cmd'] == "backend_update_node_telemetry":
244-
try:
245-
self.backend.update_node_telemetry(msg['args']['id'], msg['args']['temp'],
246-
msg['args']['batt'], msg['args']['heading'],
247-
msg['args']['source'])
248-
except KeyError:
249-
self.backend.update_node_telemetry(msg['args']['id'], msg['args']['temp'],
250-
msg['args']['batt'], msg['args']['heading'])
251182
else:
252183
print(f"Unknown command: {msg['cmd']}")
253184
else:
@@ -258,7 +189,7 @@ def main_loop(self):
258189

259190
# Interactive features
260191

261-
def updateFrame(self):
192+
def update_frame(self):
262193
try:
263194
self.window.update_idletasks()
264195
self.window.update()
@@ -318,10 +249,10 @@ def shrink(self, scale, x=None, y=None):
318249
self.universal_scale *= scale
319250

320251
def translate_screen_pos_to_canvas_pos(self, x, y):
321-
return (x - self.canvas.x_pos, y - self.canvas.y_pos)
252+
return x - self.canvas.x_pos, y - self.canvas.y_pos
322253

323254
def translate_canvas_pos_to_screen_pos(self, x, y):
324-
return (x + self.canvas.x_pos, y + self.canvas.y_pos)
255+
return x + self.canvas.x_pos, y + self.canvas.y_pos
325256

326257
def start_measure(self, event):
327258
# Save the initial point
@@ -356,7 +287,8 @@ def stop_measure(self, event):
356287
def clear_canvas(self):
357288
self.canvas.delete("obj")
358289

359-
def get_val_from_args(self, args, val):
290+
@staticmethod
291+
def get_val_from_args(args, val):
360292
if val in args:
361293
return args[val]
362294
else:
@@ -442,7 +374,7 @@ def _connect_points(self, node1_pos, node2_pos, text=None, text_size=None, text_
442374
rotation = 180 - math.degrees(math.atan2(node1_pos[1] - node2_pos[1],
443375
node1_pos[0] - node2_pos[0]))
444376
# node1_pos the rotation
445-
if rotation > 90 and rotation < 270:
377+
if 90 < rotation < 270:
446378
rotation -= 180
447379
# Convert to radians
448380
rrotation = math.radians(rotation)
@@ -455,7 +387,7 @@ def _connect_points(self, node1_pos, node2_pos, text=None, text_size=None, text_
455387
text_color = "white"
456388
self.canvas.create_text(midx, midy, text=text,
457389
fill=text_color, font=font.Font(family='Courier New', size=text_size),
458-
justify=tk.LEFT, angle=rotation, tags=['scale', 'obj'])
390+
justify=tk.LEFT, tags=['scale', 'obj']) # angle=rotation
459391
if dashed is True:
460392
self.canvas.create_line(node1_pos[0], node1_pos[1], node2_pos[0], node2_pos[1], fill=color, dash=(1, 5),
461393
tags="obj")

0 commit comments

Comments
 (0)