|
| 1 | +# cruise.py - Cruise control dynamics and control |
| 2 | +# RMM, 20 Jun 2021 |
| 3 | + |
| 4 | +import numpy as np |
| 5 | +import matplotlib.pyplot as plt |
| 6 | +from math import pi |
| 7 | +import control as ct |
| 8 | + |
| 9 | +# |
| 10 | +# Vehicle model |
| 11 | +# |
| 12 | +# The dynamics for this system are implemented using the I/O systems module |
| 13 | +# in python-control. This model is described in detail in Section 4.1 of |
| 14 | +# FBS2e. |
| 15 | +# |
| 16 | + |
| 17 | +# Engine model |
| 18 | +def motor_torque(omega, params={}): |
| 19 | + # Set up the system parameters |
| 20 | + Tm = params.get('Tm', 190.) # engine torque constant |
| 21 | + omega_m = params.get('omega_m', 420.) # peak engine angular speed |
| 22 | + beta = params.get('beta', 0.4) # peak engine rolloff |
| 23 | + |
| 24 | + return np.clip(Tm * (1 - beta * (omega/omega_m - 1)**2), 0, None) |
| 25 | + |
| 26 | + |
| 27 | +# Vehicle dynamics |
| 28 | +def vehicle_update(t, x, u, params={}): |
| 29 | + """Vehicle dynamics for cruise control system. |
| 30 | +
|
| 31 | + Parameters |
| 32 | + ---------- |
| 33 | + x : array |
| 34 | + System state: car velocity in m/s |
| 35 | + u : array |
| 36 | + System input: [throttle, gear, road_slope], where throttle is |
| 37 | + a float between 0 and 1, gear is an integer between 1 and 5, |
| 38 | + and road_slope is in rad. |
| 39 | +
|
| 40 | + Returns |
| 41 | + ------- |
| 42 | + float |
| 43 | + Vehicle acceleration |
| 44 | +
|
| 45 | + """ |
| 46 | + from math import copysign, sin |
| 47 | + sign = lambda x: copysign(1, x) # define the sign() function |
| 48 | + |
| 49 | + # Set up the system parameters |
| 50 | + m = params.get('m', 1600.) # vehicle mass, kg |
| 51 | + g = params.get('g', 9.8) # gravitational constant, m/s^2 |
| 52 | + Cr = params.get('Cr', 0.01) # coefficient of rolling friction |
| 53 | + Cd = params.get('Cd', 0.32) # drag coefficient |
| 54 | + rho = params.get('rho', 1.3) # density of air, kg/m^3 |
| 55 | + A = params.get('A', 2.4) # car area, m^2 |
| 56 | + alpha = params.get( |
| 57 | + 'alpha', [40, 25, 16, 12, 10]) # gear ratio / wheel radius |
| 58 | + |
| 59 | + # Define variables for vehicle state and inputs |
| 60 | + v = x[0] # vehicle velocity |
| 61 | + throttle = np.clip(u[0], 0, 1) # vehicle throttle |
| 62 | + gear = u[1] # vehicle gear |
| 63 | + theta = u[2] # road slope |
| 64 | + |
| 65 | + # Force generated by the engine |
| 66 | + |
| 67 | + omega = alpha[int(gear)-1] * v # engine angular speed |
| 68 | + F = alpha[int(gear)-1] * motor_torque(omega, params) * throttle |
| 69 | + |
| 70 | + # Disturbance forces |
| 71 | + # |
| 72 | + # The disturbance force Fd has three major components: Fg, the forces due |
| 73 | + # to gravity; Fr, the forces due to rolling friction; and Fa, the |
| 74 | + # aerodynamic drag. |
| 75 | + |
| 76 | + # Letting the slope of the road be \theta (theta), gravity gives the |
| 77 | + # force Fg = m g sin \theta. |
| 78 | + |
| 79 | + Fg = m * g * sin(theta) |
| 80 | + |
| 81 | + # A simple model of rolling friction is Fr = m g Cr sgn(v), where Cr is |
| 82 | + # the coefficient of rolling friction and sgn(v) is the sign of v (±1) or |
| 83 | + # zero if v = 0. |
| 84 | + |
| 85 | + Fr = m * g * Cr * sign(v) |
| 86 | + |
| 87 | + # The aerodynamic drag is proportional to the square of the speed: Fa = |
| 88 | + # 1/2 \rho Cd A |v| v, where \rho is the density of air, Cd is the |
| 89 | + # shape-dependent aerodynamic drag coefficient, and A is the frontal area |
| 90 | + # of the car. |
| 91 | + |
| 92 | + Fa = 1/2 * rho * Cd * A * abs(v) * v |
| 93 | + |
| 94 | + # Final acceleration on the car |
| 95 | + Fd = Fg + Fr + Fa |
| 96 | + dv = (F - Fd) / m |
| 97 | + |
| 98 | + return dv |
| 99 | + |
| 100 | +# Vehicle input/output model |
| 101 | +vehicle_dynamics = ct.nlsys( |
| 102 | + vehicle_update, None, name='vehicle', |
| 103 | + inputs = ['u', 'gear', 'theta'], outputs = ['v'], states=['v']) |
| 104 | + |
| 105 | +# |
| 106 | +# PI controller |
| 107 | +# |
| 108 | +# The controller for the system is designed in Example XX, but is included |
| 109 | +# here since it is needed for some earlier examples. |
| 110 | +# |
| 111 | + |
| 112 | +# Nominal controller design for remaining analyses |
| 113 | +# Construct a PI controller with rolloff, as a transfer function |
| 114 | +Kp = 0.5 # proportional gain |
| 115 | +Ki = 0.1 # integral gain |
| 116 | +PI_control = ct.tf( |
| 117 | + [Kp, Ki], [1, 0.01*Ki/Kp], name='control', inputs='u', outputs='y') |
| 118 | + |
| 119 | +cruise_PI = ct.interconnect( |
| 120 | + (vehicle_dynamics, PI_control), name='cruise', |
| 121 | + connections = [['control.u', '-vehicle.v'], ['vehicle.u', 'control.y']], |
| 122 | + inplist = ['control.u', 'vehicle.gear', 'vehicle.theta'], |
| 123 | + inputs = ['vref', 'gear', 'theta'], |
| 124 | + outlist = ['vehicle.v', 'vehicle.u'], outputs = ['v', 'u']) |
0 commit comments