def __init__(self): app.Canvas.__init__(self, keys='interactive', size=(800, 550)) vertices, faces, outline = create_box(width=1, height=1, depth=1, width_segments=4, height_segments=8, depth_segments=16) self.box = visuals.BoxVisual(width=1, height=1, depth=1, width_segments=4, height_segments=8, depth_segments=16, vertex_colors=vertices['color'], edge_color='b') self.theta = 0 self.phi = 0 self.transform = MatrixTransform() self.box.transform = self.transform self.show() self.timer = app.Timer(connect=self.rotate) self.timer.start(0.016)
def __init__(self, d1=None, d2=None, little_m=None, big_m=None, spring_k1=None, spring_k2=None, b=None, x=None, x_dot=None, theta=None, theta_dot=None, px_len=None, scale=None, pivot=False, method='Euler', dt=None, font_size=None): """ Main VisPy Canvas for simulation of physical system. Parameters ---------- d1 : float Length of rod (in meters) from pivot to upper spring. d2 : float Length of rod (in meters) from pivot to lower spring. little_m : float Mass of attached cube (in kilograms). big_m : float Mass of rod (in kilograms). spring_k1 : float Spring constant of lower spring (in N/m). spring_k2 : float Spring constant of upper spring (in N/m). b : float Coefficient of quadratic sliding friction (in kg/m). x : float Initial x-position of mass (in m). x_dot : float Initial x-velocity of mass (in m/s). theta : float Initial angle of rod, with respect to vertical (in radians). theta_dot : float Initial angular velocity of rod (in rad/s). px_len : int Length of the rod, in pixels. scale : int Scaling factor to change size of elements. pivot : bool Switch for showing/hiding pivot point. method : str Method to use for updating. dt : float Time step for simulation. font_size : float Size of font for text elements, in points. Notes ----- As of right now, the only supported methods are "euler" or "runge-kutta". These correspond to an Euler method or an order 3 Runge-Kutta method for updating x, theta, x dot, and theta dot. """ app.Canvas.__init__(self, title='Wiggly Bar', size=(800, 800)) # Some initialization constants that won't change self.standard_length = 0.97 + 0.55 self.center = np.asarray((500, 450)) self.visuals = [] self._set_up_system(d1=d1, d2=d2, little_m=little_m, big_m=big_m, spring_k1=spring_k1, spring_k2=spring_k2, b=b, x=x, x_dot=x_dot, theta=theta, theta_dot=theta_dot, px_len=px_len, scale=scale, pivot=pivot, method=method, dt=dt, font_size=font_size) piv_x_y_px = np.asarray( (self.pivot_loc_px * np.sin(self.theta), -1 * self.pivot_loc_px * (np.cos(self.theta)))) # Make the spring points points = make_spring(height=self.px_len / 4, radius=self.px_len / 24) # Put up a text visual to display time info self.font_size = 24. if font_size is None else font_size self.text = visuals.TextVisual('0:00.00', color='white', pos=[50, 250, 0], anchor_x='left', anchor_y='bottom') self.text.font_size = self.font_size # Let's put in more text so we know what method is being used to # update this self.method_text = visuals.TextVisual( 'Method: {}'.format(self.method), color='white', pos=[50, 250 + 2 / 3 * font_size, 0], anchor_x='left', anchor_y='top') self.method_text.font_size = 2 / 3 * self.font_size # Get the pivoting bar ready self.rod = visuals.BoxVisual(width=self.px_len / 40, height=self.px_len / 40, depth=self.px_len, color='white') self.rod.transform = transforms.MatrixTransform() self.rod.transform.scale( (self.scale, self.scale * self.rod_scale, 0.0001)) self.rod.transform.rotate(np.rad2deg(self.theta), (0, 0, 1)) self.rod.transform.translate(self.center - piv_x_y_px) # Show the pivot point (optional) pivot_center = (self.center[0], self.center[1], -self.px_len / 75) self.center_point = visuals.SphereVisual(radius=self.px_len / 75, color='red') self.center_point.transform = transforms.MatrixTransform() self.center_point.transform.scale((self.scale, self.scale, 0.0001)) self.center_point.transform.translate(pivot_center) # Get the upper spring ready. self.spring_2 = visuals.TubeVisual(points, radius=self.px_len / 100, color=(0.5, 0.5, 1, 1)) self.spring_2.transform = transforms.MatrixTransform() self.spring_2.transform.rotate(90, (0, 1, 0)) self.spring_2.transform.scale((self.scale, self.scale, 0.0001)) self.spring_2.transform.translate(self.center + self.s2_loc) # Get the lower spring ready. self.spring_1 = visuals.TubeVisual(points, radius=self.px_len / 100, color=(0.5, 0.5, 1, 1)) self.spring_1.transform = transforms.MatrixTransform() self.spring_1.transform.rotate(90, (0, 1, 0)) self.spring_1.transform.scale( (self.scale * (1.0 - (self.x * self.px_per_m) / (self.scale * self.px_len / 2)), self.scale, 0.0001)) self.spring_1.transform.translate(self.center + self.s1_loc) # Finally, prepare the mass that is being moved self.mass = visuals.BoxVisual(width=self.px_len / 4, height=self.px_len / 8, depth=self.px_len / 4, color='white') self.mass.transform = transforms.MatrixTransform() self.mass.transform.scale((self.scale, self.scale, 0.0001)) self.mass.transform.translate(self.center + self.mass_loc) # Append all the visuals self.visuals.append(self.center_point) self.visuals.append(self.rod) self.visuals.append(self.spring_2) self.visuals.append(self.spring_1) self.visuals.append(self.mass) self.visuals.append(self.text) self.visuals.append(self.method_text) # Set up a timer to update the image and give a real-time rendering self._timer = app.Timer('auto', connect=self.on_timer, start=True) self.show()
def __init__(self, list_of_charts, theta, fi, kp, kd, l, scale, x, y, method, control, save_charts, dt, tmax, font_size): VisualModel.__init__(self) self.center = [400, 400] self.model = VehicleKinematicModel(list_of_charts) self.list_of_charts = list_of_charts self._set_up_system( l=l, theta=theta, fi=fi, x=x, y=y, kp=kp, kd=kd, scale=scale, method=method, control=control, save_charts=save_charts, dt=dt, tmax=tmax, font_size=font_size ) # Put up a text visual to display time info self.font_size = 24. if font_size is None else font_size self.text = visuals.TextVisual('0:00.00', color='white', pos=[15, 50, 0], anchor_x='left', anchor_y='bottom') self.text.font_size = self.font_size self.rear_wheelsL = visuals.BoxVisual(width=1.0, height=1.0, depth=1.0, color='yellow') self.rear_wheelsL.transform = transforms.MatrixTransform() self.rear_wheelsL.transform.scale((self.scale*2.0, self.scale*0.5, 1)) self.rear_wheelsL.transform.translate([0.0, -self.scale*0.5*4.0]) self.rear_wheelsL.transform.rotate(np.rad2deg(-self.model.theta), (0, 0, 1)) self.rear_wheelsL.transform.translate([self.model.x, self.model.y]) self.rear_wheelsL.transform.translate(self.center) self.rear_wheelsR = visuals.BoxVisual(width=1.0, height=1.0, depth=1.0, color='yellow') self.rear_wheelsR.transform = transforms.MatrixTransform() self.rear_wheelsR.transform.scale((self.scale*2.0, self.scale*0.5, 1)) self.rear_wheelsR.transform.translate([0.0, self.scale*0.5*4.0]) self.rear_wheelsR.transform.rotate(np.rad2deg(-self.model.theta), (0, 0, 1)) self.rear_wheelsR.transform.translate([self.model.x, self.model.y]) self.rear_wheelsR.transform.translate(self.center) self.front_wheelsL = visuals.BoxVisual(width=1.0, height=1.0, depth=1.0, color='pink') self.front_wheelsL.transform = transforms.MatrixTransform() self.front_wheelsL.transform.scale((self.scale*2.0, self.scale*0.5, 1)) self.front_wheelsL.transform.rotate(np.rad2deg(-self.model.fi), (0, 0, 1)) self.front_wheelsL.transform.translate([self.scale*l, -self.scale*0.5*4.0]) self.front_wheelsL.transform.rotate(np.rad2deg(-self.model.theta), (0, 0, 1)) self.front_wheelsL.transform.translate([self.model.x, self.model.y]) self.front_wheelsL.transform.translate(self.center) self.front_wheelsR = visuals.BoxVisual(width=1.0, height=1.0, depth=1.0, color='pink') self.front_wheelsR.transform = transforms.MatrixTransform() self.front_wheelsR.transform.scale((self.scale*2.0, self.scale*0.5, 1)) self.front_wheelsR.transform.rotate(np.rad2deg(-self.model.fi), (0, 0, 1)) self.front_wheelsR.transform.translate([self.scale*l, self.scale*0.5*4.0]) self.front_wheelsR.transform.rotate(np.rad2deg(-self.model.theta), (0, 0, 1)) self.front_wheelsR.transform.translate([self.model.x, self.model.y]) self.front_wheelsR.transform.translate(self.center) self.vehicle_body = visuals.BoxVisual(width=1.0, height=1.0, depth=1.0, color='green') self.vehicle_body.transform = transforms.MatrixTransform() self.vehicle_body.transform.translate([0.5, 0.0]) self.vehicle_body.transform.scale((self.scale*self.model.l, self.scale, 1)) self.vehicle_body.transform.rotate(np.rad2deg(-self.model.theta), (0, 0, 1)) self.vehicle_body.transform.translate([self.model.x, self.model.y]) self.vehicle_body.transform.translate(self.center) self.vehicle_bodyF = visuals.BoxVisual(width=1.0, height=1.0, depth=1.0, color='red') self.vehicle_bodyF.transform = transforms.MatrixTransform() self.vehicle_bodyF.transform.scale((self.scale*0.25, self.scale*4.0, 1)) self.vehicle_bodyF.transform.translate([l*self.scale, 0.0]) self.vehicle_bodyF.transform.rotate(np.rad2deg(-self.model.theta), (0, 0, 1)) self.vehicle_bodyF.transform.translate([self.model.x, self.model.y]) self.vehicle_bodyF.transform.translate(self.center) self.vehicle_bodyR = visuals.BoxVisual(width=1.0, height=1.0, depth=1.0, color='blue') self.vehicle_bodyR.transform = transforms.MatrixTransform() self.vehicle_bodyR.transform.scale((self.scale*0.25, self.scale*4.0, 1)) self.vehicle_bodyR.transform.rotate(np.rad2deg(-self.model.theta), (0, 0, 1)) self.vehicle_bodyR.transform.translate([self.model.x, self.model.y]) self.vehicle_bodyR.transform.translate(self.center) # Append all the visuals self.visuals.append(self.vehicle_body) self.visuals.append(self.vehicle_bodyF) self.visuals.append(self.vehicle_bodyR) self.visuals.append(self.rear_wheelsL) self.visuals.append(self.rear_wheelsR) self.visuals.append(self.front_wheelsL) self.visuals.append(self.front_wheelsR) self.visuals.append(self.text)
def __init__(self, list_of_charts, theta, omega, g, M, m, kp, kd, A, f, l, scale, x, V, method, control, noise, save_charts, dt, tmax, font_size): VisualModel.__init__(self) self.model = CranePhysicalModel(list_of_charts) self.list_of_charts = list_of_charts self._set_up_system(l=l, g=g, M=M, m=m, theta=theta, omega=omega, x=x, V=V, kp=kp, kd=kd, A=A, f=f, scale=scale, method=method, control=control, noise=noise, save_charts=save_charts, dt=dt, tmax=tmax, font_size=font_size) # Put up a text visual to display time info self.font_size = 24. if font_size is None else font_size self.text = visuals.TextVisual('0:00.00', color='white', pos=[15, 50, 0], anchor_x='left', anchor_y='bottom') self.text.font_size = self.font_size self.hook_length = self.scale * 30.0 self.hook_height = self.scale * 0.2 self.hook = visuals.BoxVisual(width=1.0, height=1.0, depth=1.0, color='grey') self.hook.transform = transforms.MatrixTransform() self.hook.transform.scale((self.hook_length, self.hook_height, 0.001)) self.hook.transform.translate( [self.hook_length / 2.016 + self.scale * self.model.x, 0.0]) self.hook.transform.translate(self.center) self.rod_width = self.scale * self.model.l / 20.0 self.rod = visuals.BoxVisual(width=1.0, height=1.0, depth=1.0, color='green') self.rod.transform = transforms.MatrixTransform() self.rod.transform.scale( (self.rod_width, self.scale * self.model.l, 0.0001)) self.rod.transform.translate([0.0, self.scale * self.model.l / 2.0]) self.rod.transform.rotate(np.rad2deg(self.model.theta), (0, 0, 1)) self.rod.transform.translate([self.scale * self.model.x, 0.0]) self.rod.transform.translate(self.center) # Append all the visuals self.visuals.append(self.rod) self.visuals.append(self.hook) self.visuals.append(self.text)
def __init__(self, list_of_charts, theta, omega, g, m, kp, kd, l, scale, method, control, save_charts, dt, tmax, font_size): VisualModel.__init__(self) self.model = PendulumModel(list_of_charts) self.list_of_charts = list_of_charts self.vis_length = l self._set_up_system(l=l, g=g, m=m, theta=theta, omega=omega, kp=kp, kd=kd, scale=scale, method=method, control=control, save_charts=save_charts, dt=dt, tmax=tmax, font_size=font_size) # Put up a text visual to display time info self.font_size = 24. if font_size is None else font_size self.text = visuals.TextVisual('0:00.00', color='white', pos=[15, 50, 0], anchor_x='left', anchor_y='bottom') self.text.font_size = self.font_size self.params = [] self.params.append( visuals.TextVisual('td', color='white', pos=[15, 80, 0], anchor_x='left', anchor_y='bottom')) self.params.append( visuals.TextVisual('tr_1090', color='white', pos=[15, 100, 0], anchor_x='left', anchor_y='bottom')) self.params.append( visuals.TextVisual('tr_0595', color='white', pos=[15, 120, 0], anchor_x='left', anchor_y='bottom')) self.params.append( visuals.TextVisual('tr_0100', color='white', pos=[15, 140, 0], anchor_x='left', anchor_y='bottom')) self.params.append( visuals.TextVisual('tp', color='white', pos=[15, 160, 0], anchor_x='left', anchor_y='bottom')) self.params.append( visuals.TextVisual('over', color='white', pos=[15, 180, 0], anchor_x='left', anchor_y='bottom')) self.params.append( visuals.TextVisual('ts_2', color='white', pos=[15, 200, 0], anchor_x='left', anchor_y='bottom')) self.params.append( visuals.TextVisual('ts_5', color='white', pos=[15, 220, 0], anchor_x='left', anchor_y='bottom')) for param in self.params: param.font_size = 12 self.hook = visuals.BoxVisual(width=0.25, height=0.25, depth=0.25, color='grey') self.hook.transform = transforms.MatrixTransform() self.hook.transform.scale( (self.scale * self.model.l, self.scale * self.model.l, 0.0001)) self.hook.transform.translate(self.center) self.rod = visuals.BoxVisual(width=self.vis_length / 40, height=self.vis_length / 40, depth=self.vis_length, color='green') self.rod.transform = transforms.MatrixTransform() self.rod.transform.translate([0.0, self.vis_length / 2.0]) self.rod.transform.scale( (self.scale * self.model.l, self.scale * self.model.l, 0.0001)) self.rod.transform.rotate(np.rad2deg(self.model.theta), (0, 0, 1)) self.rod.transform.translate(self.center) # Append all the visuals self.visuals.append(self.rod) self.visuals.append(self.hook) self.visuals.append(self.text) for param in self.params: self.visuals.append(param)