From ff4aee68bdc5d9ee0e258b14ec701304b2f12837 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andreas=20L=C3=B6scher?= Date: Thu, 26 Feb 2015 13:17:33 +0100 Subject: [PATCH 01/10] Support in the mote clock interface for time deviation --- .../cooja/avrmote/interfaces/MicaClock.java | 10 ++++++++++ .../cooja/mspmote/interfaces/MspClock.java | 11 +++++++++++ .../contikimote/interfaces/ContikiClock.java | 12 +++++++++++- .../org/contikios/cooja/interfaces/Clock.java | 18 ++++++++++++++++++ 4 files changed, 50 insertions(+), 1 deletion(-) 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 468e07a73..44b81ccb2 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,10 +54,12 @@ public class MicaClock extends Clock { private MicaZMote myMote; private long timeDrift; /* Microseconds */ + private double deviation; public MicaClock(Mote mote) { simulation = mote.getSimulation(); myMote = (MicaZMote) mote; + deviation = 1.0; } public void setTime(long newTime) { @@ -90,4 +92,12 @@ 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; + } } 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 c66352e98..7a07859fe 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,9 +52,11 @@ public class MspClock extends Clock { private Simulation simulation; private long timeDrift; /* Microseconds */ + private double deviation; public MspClock(Mote mote) { simulation = mote.getSimulation(); + deviation = 1.0; } public void setTime(long newTime) { @@ -73,6 +75,15 @@ public class MspClock extends Clock { return timeDrift; } + public void setDeviation(double deviation) { + assert deviation>0.0; + this.deviation = deviation; + } + + public double getDeviation() { + return deviation; + } + public JPanel getInterfaceVisualizer() { /* TODO Show current CPU speed */ return null; diff --git a/tools/cooja/java/org/contikios/cooja/contikimote/interfaces/ContikiClock.java b/tools/cooja/java/org/contikios/cooja/contikimote/interfaces/ContikiClock.java index 127a7a3a7..2fda03239 100644 --- a/tools/cooja/java/org/contikios/cooja/contikimote/interfaces/ContikiClock.java +++ b/tools/cooja/java/org/contikios/cooja/contikimote/interfaces/ContikiClock.java @@ -76,6 +76,7 @@ public class ContikiClock extends Clock implements ContikiMoteInterface, PolledB private long moteTime; /* Microseconds */ private long timeDrift; /* Microseconds */ + private double deviation; /** * @param mote Mote @@ -89,6 +90,7 @@ public class ContikiClock extends Clock implements ContikiMoteInterface, PolledB this.moteMem = new VarMemory(mote.getMemory()); timeDrift = 0; moteTime = 0; + deviation = 1.0; } public static String[] getCoreInterfaceDependencies() { @@ -114,6 +116,15 @@ public class ContikiClock extends Clock implements ContikiMoteInterface, PolledB public long getTime() { return moteTime; } + + public void setDeviation(double deviation) { + assert deviation>0.0; + this.deviation = deviation; + } + + public double getDeviation() { + return deviation; + } public void doActionsBeforeTick() { /* Update time */ @@ -161,5 +172,4 @@ public class ContikiClock extends Clock implements ContikiMoteInterface, PolledB public void setConfigXML(Collection configXML, boolean visAvailable) { } - } diff --git a/tools/cooja/java/org/contikios/cooja/interfaces/Clock.java b/tools/cooja/java/org/contikios/cooja/interfaces/Clock.java index faefaddfc..8aebe391a 100644 --- a/tools/cooja/java/org/contikios/cooja/interfaces/Clock.java +++ b/tools/cooja/java/org/contikios/cooja/interfaces/Clock.java @@ -39,6 +39,7 @@ import org.contikios.cooja.*; * This observable never notifies. * * @author Fredrik Osterlind + * Andreas Löscher */ @ClassDescription("Clock") public abstract class Clock extends MoteInterface { @@ -76,4 +77,21 @@ public abstract class Clock extends MoteInterface { */ public abstract long getDrift(); + + /** + * The clock deviation is a factor that represents with how much speed the + * mote progresses through the simulation in relation to the simulation speed. + * + * A value of 1.0 results in the mote beeing simulated with the same speed + * as the simulation. A value of 0.5 results in the mote beeing simulation + * at half of the simulation speed. + * + * @param deviation Deviation factor + */ + public abstract void setDeviation(double deviation); + + /** + * Get deviation factor + */ + public abstract double getDeviation(); } From d2ddafb1d2a7dcb6fd7b58a18413648d2646a9f0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andreas=20L=C3=B6scher?= Date: Thu, 26 Feb 2015 13:19:30 +0100 Subject: [PATCH 02/10] typos --- tools/cooja/java/org/contikios/cooja/interfaces/Clock.java | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/tools/cooja/java/org/contikios/cooja/interfaces/Clock.java b/tools/cooja/java/org/contikios/cooja/interfaces/Clock.java index 8aebe391a..30b5b342c 100644 --- a/tools/cooja/java/org/contikios/cooja/interfaces/Clock.java +++ b/tools/cooja/java/org/contikios/cooja/interfaces/Clock.java @@ -82,8 +82,8 @@ public abstract class Clock extends MoteInterface { * The clock deviation is a factor that represents with how much speed the * mote progresses through the simulation in relation to the simulation speed. * - * A value of 1.0 results in the mote beeing simulated with the same speed - * as the simulation. A value of 0.5 results in the mote beeing simulation + * A value of 1.0 results in the mote being simulated with the same speed + * as the simulation. A value of 0.5 results in the mote being simulation * at half of the simulation speed. * * @param deviation Deviation factor From 1c4a6f701e814e5d7734ed0a1eca8fa12ad200c9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andreas=20L=C3=B6scher?= Date: Thu, 26 Feb 2015 14:04:57 +0100 Subject: [PATCH 03/10] No deviation support for Contiki motes --- .../cooja/contikimote/interfaces/ContikiClock.java | 7 ++----- 1 file changed, 2 insertions(+), 5 deletions(-) diff --git a/tools/cooja/java/org/contikios/cooja/contikimote/interfaces/ContikiClock.java b/tools/cooja/java/org/contikios/cooja/contikimote/interfaces/ContikiClock.java index 2fda03239..b033ef06d 100644 --- a/tools/cooja/java/org/contikios/cooja/contikimote/interfaces/ContikiClock.java +++ b/tools/cooja/java/org/contikios/cooja/contikimote/interfaces/ContikiClock.java @@ -76,7 +76,6 @@ public class ContikiClock extends Clock implements ContikiMoteInterface, PolledB private long moteTime; /* Microseconds */ private long timeDrift; /* Microseconds */ - private double deviation; /** * @param mote Mote @@ -90,7 +89,6 @@ public class ContikiClock extends Clock implements ContikiMoteInterface, PolledB this.moteMem = new VarMemory(mote.getMemory()); timeDrift = 0; moteTime = 0; - deviation = 1.0; } public static String[] getCoreInterfaceDependencies() { @@ -118,12 +116,11 @@ public class ContikiClock extends Clock implements ContikiMoteInterface, PolledB } public void setDeviation(double deviation) { - assert deviation>0.0; - this.deviation = deviation; + logger.fatal("Can't change deviation");; } public double getDeviation() { - return deviation; + return 1.0; } public void doActionsBeforeTick() { From 97da226255c864f2e15d19c754e6d938b0f867c5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andreas=20L=C3=B6scher?= Date: Thu, 26 Feb 2015 16:40:48 +0100 Subject: [PATCH 04/10] time deviation for MSP based nodes --- .../org/contikios/cooja/mspmote/MspMote.java | 23 ++++++++++++++++++- .../cooja/mspmote/interfaces/MspClock.java | 14 +++++++++-- 2 files changed, 34 insertions(+), 3 deletions(-) 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 403376583..2abbd7f47 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 @@ -78,6 +78,8 @@ import se.sics.mspsim.util.MapEntry; import se.sics.mspsim.util.MapTable; import se.sics.mspsim.profiler.SimpleProfiler; +import org.contikios.cooja.mspmote.interfaces.MspClock; + /** * @author Fredrik Osterlind */ @@ -313,9 +315,24 @@ 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)) { + lastExecute = t; + nextExecute = t + duration; + ((MspClock) (myMoteInterfaceHandler.getClock())).setReferenceTime(rtime + + duration + (deviation * duration)); + scheduleNextWakeup(nextExecute); + return; + } + /* Execute MSPSim-based mote */ /* TODO Try-catch overhead */ - try { + try { nextExecute = t + duration + myCpu.stepMicros(t - lastExecute, duration); @@ -333,6 +350,10 @@ public abstract class MspMote extends AbstractEmulatedMote implements Mote, Watc /*logger.debug(t + ": Schedule next wakeup at " + nextExecute);*/ 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 7a07859fe..23d23010c 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,11 +52,13 @@ 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(); - deviation = 1.0; + referenceTime = 0.0; + deviation = 0.999; } public void setTime(long newTime) { @@ -75,8 +77,16 @@ public class MspClock extends Clock { return timeDrift; } + public void setReferenceTime(double referenceTime) { + this.referenceTime = referenceTime; + } + + public double getReferenceTime() { + return referenceTime; + } + public void setDeviation(double deviation) { - assert deviation>0.0; + assert (deviation>0.0) && (deviation<=1.0); this.deviation = deviation; } From efbd17073365642890ff7c8132b1686bc65a847e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andreas=20L=C3=B6scher?= Date: Thu, 26 Feb 2015 16:51:47 +0100 Subject: [PATCH 05/10] standart deviation is 1.0 --- .../src/org/contikios/cooja/mspmote/interfaces/MspClock.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) 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 23d23010c..d6bd6a421 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 @@ -58,7 +58,7 @@ public class MspClock extends Clock { public MspClock(Mote mote) { simulation = mote.getSimulation(); referenceTime = 0.0; - deviation = 0.999; + deviation = 1.0; } public void setTime(long newTime) { 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 06/10] 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; - } } From 21a22caf33b6d85351970c12b0985603b89b83e5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andreas=20L=C3=B6scher?= Date: Fri, 27 Feb 2015 14:15:35 +0100 Subject: [PATCH 07/10] Added GUI and config file handling for Clock --- .../cooja/avrmote/interfaces/MicaClock.java | 14 ---- .../cooja/mspmote/interfaces/MspClock.java | 16 ---- .../org/contikios/cooja/interfaces/Clock.java | 80 ++++++++++++++++++- 3 files changed, 79 insertions(+), 31 deletions(-) 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 0eb52f6d2..3f27d12f2 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 @@ -95,18 +95,4 @@ public class MicaClock extends Clock { public double getReferenceTime() { return referenceTime; } - - public JPanel getInterfaceVisualizer() { - return null; - } - - public void releaseInterfaceVisualizer(JPanel panel) { - } - - public Collection getConfigXML() { - return null; - } - - public void setConfigXML(Collection configXML, boolean visAvailable) { - } } 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 d6bd6a421..568aec5f1 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 @@ -93,20 +93,4 @@ public class MspClock extends Clock { public double getDeviation() { return deviation; } - - public JPanel getInterfaceVisualizer() { - /* TODO Show current CPU speed */ - return null; - } - - public void releaseInterfaceVisualizer(JPanel panel) { - } - - public Collection getConfigXML() { - return null; - } - - public void setConfigXML(Collection configXML, boolean visAvailable) { - } - } diff --git a/tools/cooja/java/org/contikios/cooja/interfaces/Clock.java b/tools/cooja/java/org/contikios/cooja/interfaces/Clock.java index 30b5b342c..58ec05fa5 100644 --- a/tools/cooja/java/org/contikios/cooja/interfaces/Clock.java +++ b/tools/cooja/java/org/contikios/cooja/interfaces/Clock.java @@ -30,7 +30,19 @@ package org.contikios.cooja.interfaces; +import java.awt.GridLayout; +import java.awt.event.MouseAdapter; +import java.awt.event.MouseEvent; +import java.util.ArrayList; +import java.util.Collection; + +import javax.swing.JButton; +import javax.swing.JLabel; +import javax.swing.JPanel; +import javax.swing.JTextField; + import org.contikios.cooja.*; +import org.jdom.Element; /** * Represents a mote's internal clock. Notice that the overall @@ -43,7 +55,6 @@ import org.contikios.cooja.*; */ @ClassDescription("Clock") public abstract class Clock extends MoteInterface { - /** * Set mote's time to given time. * @@ -94,4 +105,71 @@ public abstract class Clock extends MoteInterface { * Get deviation factor */ public abstract double getDeviation(); + + @Override + public JPanel getInterfaceVisualizer() { + JPanel panel = new JPanel(); + GridLayout layout = new GridLayout(0,2); + + /* elements */ + final JLabel timeLabel = new JLabel("Time (ms)"); + final JTextField timeField = new JTextField(String.valueOf(getTime() / 1000)); + final JLabel deviationLabel = new JLabel("Deviation Factor"); + final JTextField deviationField = new JTextField(String.valueOf(getDeviation())); + final JButton readButton = new JButton("Read Clock Values"); + final JButton updateButton = new JButton("Write Clock Values"); + /* set layout */ + panel.setLayout(layout); + /* add components */ + panel.add(timeLabel); + panel.add(timeField); + panel.add(deviationLabel); + panel.add(deviationField); + panel.add(readButton); + panel.add(updateButton); + + readButton.addMouseListener(new MouseAdapter() { + @Override + public void mouseClicked(MouseEvent ev) { + if (ev.getButton()==1) { + timeField.setText(String.valueOf(getTime() / 1000)); + deviationField.setText(String.valueOf(getDeviation())); + } + } + }); + + updateButton.addMouseListener(new MouseAdapter() { + @Override + public void mouseClicked(MouseEvent ev) { + if (ev.getButton()==1) { + setTime(Long.parseLong(timeField.getText()) * 1000); + setDeviation(Double.parseDouble(deviationField.getText())); + } + } + }); + + return panel; + } + + @Override + public void releaseInterfaceVisualizer(JPanel panel) { + } + + @Override + public Collection getConfigXML() { + ArrayList config = new ArrayList(); + Element element = new Element("deviation"); + element.setText(String.valueOf(getDeviation())); + config.add(element); + return config; + } + + @Override + public void setConfigXML(Collection configXML, boolean visAvailable) { + for (Element element : configXML) { + if (element.getName().equals("deviation")) { + setDeviation(Double.parseDouble(element.getText())); + } + } + } } 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 08/10] 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); From 44c317ce1c3c0c79b32a2338de8ff9ca63389af9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andreas=20L=C3=B6scher?= Date: Mon, 9 Mar 2015 15:16:03 +0100 Subject: [PATCH 09/10] Fix: the last execution time must be carried over when skipping --- .../apps/mspsim/src/org/contikios/cooja/mspmote/MspMote.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) 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 62ac55036..85550ef99 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 @@ -325,7 +325,7 @@ public abstract class MspMote extends AbstractEmulatedMote implements Mote, Watc } if (((1-deviation) * executed) > skipped) { - lastExecute = t; + lastExecute = lastExecute + duration; // (t+duration) - (t-lastExecute); nextExecute = t+duration; skipped += duration; scheduleNextWakeup(nextExecute); From abe8e024ce709afa85dee992f31f82878069d2fc Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andreas=20L=C3=B6scher?= Date: Mon, 9 Mar 2015 15:27:47 +0100 Subject: [PATCH 10/10] fixed a bug where the scheduled time is not the executed one --- .../apps/mspsim/src/org/contikios/cooja/mspmote/MspMote.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) 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 85550ef99..b22fbc145 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 @@ -334,7 +334,7 @@ public abstract class MspMote extends AbstractEmulatedMote implements Mote, Watc /* Execute MSPSim-based mote */ /* TODO Try-catch overhead */ try { - nextExecute = myCpu.stepMicros(t-lastExecute, duration) + t + duration; + nextExecute = myCpu.stepMicros(Math.max(0, t-lastExecute), duration) + t + duration; lastExecute = t; } catch (EmulationException e) { String trace = e.getMessage() + "\n\n" + getStackTrace();