Real-Time Vehicle Control Systems

Our vehicles are becoming more and more computer systems, computer systems with wheels, wings, propellors, and many variations such as caterpillar tracks.

Real-time vehicle control and monitoring is an important and complex scenario for advanced C++ development. In this scenario software has to be developed that can be entrusted with the well-being of the vehicle’s crew and passengers. In this scenario, we examine what libraries might prove to be valuable building blocks, what kind of architecture and control flow might be involved, and best-practices for safe systems.

As an example, we will look at a work-horse vehicle, the caterpillar tractor. The following image shows how much a modern interface might influence the design of the interior of the cab:

Caterpillar Tractor Controls

With this high-tech control system in mind, let’s examine the role individual Boost libraries might play.

Libraries

Lbrary Possible role in the tractor

Boost.Asio

Real-time-ish I/O, CAN adapters, Ethernet, serial devices, timers

Boost.Atomic

Lock-free/shared state between control threads

Boost.Lockfree

Sensor/command queues between acquisition and control threads

Boost.Thread

Worker threads and synchronization

Boost.Chrono

Precise timing and control-loop periods

Boost.Units

Prevent mixing meters, feet, radians, PSI, kPa, etc.

Boost.Numeric/odeint

Simulating vehicle dynamics and hydraulic/engine systems

Boost.Math

Filters, interpolation, statistics, numerical functions

Boost.Geometry

Vehicle position, heading, paths and spatial calculations

Boost.CircularBuffer

Recent sensor history / moving windows

Boost.MultiIndex

Efficiently indexing large collections of sensor/device data

Boost.Log

Diagnostics and event logging

Boost.Stacktrace

Diagnostic information after failures

Boost.System

Portable error handling

Boost.ProgramOptions

Configuration/calibration parameters

Boost.Json

Configuration and diagnostic interfaces

Boost.Serialization

Saving/restoring configuration or state

Boost.Statechart

Vehicle operating modes and state machines

Boost.Msm

High-performance state-machine implementation, if appropriate

Boost.SafeNumerics

Detecting arithmetic errors in safety-sensitive calculations

Boost.Test

Automated testing of control algorithms

Architecture

The basic architecture of a vehicle control app could be as simple as:

              every 10 ms

                  ┌───────────────┐
                  │ Read sensors  │
                  └───────┬───────┘
                          │
                          ▼
                  ┌───────────────┐
                  │ Validate data │
                  └───────┬───────┘
                          │
                          ▼
                  ┌───────────────┐
                  │ Update state  │
                  └───────┬───────┘
                          │
                          ▼
                  ┌───────────────┐
                  │ Control law   │
                  │ PID / model   │
                  └───────┬───────┘
                          │
                          ▼
                  ┌───────────────┐
                  │ Send commands │
                  └───────┬───────┘
                          │
                          ▼
            wait until next 10 ms starts

PID stands for Proportional-Integral-Derivative. It is one of the most common feedback-control algorithms used in engineering. (1)

Footnotes

(1) In the tractor example, “Control law — PID/model” means that the software controlling things such as hydraulic valves, blade position, engine speed, or vehicle speed could use either a PID controller, a model-based controller, or a combination of the two. Suppose the tractor’s blade is supposed to be at a particular height. The controller continuously compares desired blade height against actual blade height, the delta being treated as an error. A PID controller uses that error in three ways:

Part Meaning Intuition

P

Proportional

“How far away am I right now?”

I

Integral

“How long have I been wrong?”

D

Derivative

“How quickly am I approaching the target?”

The P component might say: "We’re 10 cm low, so raise the blade substantially" and as the blade gets closer: "We’re only 2 cm low, so raise it much more gently". The I component notices if, for example, the blade consistently stays 1 cm below the target because of hydraulic leakage or load. The D component notices that the blade is moving rapidly toward the target and reduces the command - to help prevent overshooting.