Tools

Namespaces

Variants

Views

Actions

Search

Contents

Please note that as of October 24, 2014, the Nokia Developer Wiki will no longer be accepting user contributions, including new entries, edits and comments, as we begin transitioning to our new home, in the Windows Phone Development Wiki. We plan to move over the majority of the existing entries. Thanks for all your past and future contributions.

[http://www.gosphero.com Sphero] is a ball than can be controlled by Bluetooth on Windows Phone 8 (and other mobile OS, using their respective [http://developer.gosphero.com/ SDKs]). The SDK allows you to control the lights, move the ball, manage collision detection, create macros, and perform other tasks.

−

I went to the Bluetooth session at Build 2013 (http://channel9.msdn.com/Events/Build/2013/3-026). The session itself was so-so (only 30min, and not enough details for me) but we got a nice surprise at the end : the attendees (at least the firsts of them) will get a Sphero !

+

This code example shows how to control lights and move the ball. There is a fully working Visual Studio solution attached, and screenshots of the demo app are shown below.

+

|

+

[[File:Sphero.jpg|100px|none|Sphero]]

+

|}

−

[[File:Sphero.jpg|200px]]

+

<gallery widths="300px" heights="400px">

+

File:Screenshot1.png|Calibration screen

+

File:Screenshot2-.png|Motion control

+

</gallery>

−

Sphero is a ball than can be controlled by Bluetooth. There are several SDKs, notably for iOS ,Android and a preview version for Windows 8.1. https://developer.gosphero.com/

−

−

More info here : http://www.gosphero.com

−

−

Now let’s play with it !

== About the APIs ==

== About the APIs ==

−

You can find the doc here : https://github.com/orbotix/DeveloperResources/tree/master/docs

+

You can find the documentation here: https://github.com/orbotix/DeveloperResources/tree/master/docs

−

The problem is the API is not exactly the same as the one I have (And I made the latest firmware upgrade) so it broke my head from time to time…The biggest problem I found was with the GetVersioning command (more on that later)

+

{{Warning|The documented API is not exactly the same as in the SDK. Problems with the GetVersioning command are discussed later.}}

=== Command packet ===

=== Command packet ===

−

[[File:SpheroCommands.png|600px]]

+

The format of the command packet is shown below.

+

[[File:SpheroCommands.png|none|600px]]

−

Example, the command to get Bluetooth info is 0x00 (DID) 0x11 (CID) (See API p13). The message will be : '''0xFF,0xFF,0x00,0x11,0x01,0x01,0xF2'''

+

For example, the command to get Bluetooth information is: 0x00 (DID) 0x11 (CID) (See API p13). I put SEQ = 1 and DLEN is always 1 and I set the CHK byte to 1 too. So the full command packet message will be : '''0xFF,0xFF,0x00,0x11,0x01,0x01,0xF2'''

−

I put SEQ = 1. DLEN is always 1 at least, as there is the CHK byte.

+

An example of a command packet with some data would be one to set the back light. The command to set the backlight is 0x02, 0x21. The data is a byte giving the brightness value from 0 to 255 (0xFF in hex). We set DLEN = 0x02, and 0x01 for CHK. So in order to set the brightness to 255 the command packet would be: '''0xFF,0xFF,0X02,0x21,0x01,0x02,0xFF,0xDA'''

−

Now an example with some data. To set the back light, the command is 0x02, 0x21. The data is a byte giving the brightness value.

−

−

The message (to set brightness at 255) is : '''0xFF,0xFF,0X02,0x21,0x01,0x02,0xFF,0xDA'''

−

−

DLEN = 0x02, for data and CHK

−

−

<data> = 0xFF, to set to 255

=== Response packet ===

=== Response packet ===

−

The response format is :

−

[[File:SpheroResponse.png|600px]]

−

SEQ is the same SEQ byte as in the command sent, so you know to which command it is the response.

+

The response format is as shown below:

+

[[File:SpheroResponse.png|none|600px]]

−

Lot of the time, there is not <data> part as it is just an acknowledgement of the command. Such answer is called SIMPLE ANSWER in the documentation.

+

SEQ is the same SEQ byte as in the command sent, so you know to which command it is in the response. Often there is no <data> part as it is just an acknowledgement of the command. Such answer is called SIMPLE ANSWER in the documentation.

For the MSRP, check the APIO document appendix for the values (0x00 = OK)

For the MSRP, check the APIO document appendix for the values (0x00 = OK)

Line 66:

Line 64:

=== Stream mode ===

=== Stream mode ===

−

You can set the message to stream. With that, you will not get a response. It is faster, but some messages can be lost.

+

You can set the message to stream which is faster. Note however that in this case responses are not sent and messages can be lost.

−

To do that, just set the SOP2 to 0xFE. Example with the set brightness command : '''0xFF,0xFE,0X02,0x21,0x01,0x02,0xFF,0xDA'''

+

To do this mode set the SOP2 to 0xFE. Example with the set brightness command : '''0xFF,0xFE,0X02,0x21,0x01,0x02,0xFF,0xDA'''

== Playing with Sphero ==

== Playing with Sphero ==

−

See http://msdn.microsoft.com/en-us/library/windowsphone/develop/jj207007(v=vs.105).aspx for info about Bluetooth for WP8

+

First see [http://msdn.microsoft.com/en-us/library/windowsphone/develop/jj207007(v=vs.105).aspx Bluetooth on Windows Phone 8] documentation on Dev Center.

−

+

−

First of all, as we will be using Bluetooth, we need to set the ID_CAP_PROXIMITY, ID_CAP_NETWORKING capabilities in the manifest. Then, just a few lines of code are needed to connect :

+

+

As we will be using Bluetooth, we need to set the ID_CAP_PROXIMITY, ID_CAP_NETWORKING capabilities in the manifest. Then, just a few lines of code are needed to connect:

<code csharp>

<code csharp>

PeerFinder.AlternateIdentities["Bluetooth:Paired"] = "";

PeerFinder.AlternateIdentities["Bluetooth:Paired"] = "";

Line 105:

Line 102:

</code>

</code>

−

{{Note|the Sphero need to be paired with the phone before we can connect with it !}}

+

{{Note|The Sphero needs to be paired with the phone before we can connect with it !}}

−

Now you’ve got a socket open where you can send the commands

+

Now you've got a socket open where you can send the commands

=== Sending commands ===

=== Sending commands ===

Line 138:

Line 135:

The command byte array is send as parameter. I sum the bytes, from DID to the one before the last (CHK). I invert it, then I put it at the end of the message.

The command byte array is send as parameter. I sum the bytes, from DID to the one before the last (CHK). I invert it, then I put it at the end of the message.

−

After, I just write it in the Outpustream of the socket. I flush it to be sure.

+

After, I just write it in the {{Icode|OutputStream}} of the socket. I flush it to be sure.

== Some basic commands ==

== Some basic commands ==

Line 145:

Line 142:

=== Set Color ===

=== Set Color ===

−

You can set the Sphero color with :<br />

+

You can set the Sphero color with:

+

[[File:SpheroRGB.png|none]]

−

[[File:SpheroRGB.png|SpheroRGB.png]]<br />

+

I just put the R, G and B values in the message

−

+

−

Simple, I just put the R,G and B values in the message

+

=== Set back LED ===

=== Set back LED ===

−

As Sphero is a ball, there is no real front and back. To know what Sphero consider is back, there is a (blue) backlight. You can set the brightness of it using :<br />

+

As Sphero is a ball, there is no real front and back. To know what Sphero considers to be "the back", there is a (blue) backlight. You can set the brightness of it using:

−

[[File:SpheroBackLED.png|SpheroBackLED.png]]

+

[[File:SpheroBackLED.png|none]]

=== Get Versioning ===

=== Get Versioning ===

−

I had problem with this one, as the API doc doesn’t seems to be correct. Finally I found a message on a forum from a developer of the API who stated that they planned to do it as written, but the new response broke their SDK (a bit annoying!) so they revert to an old response message. Without changing the doc it seems.

+

This was problematic as the API documentation doesn't seem to be correct. After research, it reflects a new version of the API that does not match the SDK.

+

+

Here is what I have on my Sphero (There is a byte on 8th position I have no idea what it is for! )

+

[[File:SpheroGetVersioning.png|None]]

−

Here is what I have on my Sphero (There is a byte on 8th position I have no idea what it is for)<br />

+

Here is a {{Icode|VersioningInfo}} class I made:

−

[[File:SpheroGetVersioning.png|SpheroGetVersioning.png]]<br />

+

−

Here is a VersioningInfo class I made :

+

<code csharp>

<code csharp>

public class VersioningInfo

public class VersioningInfo

Line 248:

Line 245:

I get the first 5 bytes of the answer. I get the message response code (the third byte) and I get the rest of the message .If it is success, it should be 9 bytes. If not, just one (the CHK). Anyway, I get the remaining length using the 5th byte of the answer (DLEN).

I get the first 5 bytes of the answer. I get the message response code (the third byte) and I get the rest of the message .If it is success, it should be 9 bytes. If not, just one (the CHK). Anyway, I get the remaining length using the 5th byte of the answer (DLEN).

−

Now I check if the response is successful or not. If not, I thrown an exception. If success, I create a VersioningInfo.

+

Now I check if the response is successful or not. If not, I thrown an exception. If success, I create a {{Icode|VersioningInfo}}.

=== Roll ===

=== Roll ===

−

That is probably the main command !<br />

+

This is probably the main command.

−

[[File:SpheroRoll.png|SpheroRoll.png]]<br />

+

[[File:SpheroRoll.png|none]]

−

Not complicated. Just set a heading and a speed. Heading is 0 to 359. 0 being straight line on positive Y axis.

+

This is straightforward - just set a heading and a speed. Heading is 0 to 359. 0 being straight line on positive Y axis.

<code csharp>

<code csharp>

async public Task Move(int heading, int speed)

async public Task Move(int heading, int speed)

Line 280:

Line 277:

}

}

</code>

</code>

−

I check if speed and heading are in correct range.

+

First I check if speed and heading are in correct range and then convert the heading into bytes. The heading and speed are then added to the message and sent.

−

Then I get a convert the heading into bytes.

+

{{Warning|the STATE byte is set to 1. If you want to make Sphero to stop, it is better to set it to 0, in order to give a smooth deceleration (See appendix C in docs).}}

−

I set the heading and speed bytes in the message.

+

=== Sleep ===

+

{{Icode|Sleep()}} turns Sphero off.

+

[[File:SpheroSleep.png|none]]

−

I send the message

−

−

{Warning|the STATE byte is set to 1. If you want to make Sphero to stop, it is better to set it to 0, in order to give a smooth deceleration (See appendix C in docs).}}

−

−

=== Sleep ===

−

Turn off Sphero.<br />

−

[[File:SpheroSleep.png|SpheroSleep.png]]

−

<br />

<code csharp>

<code csharp>

public async Task Sleep()

public async Task Sleep()

Line 302:

Line 293:

}

}

</code>

</code>

−

This is a very simple version of the sleep command. No timeout, no macro.

+

This is a very simple version of the sleep command. There is no timeout and no macro.

+

=== Calibrating ===

=== Calibrating ===

As I said earlier, as Sphero is a ball, there is no real front and back. But we still need to calibrate Sphero to set the heading 0 (straight line).

As I said earlier, as Sphero is a ball, there is no real front and back. But we still need to calibrate Sphero to set the heading 0 (straight line).

The way I do it is :

The way I do it is :

−

* Set the tail light on so I can have a mark

* Set the tail light on so I can have a mark

−

* Rotate the Sphero by calling the Roll command on a heading with a speed of 0 to align it as you need.

+

* Rotate the Sphero by calling the {{Icode|Roll()}} command on a heading with a speed of 0 to align it as you need.

−

In the app I made (Sources given at the end) here is the calibration screen :<br />

+

the code example calibration screen is shown below:

−

[[File:Screenshot1.png|200px]]

+

[[File:Screenshot1.png|thumb|none|200px|Calibration screen]]

−

<br />

+

−

Keep pressing on the + or – button move the Sphero of +5 or –5 degrees.

+

Keep pressing on the + or – button to move the Sphero by+5 or –5 degrees.

=== Moving using phone sensors ===

=== Moving using phone sensors ===

−

In my app, you move the Sphero by orienting the mobile. To be more precise, the Pitch is for forward/backward and the Roll is for left/right

+

In my app, you move the Sphero by orienting the mobile. To be more precise, the pitch is for forward/backward and the roll is for left/right

−

The more the orientation is increasing, the more the speed is also.<br />

+

The greater the orientation greater the speed.

−

[[File:Screenshot2-.png|200px]]

+

[[File:Screenshot2-.png|none|200px]]

−

<br />

+

−

You see the red ball in the screenshot. Take a vector from (0,0) to the ball. Its angle gives the heading (0 being the Y axe) and its norm gives the speed.

+

−

To get sensor data,I use the [http://msdn.microsoft.com/EN-US/library/windowsphone/develop/microsoft.devices.sensors.motion(v=vs.105).aspx Motion] class

+

You see the red ball in the screenshot. Take a vector from (0,0) to the ball. Its angle gives the heading (0 being the Y axis) and its norm gives the speed.

+

+

To get sensor data, I use the [http://msdn.microsoft.com/EN-US/library/windowsphone/develop/microsoft.devices.sensors.motion(v=vs.105).aspx Motion] class

For the speed, the norm is simply square root of (x*x + y*y). I divide by Pi/2 (as Pi/2 is the maximum) in order to have a number between 0 and 1. Then, multiply it by 255 to have a number in the range 0..255.

For the speed, the norm is simply square root of (x*x + y*y). I divide by Pi/2 (as Pi/2 is the maximum) in order to have a number between 0 and 1. Then, multiply it by 255 to have a number in the range 0..255.

Sphero is a ball than can be controlled by Bluetooth on Windows Phone 8 (and other mobile OS, using their respective SDKs). The SDK allows you to control the lights, move the ball, manage collision detection, create macros, and perform other tasks.

This code example shows how to control lights and move the ball. There is a fully working Visual Studio solution attached, and screenshots of the demo app are shown below.

For example, the command to get Bluetooth information is: 0x00 (DID) 0x11 (CID) (See API p13). I put SEQ = 1 and DLEN is always 1 and I set the CHK byte to 1 too. So the full command packet message will be : 0xFF,0xFF,0x00,0x11,0x01,0x01,0xF2

An example of a command packet with some data would be one to set the back light. The command to set the backlight is 0x02, 0x21. The data is a byte giving the brightness value from 0 to 255 (0xFF in hex). We set DLEN = 0x02, and 0x01 for CHK. So in order to set the brightness to 255 the command packet would be: 0xFF,0xFF,0X02,0x21,0x01,0x02,0xFF,0xDA

SEQ is the same SEQ byte as in the command sent, so you know to which command it is in the response. Often there is no <data> part as it is just an acknowledgement of the command. Such answer is called SIMPLE ANSWER in the documentation.

For the MSRP, check the APIO document appendix for the values (0x00 = OK)

I get the first 5 bytes of the answer. I get the message response code (the third byte) and I get the rest of the message .If it is success, it should be 9 bytes. If not, just one (the CHK). Anyway, I get the remaining length using the 5th byte of the answer (DLEN).

Now I check if the response is successful or not. If not, I thrown an exception. If success, I create a VersioningInfo.

I limit orientation to 90 degrees max (pi/2). To get the angle, I use the Atan to get an angle (in radian) from a tangent (y/x). I add 90 degrees (pi/2) as for Sphero, heading 0 (angle 0) is on the positive Y axis, not on the positive X as it is by default for math. Then I multiply by 180/Pi so I have degrees and not radians.

For the speed, the norm is simply square root of (x*x + y*y). I divide by Pi/2 (as Pi/2 is the maximum) in order to have a number between 0 and 1. Then, multiply it by 255 to have a number in the range 0..255.