Access XYZ location in contiki motes
Accessing XYZ location in contiki is tricky. The main idea is to share the information in cooja (Java world) with contiki (C world). Currently (as of 10/14/2017), this can be achieved easily when using cooja motes but cannot be achieved when using non-cooja motes. However, with some changes to cooja core, it is possible to accomplish it for other kind of motes too. In this document I explain how to get XYZ coordinates in cooja motes and also on non-cooja (z1) motes. This can be used as a guide for other motes too. To make life easy, I have provided diffs that will accomplish this.
Main Idea
The main idea is to declare some non static variables in ‘C’ program (contiki). Memory and C-symbol tables are shared between contiki and cooja. In cooja, get the address of the variable using symbol table lookup using predefined libraries (tools/cooja/java/org/contikios/cooja/mote/memory/*). Copy local java variable contents to ‘C’ variable address using the above libraries.
In order to do the copy, there must be an external trigger. An interface’s copy-function can be manually triggered, for example, when you select mote interface viewer as shown in the picture below and select an interface (This is implemented as getInterfaceVisualizer() in the interface code).
For continuous updates, the updating function in java needs be be invoked regularly from the main simulation loop (execute()).
Interface programming for Cooja Motes for XYZ location:
Create directory tools/cooja/examples/xyz_interface/java and add XYZInterface.java in there. Copy build.xml and cooja.config from tools/cooja/examples/project_new_interface. Edit cooja.config to reflect XYZInterface instead of DummyInterface. Remove the line that contains dummy_intf.c. Contents of dummy_intf.c. can be directly put in the ‘C’ program that one creates inside $CONTIKI-HOME/examples directory.
“ant run” in tools/cooja, which is used to launch the simulator does not create class file. Therefore, have to explicitly compile it by running “ant” inside the xyz_interface directory.
cd tools/cooja/examples/xyz_interface ant (This will create the java class file in tools/cooja/examples/xyz_interface/java directory)
We need to load this interface into cooja, which I will show in a moment.
Functions doActionsAfterTick() and doActionsBeforeTick() are invoked by main simulation loop of cooja motes, for each interface from tools/cooja/java/org/contikios/cooja/contikimote/ContikiMote.java. Look for myInterfaceHandler.doActiveActionsAfterTick(). This is what performs continuous updates.
Place xyz.c inside your project directory. Add the following line in the Makefile, so that xyz.c get compiled.
CFLAGS += DTARGET=$(TARGET) PROJECTDIRS=. PROJECT_SOURCEFILES += xyz.c
In cooja motes, we need to register this interface as doInterfaceActionsBeforeTick() in ‘C’ is invoked regularly. Therefore, I have wrapped this with #if TARGET==cooja. In xyz.c , we are just declaring it. Actual registration should happen inside platform/cooja/contiki-cooja-main.c around the lines “SIM_INTERFACE_NAME(eeprom_interface);”. Added xyz_interface there.
SIM_INTERFACE_NAME(xyz_interface); SIM_INTERFACES(..., &xyz_interface);
Loading the interface to cooja:
Launch simulator. Select Settings->Cooja extensions. Select XYZ interface as shown.
This took me a while to figure out because when Cooja extensions is selected, it will show up as below. Make sure to extend the window by pulling the window to the right (Circled RED).
Thats it. Now when a cooja mote is installed, variables declared xyz.c are populated by Java continuously and doInterfaceActionsBeforeTick() and doInterfaceActionsAfterTick() declared in xyz.c are also continuously invoked. This screen is got by
Motes -> Add motes -> Create new mote type -> Cooja mote …
To Summarize
- Add XYXInterface.java in tools/cooja/examples/xyz_interface/java
- Copy build.xml and cooja.config from tools/cooja/examples/project_new_interface to tools/cooja/examples/xyz_interface and modify them as explained above
- Build XYXInterface.class by running “ant” in tools/cooja/examples/xyz_interface directory
- Add xyz.c into the project directory
- Load xyz_interface to cooja as shown in the diagram
- Create cooja motes. Notice XYZ interface as shown in above figure.
Interface programming for z1 Motes for XYZ location:
Place XYZInterface java in java/org/contikios/cooja/interfaces instead of tools/cooja/examples/xyz_interface and apply z1.xyz.diff. You should see XYZInterface as shown in the figure below.
Diff
diff --git a/tools/cooja/apps/mspsim/src/org/contikios/cooja/mspmote/MspMote.java b/tools/cooja/apps/mspsim/src/org/contikios/cooja/mspmote/MspMote.javadiff index b22fbc1..e39b14e 100644 --- a/tools/cooja/apps/mspsim/src/org/contikios/cooja/mspmote/MspMote.java +++ b/tools/cooja/apps/mspsim/src/org/contikios/cooja/mspmote/MspMote.java @@ -293,6 +293,7 @@ public abstract class MspMote extends AbstractEmulatedMote implements Mote, Watc private long executed = 0; private long skipped = 0; + private long counter = 0; public void execute(long time) { execute(time, EXECUTE_DURATION_US); @@ -342,6 +343,9 @@ public abstract class MspMote extends AbstractEmulatedMote implements Mote, Watc new ContikiError(trace).initCause(e); } + if ((counter++ % 100000) == 0) /* Update once in 100 millisec */ + myMoteInterfaceHandler.doActiveActionsAfterTick(); + /* Schedule wakeup */ if (nextExecute < t) { throw new RuntimeException(t + ": MSPSim requested early wakeup: " + nextExecute);
diff --git a/tools/cooja/apps/mspsim/src/org/contikios/cooja/mspmote/Z1MoteType.java b/tools/cooja/apps/mspsim/src/org/contikios/cooja/mspmote/Z1MoteType.java index b7b0ff6..3f3aacb 100644 --- a/tools/cooja/apps/mspsim/src/org/contikios/cooja/mspmote/Z1MoteType.java +++ b/tools/cooja/apps/mspsim/src/org/contikios/cooja/mspmote/Z1MoteType.java @@ -45,6 +45,7 @@ import org.contikios.cooja.mspmote.interfaces.MspDebugOutput; import org.contikios.cooja.mspmote.interfaces.MspDefaultSerial; import org.contikios.cooja.mspmote.interfaces.MspLED; import org.contikios.cooja.mspmote.interfaces.MspMoteID; +import org.contikios.cooja.interfaces.XYZInterface; @ClassDescription("Z1 mote") @AbstractionLevelDescription("Emulated level") @@ -78,6 +79,7 @@ public class Z1MoteType extends AbstractMspMoteType { @SuppressWarnings("unchecked") Class<? extends MoteInterface>[] list = createMoteInterfaceList( Position.class, + XYZInterface.class, RimeAddress.class, IPAddress.class, Mote2MoteRelations.class, diff --git a/tools/cooja/config/cooja_default.config b/tools/cooja/config/cooja_default.config index 212f480..331ae5f 100644 --- a/tools/cooja/config/cooja_default.config +++ b/tools/cooja/config/cooja_default.config @@ -1,6 +1,6 @@ org.contikios.cooja.contikimote.interfaces.ContikiRadio.RADIO_TRANSMISSION_RATE_kbps = 250 -org.contikios.cooja.contikimote.ContikiMoteType.MOTE_INTERFACES = org.contikios.cooja.interfaces.Position org.contikios.cooja.interfaces.Battery org.contikios.cooja.contikimote.interfaces.ContikiVib org.contikios.cooja.contikimote.interfaces.ContikiMoteID org.contikios.cooja.contikimote.interfaces.ContikiRS232 org.contikios.cooja.contikimote.interfaces.ContikiBeeper org.contikios.cooja.interfaces.RimeAddress org.contikios.cooja.contikimote.interfaces.ContikiIPAddress org.contikios.cooja.contikimote.interfaces.ContikiRadio org.contikios.cooja.contikimote.interfaces.ContikiButton org.contikios.cooja.contikimote.interfaces.ContikiPIR org.contikios.cooja.contikimote.interfaces.ContikiClock org.contikios.cooja.contikimote.interfaces.ContikiLED org.contikios.cooja.contikimote.interfaces.ContikiCFS org.contikios.cooja.contikimote.interfaces.ContikiEEPROM org.contikios.cooja.interfaces.Mote2MoteRelations org.contikios.cooja.interfaces.MoteAttributes +org.contikios.cooja.contikimote.ContikiMoteType.MOTE_INTERFACES = org.contikios.cooja.interfaces.Position org.contikios.cooja.interfaces.Battery org.contikios.cooja.contikimote.interfaces.ContikiVib org.contikios.cooja.contikimote.interfaces.ContikiMoteID org.contikios.cooja.contikimote.interfaces.ContikiRS232 org.contikios.cooja.contikimote.interfaces.ContikiBeeper org.contikios.cooja.interfaces.RimeAddress org.contikios.cooja.contikimote.interfaces.ContikiIPAddress org.contikios.cooja.contikimote.interfaces.ContikiRadio org.contikios.cooja.contikimote.interfaces.ContikiButton org.contikios.cooja.contikimote.interfaces.ContikiPIR org.contikios.cooja.contikimote.interfaces.ContikiClock org.contikios.cooja.contikimote.interfaces.ContikiLED org.contikios.cooja.contikimote.interfaces.ContikiCFS org.contikios.cooja.contikimote.interfaces.ContikiEEPROM org.contikios.cooja.interfaces.Mote2MoteRelations org.contikios.cooja.interfaces.MoteAttributes org.contikios.cooja.interfaces.XYZInterface
xyz.c
#include <stdio.h> #include <stdio.h> #include "platform/cooja/lib/simEnvChange.h" const struct simInterface xyz_interface; /* COOJA variables shared between Cooja and Contiki */ int xpos = 0; int ypos = 0; int zpos = 0; int xpos_mantissa = 0; int ypos_mantissa = 0; int zpos_mantissa = 0; inline int get_position(float *x, float *y, float *z) { *x = (float)xpos + (float)xpos_mantissa / 100.0; *y = (float)ypos + (float)ypos_mantissa / 100.0; *z = (float)zpos + (float)zpos_mantissa / 100.0; return 0; } inline float get_distance(float x1, float y1, float z1, float x2, float y2, float z2) { float x = x2 - x1; float y = y2 - y1; float z = z2 - z1; return (x*x + y*y + z*z); } #if TARGET==cooja /* No harm for z1, but not required */ static voiddoInterfaceActionsBeforeTick(void){ } static voiddoInterfaceActionsAfterTick(void){ } /* Register interface */ SIM_INTERFACE(xyz_interface, doInterfaceActionsBeforeTick, doInterfaceActionsAfterTick); #endif
XYZInterface.java
/* * Copyright (c) 2008, Swedish Institute of Computer Science. * All rights reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions * are met: * 1. Redistributions of source code must retain the above copyright * notice, this list of conditions and the following disclaimer. * 2. Redistributions in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in the * documentation and/or other materials provided with the distribution. * 3. Neither the name of the Institute nor the names of its contributors * may be used to endorse or promote products derived from this software * without specific prior written permission. * * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE * ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF * SUCH DAMAGE. * */ package org.contikios.cooja.interfaces; import java.util.*; import javax.swing.*; import org.apache.log4j.Logger; import org.jdom.Element; import org.contikios.cooja.*; import org.contikios.cooja.contikimote.ContikiMoteInterface; import org.contikios.cooja.interfaces.PolledAfterAllTicks; import org.contikios.cooja.interfaces.PolledBeforeAllTicks; import org.contikios.cooja.interfaces.Position; import org.contikios.cooja.mote.memory.VarMemory; /** * * Contiki variables: * <ul> * <li>int xpos * <li>int ypos * <li>int zpos * <li>int xpos_mantissa * <li>int ypos_mantissa * <li>int zpos_mantissa * </ul> * <p> * * Core interface: * <ul> * <li>xyz_interface * </ul> * <p> * * @author Shesha Sreenivasamurthy */ @ClassDescription("XYZ Interface") public class XYZInterface extends MoteInterface implements ContikiMoteInterface, PolledBeforeAllTicks, PolledAfterAllTicks, PolledAfterActiveTicks { private VarMemory moteMem = null; private static Logger logger = Logger.getLogger(XYZInterface.class); private Mote mote; public XYZInterface(Mote mote) { this.mote = mote; this.moteMem = new VarMemory(mote.getMemory()); } public static String[] getCoreInterfaceDependencies() { return new String[] { "xyz_interface" }; } public void doActionsBeforeTick() { } public void doActionsAfterTick() { Position pos = mote.getInterfaces().getPosition(); double x = pos.getXCoordinate(); double y = pos.getYCoordinate(); double z = pos.getZCoordinate(); int xpos = (int)x; int ypos = (int)y; int zpos = (int)z; int xpos_mantissa = (int)(((x - (int)x)) * 100); int ypos_mantissa = (int)(((y - (int)y)) * 100); int zpos_mantissa = (int)(((z - (int)z)) * 100); moteMem.setIntValueOf("xpos", xpos); moteMem.setIntValueOf("ypos", ypos); moteMem.setIntValueOf("zpos", zpos); moteMem.setIntValueOf("xpos_mantissa", xpos_mantissa); moteMem.setIntValueOf("ypos_mantissa", ypos_mantissa); moteMem.setIntValueOf("zpos_mantissa", zpos_mantissa); } public JPanel getInterfaceVisualizer() { JPanel panel = new JPanel(); panel.setLayout(new BoxLayout(panel, BoxLayout.Y_AXIS)); final JLabel positionLabel = new JLabel(); doActionsAfterTick(); positionLabel.setText("Set the XYZ coordinates for C"); panel.add(positionLabel); return panel; // No visualizer exists } public void releaseInterfaceVisualizer(JPanel panel) { } public Collection<Element> getConfigXML() { return null; } public void setConfigXML(Collection<Element> configXML, boolean visAvailable) { } }