# Enforce Barrier Certificate Constraints for Adaptive Cruise Control

This example shows how to enforce barrier certificate constraints for adaptive cruise control (ACC) using the Barrier Certificate Enforcement block.

### Overview

In this example, the goal is to make an ego car travel at a set velocity while maintaining a safe distance from a lead car by controlling longitudinal acceleration and braking.

Configure model parameters.

```x0_lead = 52; % Initial position for lead car (m) v0_lead = 25; % Initial velocity for lead car (m/s) x0_ego = 10; % Initial position for ego car (m) v0_ego = 20; % Initial velocity for ego car (m/s) default_spacing = 10; % Default spacing (m) time_gap = 1.5; % Time gap (s) v_set = 30; % Driver-set velocity (m/s) min_ac = -3; % Minimum acceleration for driver comfort (m/s^2) max_ac = 2; % Maximum acceleration for driver comfort (m/s^2) Ts = 0.1; % Sample time (s) T = 80; % Duration (s)```

Open the model.

```mdl = 'barrierCertificateACC'; open_system(mdl)```

### Controller Design

The controller design for ACC is based on the following principles.

• If the relative distance is less than the safe distance, then the primary goal is to slow down and maintain a safe distance.

• If the relative distance is greater than the safe distance, then the primary goal is to reach the driver-set velocity while maintaining a safe distance.

The safe distance is defined as a function of velocity.

`${\mathit{d}}_{\mathrm{safe}}=\tau \mathit{v}+{\mathit{d}}_{0}$`

Here:

• ${\mathit{d}}_{0}$ is the default spacing.

• $\tau$ is the time gap.

• $\mathit{v}$ is the longitudinal velocity of the ego vehicle.

For an example that applies the same controller structure and design principles, see Adaptive Cruise Control with Sensor Fusion (Model Predictive Control Toolbox).

Specify the gains.

```verr_gain = 0.5; % ACC velocity error gain xerr_gain = 0.1; % ACC spacing error gain vx_gain = 0.2; % ACC relative velocity gain ```

Before you apply any constraints, run the Simulink® model and view the results.

```constrained = 0; sim(mdl); accPlotResults(logsout,default_spacing,time_gap,v_set);```

### Barrier Certificate Constraints

For the ACC application, the safety set is defined as the relative distance $\mathit{d}\ge {\mathit{d}}_{\mathrm{safe}}$. Therefore, the barrier certificate is given by the safety offset $\mathit{h}=\mathit{d}-{\mathit{d}}_{\mathrm{safe}}\ge 0$.

The plant dynamics are described by the following equations.

`$\stackrel{˙}{\mathit{p}}=\mathit{v}$`

`$\stackrel{˙}{\mathit{v}}=\mathit{u}$`

`$\stackrel{˙}{\mathit{d}}=\mathit{v}-{\mathit{v}}_{\mathit{l}}$`

Here, $\mathit{p}$ is the position for the ego car and ${\mathit{v}}_{\mathit{l}}$ is the velocity for lead car.

The barrier certificate $\mathit{h}$ is a function of states $\mathit{x}=\left[\begin{array}{c}\mathit{p}\\ \mathit{v}\\ \mathit{d}\end{array}\right]$ and is given by $\mathit{h}\left(\mathit{x}\right)=\mathit{d}-\tau \mathit{v}-{\mathit{d}}_{0}$. The partial derivative of $\mathit{h}$ over states is given by $\mathit{q}\left(\mathit{x}\right)=\left[\begin{array}{ccc}0& -\tau & 1\end{array}\right]$.

The Barrier Certificate Enforcement block accepts plant dynamics in the form $\stackrel{˙}{\mathit{x}}=\mathit{f}\left(\mathit{x}\right)+\mathit{g}\left(\mathit{x}\right)\mathit{u}$. For this application, $\mathit{f}\left(\mathit{x}\right)=\left[\begin{array}{c}\mathit{v}\\ 0\\ \mathit{v}-{\mathit{v}}_{\mathit{l}}\end{array}\right]$ and $\mathit{g}\left(\mathit{x}\right)=\left[\begin{array}{c}0\\ 1\\ 0\end{array}\right]$.

### Simulate ACC Controller with Barrier Certificate Constraint

To view the constraint implementation, open the `Constraint` > `Constrained` subsystem.

Enable the constraints.

`constrained = 1;`

Run the model and plot the simulation results.

```sim(mdl); accPlotResults(logsout,default_spacing,time_gap,v_set);```

The plot shows that the barrier certificate (safety offset) is nonnegative. The relative distance is always greater than the defined safe distance.

The Barrier Certificate Enforcement block successfully constrains the control actions such that the relative distance is greater than the safe distance.

Close the model.

`bdclose(mdl)`