From 2bd50bcbdcb823a9bc917ee1cc2edcd5fd666c7e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andreas=20L=C3=B6scher?= Date: Mon, 9 Mar 2015 14:47:45 +0100 Subject: [PATCH] better implementation of the drifting --- .../contikios/cooja/avrmote/MicaZMote.java | 28 ++++++------- .../cooja/avrmote/interfaces/MicaClock.java | 9 ---- .../org/contikios/cooja/mspmote/MspMote.java | 41 +++++++++---------- .../cooja/mspmote/interfaces/MspClock.java | 10 ----- 4 files changed, 33 insertions(+), 55 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 581b091aa..21ed157ce 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 @@ -77,6 +77,10 @@ public class MicaZMote extends AbstractEmulatedMote implements Mote { private EEPROM eeprom = null; + private long executed = 0; + private long skipped = 0; + + /* Stack monitoring variables */ private boolean stopNextInstruction = false; @@ -187,6 +191,10 @@ public class MicaZMote extends AbstractEmulatedMote implements Mote { private long cyclesExecuted = 0; private long cyclesUntil = 0; public void execute(long t) { + MicaClock clock = ((MicaClock) (myMoteInterfaceHandler.getClock())); + double deviation = clock.getDeviation(); + long drift = clock.getDrift(); + /* Wait until mote boots */ if (myMoteInterfaceHandler.getClock().getTime() < 0) { scheduleNextWakeup(t - myMoteInterfaceHandler.getClock().getTime()); @@ -199,18 +207,11 @@ 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)); + + /* skip if necessary */ + if (((1-deviation) * executed) > skipped) { + skipped += 1; scheduleNextWakeup(t + Simulation.MILLISECOND); - return; } /* Execute one millisecond */ @@ -219,9 +220,8 @@ public class MicaZMote extends AbstractEmulatedMote implements Mote { cyclesExecuted += interpreter.step(); } - /* time deviation book keeping */ - ((MicaClock) (myMoteInterfaceHandler.getClock())).setReferenceTime(rtime - + (deviation * Simulation.MILLISECOND)); + /* book keeping */ + executed += 1; /* TODO Poll mote interfaces? */ 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 3f27d12f2..569e08dfd 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,7 +54,6 @@ public class MicaClock extends Clock { private MicaZMote myMote; private long timeDrift; /* Microseconds */ - private double referenceTime; /* Microseconds */ private double deviation; public MicaClock(Mote mote) { @@ -87,12 +86,4 @@ public class MicaClock extends Clock { public long getDrift() { return timeDrift; } - - public void setReferenceTime(double referenceTime) { - this.referenceTime = referenceTime; - } - - public double getReferenceTime() { - return referenceTime; - } } 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.java index 2abbd7f47..62ac55036 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 @@ -290,13 +290,22 @@ public abstract class MspMote extends AbstractEmulatedMote implements Mote, Watc private long lastExecute = -1; /* Last time mote executed */ private long nextExecute; + + private long executed = 0; + private long skipped = 0; + public void execute(long time) { execute(time, EXECUTE_DURATION_US); } + public void execute(long t, int duration) { + MspClock clock = ((MspClock) (myMoteInterfaceHandler.getClock())); + double deviation = clock.getDeviation(); + long drift = clock.getDrift(); + /* Wait until mote boots */ - if (!booted && myMoteInterfaceHandler.getClock().getTime() < 0) { - scheduleNextWakeup(t - myMoteInterfaceHandler.getClock().getTime()); + if (!booted && clock.getTime() < 0) { + scheduleNextWakeup(t - clock.getTime()); return; } booted = true; @@ -315,27 +324,17 @@ public abstract class MspMote extends AbstractEmulatedMote implements Mote, Watc throw new RuntimeException("Bad event ordering: " + lastExecute + " < " + t); } - /* time deviation skip if ahead*/ - double rtime = ((MspClock) (myMoteInterfaceHandler.getClock())) - .getReferenceTime(); - double deviation = ((MspClock) myMoteInterfaceHandler.getClock()) - .getDeviation(); - long drift = myMoteInterfaceHandler.getClock().getDrift(); - if (Math.round(rtime) < (t + drift)) { + if (((1-deviation) * executed) > skipped) { lastExecute = t; - nextExecute = t + duration; - ((MspClock) (myMoteInterfaceHandler.getClock())).setReferenceTime(rtime - + duration + (deviation * duration)); + nextExecute = t+duration; + skipped += duration; scheduleNextWakeup(nextExecute); - return; } - + /* Execute MSPSim-based mote */ /* TODO Try-catch overhead */ - try { - nextExecute = - t + duration + - myCpu.stepMicros(t - lastExecute, duration); + try { + nextExecute = myCpu.stepMicros(t-lastExecute, duration) + t + duration; lastExecute = t; } catch (EmulationException e) { String trace = e.getMessage() + "\n\n" + getStackTrace(); @@ -347,13 +346,11 @@ public abstract class MspMote extends AbstractEmulatedMote implements Mote, Watc if (nextExecute < t) { throw new RuntimeException(t + ": MSPSim requested early wakeup: " + nextExecute); } + /*logger.debug(t + ": Schedule next wakeup at " + nextExecute);*/ + executed += duration; scheduleNextWakeup(nextExecute); - /* time deviation book keeping */ - ((MspClock) (myMoteInterfaceHandler.getClock())).setReferenceTime(rtime - + deviation * (nextExecute - lastExecute)); - if (stopNextInstruction) { stopNextInstruction = false; throw new RuntimeException("MSPSim requested simulation stop"); diff --git a/tools/cooja/apps/mspsim/src/org/contikios/cooja/mspmote/interfaces/MspClock.java b/tools/cooja/apps/mspsim/src/org/contikios/cooja/mspmote/interfaces/MspClock.java index 568aec5f1..10aacffad 100644 --- a/tools/cooja/apps/mspsim/src/org/contikios/cooja/mspmote/interfaces/MspClock.java +++ b/tools/cooja/apps/mspsim/src/org/contikios/cooja/mspmote/interfaces/MspClock.java @@ -52,12 +52,10 @@ public class MspClock extends Clock { private Simulation simulation; private long timeDrift; /* Microseconds */ - private double referenceTime; /* Microseconds */ private double deviation; public MspClock(Mote mote) { simulation = mote.getSimulation(); - referenceTime = 0.0; deviation = 1.0; } @@ -76,14 +74,6 @@ public class MspClock extends Clock { public long getDrift() { return timeDrift; } - - public void setReferenceTime(double referenceTime) { - this.referenceTime = referenceTime; - } - - public double getReferenceTime() { - return referenceTime; - } public void setDeviation(double deviation) { assert (deviation>0.0) && (deviation<=1.0);