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:
- access http://www.sunspotworld.com/spotmanager/ and click the Sun SPOT icon to launch Sun SPOT Manager tool
- Select "SDKs" tab
- Click "Demos" icon on the bottom-right corner (under Available SDKs)
- 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:
- remove the lines from L.56 "long ourAddr = .." to L.66 above "notifyDestroyed();"
- 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.