Try Getting the RVR and BOLT to Interact in Creative Ways
Let's take things a step further and see if we can get the bots to interact 💃🏽🕺🏽(Note: The bots can most effectively communicate with one another via IR. The only Sphero bots that are equipped with IR are the BOLT and the RVR, so those are the two we'll be using for this tutorial).
In this tutorial, you'll use your RVR with a Raspberry Pi connected, as well as a BOLT bot (or few) to follow your RVR (per instruction from an asynchronous python script). If you haven't already connected your Raspberry Pi to your RVR, go ahead and check out our helpful docs on how to get up and running before attempting to follow along with this example (doing so will get most of the hardware setup completed already, as well 😉).
This intermediate example should take about 20 minutes to complete (if you've already set up your Raspberry Pi and connected it to the RVR).
In addition to the materials necessary to connect the Raspberry Pi to the RVR, we'll need:
- A short USB-to-Micro USB cable (to allow the RVR to provide the Raspberry Pi with power from its USB port)
- To ensure our RVR has firmware versions:
- Nordic 5.0.413 or above
- ST 5.0.421 or above
- One to Four BOLTs
- One Device (eg smart phone) running the latest release of the Edu App for each BOLT and RVR
If you've gone through our Raspberry Pi Set Up Docs, you will have already downloaded/cloned the needed code for this example, in addition to having installed the dependencies necessary for your Raspberry Pi to communicate with your RVR. Because the Raspberry Pi examples we have set up rely on files located throughout the file structure (not just the file we walk through below), we recommend that you use the version of the file included in those you downloaded/cloned from our GitHub, rather than trying to write the file from scratch as we walk through it together below (with that said, feel free to play with/add to the version you downloaded; we always love to see how creative our users are 🥰).
You can find the RVR file for this example at /projects/rvr_bolt_ir/rvr_ir_broadcast.py, in the file structure you downloaded; we'll repeat this later as we talk about how to actually run the code, but figured you might like to have the file in front of you as you follow along here...
rvr_ir_broadcast.py program below will simply initiate IR broadcasting on RVR on channels 0 and 1. It doesn't currently have any programming to allow you to drive the RVR, but, instead, relies on the Edu App to allow you to drive it. If you do want to try your hand at incorporating some driving commands into your code (rather than being tied to the Edu App), feel free to simply modify the
rvr_ir_broadcast.py program below!
Let's get started with our imports. Most of these should be pretty familiar from other examples. Line 3 of the code below is what allows us to pull from elsewhere in the file structure. It references files that are two layers above (
../../), as well as those within folders at that level, and appends the absolute path of the directory with the name after
from in the imports on lines 7-9:
import sys import os sys.path.append(os.path.abspath(os.path.join(os.path.dirname(__file__), '../../'))) import time from sphero_sdk import SpheroRvrObserver
Next, we'll initialize our global variables, including defining a
rvr 🤖 object so that we can use it to call commands off of, later on. In addition to
rvr, we also set two RGB color definitions for two different shades of green:
rvr = SpheroRvrObserver() green1 = [0, 255, 0] green2 = [0, 128, 0]
Last, but certainly not least, we'll set up our
main function, which is the heart and soul of our program 💓, letting it know when to execute which functions. We start by
waking the RVR so that we can ask it to do mechanical things; if the RVR is in soft sleep, we can still ask it for information about itself, but we can't ask it to do anything. After
waking the RVR and waiting 2 seconds to make sure it has had time to become fully alert, we ask the RVR to start broadcasting an IR signal, so that the BOLT(s) can find it.
While this script is in progress, we alternate the two green colors we set above. We also ask the RVR to stop broadcasting if we press any keys:
def main(): rvr.wake() time.sleep(2) rvr.start_robot_to_robot_infrared_broadcasting(far_code=1,near_code=0) try: while True: rvr.set_all_leds_with_32_bit_mask(0x3FFFFFFF, [color for i in range(10) for color in green1]) time.sleep(1) rvr.set_all_leds_with_32_bit_mask(0x3FFFFFFF, [color for i in range(10) for color in green2]) time.sleep(2) except KeyboardInterrupt: rvr.stop_robot_to_robot_infrared_broadcasting() time.sleep(.1) rvr.close() if __name__ == '__main__': main()
The BOLT Code
We would put the code for the
bolt_rvr_follow.lab file below, but it's not particularly pretty/human readable and is more meant to be a concise program for our app to read 😉 With that said, we did want to note that it is in the same folder in your file structure as the RVR code (/projects/rvr_bolt_ir/bolt_rvr_follow.lab).
Running the Programs
Running Programs from the App
For each BOLT, load the
bolt_rvr_follow.lab file into the Sphero Edu App (you can do this by sending it to your device in any way you like; I used AirDrop, but you can send it via email or save it in the cloud via your computer and then open it from that cloud platform's app on your phone) on the corresponding device for that BOLT and start the program.
If you haven't altered the
rvr_ir_broadcast.py program to allow you to control the RVR from a computer or the like, you'll need to follow this same process to load the
rvr_ir_broadcast.py program into the Edu App on the device you are using for your RVR.
If You Did Add Some Driving Programming...
You'll still use the Sphero Edu App to run the program for your BOLT(s), but you can drive the RVR using your computer, similarly to how you might with our Keyboard Control Example.
If you've taken our advice and decided to use SSH to access your fancy, wireless RVR 💁, you'll just need to take a few extra steps to create the connection between your RVR and the machine you are SSHing into it with. If you haven't recently grabbed your IP address, you'll need to do that with a monitor/mouse/keyboard attached to your Raspberry Pi by jumping into the terminal on your Raspberry Pi and typing:
Write down the IP address it spits out and let's unplug the mouse/keyboard/monitor from your Raspberry Pi and jump over to the machine you'll be using to control your RVR!
On your remote machine (which must be on the same Wifi network you set your Raspberry Pi to connect to), you'll also want to jump into the terminal, where you'll use the IP address you just grabbed and type:
If it is successful in finding your Raspberry Pi using its IP address (ie if you are on the correct network and typed the correct IP address (and your Raspberry Pi is powered on and has been for at least 15 seconds)), you'll be prompted to type in the password to your Raspberry Pi. As you are doing so, you won't get an indication that the terminal is registering your keystrokes, but that is just to make you feel extra safe, so dangerous hackers can't see how long your password is and steal it 🦹.
If all went to plan with that, you should get a success message and be ready to
cd into your /rvr_bolt_ir folder in your terminal and run:
Once you are finished playing, you'll want to make sure you properly shut down your Raspberry Pi. You can do this by pressing Ctrl + C in the terminal and then any other key to kill the program and then "turning off" your Raspberry Pi by running the following from the terminal on the machine you are using to control the RVR:
sudo shutdown now
Whichever way you decided to control your RVR, the above steps should have you all set to start your adventure! If you got your RVR code up and running (either through the app or by SSHing into your Raspberry Pi), you should have a BOLT or few smiling and sticking as close to your RVR as possible. Take your RVR for a spin and watch as your BOLT(s) follow like ducklings 🐥
Try to improve your IR Connection Setup! Here are some ideas to get you started:
- Try to reverse things and make the BOLTs run away from the RVR. You can use multiple RVRs to "herd" the BOLTs.
- Change the reactions of the BOLTs when they are/are not in range of the RVR(s). Have them display a message. Pick your favorite emoji.
- Incorporate some driving commands into your code, by modifying the