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 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());
@ -200,17 +208,10 @@ 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? */

View file

@ -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;
}
}

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 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);
nextExecute = myCpu.stepMicros(t-lastExecute, duration) + t + duration;
lastExecute = t;
} catch (EmulationException e) {
String trace = e.getMessage() + "\n\n" + getStackTrace();
@ -347,12 +346,10 @@ 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);*/
scheduleNextWakeup(nextExecute);
/* time deviation book keeping */
((MspClock) (myMoteInterfaceHandler.getClock())).setReferenceTime(rtime
+ deviation * (nextExecute - lastExecute));
/*logger.debug(t + ": Schedule next wakeup at " + nextExecute);*/
executed += duration;
scheduleNextWakeup(nextExecute);
if (stopNextInstruction) {
stopNextInstruction = false;

View file

@ -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;
}
@ -77,14 +75,6 @@ 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) && (deviation<=1.0);
this.deviation = deviation;