Description
Create your very own autonomous robot using a micro:bit, your favorite littleBits, and a Sphero RVR. See if you can program RVR through a maze or an obstacle course without it ever hitting a wall or object.
Lesson Objectives
Students will:
- create a circuit, using a micro:bit and micro:bit adapter
- test the proper location of sensors to ensure accurate obstacle avoidance
- program the micro:bit and RVR to work together to avoid any obstacle in RVR’s path
Lesson Guide
For this lesson, students can work alone or in small groups of (2-3). Each group will need a minimum of the bits listed, a Sphero RVR, and a laptop with access to MakeCode.
Depending on your students' experience with MakeCode, you may consider providing students with a template program to download to their micro:bit. This lesson will go through the steps and logic to build this program, but it may benefit students to have a template to look at while advancing through the lesson. A template has already been created for you and is listed under Lesson Materials.
The micro:bit adapter allows you to create countless new inventions and bridges littleBits to MakeCode and Python programming.
In this lesson, you will use the micro:bit to control RVR's movements based on the input received from the littleBits proximity sensors. With RVR's rugged capability, micro:bit's flexible integration, and littleBits's simple circuits, the possibilities are endless.
Construct the Circuit
- Connect the USB power bit to the split wire.
- Connect a proximity sensor to each end of the split wire, and connect a wire to the other end of each proximity sensor.
- Add the adhesive bitshoes to the back of each proximity sensor. These will be used to secure the sensors to the front of RVR.
- Secure the littleBits mounting board to the RVR build plate using twist-ties or some other means. If you have access to a 3D printer, you can try these mounting clips.
- Remove the red protective cover from the bitshoes. Secure the sensors to the front of RVR, like in the images below. You may need to adjust the sensor locations later to improve accuracy.
- Connect the right sensor to P1 on the micro:bit adapter.
- Connect the left sensor to P0 on the micro:bit adapter.
- Securely place the micro:bit adapter and USB power bit onto the mounting board.
Using the split USB cable that came with the micro:bit adapter:
- connect the micro USB end with the white sleeve to the micro:bit
- connect the other micro USB end to the USB power bit
- connect the USB A end to RVR's USB port.
Depending on your students' familiarity with MakeCode and block programming, consider having students simply download the template program to their micro:bit instead of following the steps to create the code. Either way, have students read through the steps to understand the logic behind the code.
Additionally, remind students to be thinking about the maze or course they will build to challenge RVR.
Now it's time to head over to the micro:bit MakeCode editor. Ask your teacher if you need to download the template program to your micro:bit or if you will be following the steps to make the code yourself.
Downloading to micro:bit
- Download the template program to your computer
- Connect your micro:bit to your computer with a USB cable
- Open the folder where the template program is and move the .hex file to the micro:bit
- Plug the micro:bit into the littleBits adapter to power it on
Programming with MakeCode
You need to add the RVR SDK to your MakeCode Library:
- Click on '+ Extensions' at the bottom of the block categories on the left
- Type 'RVR' in the search bar at the top of the Extensions screen and press enter
- You should see the RVR SDK extension. Click on the thumbnail to add it.
- Add the RVR wake (found in Power) and RVR reset the yaw (found in Movement) blocks to initialize RVR.
- Add a RVR set all LEDs block. Choose a color that RVR will wake up with.
- You also need a variable called heading.
- Add a pause of 100 ms to your main loop, forever. This number will determine how often the robot will make decisions on what to do. You can adjust this number to make the robot react faster or slower to its environment.
- Place a RVR drive block before the pause. Set the speed to 30 and use the variable heading for the heading. Adjust the speed to make it go through the maze faster or slower.
RVR needs to retrieve data from P0 and P1:
- Add an if/else block above the RVR drive block.
- Place an and boolean block where it says 'true' in the if/else block.
- Add a comparator block to each end of the boolean. Set them both to '≥'.
- Use an analog read block for the first part of each comparison. Set the first pin to P0 and the second to P1.
- Control how close you get to the obstacles by changing the value 400 in each comparison.
You need to add additional conditions to the if/else.
- Click on '+' twice to add two 'else/if' statements.
- Click on '-' on the original 'else'
- Make sure your code looks like the image below.
There are 3 scenarios that require the robot to take action:
- First scenario: if both sensors are seeing the obstacle, then the robot needs to do a sharp left turn.
- Add a change variable block, set to 90. This will adjust RVR's current heading by 90º. Adjust the heading to make it turn another direction, even 180º.
Prepare your program for the other two scenarios:
- Recreate each of the two comparators in the original if statement and place the PO comparator in the first else/if and P1 in the second.
- Make sure your code looks like the image below.
- Second scenario: if the robot is seeing a wall on the left side, you want it to turn slightly right.
- In the first else/if, remove 10º from the current heading by adding a change variable block set to -10. You can adjust the number to make it turn more or less.
- Third scenario: if the robot is seeing a wall on the right side, you want it to turn slightly left.
- In the second else/if, add 10º from the current heading by adding a change variable block set to 10. You can adjust the number to make it turn more or less.
Last, you want to make sure you don't ask RVR to go to a value beyond the boundaries. Essentially, you want RVR's heading to stay between 0º and 360º.
- Add two if/then blocks, like in the image below.
- In the first, add a comparator to determine if heading is greater than or equal to (≥) 360.
- If it is, then you need to change heading by -360.
- In the second, add a comparator to determine if heading is less than (<) 0.
- If it is, then you need to change heading by 360.
Maze / Course
Now is the time to put your program and RVR to the test. Work with your classmates to create the ultimate maze or obstacle course for RVR to get through.
Create different courses to challenge other groups to see how fast your RVR can make it through without running into any walls or obstacles.
Did RVR bump into anything?
There is probably some needed adjustments to make to both RVR and the MakeCode. Consider the following while you think of how to remix to make RVR more accurate:
- Are the proximity sensors in ideal locations? Would adjusting them help with accuracy?
- Is RVR moving too fast or too slow?
- When making a course correction, does RVR need to turn more or less each time?
- Is the course itself too difficult for RVR?
After a final remix, challenge other teams to see whose RVR can make it through an obstacle course the quickest!
After groups have completed their inventions (and possibly have remixed aspects), provide time for students to share what they observed about the invention, about the code, and about why and what they may have remixed.
Done!
