You can compile the code I propose in this page using Xamarin Studio 5.10.1 (Build 6), with Mono 4.2.1 as active runtime and the Monobrick Firmware Library 1.2.0.39486 to let it work on board the Lego Mindstorms Ev3 with the Monobrick firmware.

The source code above is happily shared under the Code Project Open Licence (CPOL) 1.02.

← Part 2 – The control strategy

### The (simple) code architecture

The code running on board the Sup3r Car in the track following program, is based on a single class indeed called the  SuperCar where i just dropped the “3” in Sup3er. The  SuperCar class starts the following concurrent threads:

• the Sensors Update Thread whose responsibility is to update the readings from the sensors;
• the LCD Thread which updates the Ev3 brick LCD with debug information;
• the Drive Thread that implements the $C_2$ external Drive loop controller described in Part 2;
• the Steering Thread whose thread is responsible of steering the front wheels and represents the $C_1$ internal Steer loop controller described in Part 2;

The responsibility of the Sensors Update Thread is to read the values from the sensors and update the corresponding private fields. This thread asynchronously also feeds the  steerPID.InputSignal with the current value read from the Ev3 Medium Motor tachometer.

The LCD Thread updates the Ev3 Brick LCD panel with useful real-time data, namely the current reflected light value and the current steering angle. You can use these data to understand the correct set-point to assign to the Drive external control loop according to the colors of the line and the floor.

The Drive Thread is maybe the most interesting of this project.

The external Drive loop is a proportional controller with a  dead band on the regulation error; this means that if the absolute value of the regulation error is smaller than the defined threshold, no correction is applied to the vehicle trajectory.

Once the correction, if any, has been computed, an adjustment to the vehicle speed is performed. It means that vehicle slows down when a curve is detected, just as a human driver would do.

The correction to the trajectory is than evaluated against the valid range and eventually saturated to the violated boundary. After that, a fixed additional correction is made to account for the gears backlash; than it is fed  steerPID to be executed.

Last, but very important, thing is to adjust the relative speed of the rear wheels according to direction and intensity of the steering. The Sup3r Car model has no differential gearbox on the rear wheels; so the differential gearbox must be emulated reducing the speed of the internal wheel during a curve.