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 http://www.sunspotworld.com/spotmanager/ 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/SunSpotApplication.java 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)
<..snip..>
import com.sun.spot.sensorboard.peripheral.ITriColorLED;
import com.sun.spot.sensorboard.peripheral.Servo;
import com.sun.spot.peripheral.radio.IRadioPolicyManager;
<..snip..>
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]);
serv0.setValue(0);
serv1.setValue(0);
Utils.sleep(500);

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());
Utils.sleep(2000);
serv0.setValue(0);
serv1.setValue(0);
}

notifyDestroyed(); // cause the MIDlet to exit
}
<..snip..>
}

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
SunSpotApplicationTemplate_1.0.0.jar
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
<..snip..>
deploy:

BUILD SUCCESSFUL
Total time: 8 seconds
$ ant run
<..snip..>
-run-spotclient-once:
[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

-run-spotclient-multiple-times-locally:

-run-spotclient:

-post-run:

run:

BUILD SUCCESSFUL
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 http://www.sunspotworld.com/. 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 https://servocar-spots.dev.java.net/.

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 http://www.sunspotworld.com/spotmanager/. 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 http://www.sunspotworld.com/spotmanager/ 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 https://www.sunspotworld.com/forums/viewtopic.php?p=2809.
  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 .sunspot.properties 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
<..snip..>
-run-spotclient-once:
[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] -isolateinit:com.sun.spot.peripheral.Spot
[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] spot.external.0.part.id: EDEMOBOARD_REV_0_2_0_0
[java] spot.hardware.rev: 5
[java] spot.mesh.management.enable: 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

-run-spotclient-multiple-times-locally:

-run-spotclient:

BUILD SUCCESSFUL
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 java.net

I've launched a new project (https://servocar-spots.dev.java.net/) on java.net 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)

Abstract

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.

Tuesday, May 20, 2008

Servo Car [ Sun SPOT app ] (2)

Hardware setup

OK, let's begin with hardware setup. For a servo car, we need the following hardware parts:

Those are
  • Sun SPOT x 2 (one on the car and the other for remote controller)
  • breadboard x 1
  • header pin x 3
  • battery (1.5V x 4) and battery pack
  • servo x 2 (each for right/left wheel)
  • jumper wires
You may find some differences between this and the previous one. Yes, the previous one doesn't use a breadboard because many jumper wires on a car look awful. However, to make it easier (in other words, to avoid any solder or other hassles), let's begin with breadboard kit.

You can choose any servo for this servo car demo, but one caveat is that you have to pick up what is so called "continuous rotation" servo. "continuous rotation" means it rotates forever in the same direction (90/180 degrees rotation servo can't be used for this demo. You know why??)

Next time, I'm showing the connections between components.

Monday, May 19, 2008

Servo Car [ Sun SPOT app ]

It's about time for me to write the detail of Sun SPOT applications. iRobot Create + Sun SPOT is such a candidate, but let's begin with a simpler one - "Servo Car". Before delving into hardware & software spec, please see the following video to get a general idea of this demo.

Wednesday, May 14, 2008

OpenSolaris 2008.05


OpenSolaris 2008.05 is now released. I feel it's much easier to install this than to do Ubuntu 8.04 at least on my Laptop because everything works fine including wifi without any hassle. One thing I had to do is to configure keyboard layout (on x window) to swap Caps Lock and Control keys like the following:
  1. migrate to single-user mode

    From GRUB menu, press "e" to enter edit mode and then hit "e" again to edit the "kernel" line. Add " -m milestone=single-user" to the end of the line. Press return and "b" to boot into single user mode

    or

    From command prompt, execute "svcadm milestone single-user" as the root user

  2. After login, execute

    # /usr/X11bin/xrogcfg

    to create & configure xorg.conf. On the GUI panel, do the following to setup keyboard.

    - press keyboard icon located topside -> select "Add new keyboard" -> "config" window appears, then configure

    Keyboard model -> Japanese 106-key
    Keyboard layout -> Japan
    Xkb options -> Control key position -> Swap Control and CapsLock

    - press ok on the "config" window
    - press Quit on the "xorgcfg" window (after doing this, press "yes" to accept default answers for all questions)

  3. execute svcadm to return to the multi-user mode.

    # svcadm milestone all