better implementation of the drifting

This commit is contained in:
Andreas Löscher 2015-03-09 14:47:45 +01:00
parent 21a22caf33
commit 2bd50bcbdc
4 changed files with 33 additions and 55 deletions

View file

@ -77,6 +77,10 @@ public class MicaZMote extends AbstractEmulatedMote implements Mote {
private EEPROM eeprom = null; private EEPROM eeprom = null;
private long executed = 0;
private long skipped = 0;
/* Stack monitoring variables */ /* Stack monitoring variables */
private boolean stopNextInstruction = false; private boolean stopNextInstruction = false;
@ -187,6 +191,10 @@ public class MicaZMote extends AbstractEmulatedMote implements Mote {
private long cyclesExecuted = 0; private long cyclesExecuted = 0;
private long cyclesUntil = 0; private long cyclesUntil = 0;
public void execute(long t) { public void execute(long t) {
MicaClock clock = ((MicaClock) (myMoteInterfaceHandler.getClock()));
double deviation = clock.getDeviation();
long drift = clock.getDrift();
/* Wait until mote boots */ /* Wait until mote boots */
if (myMoteInterfaceHandler.getClock().getTime() < 0) { if (myMoteInterfaceHandler.getClock().getTime() < 0) {
scheduleNextWakeup(t - myMoteInterfaceHandler.getClock().getTime()); scheduleNextWakeup(t - myMoteInterfaceHandler.getClock().getTime());
@ -199,18 +207,11 @@ public class MicaZMote extends AbstractEmulatedMote implements Mote {
} }
/* TODO Poll mote interfaces? */ /* TODO Poll mote interfaces? */
/* time deviation skip if ahead*/ /* skip if necessary */
double rtime = ((MicaClock) (myMoteInterfaceHandler.getClock())) if (((1-deviation) * executed) > skipped) {
.getReferenceTime(); skipped += 1;
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); scheduleNextWakeup(t + Simulation.MILLISECOND);
return;
} }
/* Execute one millisecond */ /* Execute one millisecond */
@ -219,9 +220,8 @@ public class MicaZMote extends AbstractEmulatedMote implements Mote {
cyclesExecuted += interpreter.step(); cyclesExecuted += interpreter.step();
} }
/* time deviation book keeping */ /* book keeping */
((MicaClock) (myMoteInterfaceHandler.getClock())).setReferenceTime(rtime executed += 1;
+ (deviation * Simulation.MILLISECOND));
/* TODO Poll mote interfaces? */ /* TODO Poll mote interfaces? */

View file

@ -54,7 +54,6 @@ public class MicaClock extends Clock {
private MicaZMote myMote; private MicaZMote myMote;
private long timeDrift; /* Microseconds */ private long timeDrift; /* Microseconds */
private double referenceTime; /* Microseconds */
private double deviation; private double deviation;
public MicaClock(Mote mote) { public MicaClock(Mote mote) {
@ -87,12 +86,4 @@ public class MicaClock extends Clock {
public long getDrift() { public long getDrift() {
return timeDrift; return timeDrift;
} }
public void setReferenceTime(double referenceTime) {
this.referenceTime = referenceTime;
}
public double getReferenceTime() {
return referenceTime;
}
} }

View file

@ -290,13 +290,22 @@ public abstract class MspMote extends AbstractEmulatedMote implements Mote, Watc
private long lastExecute = -1; /* Last time mote executed */ private long lastExecute = -1; /* Last time mote executed */
private long nextExecute; private long nextExecute;
private long executed = 0;
private long skipped = 0;
public void execute(long time) { public void execute(long time) {
execute(time, EXECUTE_DURATION_US); execute(time, EXECUTE_DURATION_US);
} }
public void execute(long t, int duration) { public void execute(long t, int duration) {
MspClock clock = ((MspClock) (myMoteInterfaceHandler.getClock()));
double deviation = clock.getDeviation();
long drift = clock.getDrift();
/* Wait until mote boots */ /* Wait until mote boots */
if (!booted && myMoteInterfaceHandler.getClock().getTime() < 0) { if (!booted && clock.getTime() < 0) {
scheduleNextWakeup(t - myMoteInterfaceHandler.getClock().getTime()); scheduleNextWakeup(t - clock.getTime());
return; return;
} }
booted = true; booted = true;
@ -315,27 +324,17 @@ public abstract class MspMote extends AbstractEmulatedMote implements Mote, Watc
throw new RuntimeException("Bad event ordering: " + lastExecute + " < " + t); throw new RuntimeException("Bad event ordering: " + lastExecute + " < " + t);
} }
/* time deviation skip if ahead*/ if (((1-deviation) * executed) > skipped) {
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; lastExecute = t;
nextExecute = t + duration; nextExecute = t+duration;
((MspClock) (myMoteInterfaceHandler.getClock())).setReferenceTime(rtime skipped += duration;
+ duration + (deviation * duration));
scheduleNextWakeup(nextExecute); scheduleNextWakeup(nextExecute);
return;
} }
/* Execute MSPSim-based mote */ /* Execute MSPSim-based mote */
/* TODO Try-catch overhead */ /* TODO Try-catch overhead */
try { try {
nextExecute = nextExecute = myCpu.stepMicros(t-lastExecute, duration) + t + duration;
t + duration +
myCpu.stepMicros(t - lastExecute, duration);
lastExecute = t; lastExecute = t;
} catch (EmulationException e) { } catch (EmulationException e) {
String trace = e.getMessage() + "\n\n" + getStackTrace(); String trace = e.getMessage() + "\n\n" + getStackTrace();
@ -347,13 +346,11 @@ public abstract class MspMote extends AbstractEmulatedMote implements Mote, Watc
if (nextExecute < t) { if (nextExecute < t) {
throw new RuntimeException(t + ": MSPSim requested early wakeup: " + nextExecute); throw new RuntimeException(t + ": MSPSim requested early wakeup: " + nextExecute);
} }
/*logger.debug(t + ": Schedule next wakeup at " + nextExecute);*/ /*logger.debug(t + ": Schedule next wakeup at " + nextExecute);*/
executed += duration;
scheduleNextWakeup(nextExecute); scheduleNextWakeup(nextExecute);
/* time deviation book keeping */
((MspClock) (myMoteInterfaceHandler.getClock())).setReferenceTime(rtime
+ deviation * (nextExecute - lastExecute));
if (stopNextInstruction) { if (stopNextInstruction) {
stopNextInstruction = false; stopNextInstruction = false;
throw new RuntimeException("MSPSim requested simulation stop"); throw new RuntimeException("MSPSim requested simulation stop");

View file

@ -52,12 +52,10 @@ public class MspClock extends Clock {
private Simulation simulation; private Simulation simulation;
private long timeDrift; /* Microseconds */ private long timeDrift; /* Microseconds */
private double referenceTime; /* Microseconds */
private double deviation; private double deviation;
public MspClock(Mote mote) { public MspClock(Mote mote) {
simulation = mote.getSimulation(); simulation = mote.getSimulation();
referenceTime = 0.0;
deviation = 1.0; deviation = 1.0;
} }
@ -76,14 +74,6 @@ public class MspClock extends Clock {
public long getDrift() { public long getDrift() {
return timeDrift; return timeDrift;
} }
public void setReferenceTime(double referenceTime) {
this.referenceTime = referenceTime;
}
public double getReferenceTime() {
return referenceTime;
}
public void setDeviation(double deviation) { public void setDeviation(double deviation) {
assert (deviation>0.0) && (deviation<=1.0); assert (deviation>0.0) && (deviation<=1.0);