Skip to content

Commit

Permalink
Feature/add time sync logs (#205)
Browse files Browse the repository at this point in the history
<!-- Thanks for the contribution, this is awesome. -->

# PR Details
## Description

<!--- Describe your changes in detail -->
Adds logs to support
usdot-fhwa-stol/carma-analytics-fotda#43
## Related GitHub Issue

<!--- This project only accepts pull requests related to open GitHub
issues or Jira Keys -->
<!--- If suggesting a new feature or change, please discuss it in an
issue first -->
<!--- If fixing a bug, there should be an issue describing it with steps
to reproduce -->
<!--- Please DO NOT name partially fixed issues, instead open an issue
specific to this fix -->
<!--- Please link to the issue here: -->

## Related Jira Key
CDAR-774
<!-- e.g. CAR-123 -->

## Motivation and Context
Data Analysis
<!--- Why is this change required? What problem does it solve? -->

## How Has This Been Tested?

<!--- Please describe in detail how you tested your changes. -->
<!--- Include details of your testing environment, and the tests you ran
to -->
<!--- see how your change affects other areas of the code, etc. -->

## Types of changes

<!--- What types of changes does your code introduce? Put an `x` in all
the boxes that apply: -->

- [ ] Defect fix (non-breaking change that fixes an issue)
- [ ] New feature (non-breaking change that adds functionality)
- [ ] Breaking change (fix or feature that cause existing functionality
to change)

## Checklist:

<!--- Go over all the following points, and put an `x` in all the boxes
that apply. -->
<!--- If you're unsure about any of these, don't hesitate to ask. We're
here to help! -->

- [ ] I have added any new packages to the sonar-scanner.properties file
- [ ] My change requires a change to the documentation.
- [ ] I have updated the documentation accordingly.
- [ ] I have read the
[**CONTRIBUTING**](https://github.com/usdot-fhwa-stol/carma-platform/blob/develop/Contributing.md)
document.
- [ ] I have added tests to cover my changes.
- [ ] All new and existing tests passed.
  • Loading branch information
MishkaMN authored Mar 21, 2024
1 parent 4f25a05 commit 233aaa8
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -206,6 +206,12 @@ public abstract class AbstractSumoAmbassador extends AbstractFederateAmbassador
*/
protected boolean receivedSimulationStep = false;

/**
* First time when the sumo ambassador is called to progress to the next simulation step
*
*/
protected boolean firstAttemptToAdvanceToNextStep = true;

/**
* CARLA federate is enabled
*/
Expand Down Expand Up @@ -1193,6 +1199,16 @@ public synchronized void processTimeAdvanceGrant(long time) throws InternalFeder
return;
}

// A script to validate time synchronization of tools in CDASim currently relies on the following
// log line. TODO: This line is meant to be removed in the future upon completion of this work:
// https://github.com/usdot-fhwa-stol/carma-analytics-fotda/pull/43
if (log.isDebugEnabled() && (!receivedSimulationStep && firstAttemptToAdvanceToNextStep))
{
long millis = System.currentTimeMillis();
log.info("Simulation Time: {} here current system time is: {} and nextTimeStep: {} and ambasador id: {}", (int) (time/1e6), millis, nextTimeStep, getId());
firstAttemptToAdvanceToNextStep = false;
}

if (time > lastAdvanceTime) {
// actually add vehicles in sumo, before we reach the next advance time
flushNotYetAddedVehicles(lastAdvanceTime);
Expand Down Expand Up @@ -1229,6 +1245,7 @@ public synchronized void processTimeAdvanceGrant(long time) throws InternalFeder
rti.triggerInteraction(simulationStepResult.getTrafficDetectorUpdates());
this.rti.triggerInteraction(simulationStepResult.getTrafficLightUpdates());
receivedSimulationStep = false;
firstAttemptToAdvanceToNextStep = true;
}

// System.out.println("Sumo request time advance at time: " + nextTimeStep);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,9 @@

package org.eclipse.mosaic.rti.time;

import java.util.HashMap;
import java.util.Map;

import org.eclipse.mosaic.rti.MosaicComponentParameters;
import org.eclipse.mosaic.rti.api.ComponentProvider;
import org.eclipse.mosaic.rti.api.FederateAmbassador;
Expand All @@ -31,6 +34,26 @@ public class SequentialTimeManagement extends AbstractTimeManagement {

private final int realtimeBrake;

// Debugging & Logging
HashMap<String, Long> loggingMap = new HashMap<>();

/**
* Prints log for time synchronization monitor script. Only meant to be printed for debugging purposes.
* Please see https://github.com/usdot-fhwa-stol/carma-analytics-fotda/pull/43
*
* @param event FederateEvent requested by one of the ambassadaor
* @param startTime current system time when the event request was received
*/

private void printTimeSyncDebugLogs(FederateEvent event, long startTime){
if (!loggingMap.containsKey(event.getFederateId()) ||
(loggingMap.containsKey(event.getFederateId()) && loggingMap.get(event.getFederateId()) != event.getRequestedTime()))
{
loggingMap.put(event.getFederateId(), event.getRequestedTime());
this.logger.debug("Simulation Time: {} where current system time is: {} and requested from id: {}", (int) (event.getRequestedTime()/1e6), startTime, event.getFederateId());
}
}

/**
* Creates a new instance of the sequential time management.
*
Expand Down Expand Up @@ -87,6 +110,15 @@ public void runSimulation() throws InternalFederateException, IllegalValueExcept
if (ambassador != null) {
federation.getMonitor().onBeginActivity(event);
long startTime = System.currentTimeMillis();

// A script to validate time synchronization of tools in CDASim currently relies on the following
// log line. TODO: This line is meant to be removed in the future upon completion of this work:
// https://github.com/usdot-fhwa-stol/carma-analytics-fotda/pull/43
if (this.logger.isDebugEnabled())
{
printTimeSyncDebugLogs(event, startTime);
}

ambassador.advanceTime(event.getRequestedTime());
federation.getMonitor().onEndActivity(event, System.currentTimeMillis() - startTime);

Expand Down

0 comments on commit 233aaa8

Please sign in to comment.