Connecting RVR+/RVR and BOLT via IR

Try Getting the RVR+/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+/RVR, so those are the two we'll be using for this tutorial).

In this tutorial, you'll use your RVR+/RVR with a Raspberry Pi connected, as well as a BOLT bot (or few) to follow your RVR+/RVR (per instruction from an asynchronous python script). If you haven't already connected your Raspberry Pi to your RVR+/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+/RVR).

Hardware Setup

Project Materials

In addition to the materials necessary to connect the Raspberry Pi to the RVR+/RVR, we'll need:

Software Setup

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+/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+/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...

The rvr_ir_broadcast.py program below will simply initiate IR broadcasting on RVR+/RVR on channels 0 and 1. It doesn't currently have any programming to allow you to drive the RVR+/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!

The Code

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+/RVR so that we can ask it to do mechanical things; if the RVR+/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+/RVR and waiting 2 seconds to make sure it has had time to become fully alert, we ask the RVR+/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+/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+/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.

Once you've uploaded the .lab file into the app, BOLT(s) will start patrolling back and forth with frowny face ☹️.

If you haven't altered the rvr_ir_broadcast.py program to allow you to control the RVR+/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+/RVR.

When RVR+/RVR comes within range (while running the program we walked through above), the BOLT will stop, and switch to a happy face 😀, and start following RVR+/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+/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+/RVR 💁‍, you'll just need to take a few extra steps to create the connection between your RVR+/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:

hostname -I


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+/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:

ssh pi@ipAddressYouGrabbed


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:

rvr_ir_broadcast.py


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+/RVR:

sudo shutdown now


Let's Roll

Whichever way you decided to control your RVR+/RVR, the above steps should have you all set to start your adventure! If you got your RVR+/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+/RVR as possible. Take your RVR+/RVR for a spin and watch as your BOLT(s) follow like ducklings 🐥

Now What?

Try to improve your IR Connection Setup! Here are some ideas to get you started: