Bang-Bang Controller
Lesson Objectives:
- Students will build a simple LEGO car.
- Students will learn that a bang-bang controller switches abruptly between two states.
- Students will learn how to program this on the EV3 IDE.
Materials:
- 2 motors
- EV3 brick with Wifi dongle and SD card (you should have access to the EV3 IDE)
- Ultrasonic Sensor
- Any other LEGO parts you need to build a car
What is a Bang-Bang Controller?
Let's say you are riding a bike when you see a wall in front of you and you decide that you want to stop 10 feet in front of the wall. If you were using a bang-bang controller, you would keep biking at the same speed and slam the brakes the second before you reached the 10 feet mark.
In theory, this works when moving backwards as well. If you ended up stopping 5 feet in front of the wall, you would pedal backwards fast until you reached the 10 feet mark and then stop.
When we build our LEGO car, we will program it so that it will maintain a certain distance in front of itself, and if there is an obstacle in front of it, it will either keep moving forward or backwards until it reaches its safe distance, or just stop if it is at the safe distance.
Let's say you are riding a bike when you see a wall in front of you and you decide that you want to stop 10 feet in front of the wall. If you were using a bang-bang controller, you would keep biking at the same speed and slam the brakes the second before you reached the 10 feet mark.
In theory, this works when moving backwards as well. If you ended up stopping 5 feet in front of the wall, you would pedal backwards fast until you reached the 10 feet mark and then stop.
When we build our LEGO car, we will program it so that it will maintain a certain distance in front of itself, and if there is an obstacle in front of it, it will either keep moving forward or backwards until it reaches its safe distance, or just stop if it is at the safe distance.
Let's Get Started!
Go ahead and make a car of your design that incorporates the EV3, ultrasonic sensor, and the two motors. Run IDE on the brick and load up the IDE on your computer by entering the IP number of the brick on a search tab.
First, we want to initialize the ultrasonic sensor and the motors we will be using. We also want to assign our safe distance to a variable so that this can be changed easily in the future.
All that is left is to tell the car when to move and when to stop. There are three different cases to consider:
An example of the code that was outlined above is shown here. Feel free to use this as a guide if you run into errors, but please try and write the code yourself with the help of the snippets provided on the IDE.
First, we want to initialize the ultrasonic sensor and the motors we will be using. We also want to assign our safe distance to a variable so that this can be changed easily in the future.
All that is left is to tell the car when to move and when to stop. There are three different cases to consider:
- If the obstacle is farther than the safe distance, keep moving forward at a constant speed.
- If the obstacle is closer than the safe distance, move backwards at a constant speed.
- Otherwise, just stay still.
An example of the code that was outlined above is shown here. Feel free to use this as a guide if you run into errors, but please try and write the code yourself with the help of the snippets provided on the IDE.