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 2: Controlling Sphero using Raspberry Pi and Xbox 360 controller
  • HCP IoT Showcase using Sphero – Part 7: XS Application
  • HCP IoT Showcase using Sphero – Part 8: UI


This blog explains the highlighted section in the overall architecture.

Sphero Maze on HCP - Architecture Part 2.png

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.

Prerequisites

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.

Raspbian Jessie

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.

Sphero Maze on HCP - Unofficial SDKs.png

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.

Sphero BB-8

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.

DF:79:DD:9C:B6:1D  BB-E804

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

Sphero Maze on HCP - Code 01.png

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).

python BB8joyDrive.py

That’s it. Now enjoy controlling Sphero with your Xbox 360 Controller!

Sphero SPRK

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

Then, download or clone sphero_driver.py from the Unofficial SDKs – Python which I introduced above section.

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).

From

Sphero Maze on HCP - Code 02.png

To

Sphero Maze on HCP - Code 03.png

Once you run SPRKjoyDrive.py on GUI environment, you will see Python code is finding Sphero and pairing with it.

python SPRKjoyDrive.py

Now you can control Sphero SPRK with your Xbox 360 Controller.

Additional Information

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.

Best,

Joon

To report this post you need to login first.

Be the first to leave a comment

You must be Logged on to comment or reply to a post.

Leave a Reply