Self-Balancing Two-Wheel Robot
This demo shows implementation of a controller for a self-balancing two-wheel robot built with LEGO® MINDSTORMS® NXT™ hardware. The demo illustrates basic concepts of balance and drive control using MATLAB® and Simulink®, and Target for Use with LEGO MINDSTORMS NXT Hardware.
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 demo 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 Control demo. The balance control is implemented by a two-wheeled inverted pendulum controller.
In this demo, 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.
We recommend completing Drive with PID Control demo.
- LEGO MINDSTORMS NXT Base Set
- HiTechnic NXT Gyro Sensor (NGY1044)
- USB cable
- LEGO MINDSTORMS NXT-compatible Bluetooth® dongle (optional)
1. Build the robot using the following building instructions.
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 Hardware demo.
4. On LEGO MINDSTORMS NXT Intelligent Brick, press the orange button to turn the power on.
1. Open the model. Observe two subsystems in the model.
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 Control demo 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 Start simulation 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. Select Tools > Run on Target Hardware > Run.
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 Hardware demo.
2. Connect the USB cable from your host computer to the robot.
3. In the model, select Tools > Run on Target Hardware > Options....
4. Select Enable External mode checkbox in the dialog that opens and click OK.
5. Select Tools > Run on Target Hardware > Run.
6. 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.
7. Click on the source blocks providing the speed and the turn angle signals. Experiment with different values.
8. 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 demo, 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 demo you learned that:
- With Simulink and Target for Use with LEGO MINDSTORMS NXT Hardware you can simulate and implement complex robot control algorithms.
- Simulink allows you to build the models in a step-wise manner, which was showed in the demo by sharing the PID Controller with the Drive with PID Control demo.
- 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.