blob: 1b837131fe1cdc2402739e1add8c89696fb779bf [file] [log] [blame]
Ravago Jones6d460fe2021-07-03 16:59:55 -07001import gi
2gi.require_version('Gtk', '3.0')
3from gi.repository import Gtk
4import numpy as np
John Park91e69732019-03-03 13:12:43 -08005from points import Points
John Park91e69732019-03-03 13:12:43 -08006from libspline import Spline, DistanceSpline, Trajectory
7
Ravago Jones6d460fe2021-07-03 16:59:55 -07008from matplotlib.backends.backend_gtk3agg import (FigureCanvasGTK3Agg as
9 FigureCanvas)
10from matplotlib.figure import Figure
John Park91e69732019-03-03 13:12:43 -080011
12
Ravago Jones6d460fe2021-07-03 16:59:55 -070013class Graph(Gtk.Bin):
14 def __init__(self):
15 super(Graph, self).__init__()
16 fig = Figure(figsize=(5, 4), dpi=100)
17 self.axis = fig.add_subplot(111)
18 canvas = FigureCanvas(fig) # a Gtk.DrawingArea
19 canvas.set_vexpand(True)
20 canvas.set_size_request(800, 250)
21 self.add(canvas)
John Park91e69732019-03-03 13:12:43 -080022
Ravago Jones6d460fe2021-07-03 16:59:55 -070023 def recalculate_graph(self, points):
24 if not points.getLibsplines(): return
John Park91e69732019-03-03 13:12:43 -080025
Ravago Jones6d460fe2021-07-03 16:59:55 -070026 # set the size of a timestep
27 dt = 0.00505
John Park91e69732019-03-03 13:12:43 -080028
Ravago Jones6d460fe2021-07-03 16:59:55 -070029 # call C++ wrappers to calculate the trajectory
30 distanceSpline = DistanceSpline(points.getLibsplines())
31 traj = Trajectory(distanceSpline)
32 points.addConstraintsToTrajectory(traj)
33 traj.Plan()
34 XVA = traj.GetPlanXVA(dt)
John Park91e69732019-03-03 13:12:43 -080035
Ravago Jones6d460fe2021-07-03 16:59:55 -070036 # extract values to be graphed
37 total_steps_taken = XVA.shape[1]
38 total_time = dt * total_steps_taken
39 time = np.arange(total_time, step=dt)
40 position, velocity, acceleration = XVA
41 left_voltage, right_voltage = zip(*(traj.Voltage(x) for x in position))
John Park91e69732019-03-03 13:12:43 -080042
Ravago Jones6d460fe2021-07-03 16:59:55 -070043 # update graph
44 self.axis.clear()
45 self.axis.plot(time, velocity)
46 self.axis.plot(time, acceleration)
47 self.axis.plot(time, left_voltage)
48 self.axis.plot(time, right_voltage)
49 self.axis.legend(
50 ["Velocity", "Acceleration", "Left Voltage", "Right Voltage"])
51 self.axis.xaxis.set_label_text("Time (sec)")
John Park91e69732019-03-03 13:12:43 -080052
Ravago Jones6d460fe2021-07-03 16:59:55 -070053 # renumber the x-axis to include the last point,
54 # the total time to drive the spline
55 self.axis.xaxis.set_ticks(np.linspace(0, total_time, num=8))