Contiki, having a humble begining :)

This post is about getting started with application development over Contiki i.e. www.sics.se/contiki...

Better use the release version which is latest one i.e. 2.5 rc1 in my case at the moment. Otherwise if you use development branch from CVS i.e. 2.x, than make sure you use it at your own risk :). I have the broadcast demo loosing packets for 2.x and it worked out of box for 2.4. Later got fixed in 2.5 rc1.

Getting Contiki
You can setup a CVS in parallel to the latest release, in a separate folder contiki-2.x.
MSP430 tools are required for Contiki building and installing. Download the MSP430 tools here http://www.sics.se/contiki/install-and-compile.html. On this page they are located at http://www.sics.se/~adam/msp430-gcc/

If in any of the tools installation you get error of file over write error, I got this error when installing gcc for msp430, try using 

dpkg --install --force-all gcc-msp430_3.2.3-2_i386.deb

If you dont want to go through the link and use command line, type following as such:


wget http://www.sics.se/~adam/msp430-gcc/binutils-msp430_2.17-2_i386.deb
wget http://www.sics.se/~adam/msp430-gcc/gcc-msp430_3.2.3-2_i386.deb
wget http://www.sics.se/~adam/msp430-gcc/msp430-libc_20071026_i386.deb


dpkg --install --force-all binutils-msp430_2.17-2_i386.deb
dpkg --install --force-all gcc-msp430_3.2.3-2_i386.deb
dpkg --install --force-all msp430-libc_20071026_i386.deb

Another method to get Contiki if you are not using a Linux machine is to get the Instant Contiki file. Instant Contiki is 1GB+ file, which can be run as a virtual machine within the Windows machine. More about it at http://www.sics.se/contiki/instant-contiki.html

If you have CVS version i.e. contiki-2.x from http://www.sics.se/contiki/download.html, you can keep the CVS update by going to the root of contiki-2.x and typing

cvs update -dP

If you want locally modified files to be reverted back to originals one from the repository, give:

cvs update -dPRC


Starting contiki
Once you have installed the above, try going to the infamous, hello world program of Contiki. It is located in contiki-2.4/examples/hello-world.
From command prompt type:

make TARGET=native hello-world

If you don't see any Error 1 in any message and it end with rm hello-world.co, this means your program compiled successfully. You can just run the hello-world with

./hello-world.native

If you want to upload it to the connected sky mote, using usb, type

make TARGET=sky hello-world.upload

Notice the .upload at the end of the command and sky as target.

Makefile
Every project source within the Contiki should have a Makefile having following contents. 

Makefile should be something like this. The project should be two levels below the Contiki project.

#Setting Contiki root which is TWO level up the folder hierarchy.
CONTIKI = ../..
ifndef TARGET
#The target should be defined here. It can be sky, netsim, native etc etc. After doing following you will not need to write make TARGET=native app, but only make will do the work
TARGET=native
endif

#If you are having more than one source files located in the directory, which is true in my case, than you may need to add the following define. Otherwise the files wont be compiled.
PROJECT_SOURCEFILES += a.c b.c c.c d.c

Simulation
There are native, mspsim, cooja, netsim simulations available.

NativeIf you build with TARGET=native in the command line for building like make TARGET=native app, this will build app.native file and you run it with ./app.native.

MSPSim
For mspsim, just build with make TARGET=sky app.mspsim, and you will see few dialogs popping up when the make is finished. MSPSIM is the simulator for sky motes i.e. Crossbow Telosb motes.

Cooja
For Cooja, launch cooja by entering command, ant in CONTIKI/tools/cooja type ant, this will open Cooja simulator. Select 
File->New Simulation->Create button. Again From Mote-types->Create mote type->Sky mote type. Browse and select the c file of the app. Compile, press Create, Create and Add. Press Start in control panel dialog. Here you can now see the Log Listener showing the debug prints in your application. Also the timeline windows will be showing the Radio RX/TX.

Netsim
For Netsim, one out of box fix required for building for netsim is the change in the CONTIKI/platform/netsim/Makefile.netsim file.
The change is replacing gtk-config --cflags with pkg-config --cflags gtk+-2.0 and gtk-config --libs with pkg-config --libs gtk+-2.0
Build netsim with make TARGET=netsim app. This will create app.netsim, run this with ./app.netsim. This will open a window showing the motes.

Sensors
Now something about sensors, specifically within the Crossbow TelosB Sky mote, also known as TMote sky. TMote sky contains three Light, Temperature and Humidity sensors. The later two are provided by Sensirion HT11 Temp/Humidity. 

To enable a sensor the following commands are given. 

sensor ->SHT11 sensor ->sht11_sensor enablement in code

Threading
Most of the threading done is as below:

while(1) is required, otherwise its a Noncyclic thread as below:

PROCESS_THREAD(a_non_cyclic_process, ev, data)
{
    PROCESS_BEGIN();

    this_code_will_run_only_once();

    PROCESS_END();
}

A skeleton code for Contiki process is also available at http://www.sics.se/contiki/developers/skeleton-code-for-contiki-a-process.html

ProtoThread pt_ and Multhreading API mt_
Ahh... protothreads are stack-less, and they don't keep context. They are specific set of MACRO_DEFINES, that expand to switch statement having while (1). Moreover, the function calling the pt thread should call it repeatedly within the while(1). Thus, pt threads are not self running. I dont know what they are called 'thread' for. 


For solving the above, there are mt_ library, which saves context, but get corrupted in case when it is handling complex datastructure. Timers come to rescue. Below are details about them.

Timers
The solution for Threading comes from Timer. There four types of them, Timer, ETimer, CTimer, RTimer.

Timer: its not much used, as it does nothing when it expires. 
ETimer: Fires event up there in Kernel, when it expires. A process can be "posted" with an event also. 
CTimer: Amazingly powerful Timer. It is configured to run a function when fired. And if required, within that function, one can reset it to have continuous firing. The parameters are given and kept during calls. 
RTimer: CTimer can be a bit lagging when comes to second level timely response. RTimer should be used in solution to this. Haven't used it yet, but found it being used all over within Contiki itself.

Configuration
The Contiki configuration is specific to platform. So it should be located inside CONTIKI/platform/sky/contiki-conf.h. Settings like PAN ID, RF CHANNEL, etc are configured here. 
Contiki have a main to begin with and it is located in the same folder CONTIKI/platform/sky/contiki-sky-main.c

Serial Output.
The serial output showing printf and puts console output is captured when the mote is connected to the computer. Once the software is burnt, you can give command like 

make serialdump

which is one way, i.e. only can receive and gives output on console and saves as file also. Also 

make login

is two way and you can send command to mote if there is shell imparted in it.


Sniffer
You can use sky mote as sniffer, by giving following commands

cd contiki-2.x/examples/sky-shell
edit sky-shell.c so that the call to shell_rime_sniff_init() is enabled
make sky-shell.upload
make login
sniff | hd
mac 0

Help
Trust me, the best sources for Contiki is the SOURCE CODE itself, as there is no proper documentation all :(, other than doxygen style docs available in CONTIKI/doc/ OR http://blog.gmane.org/gmane.os.contiki.devel. Next to it is http://sourceforge.net/mailarchive/forum.php?forum_name=contiki-developers, and than comes the website http://www.sics.se/contiki.
To post some question to Contiki mailing list just send an email to contiki-developers@lists.sourceforge.net. Also you can subscribe to https://lists.sourceforge.net/lists/listinfo/contiki-developers.

Comments

Popular posts from this blog

Imote2 with Camera setup IMB400 over Ubuntu 9.10/10.04

Branch and bound algorithm in C#- Article by AsiF Munir

Tensorflow - Simplest Gradient Descent using GradientDescentOptimizer