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!!