Skip to content
Snippets Groups Projects
Commit 19692c5a authored by Brutzman, Don's avatar Brutzman, Don
Browse files

more precise handling to PDU timestamp as double for seconds past hour

parent 551c86d4
No related branches found
No related tags found
No related merge requests found
......@@ -33,11 +33,11 @@ public class ExampleSimulationProgram
protected PduFactory pduFactory;
/** seconds per loop for real-time or simulation execution */
private double timeStepDuration = 1.0; // seconds TODO encapsulate
/** initial simulation time */
double initialTime = 0.0;
/** current simulation time */
double simulationTime = initialTime;
private double simulationTimeStepDuration = 1.0; // seconds TODO encapsulate
/** initial simulation time in secondse */
double simulationTimeInitial = 0.0;
/** current simulation time in seconds */
double simulationTimeSeconds = simulationTimeInitial;
/** Maximum number of simulation loops */
int MAX_LOOP_COUNT = 4;
......@@ -92,17 +92,19 @@ public class ExampleSimulationProgram
initialize();
}
/** Initialize channel setup for OpenDis7 and report a test PDU */
/** Initialize channel setup for OpenDis7 and report a test PDU
* @see initializeDisChannel
* @see initializeSimulationEntities */
private void initialize()
{
initializeDisChannel(); // must come first
initializeSimulationEntities();
initializeDisChannel(); // must come first, uses PduFactory
initializeSimulationEntities(); // set unchanging parameters
disChannel.join(); // TODO further functionality expected
String timeStepMessage = "Simulation timestep duration " + getTimeStepDuration() + " seconds";
int simulationTimeMsec = 0;
disChannel.sendCommentPdu(simulationTimeMsec, disChannel.COMMENTPDU_SIMULATION_TIMESTEP, timeStepMessage);
String timeStepMessage = "Simulation timestep duration " + getSimulationTimeStepDuration() + " seconds";
disChannel.sendCommentPdu(simulationTimeSeconds, disChannel.COMMENTPDU_SIMULATION_TIMESTEP, timeStepMessage);
// additional constructor initialization goes here
}
......@@ -182,7 +184,7 @@ public class ExampleSimulationProgram
* All of the other methods are setup, teardown and configuration that you may find
* interesting, even helpful, but don't really have to worry about.
*/
@SuppressWarnings("SleepWhileInLoop") // yes we do that
@SuppressWarnings("SleepWhileInLoop") // yes we might do that
public void runSimulationLoops ()
{
try
......@@ -192,8 +194,8 @@ public class ExampleSimulationProgram
boolean simulationComplete = false; // sentinel variable as termination condition, are we done yet?
// TODO reset Clock Time for today's date and timestamp to zero, providing consistent outputs for each simulation run
String timeMessage = "Simulation time " + simulationTime + " at LocalDateTime " + LocalDateTime.now();
disChannel.sendCommentPdu(VariableRecordType.TIME, timeMessage);
String timeMessage = "Simulation time " + simulationTimeSeconds + " at LocalDateTime " + LocalDateTime.now();
disChannel.sendCommentPdu(simulationTimeSeconds, disChannel.COMMENTPDU_TIME, timeMessage);
// TODO replace enumeration with disChannel.COMMENTPDU_TIME
// TODO fix VariableRecordType.TIME_AMP_DATE_VALID
......@@ -239,8 +241,8 @@ public class ExampleSimulationProgram
// staying synchronized with timestep: wait duration for elapsed time in this loop
// Thread.sleep needs a (long) parameter for milliseconds, which are clumsy to use sometimes
Thread.sleep((long)(getTimeStepDuration() * 1000)); // units of seconds * (1000 msec/sec) = milliseconds
System.out.println ("... [Pausing for " + getTimeStepDuration() + " seconds]");
Thread.sleep((long)(getSimulationTimeStepDuration() * 1000)); // units of seconds * (1000 msec/sec) = milliseconds
System.out.println ("... [Pausing for " + getSimulationTimeStepDuration() + " seconds]");
// OK now send the status PDUs for this loop, and then continue
System.out.println ("... sending PDUs of interest for simulation step " + simulationLoopCount + ", monitor loopback to confirm sent");
......@@ -248,12 +250,12 @@ public class ExampleSimulationProgram
// TODO set timesteps in PDUs
sendAllPdusForLoopTimestep(simulationTime,
entityStatePdu_1,
firePdu_1a,
disChannel.COMMENTPDU_APPLICATION_STATUS,
sendAllPdusForLoopTimestep(simulationTimeSeconds,
entityStatePdu_1,
firePdu_1a,
disChannel.COMMENTPDU_APPLICATION_STATUS,
narrativeMessage1, narrativeMessage2, narrativeMessage3);
disChannel.sendSinglePdu(simulationTime, entityStatePdu_2); // me too i.e. 2!
disChannel.sendSinglePdu(simulationTimeSeconds, entityStatePdu_2); // me too i.e. 2!
System.out.println ("... [PDUs of interest successfully sent for this loop]");
System.out.flush();
......@@ -266,10 +268,10 @@ public class ExampleSimulationProgram
System.out.flush();
break;
}
simulationTime += getTimeStepDuration();
simulationTimeSeconds += getSimulationTimeStepDuration(); // good practice: increment simulationTime as lastst action in that loop
} // end of simulation loop
// ===================================================================================================// ===================================================================================================
} // end of simulation loop, continue until done
// ===================================================================================================// ===================================================================================================// ===================================================================================================// ===================================================================================================
narrativeMessage2 = "runSimulation() completed successfully"; // all done, so tell everyone else on the channel
// TODO better javadoc needs to be autogenerated for VariableRecordType enumerations
......@@ -286,29 +288,29 @@ public class ExampleSimulationProgram
/**
* Send EntityState, Fire, Comment PDUs that got updated for this loop, reflecting state of current simulation timestep.
* @param simulationTimeSeconds simulation time, applied as PDU timestamp
* @param simTimeSeconds simulation time in second, applied to PDU as timestamp
* @param entityStatePdu the ESPDU to send, if any
* @param firePdu the FirePDU to send, if any
* @param commentType enumeration value describing purpose of the narrative comment
* @param commentType enumeration value describing purpose of the narrative comment PDU
* @param comments String array of narrative comments
* @see DisChannel
* @see DisTime
* @see <a href="https://docs.oracle.com/javase/tutorial/java/javaOO/arguments.html">Passing Information to a Method or a Constructor</a> Arbitrary Number of Arguments
*/
public void sendAllPdusForLoopTimestep(double simulationTimeSeconds,
EntityStatePdu entityStatePdu,
FirePdu firePdu,
VariableRecordType commentType,
// vararg... variable-length set of String comments can optionally follow
String... comments)
public void sendAllPdusForLoopTimestep(double simTimeSeconds,
EntityStatePdu entityStatePdu,
FirePdu firePdu,
VariableRecordType commentType,
// vararg... variable-length set of String comments can optionally follow
String... comments)
{
int simulationTimeMsec = (int)(simulationTimeSeconds * 1000.0);
if (entityStatePdu != null)
disChannel.sendSinglePdu(simulationTimeMsec, entityStatePdu);
disChannel.sendSinglePdu(simTimeSeconds, entityStatePdu);
if (firePdu != null)
disChannel.sendSinglePdu(simulationTimeMsec, firePdu); // bang
disChannel.sendSinglePdu(simTimeSeconds, firePdu); // bang
disChannel.sendCommentPdu(simulationTimeMsec, commentType, comments); // empty comments are filtered
disChannel.sendCommentPdu(simTimeSeconds, commentType, comments); // empty comments are filtered
}
/**
......@@ -349,6 +351,22 @@ public class ExampleSimulationProgram
newDescriptor = "";
this.descriptor = newDescriptor;
}
/**
* parameter accessor method
* @return the simulationTimeStepDuration in seconds
*/
public double getSimulationTimeStepDuration() {
return simulationTimeStepDuration;
}
/**
* parameter accessor method
* @param timeStepDurationSeconds the simulationTimeStepDuration in seconds to set
*/
public void setSimulationTimeStepDuration(double timeStepDurationSeconds) {
this.simulationTimeStepDuration = timeStepDurationSeconds;
}
/** Locally instantiable copy of program, can be subclassed. */
protected static ExampleSimulationProgram thisProgram;
......@@ -365,7 +383,7 @@ public class ExampleSimulationProgram
thisProgram.disChannel.printlnTRACE("main() started...");
thisProgram.handleArgs (args); // process command-line invocation arguments
thisProgram.handleArgs(args); // process any command-line invocation arguments
thisProgram.runSimulationLoops(); // ... your simulation execution code goes in there ...
......@@ -375,20 +393,4 @@ public class ExampleSimulationProgram
System.exit(0); // ensure all threads and sockets released
}
/**
* parameter accessor method
* @return the timeStepDuration
*/
public double getTimeStepDuration() {
return timeStepDuration;
}
/**
* parameter accessor method
* @param timeStepDuration the timeStepDuration to set
*/
public void setTimeStepDuration(double timeStepDuration) {
this.timeStepDuration = timeStepDuration;
}
}
......@@ -66,7 +66,7 @@ public class ExampleTrackInterpolation extends ExampleSimulationProgram
// TODO reset Clock Time to today's date and timestamp to zero, providing consistent outputs for each simulation run
DisTime.setEpochLvcNow();
simulationTime = initialTime - getTimeStepDuration(); // pre-initialization for first loop
simulationTimeSeconds = simulationTimeInitial - getSimulationTimeStepDuration(); // pre-initialization for first loop
initializeSimulationEntities();
......@@ -113,7 +113,7 @@ public class ExampleTrackInterpolation extends ExampleSimulationProgram
while (simulationLoopCount < SIMULATION_MAX_LOOP_COUNT) // are we done yet?
{
simulationLoopCount++; // good practice: increment loop counter as first action in that loop
simulationTime += getTimeStepDuration(); // good practice: update clock along with loop index
simulationTimeSeconds += getSimulationTimeStepDuration(); // good practice: update clock along with loop index
// =============================================================================================
// * your own simulation code starts here! *****************************************************
......@@ -135,11 +135,11 @@ public class ExampleTrackInterpolation extends ExampleSimulationProgram
espdu_1.setEntityLinearVelocity(speedEntity_1, directionEntity_1);
// Where is my entity? Insert changes in position; this sample only changes X position.
espdu_1.advanceEntityLocation(getTimeStepDuration());
espdu_1.advanceEntityLocation(getSimulationTimeStepDuration());
// make your reports: narrative code for CommentPdu here (set all to empty strings to avoid sending)
narrativeMessage1 = "MV3500 TrackSimulationProgram";
narrativeMessage2 = "runSimulation() loop " + simulationLoopCount + " at time " + simulationTime;
narrativeMessage2 = "runSimulation() loop " + simulationLoopCount + " at time " + simulationTimeSeconds;
narrativeMessage3 = ""; // intentionally blank for testing
// your loop termination condition goes here
......@@ -155,8 +155,8 @@ public class ExampleTrackInterpolation extends ExampleSimulationProgram
// Thread.sleep needs a (long) parameter for milliseconds, which are clumsy to use sometimes
if (false) // real-time operation or simulation speedup
{
Thread.sleep((long) (getTimeStepDuration() * 1000)); // seconds * (1000 msec/sec) = milliseconds
System.out.println(disChannel.getTRACE_PREFIX() + "Pausing for " + getTimeStepDuration() + " seconds");
Thread.sleep((long) (getSimulationTimeStepDuration() * 1000)); // seconds * (1000 msec/sec) = milliseconds
System.out.println(disChannel.getTRACE_PREFIX() + "Pausing for " + getSimulationTimeStepDuration() + " seconds");
}
// OK now send the status PDUs for this loop, and then continue
......
......@@ -106,7 +106,7 @@ public class PduTrack
// initialization code here
// https://docs.oracle.com/javase/tutorial/datetime/TOC.html
// https://docs.oracle.com/en/java/javase/17/docs/api/java.base/java/time/package-summary.html
// https://docs.oracle.com/en/java/javase/18/docs/api/java.base/java/time/package-summary.html
// https://stackoverflow.com/questions/5175728/how-to-get-the-current-date-time-in-java/5175900 (scroll down to java.time)
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("d MMMM yyyy");
todaysDateString = LocalDate.now().format(formatter);
......@@ -346,7 +346,7 @@ public class PduTrack
/**
* Sort all PDUs by timestamp
* @see <a href="https://stackoverflow.com/questions/16252269/how-to-sort-an-arraylist">StackOverflow: How to sort an ArrayList?</a>
* @see <a href="https://docs.oracle.com/en/java/javase/17/docs/api/java.base/java/util/doc-files/coll-overview.html">Collections Framework Overview</a>
* @see <a href="https://docs.oracle.com/en/java/javase/18/docs/api/java.base/java/util/doc-files/coll-overview.html">Collections Framework Overview</a>
* @return same object to permit progressive setters
*/
public PduTrack sortPdus()
......@@ -368,7 +368,7 @@ public class PduTrack
/**
* Reverse order of PDU list
* @see <a href="https://stackoverflow.com/questions/10766492/what-is-the-simplest-way-to-reverse-an-arraylist">StackOverflow: What is the Simplest Way to Reverse an ArrayList?</a>
* @see <a href="https://docs.oracle.com/en/java/javase/17/docs/api/java.base/java/util/doc-files/coll-overview.html">Collections Framework Overview</a>
* @see <a href="https://docs.oracle.com/en/java/javase/18/docs/api/java.base/java/util/doc-files/coll-overview.html">Collections Framework Overview</a>
* @return same object to permit progressive setters
*/
public PduTrack reversePdus()
......
This diff is collapsed.
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment