Sunday Mar 22, 2009

---------------------------------------------------------------------------------------------------------------------------------

This post was originally published in portuguese here.

This is the third part of this tutorial. You can read the first part here, and the second one here

---------------------------------------------------------------------------------------------------------------------------------

A few days ago I received a Sun SPOT kit from Sun, and got a kit that was borrowed to a professor at UFSC. I have two plans for these kits:

  1. Develop something (possibly a driver, or a mini-software) which helps people with disabilities related to movement of the hands and fingers use a computer. Something like a mouse with an accelerometer.
  2. I intend to develop a small course involving the Sun Spots. If everything goes well and some people are interested, I hope I can build a small team interested in developing a project with it. What project? Well, let's see what happens in the course ... :)

Oh, you want to see the Sun Spots? Well, I took some pictures:

his is the box. It is a really small box, with drawings of the Sun SPOT.

Sun SPOT - dentro da caixa

This illustration is inside the box ... in 9 steps, how to put your Sun SPOT to work! My favorite detail is the Sun SPOT with rays of light in the last image ...

Sun SPOT - os aparelhos

These are the Sun Spots themselves. In the middle is the base station,  which is connected to the PC and communicates with the other two, which have the sensors, accelerometers, etc..

Sun SPOT - todo o conteudo da caixa

And that is all that came in the box: the CD with the manual and the SDKs, the Sun spots, the USB cable and. .. I am still trying to figure out what are those other things.

Well, to play a little, we will try to connect the Sun SPOT to the PC and test a small program.

First step: connect the Sun Spot to the PC using the USB cable.To see the LEDs, let's open the Spot, like is show in this picture (you can see the LEDs with the SPOT closed, but you can see them better with it opened):

Sun SPOT - abrindo o SPOT

And here's a photo of it:

Sun SPOT aberto

To deploy an application to the SPOT, all you have to do is connect the SPOT to the PC and, on netbeans, right-click your project and select "build project + deploy to sunSPOT". The first time I did this, it told me to upgrade the sdk of my Sun SPOT.

To do that, I opened a terminal and, on the Sun SPOT sdk directory, (in my case, /home/cindy/sunSPOT/sdk) and ran the command ant upgrade -Dport=port_your_spot_is_connected to. After that, just select "Deploy to Sun SPOT" on your project.

Hint: to see this port, look at your console on Netbeans. At some point you can see somethin like (please reset SPOT 0014.4F01.0000.1143 on port /dev/ttyACM0), where /dev/ttyACM0 is the name of the port where the Spot is connected to.

To see your program running, press once the Sun SPOT button (the small one on the side of the USB connection). 

The Sun SPOTs are delivered with an example, which you can see when you turn it on. It's pretty simple, and much alike the small program we made in the last Sun SPOT tutorial. Here's a video of it:

And that's it. In the next tutorial, let's start making that project I talked about, of making a "mouse with accelerometer".

Doubts? Suggestions? Please, feel free to comment :) 

Wednesday Feb 25, 2009

---------------------------------------------------------------------------------------------------------------------------------

This is the second parte of the Sun SPOT tutorial. It was originally published in Portuguese here. You can check out the first part in English here. Also, the third part of this tutorial is here.

---------------------------------------------------------------------------------------------------------------------------------

Getting to Know the accelerometer


In the documentation page you can find a PDF with information on the accelerometer. Before starting to program frantically, let's know it a little better: it is a three-axis accelerometer, as illustrated below:

Sun SPOT - Accelorometer

The arrows and signs indicate the direction in which the values increase or deacrease - for instance, if you tilt the accelerometer upwards, the value of the Z axis will be positive.

I won't enter in further details of the technical aspects of the accelerometer since I do not think it's relevant for those interested in using it. For those who have interest, however, you can see the original document in the SunSpots Documentation page, or read this PDF on the accelerometer used.

The library includes the Sunspot IAccelerometer3D interface, which has all the common methods and the accelerometers LIS3L02AQAccelerometer class that implements the interface and adds some unique methods of the accelerometer.

The API has very simple methods to obtain the values of acceleration in each axis: getAccelX (), getAccelY () and getAccelZ (). Moreover, the method getAccel () is used to obtain the module of the sum of the vectors, ie the root of the sum of the squares of each axis. Ie | a | = (| X | 2 + | Y | 2 + | Z | 2) ½.

There is an important detail to be observed: at rest, the Sunspot is subject to an acceleration of 1g in the positive Z axis in response to the action of gravity. Ie to verify that Sunspot is moving, you have a method like this:

public boolean isMoving() throws IOException {
    double mag = acc.getAccel();
    return Math.abs(mag – 1.0) >= 0.1;
}

Where you should subtract 1 precisely because of 1g of gravity. Furthermore, as you can see, the acceleration is measured in terms of the gravitational force of Earth. That is, an acceleration of 4.9 m/s2 is around 0.5 for the Sunspot.

If you want to measure the relative acceleration, you will use the methods getRelativeAccelX (), getRelativeAccelY (), getRelativeAccelZ () and getRelativeAccel for the accelerations and the method setRestOffsets () to define the initial position. In this case, to verify that the Sun SPOT is moving only involves checking whether getRelativeAccel () returns a value greater than 0.1, without the need to subtract the 1g of gravity, and a setRestOffsets () before the loop to set the initial position.

Besides these methods, we also have the methods getTiltX (), getTiltY () and getTiltZ () in IAccelerometer3D. They serve to give the inclination of the accelerometer. A diagram of the relationship between inclination and acceleration is shown below:

Relation between variables of the accelerometer

The method getTiltX () returns the inclination angle in radians (in a range of -pi / 2 to + pi / 2). If we see the triangle below, the hypotenuse of the angle is the module of total acceleration, and the sinus of the angle is the acceleration of axis X divided by the module of total acceleration.

The document of the accelerometer shows an example of a simple application that blinks the LEDs according to the position of the accelerometer.

A Simple Example Using the Accelerometer

Let's look at the code and test it. If you are in doubt of how to create your project or how to test it in the Sun SPOT emulator, see the first part of this tutorial.

Make sure you have these imports in StartApplication:

import com.sun.spot.sensorboard.*;
import com.sun.spot.sensorboard.peripheral.IAccelerometer3D;
import com.sun.spot.util.Utils;

And here's the startApp method:

//first, we retrieve LEDs and accelerometers instances for these devices so they can be accessed and manipulated.
private ITriColorLED [] leds = DemoBoard.getInstance().getLEDs();
IAccelerometer3D acc = EDemoBoard.getInstance().getAccelerometer();


protected void startApp() throws MIDletStateChangeException {         

for (int i = 0; i < 8; i++) {
        leds[i].setOff();                   // turns off all LEDs
        leds[i].setRGB(0,200,0);     // turns LEDs green when turned on.
}
while (true) {
        try {
            int tiltX = (int)Math.toDegrees(acc.getTiltX()); // returns the value, in degrees, of the inclination on the X axis.
            int offset = -tiltX / 15;      
            if (offset < -3) offset = -3;  
            if (offset > 3) offset = 3;
            leds[3 + offset].setOn();      
            leds[4 + offset].setOn();     
            Utils.sleep(50);               
            leds[3 + offset].setOff();    
            leds[4 + offset].setOff();
        } catch (IOException ex) {
            System.out.println("Error reading accelerometer: " + ex);
        }
    }
}

After building the project and deploying the jar created in the virtual Sun SPOT solarium, you can right-click on the Sun SPOT, select "show sensors values", and play around with the X axis on the accelerometer tab to see the program running, as below:

Sun Spot - Accelerometer Example

Moving the cursor on the direction of negative values in the X axis is equivalent to moving the real Sun SPOT to the right, as seen in the picture. You can move and see that the LEDs follow the movement.

These are the methods that you probably will use in your project, but they are not the only ones. The LIS3L02AQAccelerometer class implements some specific methods.

For example, you can define in which scale to work from-2g to +2 g, or-6 to +6 g. The default, as seen in the screenshot above is from -2 to +2.

To change the scale, you must use the method setScale (LIS3L02AQAccelerometer.SCALE_2G); where SCALE_2G is the constant for the range of 2G and SCALE_6G is the constant for a scale of 6G. To use this method, you must include the library com.sun.spot.sensorboard.LIS3L02AQAccelerometer in your class. And to see the scale being used at the moment, you have the method getCurrentScale ().

Also, if you want to develop any application that uses the accelerometer in a very different way, you probably will be interested in more obscure methods such as getRawX (), getRawY () and getRawZ (), which are methods that return the values of voltage in each axis. This voltage is used to calculate the acceleration in each axis, using the formula a = (Raw - zeroOffset) / gain, where zeroOffset is the tension at rest and has a nominal value of 465.5 and the gain has a nominal value of 186.2 on the scale of 2G and 62 on the 6G scale.

To obtain the values of offset and gain, you have the methods getZeroOffsets (), getGains () and getRestOffsets () (the latter used to calculate relative acceleration). These methods return, each, a matrix [2] [3], where the first is the scale and then the desired axis. For example, zeroOffsets [scale] [0] returns zeroOffset on the scale used in the axis X. These matrices can be determined by methods setZeroOffsets, setGains and setRestOffsets.

The last method to be mentioned in this part of the tutorial is reset () which returns the scale to the default values and ensures that the accelerometer is not in self-test mode.


Calibrating the accelerometer


As each accelerometer is different, it is necessary to calibrate the Sun Spot. This is done by moving the accelerometer up and down in different ways. An application to calibrate the Sun SPOT is in the Sun SPOT SDK, Demos folder / CalibrateAccelorometer.

Because I don't have a real Sun SPOT yet, I will look further into this later.

And here we finish the second part of the tutorial. As I am studying the Sun SPOT for a project, I will focus my studies on what I intend to use, but if you are interested in anything in particular, tell me and I will try to talk about it here, ok?

---------------------------------------------------------------------------------------------------------------------------------

To proceed to the next part of the tutorial (Meet the Sun SPOTs, with photos and a video of the Sun SPOT that arrived here), click here.

---------------------------------------------------------------------------------------------------------------------------------

Thursday Feb 19, 2009

----------------------------------------------------------------------------------------------------------

Hello, this is my first post at Geek Pink, and the first time I write an entire post in English. Please, feel free to make any corrections.

This tutorial was originally written in Portuguese (which you can read here) on my personal blog.

---------------------------------------------------------------------------------------------------------

Currently, there are some alternatives for those who want to tinker with hardware that are more interesting and less complicated than what most of us have seen at an engineering course - one that I can mention is the Lego Mindstorms kit - you can create robots, put  light and/or pressure sensors, use engines, and program it all with a simple (but limited) programming IDE using blocks provided with the kit, or programming in C and pass the program through infrared to the Lego robot.

Another solution is the Bug Labs, which offers a basic kit and "accesories" to make more interesting projects, like a camera of 2MP, GPS, a base to connect USB gear, motion sensor, accelerometer ... the problem, again, is the price: $ 250 for the base, and 50 to $ 80 for extra modules. But it brings a SDK to help in development, and is open-source.

Also, there's Arduino, which is also open-source. It's the less expensive solution because it's mostly a pre-build micro-controller on a board with a developer enviromment. It's a solution for more experienced  circuit designers because it's not so easy to use more advanced gear with it - even if it's easier than building your own board, of course.

And the solution I'm going to talk about is the Sun one: Sun SPOTS. Basically, they are small hardware that you can program in Java, because it runs a virtual machine. It has accelerometers, light sensors, analog and digital buttons, etc..

Again, prices are a problem: $ 750 for the base and its accesories. There is a promotional discount for American students, leaving it at a $ 300 price tag. For some time, they were offering kits to students who submited proposals of interesting projects - a friend of mine got a kit this way. This promotion is no longer valid, but I still have a chance to get a kit, as a Sun Campus Ambassador who's doing a course in Control and Automation Engineering.

To get a kit I need to make an interesting proposal to use the Sun Spots. And to do that, I must know it better, to learn what it can do.

And I can do this not by just reading the site, but actually programming and testing in Sun Spots! Not in a real one, of course, but in an emulator provided by Sun.

Because I love to write tutorials, I will try to write about my learning experience.

Installing the Emulator

First of all, you must have Sun Java JRE. If you program in Java, you have that, no need tp worry. More specific instructions can be found here, according to the operating system you use.

With the required Runtimes, all you need do to is go to the page of the Sun Spot Manager and download the application.

Sun Spot Manager - Install Now

Run it with Sun Java WebStart (your system probably will know the extension and open it with this program, anyway), read it all (I'm sure you will do this ...), scroll down to the end, click next, next ... if you want, create a shortcut to the Sun Spot Manager on your desktop for easier access when you need it later.

It will check if you have NetBeans. If you don't, install it as suggested, and Apacje Ant. This helps a lot when developing, trust me.

Moreover, there aren't any secrets to install this - it will check the requirements of the system, suggest installing something that is required and you don't have ... if you read at least the minimum (you know, those messages that usually appear with an exclamation ), you should not encounter problems here. But if you find any, post it in the comments section and I will try to help, of course.

If you did everything right, double-click the shortcut that is on your desktop and it will open the Sun SPOT Manager. The part that interests us is the Solarium

Sun Spots Manager - Solarium

It is here that we have access to the Sun SPOT Emulator - click the small button written "Solarium" in the lower right corner and it will open the emulator (and where you can see the actual Sun SPOTS that you have connected to your PC).

Sun Spots - Solarium

Go to the "emulator" menu and select the option "new virtual spot". You will see an image of a small Sun Spot on your screen:

Virtual Sun Spot

Okay, this is the emulator. By right-clicking it you will see the "Deploy MIDlet bundle," which is where you will select the program you did to test on the sun SPOT ... which leads us, of course, to the next step: how to do a program for the Sun SPOT?

Developing for the Sun SPOT with NetBeans

If you installed NetBeans with the Sun SPOT Manager, you already have the necessary plugins. But this is not the case for many people, so let's see how to install the necessary plugins. First, you must download this plugin and install it.

To install this plugin, open NetBeans and go the "Tools" menu -> Plug-ins.

Plugins Tab - Netbeans

On the Tab "downloads", select "add plug-ins" and select the plugin you just downloaded. After you install it, go to the available plug-ins tab, and reload the catalog. Install the plugins in the Sunspot category that appear:

SunSpot Plugins on Netbeans

Then, create a new project in the Java category, of Sun Spot Application type:

Netbeans - Sun Spot new Project

You will see that it creates a default class, StartApplication.java, which has all the "imports" needed to boot your Spot  - in fact, this is a program that you can test on your emulator. It doesn't do much, though - it comes down to blinking a light until you press a button.

The code for this is in startApp ():

Sunspot - Example Code

The first line uses a method to get an instance of Switch 1, the first light is placed in a slightly red color on the second line and then it starts a loop that runs while Switch 1 is "open" (a switch closes a circuit when it is pressed, so the term "open" means an open circuit, ie, a button not pressed): turns a led on, waits a quarter of a second, turns off the led, waits a second and restarts the process.

When the switch is pressed, the program leaves the loop and notifyDestroyed is called to end the program (which calls destroyApp, that does nothing but make sure that all LEDs are turned off in this case).

To build the program and test it, you should build it, right-clicking on the project and selecting "build". This will generate a jar in the "suite" of your project.

Now, let's go to the last step: test your "Hello, Sun Spot world." project.

Testing the Project on the Emulator

Remember what I said about how we would test the application in Solarium? Return to the Solarium, where you created the Virtual Spot, right-click on it in "Deploy MIDlet bundle" and then select the jar file that you created.

To test, right-click on the SPOT, "MIDlet Run" -> "StartApplication". Okay, now you have an amazing sun spot that... ... flashes a LED!!

If you click again with the right button and then "Display Sensor Output" -> Internal frame, you will also see the values of the sensors (you can also change them through this interface), as below:

Sun Spot - First Example

And here we finish the first part of the tutorial. I hope that in the next part we learn things more interesting than flashing LEDs, right?