Radiofotboll
Innehåll
Introduction
Maker: Matthias Palmér
Cost: ca. 2000 kr.
Status: First version, ca. 2 days
Sphero is a radio controlled toy that you steer from you smartphone. But the yoystick imitation on the touchscreen was not as fun as I would have expected. It got me thinking of how it could be changed. Since I had a Wii-board at home I thought it was worth a shot.
Implementation
Both Sphero and Wii-board have their own communication protocolls on top of bluetooth.
Wii-board
I found the CWiid project to be quite useful. First, I just tried installing the stock versions according to the instructions by Matts Cutts:
sudo apt-get install wminput wmgui lswm
Unfortunately, this was not enough since CWiid did not support the Wii-board, or, so I though until I found that someone made a patch. And even better, the patch had been merged upstrean already 2011. The problem was that the project had grinded to a halt and there had been no official stable releases since before the patch. Hence, the library provided in Ubuntu was from before the patch.
The resolution was simply to follow the instructions from Mats Cutts again and do the install on top of the installed official packages. Although, I did not need to apply the patch, only download the latest code from abstrakraft at github.
Then I downloaded the scalesgui.py and system.ini from ticket #63 and got the demo to work.
Sphero
From the developer page for Sphero there are SDKs for multiple platforms, including an unofficial Python SDK. Unfortunately the Python SDK relies on the ROS framework who's documentation for newer Ubuntu releases where lacking. After some digging I found that I could install a newer version of ROS, although it seemed to have changed in unspecific ways that broke the Python SDK. I simply gave up at this point and tried another approach.
The spheron Node SDK showed greater promise as I did not get stuck on dependencies, hence a simple command and I was i principle set to go:
npm install spheron
The hurdle that remained was to successfully pair the Sphero under Linux. I thought this would be straightforward, but I failed to use the regular bluetooth-manager to accomplish this. Instead I had to rely on the following appproach (described in the first comment on this Github Gist):
bluetooth-agent 1234 sudo rfcomm bind _MAC_ADDRESS_TO_SPHERO_
Where the mac address is detected by running:
hcitool scan
Combining python and javascript
Now I could communicate with Sphero via nodejs and the Wii-board in Python. To combine the two I found python-shell quite useful:
npm install python-shell
Python-shell allows you to run a python program from within node and communicate with the program via stdin/stdout. To make it as simple as possible I focused on printing JSON from python that the python-shell interpreted for me. A minor hassle was that it took me a while to realize that I needed to flush the stdout, otherwise messages where buffered and not sent to the javascript program until the Python program was terminated.
Translating Wii-board output to Sphero commands
Finally it was time for some math. Starting from the scalesgui program I already had an event loop with total wheight and a coordinate in a x/y-plane. The following steps was taken in a javascript callback upon events recieved .
- If the wheight is lower than 15 kilos, send stop commands to sphero.
- Translate x/y-coordinates to an angle via arctan (important to remember that you need to divide into two cases due to arctan only being defined in the interval [-pi/2,pi/2].
- Normalize the length of the vector to be of maximum length 1.0.
Finally I could send a command to sphero to move in a specific angle (called heading) and with a specific velocity. A final hurdle turned out to be that sphero API expected the angle straight forward to be a mirror of how you would expect angles to be defined in a regular x/y-plane. That is, 90 degrees is forward but 0 degrees is to the left and 180 degrees to the right.
Result
See the post on google+ with a video:
The good
Steering Sphero by leaning back and forth on the Wii-board feels natural and a lot of fun. Setting up small challenges in the form of things to hit or avoid is a challenge for all ages. In general, it is a crowd pleaser. I imagine that it would be possible to further extend the functionality with slow driving, changing colors and maybe other modes of control.
The bad
The orientation of Sphero after wakeup is random and it slowly drifts after usage. It should be possible to change the orientation from the wii-board instead of forcing the user to rotate the wii-board to be consistent with the Sphero.
The ugly
Unfortunately the solution is not as stable as I would hope. The intitialization process crashes quite often while attempting to connect to Sphero. When it works you have to quickly press the red button underneeth the wii-board to make it connect. But if you manage that the system can run smoothly for quite some time. Although sometimes the connection to the Sphero is lost and you have to reconnect it.
Another bad thing with Sphero is that if the connection is lost at a time where it has been sent a command to roll, it keeps rolling until it is connected again and sent a stop command. This seems to be a design flaw in Sphero.