Cruise control design example (as a nonlinear I/O system)

Code

  1# cruise-control.py - Cruise control example from FBS
  2# RMM, 16 May 2019
  3#
  4# The cruise control system of a car is a common feedback system encountered
  5# in everyday life. The system attempts to maintain a constant velocity in the
  6# presence of disturbances primarily caused by changes in the slope of a
  7# road. The controller compensates for these unknowns by measuring the speed
  8# of the car and adjusting the throttle appropriately.
  9#
 10# This file explores the dynamics and control of the cruise control system,
 11# following the material presented in Feedback Systems by Astrom and Murray.
 12# A full nonlinear model of the vehicle dynamics is used, with both PI and
 13# state space control laws.  Different methods of constructing control systems
 14# are shown, all using the InputOutputSystem class (and subclasses).
 15
 16import numpy as np
 17import matplotlib.pyplot as plt
 18from math import pi
 19import control as ct
 20
 21#
 22# Section 4.1: Cruise control modeling and control
 23#
 24
 25# Vehicle model: vehicle()
 26#
 27# To develop a mathematical model we start with a force balance for
 28# the car body. Let v be the speed of the car, m the total mass
 29# (including passengers), F the force generated by the contact of the
 30# wheels with the road, and Fd the disturbance force due to gravity,
 31# friction, and aerodynamic drag.
 32
 33def vehicle_update(t, x, u, params={}):
 34    """Vehicle dynamics for cruise control system.
 35
 36    Parameters
 37    ----------
 38    x : array
 39         System state: car velocity in m/s
 40    u : array
 41         System input: [throttle, gear, road_slope], where throttle is
 42         a float between 0 and 1, gear is an integer between 1 and 5,
 43         and road_slope is in rad.
 44
 45    Returns
 46    -------
 47    float
 48        Vehicle acceleration
 49
 50    """
 51    from math import copysign, sin
 52    sign = lambda x: copysign(1, x)         # define the sign() function
 53    
 54    # Set up the system parameters
 55    m = params.get('m', 1600.)
 56    g = params.get('g', 9.8)
 57    Cr = params.get('Cr', 0.01)
 58    Cd = params.get('Cd', 0.32)
 59    rho = params.get('rho', 1.3)
 60    A = params.get('A', 2.4)
 61    alpha = params.get(
 62        'alpha', [40, 25, 16, 12, 10])      # gear ratio / wheel radius
 63
 64    # Define variables for vehicle state and inputs
 65    v = x[0]                           # vehicle velocity
 66    throttle = np.clip(u[0], 0, 1)     # vehicle throttle
 67    gear = u[1]                        # vehicle gear
 68    theta = u[2]                       # road slope
 69
 70    # Force generated by the engine
 71
 72    omega = alpha[int(gear)-1] * v      # engine angular speed
 73    F = alpha[int(gear)-1] * motor_torque(omega, params) * throttle
 74
 75    # Disturbance forces
 76    #
 77    # The disturbance force Fd has three major components: Fg, the forces due
 78    # to gravity; Fr, the forces due to rolling friction; and Fa, the
 79    # aerodynamic drag.
 80
 81    # Letting the slope of the road be \theta (theta), gravity gives the
 82    # force Fg = m g sin \theta.
 83    
 84    Fg = m * g * sin(theta)
 85
 86    # A simple model of rolling friction is Fr = m g Cr sgn(v), where Cr is
 87    # the coefficient of rolling friction and sgn(v) is the sign of v (+/- 1) or
 88    # zero if v = 0.
 89    
 90    Fr = m * g * Cr * sign(v)
 91
 92    # The aerodynamic drag is proportional to the square of the speed: Fa =
 93    # 1/\rho Cd A |v| v, where \rho is the density of air, Cd is the
 94    # shape-dependent aerodynamic drag coefficient, and A is the frontal area
 95    # of the car.
 96
 97    Fa = 1/2 * rho * Cd * A * abs(v) * v
 98    
 99    # Final acceleration on the car
100    Fd = Fg + Fr + Fa
101    dv = (F - Fd) / m
102    
103    return dv
104
105# Engine model: motor_torque
106#
107# The force F is generated by the engine, whose torque is proportional to
108# the rate of fuel injection, which is itself proportional to a control
109# signal 0 <= u <= 1 that controls the throttle position. The torque also
110# depends on engine speed omega.
111    
112def motor_torque(omega, params={}):
113    # Set up the system parameters
114    Tm = params.get('Tm', 190.)             # engine torque constant
115    omega_m = params.get('omega_m', 420.)   # peak engine angular speed
116    beta = params.get('beta', 0.4)          # peak engine rolloff
117
118    return np.clip(Tm * (1 - beta * (omega/omega_m - 1)**2), 0, None)
119
120# Define the input/output system for the vehicle
121vehicle = ct.NonlinearIOSystem(
122    vehicle_update, None, name='vehicle',
123    inputs=('u', 'gear', 'theta'), outputs=('v'), states=('v'))
124
125# Figure 1.11: A feedback system for controlling the speed of a vehicle. In
126# this example, the speed of the vehicle is measured and compared to the
127# desired speed.  The controller is a PI controller represented as a transfer
128# function.  In the textbook, the simulations are done for LTI systems, but
129# here we simulate the full nonlinear system.
130
131# Construct a PI controller with rolloff, as a transfer function
132Kp = 0.5                        # proportional gain
133Ki = 0.1                        # integral gain
134control_tf = ct.tf2io(
135    ct.TransferFunction([Kp, Ki], [1, 0.01*Ki/Kp]),
136    name='control', inputs='u', outputs='y')
137
138# Construct the closed loop control system
139# Inputs: vref, gear, theta
140# Outputs: v (vehicle velocity)
141cruise_tf = ct.InterconnectedSystem(
142    (control_tf, vehicle), name='cruise',
143    connections=(
144        ['control.u', '-vehicle.v'],
145        ['vehicle.u', 'control.y']),
146    inplist=('control.u', 'vehicle.gear', 'vehicle.theta'),
147    inputs=('vref', 'gear', 'theta'),
148    outlist=('vehicle.v', 'vehicle.u'),
149    outputs=('v', 'u'))
150
151# Define the time and input vectors
152T = np.linspace(0, 25, 101)
153vref = 20 * np.ones(T.shape)
154gear = 4 * np.ones(T.shape)
155theta0 = np.zeros(T.shape)
156
157# Now simulate the effect of a hill at t = 5 seconds
158plt.figure()
159plt.suptitle('Response to change in road slope')
160vel_axes = plt.subplot(2, 1, 1)
161inp_axes = plt.subplot(2, 1, 2)
162theta_hill = np.array([
163    0 if t <= 5 else
164    4./180. * pi * (t-5) if t <= 6 else
165    4./180. * pi for t in T])
166
167for m in (1200, 1600, 2000):
168    # Compute the equilibrium state for the system
169    X0, U0 = ct.find_eqpt(
170        cruise_tf, [0, vref[0]], [vref[0], gear[0], theta0[0]], 
171        iu=[1, 2], y0=[vref[0], 0], iy=[0], params={'m': m})
172
173    t, y = ct.input_output_response(
174        cruise_tf, T, [vref, gear, theta_hill], X0, params={'m': m})
175
176    # Plot the velocity
177    plt.sca(vel_axes)
178    plt.plot(t, y[0])
179
180    # Plot the input
181    plt.sca(inp_axes)
182    plt.plot(t, y[1])
183
184# Add labels to the plots
185plt.sca(vel_axes)
186plt.ylabel('Speed [m/s]')
187plt.legend(['m = 1000 kg', 'm = 2000 kg', 'm = 3000 kg'], frameon=False)
188
189plt.sca(inp_axes)
190plt.ylabel('Throttle')
191plt.xlabel('Time [s]')
192
193# Figure 4.2: Torque curves for a typical car engine. The graph on the
194# left shows the torque generated by the engine as a function of the
195# angular velocity of the engine, while the curve on the right shows
196# torque as a function of car speed for different gears.
197
198# Figure 4.2
199fig, axes = plt.subplots(1, 2, figsize=(7, 3))
200
201# (a) - single torque curve as function of omega
202ax = axes[0]
203omega = np.linspace(0, 700, 701)
204ax.plot(omega, motor_torque(omega))
205ax.set_xlabel(r'Angular velocity $\omega$ [rad/s]')
206ax.set_ylabel('Torque $T$ [Nm]')
207ax.grid(True, linestyle='dotted')
208
209# (b) - torque curves in different gears, as function of velocity
210ax = axes[1]
211v = np.linspace(0, 70, 71)
212alpha = [40, 25, 16, 12, 10]
213for gear in range(5):
214    omega = alpha[gear] * v
215    T = motor_torque(omega)
216    plt.plot(v, T, color='#1f77b4', linestyle='solid')
217
218# Set up the axes and style
219ax.axis([0, 70, 100, 200])
220ax.grid(True, linestyle='dotted')
221
222# Add labels
223plt.text(11.5, 120, '$n$=1')
224ax.text(24, 120, '$n$=2')
225ax.text(42.5, 120, '$n$=3')
226ax.text(58.5, 120, '$n$=4')
227ax.text(58.5, 185, '$n$=5')
228ax.set_xlabel('Velocity $v$ [m/s]')
229ax.set_ylabel('Torque $T$ [Nm]')
230
231plt.suptitle('Torque curves for typical car engine')
232plt.tight_layout()
233plt.show(block=False)
234
235# Figure 4.3: Car with cruise control encountering a sloping road
236
237# PI controller model: control_pi()
238#
239# We add to this model a feedback controller that attempts to regulate the
240# speed of the car in the presence of disturbances. We shall use a
241# proportional-integral controller
242
243def pi_update(t, x, u, params={}):
244    # Get the controller parameters that we need
245    ki = params.get('ki', 0.1)
246    kaw = params.get('kaw', 2)  # anti-windup gain
247
248    # Assign variables for inputs and states (for readability)
249    v = u[0]                    # current velocity
250    vref = u[1]                 # reference velocity
251    z = x[0]                    # integrated error
252
253    # Compute the nominal controller output (needed for anti-windup)
254    u_a = pi_output(t, x, u, params)
255
256    # Compute anti-windup compensation (scale by ki to account for structure)
257    u_aw = kaw/ki * (np.clip(u_a, 0, 1) - u_a) if ki != 0 else 0
258
259    # State is the integrated error, minus anti-windup compensation
260    return (vref - v) + u_aw
261
262def pi_output(t, x, u, params={}):
263    # Get the controller parameters that we need
264    kp = params.get('kp', 0.5)
265    ki = params.get('ki', 0.1)
266
267    # Assign variables for inputs and states (for readability)
268    v = u[0]                    # current velocity
269    vref = u[1]                 # reference velocity
270    z = x[0]                    # integrated error
271
272    # PI controller
273    return kp * (vref - v) + ki * z
274
275control_pi = ct.NonlinearIOSystem(
276    pi_update, pi_output, name='control',
277    inputs=['v', 'vref'], outputs=['u'], states=['z'],
278    params={'kp': 0.5, 'ki': 0.1})
279
280# Create the closed loop system
281cruise_pi = ct.InterconnectedSystem(
282    (vehicle, control_pi), name='cruise',
283    connections=(
284        ['vehicle.u', 'control.u'],
285        ['control.v', 'vehicle.v']),
286    inplist=('control.vref', 'vehicle.gear', 'vehicle.theta'),
287    outlist=('control.u', 'vehicle.v'), outputs=['u', 'v'])
288
289# Figure 4.3b shows the response of the closed loop system.  The figure shows
290# that even if the hill is so steep that the throttle changes from 0.17 to
291# almost full throttle, the largest speed error is less than 1 m/s, and the
292# desired velocity is recovered after 20 s.
293
294# Define a function for creating a "standard" cruise control plot
295def cruise_plot(sys, t, y, label=None, t_hill=None, vref=20, antiwindup=False,
296                linetype='b-', subplots=None, legend=None):
297    if subplots is None:
298        subplots = [None, None]
299    # Figure out the plot bounds and indices
300    v_min = vref-1.2; v_max = vref+0.5; v_ind = sys.find_output('v')
301    u_min = 0; u_max = 2 if antiwindup else 1; u_ind = sys.find_output('u')
302
303    # Make sure the upper and lower bounds on v are OK
304    while max(y[v_ind]) > v_max: v_max += 1
305    while min(y[v_ind]) < v_min: v_min -= 1
306
307    # Create arrays for return values
308    subplot_axes = list(subplots)
309
310    # Velocity profile
311    if subplot_axes[0] is None:
312        subplot_axes[0] = plt.subplot(2, 1, 1)
313    else:
314        plt.sca(subplots[0])
315    plt.plot(t, y[v_ind], linetype)
316    plt.plot(t, vref*np.ones(t.shape), 'k-')
317    if t_hill:
318        plt.axvline(t_hill, color='k', linestyle='--', label='t hill')
319    plt.axis([0, t[-1], v_min, v_max])
320    plt.xlabel('Time $t$ [s]')
321    plt.ylabel('Velocity $v$ [m/s]')
322
323    # Commanded input profile
324    if subplot_axes[1] is None:
325        subplot_axes[1] = plt.subplot(2, 1, 2)
326    else:
327        plt.sca(subplots[1])
328    plt.plot(t, y[u_ind], 'r--' if antiwindup else linetype, label=label)
329    # Applied input profile
330    if antiwindup:
331        # TODO: plot the actual signal from the process?
332        plt.plot(t, np.clip(y[u_ind], 0, 1), linetype, label='Applied')
333    if t_hill:
334        plt.axvline(t_hill, color='k', linestyle='--')
335    if legend:
336        plt.legend(frameon=False)
337    plt.axis([0, t[-1], u_min, u_max])
338    plt.xlabel('Time $t$ [s]')
339    plt.ylabel('Throttle $u$')
340
341    return subplot_axes
342
343# Define the time and input vectors
344T = np.linspace(0, 30, 101)
345vref = 20 * np.ones(T.shape)
346gear = 4 * np.ones(T.shape)
347theta0 = np.zeros(T.shape)
348
349# Compute the equilibrium throttle setting for the desired speed (solve for x
350# and u given the gear, slope, and desired output velocity)
351X0, U0, Y0 = ct.find_eqpt(
352    cruise_pi, [vref[0], 0], [vref[0], gear[0], theta0[0]],
353    y0=[0, vref[0]], iu=[1, 2], iy=[1], return_y=True)
354
355# Now simulate the effect of a hill at t = 5 seconds
356plt.figure()
357plt.suptitle('Car with cruise control encountering sloping road')
358theta_hill = [
359    0 if t <= 5 else
360    4./180. * pi * (t-5) if t <= 6 else
361    4./180. * pi for t in T]
362t, y = ct.input_output_response(cruise_pi, T, [vref, gear, theta_hill], X0)
363cruise_plot(cruise_pi, t, y, t_hill=5)
364
365#
366# Example 7.8: State space feedback with integral action
367#
368
369# State space controller model: control_sf_ia()
370#
371# Construct a state space controller with integral action, linearized around
372# an equilibrium point.  The controller is constructed around the equilibrium
373# point (x_d, u_d) and includes both feedforward and feedback compensation.
374#
375# Controller inputs: (x, y, r)    system states, system output, reference
376# Controller state:  z            integrated error (y - r)
377# Controller output: u            state feedback control
378#
379# Note: to make the structure of the controller more clear, we implement this
380# as a "nonlinear" input/output module, even though the actual input/output
381# system is linear.  This also allows the use of parameters to set the
382# operating point and gains for the controller.
383
384def sf_update(t, z, u, params={}):
385    y, r = u[1], u[2]
386    return y - r
387
388def sf_output(t, z, u, params={}):
389    # Get the controller parameters that we need
390    K = params.get('K', 0)
391    ki = params.get('ki', 0)
392    kf = params.get('kf', 0)
393    xd = params.get('xd', 0)
394    yd = params.get('yd', 0)
395    ud = params.get('ud', 0)
396
397    # Get the system state and reference input
398    x, y, r = u[0], u[1], u[2]
399
400    return ud - K * (x - xd) - ki * z + kf * (r - yd)
401
402# Create the input/output system for the controller
403control_sf = ct.NonlinearIOSystem(
404    sf_update, sf_output, name='control',
405    inputs=('x', 'y', 'r'),
406    outputs=('u'),
407    states=('z'))
408
409# Create the closed loop system for the state space controller
410cruise_sf = ct.InterconnectedSystem(
411    (vehicle, control_sf), name='cruise',
412    connections=(
413        ['vehicle.u', 'control.u'],
414        ['control.x', 'vehicle.v'],
415        ['control.y', 'vehicle.v']),
416    inplist=('control.r', 'vehicle.gear', 'vehicle.theta'),
417    outlist=('control.u', 'vehicle.v'), outputs=['u', 'v'])
418
419# Compute the linearization of the dynamics around the equilibrium point
420
421# Y0 represents the steady state with PI control => we can use it to
422# identify the steady state velocity and required throttle setting.
423xd = Y0[1]
424ud = Y0[0]
425yd = Y0[1]
426
427# Compute the linearized system at the eq pt
428cruise_linearized = ct.linearize(vehicle, xd, [ud, gear[0], 0])
429
430# Construct the gain matrices for the system
431A, B, C = cruise_linearized.A, cruise_linearized.B[0, 0], cruise_linearized.C
432K = 0.5
433kf = -1 / (C * np.linalg.inv(A - B * K) * B)
434
435# Response of the system with no integral feedback term
436plt.figure()
437plt.suptitle('Cruise control with proportional and PI control')
438theta_hill = [
439    0 if t <= 8 else
440    4./180. * pi * (t-8) if t <= 9 else
441    4./180. * pi for t in T]
442t, y = ct.input_output_response(
443    cruise_sf, T, [vref, gear, theta_hill], [X0[0], 0],
444    params={'K': K, 'kf': kf, 'ki': 0.0, 'kf': kf, 'xd': xd, 'ud': ud, 'yd': yd})
445subplots = cruise_plot(cruise_sf, t, y, label='Proportional', linetype='b--')
446
447# Response of the system with state feedback + integral action
448t, y = ct.input_output_response(
449    cruise_sf, T, [vref, gear, theta_hill], [X0[0], 0],
450    params={'K': K, 'kf': kf, 'ki': 0.1, 'kf': kf, 'xd': xd, 'ud': ud, 'yd': yd})
451cruise_plot(cruise_sf, t, y, label='PI control', t_hill=8, linetype='b-',
452            subplots=subplots, legend=True)
453
454# Example 11.5: simulate the effect of a (steeper) hill at t = 5 seconds
455#
456# The windup effect occurs when a car encounters a hill that is so steep (6
457# deg) that the throttle saturates when the cruise controller attempts to
458# maintain speed.
459
460plt.figure()
461plt.suptitle('Cruise control with integrator windup')
462T = np.linspace(0, 70, 101)
463vref = 20 * np.ones(T.shape)
464theta_hill = [
465    0 if t <= 5 else
466    6./180. * pi * (t-5) if t <= 6 else
467    6./180. * pi for t in T]
468t, y = ct.input_output_response(
469    cruise_pi, T, [vref, gear, theta_hill], X0,
470    params={'kaw': 0})
471cruise_plot(cruise_pi, t, y, label='Commanded', t_hill=5, antiwindup=True,
472            legend=True)
473
474# Example 11.6: add anti-windup compensation
475#
476# Anti-windup can be applied to the system to improve the response. Because of
477# the feedback from the actuator model, the output of the integrator is
478# quickly reset to a value such that the controller output is at the
479# saturation limit.
480
481plt.figure()
482plt.suptitle('Cruise control with integrator anti-windup protection')
483t, y = ct.input_output_response(
484    cruise_pi, T, [vref, gear, theta_hill], X0,
485    params={'kaw': 2.})
486cruise_plot(cruise_pi, t, y, label='Commanded', t_hill=5, antiwindup=True,
487            legend=True)
488
489# If running as a standalone program, show plots and wait before closing
490import os
491if __name__ == '__main__' and 'PYCONTROL_TEST_EXAMPLES' not in os.environ:
492    plt.show()
493else:
494    plt.show(block=False)

Notes

1. The environment variable PYCONTROL_TEST_EXAMPLES is used for testing to turn off plotting of the outputs.