Drive with PID Control
This demo shows how to simulate a simple closed-loop control algorithm in Simulink® and how to run it on LEGO® MINDSTORMS® NXT™ hardware.
Contents
- Introduction
- Prerequisites
- Required Hardware
- Task 1 - Build the LEGO MINDSTORMS NXT Vehicle
- Task 2 - Simulate Open-Loop Control Model
- Task 3 - Run Open-Loop Control Model on LEGO MINDSTORMS NXT Hardware
- Task 4 - Simulate Closed-Loop Control Model
- Task 5 - Run Closed-Loop Control Model on LEGO MINDSTORMS NXT Hardware
- Other Things to Try
- Summary
Introduction
In a vehicle using independent wheel control, applying the same power to each wheel generally does not result in the vehicle moving straight. This is caused by mechanical and surface differences experienced by each of the wheels. To reduce deviation in the vehicle heading, a better approach is to use a closed-loop controller which adjusts the power applied to two motors based on difference in their rotations. One such controller is a well-known proportional–integral–derivative (PID) controller.
Proportional–integral–derivative control is a basic control loop feedback mechanism. The controller minimizes the difference between the measured and the desired value of a chosen system variable by adjusting the system control inputs.
This demo shows you how to simulate the controller using a simple plant model, first with no feedback control (Open-Loop Control), and then with feedback control (Closed-Loop Control). This demo also illustrates how to switch between simulating the PID controller and running it on hardware in the same model.
Prerequisites
We recommend completing Communicating with LEGO MINDSTORMS NXT Hardware demo.
Required Hardware
- LEGO MINDSTORMS NXT Intelligent Brick
- Two LEGO MINDSTORMS NXT interactive servo motors
- USB cable
- LEGO MINDSTORMS NXT-compatible Bluetooth® dongle (optional for Steps 3 and 5)
Task 1 - Build the LEGO MINDSTORMS NXT Vehicle
1. Build a vehicle using two motors to control two independent wheels. For example, you can use a vehicle similar to LEGO Tri Bot. The building instructions for Tri Bot are given on the LEGO Web site.
2. Connect a USB cable from your computer to LEGO MINDSTORMS NXT Intelligent Brick.
3. If you are using External mode, set up the Bluetooth connection between the computer and LEGO MINDSTORMS NXT Intelligent Brick. See Task 1 of the Communicating with LEGO MINDSTORMS NXT Hardware demo for details.
4. On LEGO MINDSTORMS NXT Intelligent Brick, press the orange button to turn the power on.
Task 2 - Simulate Open-Loop Control Model
This step illustrates that independently powered wheels cause deviations in vehicle heading.
1. Open the model. Observe three different subsystems in the model.
2. Open the Open-Loop Controller subsystem. This subsystem controls the vehicle driving. Observe that the controller does not use the difference between two encoder outputs to control the motors.
3. Notice the Motors subsystem. The subsystem contains both simulated and actual motors. The Environment Controller block takes the outputs of the simulated or actual motors, depending on the current environment. This allows you to represent both simulated and actual motors in one model. As an alternative, you may create two models, one for simulation, and the other one for running on actual hardware.
4. Click Start simulation in the Simulink toolbar. Click the Scope block and observe that the Encoder Output Mismatch increases over time. This indicates that the vehicle will not move straight.
Task 3 - Run Open-Loop Control Model on LEGO MINDSTORMS NXT Hardware
1. If you are using External mode, configure the model as described in Task 2 of the Communicating with LEGO MINDSTORMS NXT Hardware demo.
2. Select Tools > Run on Target Hardware > Run. The model runs on LEGO MINDSTORMS NXT hardware and the vehicle starts moving.
3. Notice that the path of the vehicle is not straight, as predicted by the simulation.
4. If you are using External mode, press the Stop button on the model. Otherwise, press the gray rectangle button on LEGO MINDSTORMS NXT Intelligent Brick to turn the power off.
Task 4 - Simulate Closed-Loop Control Model
1. Open the model. Observe two subsystems in the model.
2. Double-click PID Controller subsystem. Notice that the P control is used to synchronize two motors when the vehicle runs straight. Also, notice that during a turn, no synchronization is applied.
3. Click Start simulation in the Simulink toolbar. Click the Scope block and observe that the Encoder Output Mismatch remains close to zero. This indicates that the vehicle will skew less compared to the Open Loop Control model.
Task 5 - Run Closed-Loop Control Model on LEGO MINDSTORMS NXT Hardware
1. If you are using External mode, configure the model as described in Task 2 of the Communicating with LEGO MINDSTORMS NXT Hardware demo.
2. Select Tools > Run on Target Hardware > Run. The model runs on the LEGO MINDSTORMS NXT hardware and the vehicle starts moving.
3. Observe that the path of the vehicle is more straight, as predicted by the simulation.
4. If you are using External mode, press the Stop button on the model. Otherwise, press the gray rectangle button on LEGO MINDSTORMS NXT Intelligent Brick to turn the power off.
Other Things to Try
- Adjust the PID Controller settings. Improve the vehicle’s ability to move straight on a rough or tilted surface.
Summary
This demo showed how to simulate and implements a basic closed-loop controller on LEGO MINDSTORMS NXT hardware. In the demo you learned that:
- Open-Loop Control does not ensure straight driving in a vehicle with independently powered wheels.
- Closed-Loop Control uses the difference between two encoder outputs to calculate the power that should be applied to each wheel individually.
- Both simulated and actual hardware may be used in the same model, provided that a mechanism for switching between them exists.
