Wednesday, June 11, 2008

Servo Car [ Sun SPOT app ] (8)

Let's glance over servocar-spots codes from here. First of all, check out source codes for release 1.0.0 as the following (For this, you need to have a subversion client installed on your computer and an account for, so please prepare both of them if you don't have).
$ mkdir work; cd work
$ svn co servocar-spots --username <username>
You can, of course, check out sources from trunk, but I'm explaining the details based on this release (1.0.0).

Then, build & deploy two Sun SPOT applications - one is for a remote controller named "ServoCar-Controller" and the other is for an actuator of servos named "ServoCar-OnCar".
$ cd servocar-spots/ServoCar-Controller
$ ant deploy
$ cd ../ServoCar-OnCar
$ ant deploy
Next, let's look at the ServoCar-Controller codes, especially related to Sun SPOT networking & accelerometer on EDemoBoard.

Tuesday, June 3, 2008

Servo Car [ Sun SPOT app ] (7)

Sun SPOT application (MIDlet) class has to extend "javax.microedition.midlet.MIDlet" and its life cycle begins from startApp() method.
import javax.microedition.midlet.MIDlet;
import javax.microedition.midlet.MIDletStateChangeException;
// ...
public class SunSpotApplication extends MIDlet {
// ...

protected void startApp() throws MIDletStateChangeException {
System.out.println("Hello, world");
// ...
To rotate a servo with Sun SPOT, you only have to write the following 2 lines.
Servo serv0 = new Servo(EDemoBoard.getInstance().getOutputPins()[EDemoBoard.H0]);
For the details about "how to move/turn a servo motor", please refer to Servomechanism or Google says a lot about it. Here it's enough for you to know that a servo needs constant "square wave" to move/turn itself and its wave length directly maps to servo angle (or velocity for continuous rotation servo). A typical servo expects to see a pulse every 20 milliseconds and a 1500 microseconds (1.5 millisecond) pulse will make the motor turn to the 90 degrees (it's called "Neutral" position). Other pulses will do it based on their lengths as follows:
  • 1500 - value: 0 degrees
  • 1500 + value: 180 degrees
and the parameter "value" above may vary per servo, but typically it's from 250 to 500 (you may need to investigate your servo before using it). Servo#setValue(2000) just set this length to 2000 microseconds and keep sending pulse to the servo. It's all about your servo code on Sun SPOT!

If you have written embedded programs, you may notice it is not so easy to write the same program with C. Of course it depends on how many libraries you're provided, but typically you need a lot of include files as well as check how to use registers and command syntax for serial communication. Then, you also need to write a program that bring high a digital port, wait for around 1.5ms, bring low the same port (and loop these procedures for a while). Or even if you can use a bit high level operation like ATmega88 "8-bit Timer/Counter2 with PWM and Asynchronous Operation" to generate PWM (pulse-width modulation), the root complication is not solved as long as you use embedded C.

If you feel this is cool, NOW TIME TO CHANGE YOUR PLATFORM TO Sun SPOT!!

Tuesday, May 27, 2008

Servo Car [ Sun SPOT app ] (6)

Let's write a very simple Sun SPOT program for the servos here. We can begin it with the "SunSpotApplicationTemplate" project in ${SUNSPOT_DIRECTORY}/Demos directory (${SUNSPOT_DIRECTORY} is usually "C:\Program Files\Sun\SunSPOT" on Windows, but if you are true to my previous blog entry, it may be "C:\Sun\SunSPOT").

If you haven't had the Demos yet, you could also download it from the web site as we did before (see installing Sun SPOT SDK). Here is a quick summary to install Demos:
  1. access and click the Sun SPOT icon to launch Sun SPOT Manager tool
  2. Select "SDKs" tab
  3. Click "Demos" icon on the bottom-right corner (under Available SDKs)
  4. Press "Install" on the "Demo Installer" panel
Once you got it, please copy ${SUNSPOT_DIRECTORY}/Demos/CodeSamples/SunSpotApplicationTemplate directory into your working directory.
$ mkdir ~/work
$ cp -r ${SUNSPOT_DIRECTORY}/Demos/CodeSamples/SunSpotApplicationTemplate ~/work/ServoTest
$ cd ~/work/ServoTest
Then, modify the code src/org/sunspotworld/demo/ with your favorite editor as follows:
  1. remove the lines from L.56 "long ourAddr = .." to L.66 above "notifyDestroyed();"
  2. add the following code (add bold lines to your code)
public class SunSpotApplication extends MIDlet {
private static final int CENTER = 1500;
private static final int RANGE = 1000;

private ITriColorLED [] leds = EDemoBoard.getInstance().getLEDs();

protected void startApp() throws MIDletStateChangeException {
System.out.println("Hello, world");
new BootloaderListener().start(); // monitor the USB (if connected) and recognize commands from host

EDemoBoard demo = EDemoBoard.getInstance();
Servo serv0 = new Servo(demo.getOutputPins()[EDemoBoard.H0]);
Servo serv1 = new Servo(demo.getOutputPins()[EDemoBoard.H1]);

for (int i = 0; i < 4; i++) {
int r = (i % 2 == 0) ? RANGE / 2 : -RANGE / 2;
serv0.setValue(CENTER + r);
serv1.setValue(CENTER - r);
System.out.println("serv0 value: " + serv0.getValue());
System.out.println("serv1 value: " + serv1.getValue());

notifyDestroyed(); // cause the MIDlet to exit

Then build your project with "ant jar-app". This command compiles all the codes under the project and create jar file under "suite" directory.
$ ant jar-app
$ ls suite
Let's deploy this jar file into Sun SPOT Servo Car (configured before) and run the application. To do so, connect Sun SPOT to your PC via USB cable and execute the following commands.
$ ant deploy

Total time: 8 seconds
$ ant run
[java] SPOT Client starting...
[java] [waiting for reset]

[java] Local Monitor (purple-071018)
[java] SPOT serial number = 0014.4F01.0000.01A8

[java] ** VM stopped: exit code = 0 **

[java] Squawk VM Starting (purple-071018)...
[java] [NetManagementServer] starting on port 20
[java] Hello, world
[java] serv0 value: 2000
[java] serv1 value: 1000
[java] serv0 value: 1000
[java] serv1 value: 2000
[java] serv0 value: 2000
[java] serv1 value: 1000
[java] serv0 value: 1000
[java] serv1 value: 2000

[java] ** VM stopped: exit code = 0 **

[java] Exiting





Total time: 14 seconds

If you can watch one servo rotates clockwise (for 2 seconds), counterclockwise (for 2seconds), clockwise (for 2seconds) and counterclockwise (for 2seconds) while the other servo does reversely, your program is now successfully deployed into your Sun SPOT. Next time, let's investigate this code a little more.

Saturday, May 24, 2008

Servo Car [ Sun SPOT app ] (5)

I've written how to install Sun SPOT SDK, but missed a few things to mention. So I'm doing them here.

First of all, the Project Sun SPOT official URL is You can download SDK, tutorials and manuals here. You can also ask your questions at the forum in this site as well as purchase Sun SPOT Java Development Kit. So this is the start point for your Sun SPOT life.

Before installing Sun SPOT SDK on your computer, you need
installed on your computer (See the installation instructions in your Sun SPOT Java Development Kit for the detail).

Now, we've had all stuff, hardware & software, to write a code for the Sun SPOT Servo Car. Next time, I'm planning to begin with a very simple code other than this, but if you'd like to try it in advance, you can freely check out it from

Thursday, May 22, 2008

Servo Car [ Sun SPOT app ] (4)

Software setup

Let's write a first Sun SPOT application for Servo Car! Typically, Java IDE (like NetBeans) is very useful to write a large Sun SPOT application and I usually use it. However, to make things easy, let's begin with minimal developing environment -- Sun SPOT SDK, your favorite text editor & terminal.

Sun SPOT SDK has a color name according to its version. The current stable version (as of 5/23/2008) is v3 ("Purple" release), so let's use this version for the demo.

If you have a Sun SPOT Java Development Kit with Purple SDK CD-ROM, you may install it from CD. Otherwise, you can install SDK from This site keeps many different SDK versions other than Purple (v3) including beta versions, so I recommend the latter and will explain how to install SDK by using this site.

How to install Sun SPOT SDK
  1. access and click the Sun SPOT icon to launch Sun SPOT Manager tool
  2. Select "SDKs" tab
  3. Select purple-071018 (it may be labeled "Purple Beta RC5") from Available SDKs. If you don't see any versions in it, press "Refresh" button under Available SDKs.
  4. Press "Install" and "Sun SPOTs SDK Installer Tool" will be launched.
  5. Accept defaults other than "New SDK Directory Location". If you're a Windows user, it may begin with "C:\Program Files\Sun ..". There is no problem with this to develop Sun SPOT applications. But if you use Sun SPOT emulator, it causes some problems. So change this location ("C:\Program Files\Sun ..") to other place (e.g. "C:\Sun .."). For details, please refer to
  6. Press "Install". After installing, press "Done".
  7. You should see the installed SDK on "Installed SDKs" panel.
If you change the "New SDK Directory Location" according to the above procedure, please edit and change "sunspot.home" value to meet your change.

To confirm your installation, connect a Sun SPOT to PC via USB cable , open your favorite terminal emulator and check the following command output.

$ cd $(SDK_DIRECTORY_LOCATION) <- specify your directory 
ant info
[java] SPOT Client starting...
[java] [waiting for reset]

[java] Local Monitor (purple-071018)
[java] SPOT serial number = 0014.4F01.0000.01A8

[java] Application slot contents:
[java] C:\cygwin\home\miyake\tmp\ServoTest
[java] 2350 bytes
[java] last modified Fri May 23 11:58:24 JST 2008

[java] Startup:
[java] Squawk startup command line:
[java] -flashsuite:10900000
[java] -Xboot:268763136
[java] -Xmx:478000
[java] -Xmxnvm:128
[java] -dma:1024
[java] -MIDlet-1
[java] OTA Command Server is enabled
[java] Configured to run the current application

[java] Library suite:
[java] hash=0xa2fb25
[java] Installed library matches current SDK library
[java] Installed library matches shipped SDK library
[java] Current SDK library matches shipped SDK library

[java] Security:
[java] Owner key on device matches key on host

[java] Configuration properties:
[java] spot.external.0.firmware.version: 1.9
[java] spot.external.0.hardware.rev: 5.0
[java] EDEMOBOARD_REV_0_2_0_0
[java] spot.hardware.rev: 5
[java] true
[java] spot.mesh.traceroute.enable: false
[java] spot.ota.enable: true
[java] spot.powercontroller.firmware.version: PCTRL-1.79
[java] spot.sdk.version: purple-071018

[java] Exiting



Total time: 2 seconds

"ant info" checks the connected Sun SPOT information. If you see the same output as the above one, congratulations! Your installation was successful.

Sun SPOT Servo Car codes are now on

I've launched a new project ( on and imported all source codes for Sun SPOT Servo Car into it. Your feedbacks, comments and/or questions would be welcome!

Wednesday, May 21, 2008

Servo Car [ Sun SPOT app ] (3)

Hardware setup (cont'd)


In a standard breadboard the holes of the breadboard are connected underneath. Different sets of holes are connected to each other in a variety of ways. This time I selected a breadboard having the following layout (the holes on the same red lines are interconnected each other) .

A servo has 3 connectors: Vdd, GND and I/O pin. I/O pin is used to send a pulse, so this pin has to be connected to Sun SPOT Hxx (H0 -- H3) pin. Here I choose H0 for the servo of right wheel and H1 for the one of left wheel. For this servo, White line is I/O pin, Red one is Vdd and Black one is GND (see below). Check the spec for your servo.

A battery pack has, of course, two wires + and -, so we need the following connections to make a servo work well with Sun SPOT.
  • battery + <---> servo Vdd <---> Sun SPOT VH
  • battery - <---> servo GND <---> Sun SPOT GND
  • servo I/O pin <---> Sun SPOT H0 (or H1)
How to connect

1. Place 2 headers on the breadboard

2. Connect the first servo to the first header

3. Connect the battery to the breadboard

4. Connect Sun SPOT and the first header on the breadboard (SPOT's H0 to I/O pin, SPOT's VH to Vdd and SPOT's GND to GND)

5. Connect the second servo to the second header. To supply the power to the second servo, you also have to connect two headers as the following (with 2 x orange wires).

6. Connect Sun SPOT and the second header on the breadboard (SPOT's H1 to I/O pin)

7. Finish

Next time, let's write a simple program for those servos.