HCP IoT Showcase using Sphero – Part 2: Controlling Sphero using Raspberry Pi and Xbox 360 controller
In the previous blog, I introduced what SAP Korea team did with Sphero and its overall architecture. In this blog, I’ll explain how we control the Sphero using Raspberry Pi and Xbox 360 controller.
Now, we’re in the second blog as below.
|HCP IoT Showcase using Sphero
• Part 1: Overview
• Part 2: Controlling Sphero using Raspberry Pi and Xbox 360 controller
• Part 3: Maze Setup (Hardware)
• Part 4: Unit Test (Gathering & Sending Data)
• Part 5: Unit Test (MQTT)
• Part 6: Integration Test
• Part 7: XS Application
This blog explains the highlighted section in the overall architecture.
As you know, Sphero is originally controlled by official apps on your smartphone but SAP Korea team used Xbox 360 Controller instead for this showcase.
Following things are required in order to implement this part.
- Raspberry Pi 3 Model B
- Microsoft Xbox 360 Wired Controller
- Sphero SPRK or BB-8
Raspberry Pi 2 with Wi-Fi and BLE (Bluetooth low energy) supported bluetooth dongles will work but in this blog, I assume that you have a Raspberry Pi 3 Model B. Similarly, I believe other usb game controllers can be applicable but I also assume that you have a Xbox 360 controller.
Raspberry Pi 3 Setup
The first thing you need to do is obviously installing the OS, Raspbian Jessie. For your reference, I installed the following version.
Version: May 2016
Release date: 2016.05.27
Kernel version: 4.4
Since many blogs and websites already explained this Raspbian installation step, let me skip it in this blog.
The second step you need to do is installing bluetooth packages for pairing with Sphero and Unofficial SDKs from Sphero website are available as below.
In this blog, I’ll leverage the Unofficial SDKs – Python.
Since the bluetooth communication method of Sphero SPRK (Bluetooth Virtual Serial Port) is different from that of Sphero BB-8 (BLE), let me explain Sphero BB-8 first and then Sphero SPRK.
In order to pair with Sphero BB-8, installing Ian Harvey’s bluepy on your Raspberry Pi 3 is required as below.
sudo apt-get install python-pip libglib2.0-dev
sudo pip install bluepy
The next step is preparing Python codes for pairing and controlling Sphero. Thanks to jchadwhite’s great work, you can download or clone the code from here.
All you need to do is copying two files (BB8_driver.py, BB8joyDrive.py) to your Raspberry Pi 3 and change MAC address in BB8_driver.py file. Assume that your Sphero BB-8 is ready to connect, you can identify its MAC address using the following command.
sudo hcitool lescan
The result you need to find will look like this.
Once you noted down Sphero BB-8’s MAC address, then you need to modify the code as below (line 244).
sudo nano BB8_driver.py
Then plug your Xbox 360 Controller to your Raspberry Pi 3 and run BB8joyDrive.py on you Raspberry Pi. This code will only work on GUI environment (X Window System).
That’s it. Now enjoy controlling Sphero with your Xbox 360 Controller!
In order to pair with Sphero SPRK, I installed two more packages additionally as below.
sudo apt-get install bluetooth
sudo apt-get install python-bluez
By leveraging the BB8joyDrive.py again, I just renamed it to SPRKjoyDrive.py and changed the code as below to import sphero_driver.py instead of BB8_driver.py (line 7 to 10).
Once you run SPRKjoyDrive.py on GUI environment, you will see Python code is finding Sphero and pairing with it.
Now you can control Sphero SPRK with your Xbox 360 Controller.
Gathering data from Sphero was not implemented in this showcase but you can also try it using many get and set functions in sphero_driver.py and BB8_driver.py.
Again, hope these blog series help you to get some insight and let me get back to you soon with next posting.