From cb6759c0a987ddaa9029e0f2006ce653f0cf6dfb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andreas=20L=C3=B6scher?= Date: Thu, 26 Feb 2015 16:51:57 +0100 Subject: [PATCH] Time deviation support for Mica Motes --- .../contikios/cooja/avrmote/MicaZMote.java | 20 ++++++++++++++ .../cooja/avrmote/interfaces/MicaClock.java | 27 ++++++++++++------- 2 files changed, 38 insertions(+), 9 deletions(-) diff --git a/tools/cooja/apps/avrora/src/org/contikios/cooja/avrmote/MicaZMote.java b/tools/cooja/apps/avrora/src/org/contikios/cooja/avrmote/MicaZMote.java index 0819b616e..581b091aa 100644 --- a/tools/cooja/apps/avrora/src/org/contikios/cooja/avrmote/MicaZMote.java +++ b/tools/cooja/apps/avrora/src/org/contikios/cooja/avrmote/MicaZMote.java @@ -44,6 +44,7 @@ import org.contikios.cooja.MoteType; import org.contikios.cooja.Simulation; import org.contikios.cooja.mote.memory.MemoryInterface; import org.contikios.cooja.motes.AbstractEmulatedMote; + import avrora.arch.avr.AVRProperties; import avrora.core.LoadableProgram; import avrora.sim.AtmelInterpreter; @@ -54,6 +55,8 @@ import avrora.sim.mcu.EEPROM; import avrora.sim.platform.MicaZ; import avrora.sim.platform.PlatformFactory; +import org.contikios.cooja.avrmote.interfaces.MicaClock; + /** * @author Joakim Eriksson, Fredrik Osterlind */ @@ -197,12 +200,29 @@ public class MicaZMote extends AbstractEmulatedMote implements Mote { /* TODO Poll mote interfaces? */ + /* time deviation skip if ahead*/ + double rtime = ((MicaClock) (myMoteInterfaceHandler.getClock())) + .getReferenceTime(); + double deviation = ((MicaClock) myMoteInterfaceHandler.getClock()) + .getDeviation(); + long drift = myMoteInterfaceHandler.getClock().getDrift(); + if (Math.round(rtime) < (t + drift)) { + ((MicaClock) (myMoteInterfaceHandler.getClock())).setReferenceTime(rtime + + Simulation.MILLISECOND + (deviation * Simulation.MILLISECOND)); + scheduleNextWakeup(t + Simulation.MILLISECOND); + return; + } + /* Execute one millisecond */ cyclesUntil += NR_CYCLES_PER_MSEC; while (cyclesExecuted < cyclesUntil) { cyclesExecuted += interpreter.step(); } + /* time deviation book keeping */ + ((MicaClock) (myMoteInterfaceHandler.getClock())).setReferenceTime(rtime + + (deviation * Simulation.MILLISECOND)); + /* TODO Poll mote interfaces? */ /* Schedule wakeup every millisecond */ diff --git a/tools/cooja/apps/avrora/src/org/contikios/cooja/avrmote/interfaces/MicaClock.java b/tools/cooja/apps/avrora/src/org/contikios/cooja/avrmote/interfaces/MicaClock.java index 44b81ccb2..0eb52f6d2 100644 --- a/tools/cooja/apps/avrora/src/org/contikios/cooja/avrmote/interfaces/MicaClock.java +++ b/tools/cooja/apps/avrora/src/org/contikios/cooja/avrmote/interfaces/MicaClock.java @@ -54,6 +54,7 @@ public class MicaClock extends Clock { private MicaZMote myMote; private long timeDrift; /* Microseconds */ + private double referenceTime; /* Microseconds */ private double deviation; public MicaClock(Mote mote) { @@ -70,6 +71,15 @@ public class MicaClock extends Clock { return simulation.getSimulationTime() + timeDrift; } + public double getDeviation() { + return deviation; + } + + public void setDeviation(double deviation) { + assert (deviation>0.0) && (deviation<=1.0); + this.deviation = deviation; + } + public void setDrift(long drift) { timeDrift = drift; } @@ -78,6 +88,14 @@ public class MicaClock extends Clock { return timeDrift; } + public void setReferenceTime(double referenceTime) { + this.referenceTime = referenceTime; + } + + public double getReferenceTime() { + return referenceTime; + } + public JPanel getInterfaceVisualizer() { return null; } @@ -91,13 +109,4 @@ public class MicaClock extends Clock { public void setConfigXML(Collection configXML, boolean visAvailable) { } - - public double getDeviation() { - return deviation; - } - - public void setDeviation(double deviation) { - assert deviation > 0.0; - this.deviation = deviation; - } }