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)
import com.sun.spot.sensorboard.peripheral.ITriColorLED;
import com.sun.spot.sensorboard.peripheral.Servo;
import com.sun.spot.peripheral.radio.IRadioPolicyManager;
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.

No comments: