Cyber Sphero

Recently I have bought the infamous Sphero for my daughter in recognition of her outstanding academic results, also to provide her a chance to code for fun.

After a few moments playing with the Sphero using the stock app, I fired up the XCode and grabbed the Sphero SDK. The first and foremost problem immediately ran into after pressing the Build button, is 64-bit support. Alright, since it is a test, took away the arm64 from the build settings. Everything compiled ok and the test runs were all fine.

Looking around for more fun, I have spotted a brainwave sensor, the Mindwave Mobile from Neurosky, lying around and I thought, why not?

So digging out the Neurosky SDK, inserted the Neurosky lib and the code base as documented in the SDK into the Sphero’s RobotUISample which comes as an example in the Sphero SDK.

Same as the Sphero, the Mindwave Mobile headset communicates with the phone also using Bluetooth, and the dataReceived: method of the delegate will be invoked whenever the headset has data.

Among those output contains values measuring your concentration, that is measures how focus you are. I took that value and translate it into a value that can be used to control the speed of the robot. A threshold is also added to stop the robot from wondering mindlessly when I’m not focusing.

      - (void)dataReceived:(NSDictionary *)data {
        ...
        if (eSenseValues.attention >= 50) {
            EEGVelocity = (eSenseValues.attention - 50)/100.0;
        } else {
            EEGVelocity = 0.0;
        }

Since I defined the variable EEGvelocity as a global variable, so it is easy to use just by replacing the velocity controlled by the joystick by this one, as in:

- (void)joystickDidMoveWithHeading:(float)heading andVelocity:(float)velocity {
    [_robot driveWithHeading:heading andVelocity:EEGVelocity];
}

I’ve then added some more fun by changing the Sphero LED’s color so that it starts with blue, a cool color, when it is at rest, then gradually shifted to a raging red when it is moving really fast and furious. To implement this, I have to convert the speed value into RGB color – this is easy since I can map the speed into the HSV (Hue-Satuation-Value) model’s Hue value from 180º (speed = 0.0) to 360º (speed = 1.0). Conversion of this HSV color space to the RGB color space can be found by using the following code from Wikipedia:

// (float h, float s, float v)
float r = v;
float g = v;
float b = v;
if (s > 0.0f) {
    h *= 6.0f;
    final int i = (int) h;
    final float f = h - (float) i;
    switch (i) {
        default:
        case 0:
            g *= 1 - s * (1 - f);
            b *= 1 - s;
            break;
        case 1:
            r *= 1 - s * f;
            b *= 1 - s;
            break;
        case 2:
            r *= 1 - s;
            b *= 1 - s * (1 - f);
            break;
        case 3:
            r *= 1 - s;
            g *= 1 - s * f;
            break;
        case 4:
            r *= 1 - s * (1 - f);
            g *= 1 - s;
            break;
        case 5:
            g *= 1 - s;
            b *= 1 - s * f;
            break;
    }
}

Since I want the Robot be Blue at rest, then gradually change to raging Red when charging full speed ahead, therefore the Hue is shifted to become 0.5 to 1.0 when velocity is from 0.0 to 1.0. Then, to change the color of the Robot, we simply add the following to the EEG data update handler:

 - (void)dataReceived:(NSDictionary *)data {
     ...
     hsv.H = EEGVelocity/2.0+0.5;
     rgb = [self HSV_to_RGB:hsv];
     [_robot setLEDWithRed:rgb.R green:rgb.G blue:rgb.B ];

When this is done, I fire up the app on the phone and give it a run. Guess what? It didn’t run! The Sphero SDK keep saying that it is receiving bogus values. So I guessed if this is caused by the Sphero SDK getting the output not from the robot but from the Mindwave headset. Turning off the headset proved my theory is correct. Turning back on the headset, however, did get the robot moving and there is no more complain from the Sphero SDK!! Cool!

In the next half and hour, I have to learn hard to work my brain to control the speed of the robot while steering its direction form the phone, which was quite a challenge even for my daughter!

Comments

No comments yet. Why don’t you start the discussion?

Leave a Reply