Hi there!
Inside a for-loop I'm controlling the simulation-step-based traffic simulator SUMO by retrieving and processing information of vehicles. To make sure that my program simulates in "real-time" (1 simulation-step = 1 second) I want to sleep my program after the processing phase until the next time step begins. To get better results I'm calculating the time stamp based on a initially taken reference time stamp.
The loop looks like this:
System.out.println("start of traffic simulation ...");
for (int i = 0; i < stepCount; i++)
{
System.out.println("step: " + i);
// set before timeStamp
beforeTimeStamp = System.currentTimeMillis();
if (firstStep)
{
// get reference timeStamp
referenceTimeStamp = beforeTimeStamp;
firstStep = false;
}
else
{
// get next vehicleVector
vehicleVector = masterControl.traCIclient.simulateStep();
}
// process vehicleVector
// set after timeStamp
afterTimeStamp = System.currentTimeMillis();
processingTime = afterTimeStamp - beforeTimeStamp;
// calculate sleepTime
sleepTime = referenceTimeStamp + ((i + 1) * 1000) - afterTimeStamp;
// sleep for sleepTime ms
Thread.sleep(sleepTime);
}
System.out.println("end of traffic simulation ...");
Here's the output of some variables:
step: 0 beforeTimeStamp 1252317242565 reference time: 1252317242565 processing time: 394 test time: 1252317243565 afterTimeStamp 1252317242959 sleepTime: 606 step: 1 beforeTimeStamp 1252317242961 processing time: 665 test time: 1252317244565 afterTimeStamp 1252317243626 sleepTime: 939 (exspected: 1000 - 665 = 335)
As you can see the sleep time is only correct for the first simulation step. I have no clue what might me going wrong here. Does anybody has an idea?
BR,
Markus