blob: c639379b8c084b9b261ba045a6766b1b7c19bbb9 [file] [log] [blame]
Tabitha Jarvis1007a132018-12-12 21:47:54 -08001#!/usr/bin/python3
Tabitha Jarvis1007a132018-12-12 21:47:54 -08002from __future__ import print_function
3import os
Andrew Runke0f945fd2019-01-27 21:10:37 -08004import sys
Ravago Jones6d460fe2021-07-03 16:59:55 -07005from color import palette
6from graph import Graph
Tabitha Jarvis1007a132018-12-12 21:47:54 -08007import gi
8import numpy as np
Tabitha Jarvis1007a132018-12-12 21:47:54 -08009gi.require_version('Gtk', '3.0')
John Park91e69732019-03-03 13:12:43 -080010gi.require_version('Gdk', '3.0')
Andrew Runke6842bf92019-01-26 15:38:25 -080011from gi.repository import Gdk, Gtk, GLib
Tabitha Jarvis1007a132018-12-12 21:47:54 -080012import cairo
Ravago Jones6d460fe2021-07-03 16:59:55 -070013from libspline import Spline
Tabitha Jarvis1007a132018-12-12 21:47:54 -080014import enum
John Park91e69732019-03-03 13:12:43 -080015import json
John Park91e69732019-03-03 13:12:43 -080016from constants import *
Ravago Jones6d460fe2021-07-03 16:59:55 -070017from drawing_constants import set_color, draw_px_cross, draw_px_x, display_text, draw_control_points
John Park91e69732019-03-03 13:12:43 -080018from points import Points
Ravago Jones6d460fe2021-07-03 16:59:55 -070019import time
Andrew Runke6842bf92019-01-26 15:38:25 -080020
Tabitha Jarvis1007a132018-12-12 21:47:54 -080021
22class Mode(enum.Enum):
23 kViewing = 0
24 kPlacing = 1
25 kEditing = 2
Andrew Runke6842bf92019-01-26 15:38:25 -080026
27
Ravago Jones6d460fe2021-07-03 16:59:55 -070028class FieldWidget(Gtk.DrawingArea):
Andrew Runke6842bf92019-01-26 15:38:25 -080029 """Create a GTK+ widget on which we will draw using Cairo"""
Ravago Jones26f7ad02021-02-05 15:45:59 -080030
Tabitha Jarvis1007a132018-12-12 21:47:54 -080031 def __init__(self):
Ravago Jones6d460fe2021-07-03 16:59:55 -070032 super(FieldWidget, self).__init__()
33 self.set_size_request(mToPx(FIELD.width), mToPx(FIELD.length))
Tabitha Jarvis1007a132018-12-12 21:47:54 -080034
John Park91e69732019-03-03 13:12:43 -080035 self.points = Points()
Ravago Jones6d460fe2021-07-03 16:59:55 -070036 self.graph = Graph()
37 self.set_vexpand(True)
38 self.set_hexpand(True)
John Park91e69732019-03-03 13:12:43 -080039
Tabitha Jarvis1007a132018-12-12 21:47:54 -080040 # init field drawing
41 # add default spline for testing purposes
42 # init editing / viewing modes and pointer location
43 self.mode = Mode.kPlacing
Ravago Jones6d460fe2021-07-03 16:59:55 -070044 self.mousex = 0
45 self.mousey = 0
46 self.module_path = os.path.dirname(os.path.realpath(sys.argv[0]))
47 self.path_to_export = os.path.join(self.module_path,
John Park91e69732019-03-03 13:12:43 -080048 'points_for_pathedit.json')
49
Tabitha Jarvis1007a132018-12-12 21:47:54 -080050 # For the editing mode
Andrew Runke6842bf92019-01-26 15:38:25 -080051 self.index_of_edit = -1 # Can't be zero beause array starts at 0
Tabitha Jarvis1007a132018-12-12 21:47:54 -080052 self.held_x = 0
Andrew Runke6842bf92019-01-26 15:38:25 -080053 self.spline_edit = -1
Tabitha Jarvis1007a132018-12-12 21:47:54 -080054
Ravago Jonesc26b9162021-06-30 20:12:48 -070055 try:
Ravago Jones6d460fe2021-07-03 16:59:55 -070056 self.field_png = cairo.ImageSurface.create_from_png(
57 "frc971/control_loops/python/field_images/" + FIELD.field_id +
58 ".png")
Ravago Jonesc26b9162021-06-30 20:12:48 -070059 except cairo.Error:
60 self.field_png = None
61
John Park91e69732019-03-03 13:12:43 -080062 def draw_robot_at_point(self, cr, i, p, spline):
63 p1 = [mToPx(spline.Point(i)[0]), mToPx(spline.Point(i)[1])]
64 p2 = [mToPx(spline.Point(i + p)[0]), mToPx(spline.Point(i + p)[1])]
Tabitha Jarvis1007a132018-12-12 21:47:54 -080065
John Park91e69732019-03-03 13:12:43 -080066 #Calculate Robot
67 distance = np.sqrt((p2[1] - p1[1])**2 + (p2[0] - p1[0])**2)
68 x_difference_o = p2[0] - p1[0]
69 y_difference_o = p2[1] - p1[1]
Ravago Jones6d460fe2021-07-03 16:59:55 -070070 x_difference = x_difference_o * mToPx(
71 FIELD.robot.length / 2) / distance
72 y_difference = y_difference_o * mToPx(
73 FIELD.robot.length / 2) / distance
John Park91e69732019-03-03 13:12:43 -080074
75 front_middle = []
76 front_middle.append(p1[0] + x_difference)
77 front_middle.append(p1[1] + y_difference)
78
79 back_middle = []
80 back_middle.append(p1[0] - x_difference)
81 back_middle.append(p1[1] - y_difference)
82
83 slope = [-(1 / x_difference_o) / (1 / y_difference_o)]
84 angle = np.arctan(slope)
85
Ravago Jones5e09c072021-03-27 13:21:03 -070086 x_difference = np.sin(angle[0]) * mToPx(FIELD.robot.width / 2)
87 y_difference = np.cos(angle[0]) * mToPx(FIELD.robot.width / 2)
John Park91e69732019-03-03 13:12:43 -080088
89 front_1 = []
90 front_1.append(front_middle[0] - x_difference)
91 front_1.append(front_middle[1] - y_difference)
92
93 front_2 = []
94 front_2.append(front_middle[0] + x_difference)
95 front_2.append(front_middle[1] + y_difference)
96
97 back_1 = []
98 back_1.append(back_middle[0] - x_difference)
99 back_1.append(back_middle[1] - y_difference)
100
101 back_2 = []
102 back_2.append(back_middle[0] + x_difference)
103 back_2.append(back_middle[1] + y_difference)
104
105 x_difference = x_difference_o * mToPx(
Ravago Jones5e09c072021-03-27 13:21:03 -0700106 FIELD.robot.length / 2 + ROBOT_SIDE_TO_BALL_CENTER) / distance
John Park91e69732019-03-03 13:12:43 -0800107 y_difference = y_difference_o * mToPx(
Ravago Jones5e09c072021-03-27 13:21:03 -0700108 FIELD.robot.length / 2 + ROBOT_SIDE_TO_BALL_CENTER) / distance
John Park91e69732019-03-03 13:12:43 -0800109
110 #Calculate Ball
111 ball_center = []
112 ball_center.append(p1[0] + x_difference)
113 ball_center.append(p1[1] + y_difference)
114
115 x_difference = x_difference_o * mToPx(
Ravago Jones5e09c072021-03-27 13:21:03 -0700116 FIELD.robot.length / 2 + ROBOT_SIDE_TO_HATCH_PANEL) / distance
John Park91e69732019-03-03 13:12:43 -0800117 y_difference = y_difference_o * mToPx(
Ravago Jones5e09c072021-03-27 13:21:03 -0700118 FIELD.robot.length / 2 + ROBOT_SIDE_TO_HATCH_PANEL) / distance
John Park91e69732019-03-03 13:12:43 -0800119
120 #Calculate Panel
121 panel_center = []
122 panel_center.append(p1[0] + x_difference)
123 panel_center.append(p1[1] + y_difference)
124
125 x_difference = np.sin(angle[0]) * mToPx(HATCH_PANEL_WIDTH / 2)
126 y_difference = np.cos(angle[0]) * mToPx(HATCH_PANEL_WIDTH / 2)
127
128 panel_1 = []
129 panel_1.append(panel_center[0] + x_difference)
130 panel_1.append(panel_center[1] + y_difference)
131
132 panel_2 = []
133 panel_2.append(panel_center[0] - x_difference)
134 panel_2.append(panel_center[1] - y_difference)
135
136 #Draw Robot
137 cr.move_to(front_1[0], front_1[1])
138 cr.line_to(back_1[0], back_1[1])
139 cr.line_to(back_2[0], back_2[1])
140 cr.line_to(front_2[0], front_2[1])
141 cr.line_to(front_1[0], front_1[1])
142
143 cr.stroke()
144
145 #Draw Ball
146 set_color(cr, palette["ORANGE"], 0.5)
147 cr.move_to(back_middle[0], back_middle[1])
148 cr.line_to(ball_center[0], ball_center[1])
149 cr.arc(ball_center[0], ball_center[1], mToPx(BALL_RADIUS), 0,
150 2 * np.pi)
151 cr.stroke()
152
153 #Draw Panel
154 set_color(cr, palette["YELLOW"], 0.5)
155 cr.move_to(panel_1[0], panel_1[1])
156 cr.line_to(panel_2[0], panel_2[1])
157
158 cr.stroke()
159 cr.set_source_rgba(0, 0, 0, 1)
160
Ravago Jones6d460fe2021-07-03 16:59:55 -0700161 def do_draw(self, cr): # main
Tabitha Jarvis1007a132018-12-12 21:47:54 -0800162
Ravago Jones6d460fe2021-07-03 16:59:55 -0700163 start_time = time.perf_counter()
Tabitha Jarvis1007a132018-12-12 21:47:54 -0800164
James Kuszmaul1c933e02020-03-07 16:17:51 -0800165 cr.save()
Tabitha Jarvis1007a132018-12-12 21:47:54 -0800166 set_color(cr, palette["BLACK"])
Ravago Jones5f787df2021-01-23 16:26:27 -0800167
Ravago Jones6d460fe2021-07-03 16:59:55 -0700168 cr.rectangle(0, 0, mToPx(FIELD.width), mToPx(FIELD.length))
Tabitha Jarvis1007a132018-12-12 21:47:54 -0800169 cr.set_line_join(cairo.LINE_JOIN_ROUND)
170 cr.stroke()
Ravago Jones5f787df2021-01-23 16:26:27 -0800171
Ravago Jonesc26b9162021-06-30 20:12:48 -0700172 if self.field_png:
173 cr.save()
Ravago Jonesc26b9162021-06-30 20:12:48 -0700174 cr.scale(
Ravago Jones6d460fe2021-07-03 16:59:55 -0700175 mToPx(FIELD.width) / self.field_png.get_width(),
176 mToPx(FIELD.length) / self.field_png.get_height(),
177 )
Ravago Jonesc26b9162021-06-30 20:12:48 -0700178 cr.set_source_surface(self.field_png)
179 cr.paint()
180 cr.restore()
Andrew Runke6842bf92019-01-26 15:38:25 -0800181
John Park91e69732019-03-03 13:12:43 -0800182 # update everything
Tabitha Jarvis1007a132018-12-12 21:47:54 -0800183
John Park91e69732019-03-03 13:12:43 -0800184 if self.mode == Mode.kPlacing or self.mode == Mode.kViewing:
Tabitha Jarvis1007a132018-12-12 21:47:54 -0800185 set_color(cr, palette["BLACK"])
Ravago Jones36c92f02021-07-24 16:35:33 -0700186 for i, point in enumerate(self.points.getPoints()):
187 draw_px_x(cr, mToPx(point[0]), mToPx(point[1]), 10)
John Park91e69732019-03-03 13:12:43 -0800188 set_color(cr, palette["WHITE"])
Tabitha Jarvis1007a132018-12-12 21:47:54 -0800189 elif self.mode == Mode.kEditing:
190 set_color(cr, palette["BLACK"])
John Park91e69732019-03-03 13:12:43 -0800191 if self.points.getSplines():
192 self.draw_splines(cr)
193 for i, points in enumerate(self.points.getSplines()):
Andrew Runke6842bf92019-01-26 15:38:25 -0800194
Ravago Jones36c92f02021-07-24 16:35:33 -0700195 points = [
196 np.array([mToPx(x), mToPx(y)])
197 for (x, y) in points
198 ]
199 draw_control_points(cr, points)
Tabitha Jarvis1007a132018-12-12 21:47:54 -0800200
Ravago Jones36c92f02021-07-24 16:35:33 -0700201 p0, p1, p2, p3, p4, p5 = points
John Park91e69732019-03-03 13:12:43 -0800202 first_tangent = p0 + 2.0 * (p1 - p0)
203 second_tangent = p5 + 2.0 * (p4 - p5)
204 cr.set_source_rgb(0, 0.5, 0)
205 cr.move_to(p0[0], p0[1])
206 cr.set_line_width(1.0)
207 cr.line_to(first_tangent[0], first_tangent[1])
208 cr.move_to(first_tangent[0], first_tangent[1])
209 cr.line_to(p2[0], p2[1])
Tabitha Jarvis1007a132018-12-12 21:47:54 -0800210
John Park91e69732019-03-03 13:12:43 -0800211 cr.move_to(p5[0], p5[1])
212 cr.line_to(second_tangent[0], second_tangent[1])
Tabitha Jarvis1007a132018-12-12 21:47:54 -0800213
John Park91e69732019-03-03 13:12:43 -0800214 cr.move_to(second_tangent[0], second_tangent[1])
215 cr.line_to(p3[0], p3[1])
216
217 cr.stroke()
218 cr.set_line_width(2.0)
219 self.points.update_lib_spline()
220 set_color(cr, palette["WHITE"])
221
222 cr.paint_with_alpha(0.2)
223
Ravago Jones6d460fe2021-07-03 16:59:55 -0700224 draw_px_cross(cr, self.mousex, self.mousey, 10)
James Kuszmaul1c933e02020-03-07 16:17:51 -0800225 cr.restore()
Ravago Jones6d460fe2021-07-03 16:59:55 -0700226
227 print("spent {:.2f} ms drawing the field widget".format(1000 * (time.perf_counter() - start_time)))
Tabitha Jarvis1007a132018-12-12 21:47:54 -0800228
John Park91e69732019-03-03 13:12:43 -0800229 def draw_splines(self, cr):
John Park91e69732019-03-03 13:12:43 -0800230 for i, points in enumerate(self.points.getSplines()):
231 array = np.zeros(shape=(6, 2), dtype=float)
232 for j, point in enumerate(points):
233 array[j, 0] = point[0]
234 array[j, 1] = point[1]
235 spline = Spline(np.ascontiguousarray(np.transpose(array)))
236 for k in np.linspace(0.01, 1, 100):
Ravago Jones26f7ad02021-02-05 15:45:59 -0800237 cr.move_to(
238 mToPx(spline.Point(k - 0.01)[0]),
239 mToPx(spline.Point(k - 0.01)[1]))
240 cr.line_to(
241 mToPx(spline.Point(k)[0]), mToPx(spline.Point(k)[1]))
John Park91e69732019-03-03 13:12:43 -0800242 cr.stroke()
John Park91e69732019-03-03 13:12:43 -0800243 if i == 0:
244 self.draw_robot_at_point(cr, 0.00, 0.01, spline)
245 self.draw_robot_at_point(cr, 1, 0.01, spline)
John Park91e69732019-03-03 13:12:43 -0800246
247 def mouse_move(self, event):
Ravago Jones6d460fe2021-07-03 16:59:55 -0700248 old_x = self.mousex
249 old_y = self.mousey
Ravago Jones128fb992021-07-31 13:56:58 -0700250 self.mousex, self.mousey = event.x, event.y
Ravago Jones6d460fe2021-07-03 16:59:55 -0700251 dif_x = self.mousex - old_x
252 dif_y = self.mousey - old_y
John Park91e69732019-03-03 13:12:43 -0800253 difs = np.array([pxToM(dif_x), pxToM(dif_y)])
254
Ravago Jones128fb992021-07-31 13:56:58 -0700255 if self.mode == Mode.kEditing and self.spline_edit != -1:
Ravago Jones6d460fe2021-07-03 16:59:55 -0700256 self.points.updates_for_mouse_move(self.index_of_edit,
Ravago Jones36c92f02021-07-24 16:35:33 -0700257 self.spline_edit,
258 pxToM(self.mousex),
259 pxToM(self.mousey), difs)
John Park91e69732019-03-03 13:12:43 -0800260
Ravago Jones128fb992021-07-31 13:56:58 -0700261 self.points.update_lib_spline()
262 self.graph.recalculate_graph(self.points)
263 self.queue_draw()
264
John Park909c0392020-03-05 23:56:30 -0800265 def export_json(self, file_name):
Ravago Jones09f59722021-03-03 21:11:41 -0800266 self.path_to_export = os.path.join(
267 self.module_path, # position of the python
268 "../../..", # root of the repository
269 get_json_folder(FIELD), # path from the root
270 file_name # selected file
271 )
Ravago Jones3b92afa2021-02-05 14:27:32 -0800272
Ravago Jones6d460fe2021-07-03 16:59:55 -0700273 # Will export to json file
274 multi_spline = self.points.toMultiSpline()
275 print(multi_spline)
276 with open(self.path_to_export, mode='w') as points_file:
277 json.dump(multi_spline, points_file)
John Park909c0392020-03-05 23:56:30 -0800278
279 def import_json(self, file_name):
Ravago Jones09f59722021-03-03 21:11:41 -0800280 self.path_to_export = os.path.join(
281 self.module_path, # position of the python
282 "../../..", # root of the repository
283 get_json_folder(FIELD), # path from the root
284 file_name # selected file
285 )
286
Ravago Jones6d460fe2021-07-03 16:59:55 -0700287 # import from json file
288 print("LOADING LOAD FROM " + file_name) # Load takes a few seconds
289 with open(self.path_to_export) as points_file:
290 multi_spline = json.load(points_file)
John Park909c0392020-03-05 23:56:30 -0800291
Ravago Jones6d460fe2021-07-03 16:59:55 -0700292 # if people messed with the spline json,
293 # it might not be the right length
294 # so give them a nice error message
295 try: # try to salvage as many segments of the spline as possible
296 self.points.fromMultiSpline(multi_spline)
297 except IndexError:
298 # check if they're both 6+5*(k-1) long
299 expected_length = 6 + 5 * (multi_spline["spline_count"] - 1)
300 x_len = len(multi_spline["spline_x"])
301 y_len = len(multi_spline["spline_x"])
302 if x_len is not expected_length:
303 print(
304 "Error: spline x values were not the expected length; expected {} got {}"
305 .format(expected_length, x_len))
306 elif y_len is not expected_length:
307 print(
308 "Error: spline y values were not the expected length; expected {} got {}"
309 .format(expected_length, y_len))
Ravago Jones3b92afa2021-02-05 14:27:32 -0800310
Ravago Jones6d460fe2021-07-03 16:59:55 -0700311 print("SPLINES LOADED")
312 self.mode = Mode.kEditing
John Park909c0392020-03-05 23:56:30 -0800313
Ravago Jones128fb992021-07-31 13:56:58 -0700314 def key_press(self, event):
Tabitha Jarvis1007a132018-12-12 21:47:54 -0800315 keyval = Gdk.keyval_to_lower(event.keyval)
John Park91e69732019-03-03 13:12:43 -0800316
Ravago Jones6d460fe2021-07-03 16:59:55 -0700317 # TODO: This should be a button
Andrew Runke6842bf92019-01-26 15:38:25 -0800318 if keyval == Gdk.KEY_p:
319 self.mode = Mode.kPlacing
320 # F0 = A1
321 # B1 = 2F0 - E0
322 # C1= d0 + 4F0 - 4E0
John Park91e69732019-03-03 13:12:43 -0800323 spline_index = len(self.points.getSplines()) - 1
324 self.points.resetPoints()
325 self.points.extrapolate(
326 self.points.getSplines()[len(self.points.getSplines()) - 1][5],
327 self.points.getSplines()[len(self.points.getSplines()) - 1][4],
328 self.points.getSplines()[len(self.points.getSplines()) - 1][3])
Ravago Jones128fb992021-07-31 13:56:58 -0700329 self.queue_draw()
Tabitha Jarvis1007a132018-12-12 21:47:54 -0800330
Ravago Jones6d460fe2021-07-03 16:59:55 -0700331 def button_press(self, event):
Ravago Jones128fb992021-07-31 13:56:58 -0700332 self.mousex, self.mousey = event.x, event.y
Ravago Jones6d460fe2021-07-03 16:59:55 -0700333
Andrew Runke6842bf92019-01-26 15:38:25 -0800334 if self.mode == Mode.kPlacing:
Ravago Jones36c92f02021-07-24 16:35:33 -0700335 if self.points.add_point(
336 pxToM(self.mousex), pxToM(self.mousey)):
John Park91e69732019-03-03 13:12:43 -0800337 self.mode = Mode.kEditing
Andrew Runke6842bf92019-01-26 15:38:25 -0800338 elif self.mode == Mode.kEditing:
339 # Now after index_of_edit is not -1, the point is selected, so
340 # user can click for new point
Ravago Jones128fb992021-07-31 13:56:58 -0700341 if self.index_of_edit == -1:
Andrew Runke6842bf92019-01-26 15:38:25 -0800342 # Get clicked point
343 # Find nearest
344 # Move nearest to clicked
Ravago Jones6d460fe2021-07-03 16:59:55 -0700345 cur_p = [pxToM(self.mousex), pxToM(self.mousey)]
Andrew Runke6842bf92019-01-26 15:38:25 -0800346 # Get the distance between each for x and y
347 # Save the index of the point closest
John Park13d3e282019-01-26 20:16:48 -0800348 nearest = 1 # Max distance away a the selected point can be in meters
Andrew Runke6842bf92019-01-26 15:38:25 -0800349 index_of_closest = 0
James Kuszmaul1c933e02020-03-07 16:17:51 -0800350 for index_splines, points in enumerate(
351 self.points.getSplines()):
Andrew Runke6842bf92019-01-26 15:38:25 -0800352 for index_points, val in enumerate(points):
Andrew Runke6842bf92019-01-26 15:38:25 -0800353 distance = np.sqrt((cur_p[0] - val[0])**2 +
354 (cur_p[1] - val[1])**2)
355 if distance < nearest:
356 nearest = distance
357 index_of_closest = index_points
358 print("Nearest: " + str(nearest))
359 print("Index: " + str(index_of_closest))
360 self.index_of_edit = index_of_closest
361 self.spline_edit = index_splines
Ravago Jones6d460fe2021-07-03 16:59:55 -0700362 self.held_x = self.mousex
Ravago Jones128fb992021-07-31 13:56:58 -0700363 self.queue_draw()
364
365 def button_release(self, event):
366 self.mousex, self.mousey = event.x, event.y
367 if self.mode == Mode.kEditing:
368 if self.index_of_edit > -1 and self.held_x != self.mousex:
369
370 self.points.setSplines(self.spline_edit, self.index_of_edit,
371 pxToM(self.mousex),
372 pxToM(self.mousey))
373
374 self.points.splineExtrapolate(self.spline_edit)
375
376 self.points.update_lib_spline()
377 self.graph.recalculate_graph(self.points)
378
379 self.index_of_edit = -1
380 self.spline_edit = -1