This example shows implementation of a controller for a self-balancing two-wheel robot built with LEGO® MINDSTORMS® NXT™ hardware. The example illustrates basic concepts of balance and drive control using MATLAB® and Simulink®, and Simulink Support Package for LEGO MINDSTORMS NXT Hardware.
|On this page…|
A robot is an automatic device that performs functions usually attributed to humans. The robots are normally controlled by embedded processors that execute a controller algorithm. A robot usually operates autonomously, but sometimes can be assisted by a human over a communication channel.
The robot in this example uses feedback from a single-axis gyro sensor to maintain vertical balance while it moves around on two motorized wheels. The robot's controller handles both driving and vertical balance. The drive control is identical to the one used in the Drive with PID ControlDrive with PID Control example. The balance control is implemented by a two-wheeled inverted pendulum controller.
In this example, you will first verify the robot's controller by running it in simulation. In simulation, you will use simulated hardware to confirm the controller correct operation. Then, you will validate the controller by running it on actual hardware. As you run the controller on the robot, you will be able to change the controller parameters in order to modify the robot's behavior.
LEGO MINDSTORMS NXT Base Set
HiTechnic NXT Gyro Sensor (NGY1044)
LEGO MINDSTORMS NXT-compatible Bluetooth® dongle (optional)
2. Connect the USB cable from your computer to the robot.
3. If you are using Bluetooth, set up the Bluetooth connection between the computer and the robot. For more details, see the Task 1 in Communicating with LEGO MINDSTORMS NXT HardwareCommunicating with LEGO MINDSTORMS NXT Hardware example.
4. On LEGO MINDSTORMS NXT Intelligent Brick, press the orange button to turn the power on.
2. Double-click the Controller subsystem to open it. Next, open the Balance and Drive Control subsystem. In the Balance and Drive Control subsystem, open the PWM Control subsystem and notice the PID Control subsystem. This is the same controller that is used in the Drive with PID ControlDrive with PID Control example to ensure that the vehicle drives straight.
3. Open the Hardware subsystem. The subsystem contains both simulated and actual motors, as well as the associated sensors and actuators. Notice that the Environment Controller block takes either simulated or actual sensor signals, depending on the current environment.
4. Click Run button in the Simulink toolbar. Next, double-click the Scope block in the Hardware subsystem. Notice that the Vertical Displacement signal remains close to 0 during the simulation. This indicates that the robot will be stable.
5. Use the source blocks providing the Speed and Turn Angle inputs to the Controller subsystem to change the robot's speed and direction. Observe that the robot remains stable even while moving.
1. In the model, click the Deploy To Hardware button on the toolbar.
2. Place the robot on the ground and hold it in vertical position. A series of short beeps indicates that the controller is calibrating. Disconnect the USB cable from the robot and let the robot go when the beeping stops.
In this task, you will control the robot from the Simulink model. Again use the source blocks providing the Speed and Turn Angle inputs to the Controller subsystem to change the robot's speed and direction. While both signals are 0, the robot remains balanced in one place. Enter a positive value for speed to cause the robot move forward and a negative value to go in the reverse direction. Enter a positive value for turn angle to have robot turn right and a negative value to turn left.
1. Set up the Bluetooth connection between the computer and the robot. For more details, see the Task 1 in Communicating with LEGO MINDSTORMS NXT HardwareCommunicating with LEGO MINDSTORMS NXT Hardware example.
2. Connect the USB cable from your host computer to the robot.
3. In the model, change the Simulation mode on the toolbar to External.
4. In the model, click the Run button on the toolbar.
5. Place the robot on the ground and hold it in vertical position. A series of short beeps indicates that the controller is calibrating. Disconnect the USB cable from the robot and let the robot go when the beeping stops.
6. Click on the source blocks providing the speed and the turn angle signals. Experiment with different values.
7. Press the Stop button in the model to stop the robot.
Change the controller to use an ultrasonic sensor to turn away if an obstacle is detected.
Change the robot's controller to use a light sensor to follow a black line.
In this example, you ran a Simulink model on a LEGO MINDSTORMS NXT robot. The robot was controlled by a controller implemented in a Simulink model. The controller was responsible for maintaining the robot's vertical balance and for allowing to drive in the desired direction. You also controlled the robot remotely by adjusting the parameters of the Simulink model to change how the robot moves around. In the example you learned that:
With Simulink and Simulink Support Package for LEGO MINDSTORMS NXT Hardware you can simulate and implement complex robot control algorithms.
You can build Simulink models hierarchically, which allows you to hide or reveal the algorithm complexity as appropriate for the given analysis or design task.