Skip to content

Delay in TrafficLightController::UpdateStates accelerates phases but does not delay

currently merged in branch servant : sim/src/core/slave/modules/World_OSI/TrafficLightNetwork.cpp the delay parameter of a TrafficSignalController is handled in such a way that the controller accelerates through the phases, not delays:

In TrafficLightController::UpdateStates (...)
{
   double deltaTime = time - lastTime;       //0 - (-delay) = +delay
   lastTime = time;
   timeRemainingInCurrentPhase -= deltaTime;  // tRiCP -= +delay
   while (timeRemainingInCurrentPhase <= 0)
   {
       currentPhase++;
       if (currentPhase == phases.cend())
       {
           currentPhase = phases.begin();
       }
       timeRemainingInCurrentPhase += currentPhase->duration;
       for (auto [trafficLight, newState] : currentPhase->states)
       {
           trafficLight->SetState(newState);
       }
   }
}

lastTime is initialized via the constructor by -delay this results in the deltaTime being positive for the delay, causing the whole controller to speed up towards the next phase. It might be that this implementation opposes the wording of delay.

The constructor is here:

TrafficLightController::TrafficLightController(std::vector<TrafficLightController::Phase> &&phases, double delay) :
    phases(phases),
    currentPhase(this->phases.begin()),
    timeRemainingInCurrentPhase(currentPhase->duration),
    lastTime(-delay)
{
    for (auto [trafficLight, newState] : currentPhase->states)
    {
        trafficLight->SetState(newState);
    }
    UpdateStates(0);
}