# [MOOC] Apollo Lesson 7: Control

This is my note for lesson 7 of MOOC course: Self-Driving Fundamentals - Featuring Apollo. Content: Understand how to use steering, throttle and brake to execute our planned trajectory and master different types of controllers in Apollo. The control module goal is to use variable control inputs to minimize the deviation from target trajectory and maximize passenger comfort.

**Inputs:**

- Planning trajectory
- Car status
- Localization

**Output:** Control commands (steering, throttle, brake) to the chassis.

**This section review 3 control strategies:**

- Proportional Integral Derivative Control (PID)
- Linear Quadratic Regulator (LQR)
- Model Predictive Control (MPC)

## Proportional–Integral–Derivative (PID) Controller

A proportional–integral–derivative controller (PID controller or three-term controller) is a control loop mechanism employing feedback that is widely used in industrial control systems and a variety of other applications requiring continuously modulated control. A PID controller continuously calculates an error value $e(t)$ as the difference between a desired setpoint (SP) and a measured process variable (PV) and applies a correction based on proportional, integral, and derivative terms (denoted P, I, and D respectively), hence the name.

**-Wikipedia**

**Pros:**

- It's simple but works pretty well in many situations

**Cons:**

- Only a linear algorithm →
**Not good for really complex problems**. For example in self-driving car, we need to apply different PID controller for steering and acceleration → It's hard to combine a latitudinal and longitudinal control. - Depend on realtime error measurement →
**will probably fail with measurement delays**.

**In Apollo:** PID is used in the longitudinal controller.

## Linear–Quadratic Regulator (LQR) Controller

LQR is a model-based controller that uses the state of the vehicle to minimize error. LQR is used for lateral control in Apollo.

**Lateral control state has 4 components:**

- The lateral error
- The rate of change of the lateral error
- The heading error
- The rate of change of the heading

The rate of change is derivative. Here is the mathematical expression of above components:

**3 control inputs to vehicle:**

- Steering
- Acceleration
- Brake

We denote control inputs as $u$:

**LQR handles linear control. Equation:**

Or

The equation is linear because:

**Quadratic cost function:**

## Model Predictive Controller (MPC)

MPC uses a model of the system to make predictions about the system's future behavior. MPC solves an online optimization algorithm to find the optimal control action that drives the predicted output to the reference.

**https://www.mathworks.com/videos/series/understanding-model-predictive-control.html**

**MPC Loop:**

- Build a model of vehicle.

- Use a optimization engine to calculate a sequence of control inputs over a time horizon.

- Implement the first set of control inputs.

- Return to step 2.

**Vehicle model** approximates the physics of our car. It can use vehicle state and control inputs to predict the car's trajectory. MPC defines a cost function and minimize it to optimize the trajectory.

### Pros and Cons

**Pros:**

- It takes vehicle model into consideration →
**More accurate than PID**. - It works with different cost functions →
**Optimize differents costs in different situations**.

**Cons:**

- Complex →
**Slower**at runtime and**Harder**to implement.