Cilia/Cilia-SunSPOT Hello World

From Wiki Adele Team
Jump to: navigation, search

Cilia has been deployed on Java SunSPOT.

Here is a little tutorial for beginning an application on a SunSPOT with the framework Cilia.


Introduction

First of all, in order to deploy an application on a SunSPOT, you need SunSPOT devices. In order to learn to use it, you should go to the SunSPOT site, in which you will find :

  • a forum which is active and full of information.


You will find there a lot of elements about Java SunSPOTs technology, and its possible use.

However, I give you here some hints about Java SunSPOTs :

  • Follow the site's installation guide, it should install you the SDK, NetBeans, the right JDK and Ant, in adapted versions for your computer. However, you may meet several problems during the whole installation : you should find some answers on the SunSPOT forum.
  • it is possible to develop SunSPOT applications with Eclipse and Maven, but Sun recommends the use of NetBeans and Ant. The main advantage of following this advice is about installation and support.
  • once you managed to install the SDK, open your IDE (NetBeans, Eclipse, ...), and open one demo project inside your IDE. You can find some in /your_path/SunSPOT/sdk/Demos.
  • Some useful commands (NetBeans command when right-clicking on the project, and the corresponding ant command, which must be realized from your project directory) :
    • clean and build (ant clean ; ant jar-app) builds the project
    • deploy on SunSPOT (ant jar-deploy) deploys the application on a connected device
    • run (ant run) runs the application on the SunSPOT device, and prints every trace on the console as long as the device is connected by USB cable
    • ant info gives you information on a connected device (and tells you if there is no detected device)
    • ant host-run runs an application on the basestation
  • Once you are a few familiar with the development environment, try to use as much hardware as you can (radio, sensors, switches, memory, ...), helping yourself with the examples in the Demos directory.

Installing Cilia on a SunSPOT device

Now that you know how to develop for SunSPOT devices, let's see how to include Cilia in a SunSPOT device.

The idea is actually simple : we are going to add Cilia as an extra library inside the SunSPOT device.

  • First, you will need the cilia-sunspot jars. You can find these jars here, on the svn repository. Download the four jars : cilia-minimal-core, cilia-embedded-runtime, cilia-minimal-compendium, and cilia-sunspot-components.
  • Then, you should indicate to the ant scripts that you want it to include these jars to the library. In your home directory, there should be a .sunspot.properties file. You should change the spot.library.name field, for instance in "cilialib", and you must change the spot.library.add.jars field, following the example :
spot.library.name=adderlib
spot.library.addin.jars=${sunspot.lib}/multihop_common.jar:${sunspot.lib}/transducer_device.jar

spot.library.name=cilialib
spot.library.addin.jars=${sunspot.lib}/cilia-core-1.1.2-SNAPSHOT.jar:${sunspot.lib}/cilia-embedded-runtime-1.1.2-SNAPSHOT.jar:
${sunspot.lib}/cilia-sunspot-components-1.0.0-SNAPSHOT.jar:${sunspot.lib}/cilia-minimal-compendium-1.0.0-SNAPSHOT.jar:
${sunspot.lib}/multihop_common.jar:${sunspot.lib}/transducer_device.jar

Be sure to keep the former jars. It is recommended to put the jars in sunspot.lib (generally your_path/SunSPOT/sdk/lib), however, if you want to keep it somewhere else, simply replace ${sunspot.lib} by your path.

  • Next, you must build this library : place yourself in your project's directory, and use the command ant library. The console should finally print BUILD SUCCESS.
  • Finally, connect your device, use the command ant info to check that the computer detects it, and use the command ant flashlibrary to update the device's library. The console should also print BUILD SUCCESS.

In case of problem/bug in this part, see in the SunSPOT documentation (Sun SPOT Developer's Guide, "Adding user code to the library suite"), or in the forum.

Develop a Hello World application

Now, Cilia is installed on your SunSPOT device. You are now going to develop your application, before deploying it on the SunSPOT.

  • Create a new SunSPOT project. There is a lot of solutions for this : copy another project, create a new one from your IDE and parameter it, create a new one from your shell and import it from your IDE, etc... You should already have made it while familiarizing with SunSPOTs.
  • Get the Main.java here. Import or copy it inside your project. It is the main class for the SunSPOT device.
  • Create a new file Midlet.java. You should correct Main.java so that startApp() creates a Midlet from this Midlet.java file. Then, edit Midlet.java as follow :
package fr.liglab.adele.cilia.sunspot.device.main;

import fr.liglab.adele.runtime.impl.CiliaContextImpl;
import fr.liglab.adele.cilia.CiliaContext;
import fr.liglab.adele.cilia.model.Adapter;
import fr.liglab.adele.cilia.model.Chain;
import fr.liglab.adele.cilia.model.Mediator;
import fr.liglab.adele.cilia.model.PatternType;

import fr.liglab.adele.cilia.sunspot.device.adapters.outonly.HeatAdapter;
import fr.liglab.adele.cilia.sunspot.device.adapters.inonly.LEDAdapter;
import fr.liglab.adele.cilia.sunspot.device.adapters.Mode;

public class Midlet{

public Midlet(boolean debug){
Mode.setMode(debug);
}

public void createChain(){
CiliaContext cc = CiliaContextImpl.getInstance();

Chain chain1 = new Chain("HelloWorld",null,null,null);

Adapter heatAdapter = new Adapter("heatAdapter",HeatAdapter.class.getName(), null, null, PatternType.OUT_ONLY);
Adapter ledAdapter = new Adapter("ledAdapter",LEDAdapter.class.getName(), null, null, PatternType.IN_ONLY);

chain1.add(heatAdapter);
chain1.add(ledAdapter);

chain1.bind(heatAdapter.getOutPort("std"), ledAdapter.getInPort("std"));

cc.addChain(chain1);

cc.startChain("FireDetection");
}

}


You have now created a mediation chain that senses periodically the temperature, and you want it to set one led in green if this temperature is lower than a certain value, and in red if it is higher. But for the moment, your mediation chain sends the value of the temperature directly to the LED Adapter (which doesn't know at all what to do with this value...). You need to place a mediator between the two.

  • In order to do so, you must create a file that will implement a mediator : create a file named FilterMediator.java, and edit it as follow :
package fr.liglab.adele.cilia.sunspot.device.main;

import fr.liglab.adele.cilia.minimal.dispatchers.MulticastDispatcher;
import fr.liglab.adele.cilia.minimal.processors.MereProcessor;
import fr.liglab.adele.cilia.minimal.schedulers.ImmediateScheduler;

import fr.liglab.adele.cilia.runtime.MinimalMediator;

public class FilterMediator extends MinimalMediator{

public MinimalMediator(){
super();
processor = MereProcessor.class;
scheduler = ImmediateScheduler.class;
dispatcher = MulticastDispatcher.class;
}
}
  • Then, you can edit your Midlet.java back :
(...)
import fr.liglab.adele.cilia.sunspot.device.main.FilterMediator;

public class Midlet{

(...)


public void createChain(){
CiliaContext cc = CiliaContextImpl.getInstance();

Chain chain1 = new Chain("HelloWorld",null,null,null);

Adapter heatAdapter = new Adapter("heatAdapter",HeatAdapter.class.getName(), null, null, PatternType.OUT_ONLY);
Adapter ledAdapter = new Adapter("ledAdapter",LEDAdapter.class.getName(), null, null, PatternType.IN_ONLY);

chain1.add(heatAdapter);
chain1.bind(ledAdapter);

Mediator filterMediator = new Mediator("filterMediator", FilterMediator.class.getName());
chain1.add(filterMediator);

chain1.bind(heatAdapter.getOutPort("std"), filterMediator.getInPort("in"));
chain1.bind(filterMediator.getOutPort("out"), ledAdapter.getInPort("std"));

cc.addChain(chain1);

cc.startChain("FireDetection");
}

}

Now, there is a mediator that takes messages from the heat adapter, and sends messages to the led adapter. Moreover, it has the right scheduler (Immediate scheduler : triggers the processing as soon as it receives a message), and the right dispatcher (Multicast dispatcher : sends messages to anyone that is bound to this mediator). Still, it doesn't make anything : the MereProcessor simply takes a message, and sends it to the dispatcher. So, you need to change the processor for an active processor that will read data coming from the heat adapter, decide if it is higher or lower than the threshold value, and send the right message to the led adapter.

  • In order to do so, you must create a file that will implement the right processor : create a file named FilterProcessor.java, and edit it as follow :
package fr.liglab.adele.cilia.sunspot.device.main;

import fr.liglab.adele.cilia.Data;
import fr.liglab.adele.cilia.framework.CiliaProcessor;
import java.util.Vector;

public class FilterProcessor extends CiliaProcessor{

private static double alpha = 25;

public Data process(Vector dataSet){
Data data = null;
String res = "";

if (dataSet != null && !dataSet.isEmpty() && dataSet.elementAt(0) instanceof Data){
data = (Data) dataSet.elementAt(0);
}

if (data!=null){
double value = -66;
try{
value=Double.parseDouble((String)data.getContent());
}catch(Exception ex){
System.out.println("Error");
value=-66;
}
}else{
System.out.println("Error");
value=-66;
}


if (value > alpha){
res = "setOn RED 0";
}else{
if (value = -66){
res = "setOn BLUE 0";
}else{
res = "setOn GREEN 0";
}

Data dataRes = new Data(res);
dataRes.setContent(res);
return dataRes;
}
}
  • And you can edit your FilterMediator.java :
(...)

import fr.liglab.adele.cilia.minimal.dispatchers.MulticastDispatcher;
import fr.liglab.adele.cilia.minimal.schedulers.ImmediateScheduler;
import fr.liglab.adele.cilia.sunspot.device.main.FilterProcessor;

(...)
processor = FilterProcessor.class;
(...)
  • Finally your mediation chain is elaborated.

However, you may appreciate one little precision in order to ease your development : you can parameter your IDE so that it compiles (and checks) your programs with the SPOT library as classpath. To do so, right-click on your project, modify the classpath/buildpath, remove the former libraries, and add the libraries for SunSPOT : squawk_common.jar, spotlib_common.jar, spotlib_device.jar, and all the jars you added in the .sunspot.properties file.

Run the application

Now that you have elaborated your mediation, you should want to deploy it and to run it on the device.

  • First, connect a SunSPOT device. Check that it is detected by running the command ant info in your project directory (BUILD SUCCESS should be printed).
  • Then, clean and build your project (ant clean ; ant jar-app).
  • Next, deploy your application on the SunSPOT (ant jar-deploy).
  • Finally, you should run it from your IDE (ant run). Traces could then be printed. If you are sure of your application, you can simply remove the USB cable, and reset the SunSPOT device.

The first led on the left of the device should get on after a while.