def test_fbs_bode(self): ct.use_fbs_defaults() # Generate a Bode plot plt.figure() omega = np.logspace(-3, 3, 100) ct.bode_plot(self.sys, omega) # Get the magnitude line mag_axis = plt.gcf().axes[0] mag_line = mag_axis.get_lines() mag_data = mag_line[0].get_data() mag_x, mag_y = mag_data # Make sure the x-axis is in rad/sec and y-axis is in natural units np.testing.assert_almost_equal(mag_x[0], 0.001, decimal=6) np.testing.assert_almost_equal(mag_y[0], 10, decimal=3) # Get the phase line phase_axis = plt.gcf().axes[1] phase_line = phase_axis.get_lines() phase_data = phase_line[0].get_data() phase_x, phase_y = phase_data # Make sure the x-axis is in rad/sec and y-axis is in degrees np.testing.assert_almost_equal(phase_x[-1], 1000, decimal=0) np.testing.assert_almost_equal(phase_y[-1], -180, decimal=0) # Override the defaults and make sure that works as well plt.figure() ct.bode_plot(self.sys, omega, dB=True) mag_x, mag_y = (((plt.gcf().axes[0]).get_lines())[0]).get_data() np.testing.assert_almost_equal(mag_y[0], 20 * log10(10), decimal=3) plt.figure() ct.bode_plot(self.sys, omega, Hz=True) mag_x, mag_y = (((plt.gcf().axes[0]).get_lines())[0]).get_data() np.testing.assert_almost_equal(mag_x[0], 0.001 / (2 * pi), decimal=6) plt.figure() ct.bode_plot(self.sys, omega, deg=False) phase_x, phase_y = (((plt.gcf().axes[1]).get_lines())[0]).get_data() np.testing.assert_almost_equal(phase_y[-1], -pi, decimal=2) ct.reset_defaults()
def test_fbs_bode(self): ct.use_fbs_defaults(); # Generate a Bode plot plt.figure() omega = np.logspace(-3, 3, 100) ct.bode_plot(self.sys, omega) # Get the magnitude line mag_axis = plt.gcf().axes[0] mag_line = mag_axis.get_lines() mag_data = mag_line[0].get_data() mag_x, mag_y = mag_data # Make sure the x-axis is in rad/sec and y-axis is in natural units np.testing.assert_almost_equal(mag_x[0], 0.001, decimal=6) np.testing.assert_almost_equal(mag_y[0], 10, decimal=3) # Get the phase line phase_axis = plt.gcf().axes[1] phase_line = phase_axis.get_lines() phase_data = phase_line[0].get_data() phase_x, phase_y = phase_data # Make sure the x-axis is in rad/sec and y-axis is in degrees np.testing.assert_almost_equal(phase_x[-1], 1000, decimal=0) np.testing.assert_almost_equal(phase_y[-1], -180, decimal=0) # Override the defaults and make sure that works as well plt.figure() ct.bode_plot(self.sys, omega, dB=True) mag_x, mag_y = (((plt.gcf().axes[0]).get_lines())[0]).get_data() np.testing.assert_almost_equal(mag_y[0], 20*log10(10), decimal=3) plt.figure() ct.bode_plot(self.sys, omega, Hz=True) mag_x, mag_y = (((plt.gcf().axes[0]).get_lines())[0]).get_data() np.testing.assert_almost_equal(mag_x[0], 0.001 / (2*pi), decimal=6) plt.figure() ct.bode_plot(self.sys, omega, deg=False) phase_x, phase_y = (((plt.gcf().axes[1]).get_lines())[0]).get_data() np.testing.assert_almost_equal(phase_y[-1], -pi, decimal=2) ct.reset_defaults()
#!/usr/bin/env python # coding: utf-8 import numpy as np import matplotlib.pyplot as plt import control as ct import control.optimal as opt ct.use_fbs_defaults() # # # ## Vehicle steering dynamics # ## def vehicle_update(t, x, u, params): # Get the parameters for the model a = params.get('refoffset', 1.5) # offset to vehicle reference point b = params.get('wheelbase', 3.) # vehicle wheelbase maxsteer = params.get('maxsteer', 0.5) # max steering angle (rad) # Saturate the steering input delta = np.clip(u[1], -maxsteer, maxsteer) alpha = np.arctan2(a * np.tan(delta), b) # Return the derivative of the state return np.array([ u[0] * np.cos(x[2] + alpha), # xdot = cos(theta + alpha) v u[0] * np.sin(x[2] + alpha), # ydot = sin(theta + alpha) v (u[0] / b) * np.tan(delta) # thdot = v/l tan(phi)