Skip to content

An Introduction to Cooja

tidyjiang8 edited this page Apr 19, 2016 · 10 revisions

本页包含Contiki的COOJA网络模拟器信息。读者为了更好地明白这里的内容,应该具备以下Contiki基础知识:怎么使用Contiki编译系统、怎么创建简单的Contiki进程。Contiki从2.0版之后中都包含有COOJA。在Contiki源码树下,COOJA位于:

  • tools/cooja 模拟器代码
  • platform/cooja 包含各种驱动的本地平台

Table of Contents

COOJA仿真器

本节对COOJA的传感器网络模拟器做了一个顶层概述。这些信息在使用COOJA时不完全需要,但有助于肚子更容易理解模拟器的问题,局限性和特性。

Contiki level: the Contiki Mote Type

COOJA中的模拟的Contiki Mote是实际被编译且正在执行的Contiki系统。通过将Contiki编译为本地平台的共享库,然后使用Java本地接口(JNI)将库加载到Java中就实现了这样的功能。不同的Contiki库可以被编译和加载到同一个模拟器中,代表不同种类的传感器节点(异构网络)。 COOJA只通过很少的功能就能实现控制和分析Contiki系统。例如,模拟器通知Contiki系统去处理事件,或者分析获取到的Contiki系统的完整内存。这些方法使模拟器能完全控制被模拟的系统。不幸的是,使用JNI也有一些恼人的副作用。最大的问题是依赖于外部工具,例如编译器、链接器和运行时参数。COOJA最初被开发用于Cygwin/Windows和Linux平台,后来被移植到MacOS。

入门指南

COOJA运行时需要Java 1.6或者之后的版本。我们推荐使用Sun的最新版本。此外,编译工具ant被用于编译COOJA。对于Windows用户,我们推荐使用Cygwin,且需要将Cygwin二进制路径(比如c:\cygwin\bin)添加到哦你的PATH环境变量中。 编译和开始COOJA:

   cd tools/cooja
   ant run

COOJA编译和仿真开始了。但是,在仿真Contiki系统前,你需要配置你的COOJA,使其符合你的工具链。

  • 打开菜单 Settings > External tools paths
这个对话框显示你当前的COOJA配置:编译器路径和参数,一串用于解析这些工具信息的常规表达式等等。这些设置保存在你的home目录:.cooja.user.properties, 比如位于home/myuser或者C:\Documents And Settings\myuser

配置向导

(This wizard replaces the old JNI tests in /tools/cooja/examples/jni_tests.) The configuration wizard helps configuring COOJA for using JNI, a challenging task on many systems. The wizard consists of 4 steps and tests compiling, linking, loading libraries, analyzing library memory, and controlling library execution.

  • Open menu Settings > Cooja mote configuration wizard
Complete all 4 steps. Note that you may have to change the recommended settings. Any changes can later be reviewed in the External tools paths dialog. If errors related to jni.h not being found, review the $JAVA_HOME path points to the right files.

Create a Hello World simulation

After completing the configuration wizard, we are now ready to create a simulation.

  • Open menu File > New simulation, and click Create
A simulation without motes and using the default parameters is created. Before adding motes to the simulation, we first need to create a mote type. The mote type determines the type of sensor hardware and which Contiki applications are to be simulated.
  • Open menu Motes > Add Motes > Create new mote type > Cooja Mote Type
A dialog allowing you to configure the new mote type appears.
  • Enter a suitable description: "My first hello world mote type"
  • Click Browse, and select the Contiki Hello World application: hello-world.c
  • Compile the Contiki shared library by clicking Compile
  • When the compilation finishes, load the library and create the mote type by clicking Create.
We have now added a mote type, however, the simulation does not yet contain any simulated motes. In the Add Motes dialog:
  • Enter 10 and click Add motes
You may later add further motes of this type by menu Motes > Add motes > My first hello world mote type. A few plugins are started: a control panel for starting and pausing the simulation, a visualizer that shows the node positions, and a log listener showing printouts of all simulated nodes.
  • Press Start (or CTRL+S) in the Control Panel plugin to start simulating.

Save and load simulations

Cooja allows for saving and loading simulation configurations. A simulation configuration contains the simulated modes and mote types, radio medium configuration, active plugins, etc.

Note that only the configuration, not the state of the simulation is saved. Hence, when a simulation is loaded, the simulation will start over from time 0.

To save the current simulation:

  • Open menu File > Save simulation as
  • Enter suitable name. Configuration files are stored with the file extension .csc
To load a simulation:
  • Open menu File > Open simulation > Browse...
  • Select configuration file to load.
The simulation is loaded, and the plugins will appear after a while. Loading a simulation with several mote types may take some time, since Contiki is recompiled in the background.

A similar functionality as saving and loading simulation, is reloading a simulation. To reload the current simulation:

  • Open menu Simulation > Reload simulation > Reload with same random seed

COOJA configs for Contiki development: sharing simulation configs

When developing Contiki applications, you should normally keep all your code in a project directory. Your project directory may be a subfolder of Contiki (e.g. examples/myproject), or external to Contiki (e.g. /home/user/mycontikiproject).

COOJA leverages the Contiki project directories by storing any simulation's external file references (such as to Contiki applications) relative to the directory where you save the simulation configuration. If you save a COOJA configuration in your project directory, any references to Contiki code residing in that directory will hence be stored as portable relative paths. This feature enables moving and sharing project directories, for example committing Contiki projects to version control.

Example: Contiki path: /home/user/contiki-2.x Contiki project path: /home/user/mycontikiproject Contiki application: /home/user/mycontikiproject/myapp.c

  • Create a COOJA simulation, compile myapp.c and finally save the simulation configuration in the project directory.
COOJA config: /home/user/mycontikiproject/mysimconfig.csc

The Contiki application path will be stored as "[CONFIG]/myapp.c" i.e. relative to where the configuration file is stored.

COOJA configs for Contiki development: simulation quickstart

It is also possible to quickstart COOJA, instead of starting COOJA from the tools/cooja/ directory.

To quickstart COOJA from a Contiki project directory, /home/user/mycontikiproject/:

   make myapp.cooja TARGET=cooja

or

   make mysimconfig.csc TARGET=cooja

Commandline options

  • -quickstart=Simulation Load Simulation at startup. This must be the first parameter.
  • -nogui=Simulation Run simulation without gui. This must be the first parameter.
  • -applet Run as applet. Must be the first parameter.
  • -log4j=File Load Log4j-Config from file.
  • -contiki=Path Set Contiki path
  • -external_tools_config=File Set Cooja config file.

Feature requests, bug reporting, and questions

Questions and requests should normally be posted to the main Contiki developers mailing list.

Clone this wiki locally