From a74b9ded68c300f1f8db4be834fccdfeb5b41455 Mon Sep 17 00:00:00 2001
From: Markus Quaritsch <markus.quaritsch@tugraz.at>
Date: Wed, 18 Jan 2017 09:47:07 +0100
Subject: [PATCH] fix PTO cycles: correction reset cycle iterator, correction
 detect end of cycle, commit pto cycle only if active

---
 .../SimulationComponent/Impl/DistanceBasedDrivingCycle.cs  | 3 ++-
 .../SimulationComponent/Impl/DrivingCycleEnumerator.cs     | 1 +
 .../SimulationComponent/Impl/IdleControllerSwitcher.cs     | 7 +++++--
 .../Models/SimulationComponent/Impl/PTOCycleController.cs  | 2 +-
 4 files changed, 9 insertions(+), 4 deletions(-)

diff --git a/VectoCore/VectoCore/Models/SimulationComponent/Impl/DistanceBasedDrivingCycle.cs b/VectoCore/VectoCore/Models/SimulationComponent/Impl/DistanceBasedDrivingCycle.cs
index a2430f1021..81cbc004fa 100644
--- a/VectoCore/VectoCore/Models/SimulationComponent/Impl/DistanceBasedDrivingCycle.cs
+++ b/VectoCore/VectoCore/Models/SimulationComponent/Impl/DistanceBasedDrivingCycle.cs
@@ -217,7 +217,7 @@ namespace TUGraz.VectoCore.Models.SimulationComponent.Impl
 
 		private IResponse DriveTimeInterval(Second absTime, Second dt)
 		{
-			CurrentState.AbsTime = PreviousState.AbsTime + dt;
+			CurrentState.AbsTime = absTime;
 			CurrentState.WaitTime = PreviousState.WaitTime + dt;
 			CurrentState.Gradient = ComputeGradient(0.SI<Meter>());
 			CurrentState.VehicleTargetSpeed = Left.VehicleTargetSpeed;
@@ -250,6 +250,7 @@ namespace TUGraz.VectoCore.Models.SimulationComponent.Impl
 						retVal = NextComponent.Request(absTime, r.DeltaT, 0.SI<MeterPerSecond>(), CurrentState.Gradient);
 						retVal = NextComponent.Request(absTime, ds, CurrentState.VehicleTargetSpeed, CurrentState.Gradient);
 					});
+			CurrentState.AbsTime = absTime;
 			return retVal;
 		}
 
diff --git a/VectoCore/VectoCore/Models/SimulationComponent/Impl/DrivingCycleEnumerator.cs b/VectoCore/VectoCore/Models/SimulationComponent/Impl/DrivingCycleEnumerator.cs
index 8bd4a52d3f..03ba010b74 100644
--- a/VectoCore/VectoCore/Models/SimulationComponent/Impl/DrivingCycleEnumerator.cs
+++ b/VectoCore/VectoCore/Models/SimulationComponent/Impl/DrivingCycleEnumerator.cs
@@ -99,6 +99,7 @@ namespace TUGraz.VectoCore.Models.SimulationComponent.Impl
 		public void Reset()
 		{
 			_currentCycleIndex = 0;
+			LastEntry = false;
 		}
 
 		public void Dispose() {}
diff --git a/VectoCore/VectoCore/Models/SimulationComponent/Impl/IdleControllerSwitcher.cs b/VectoCore/VectoCore/Models/SimulationComponent/Impl/IdleControllerSwitcher.cs
index f5758152b4..a6a415cba3 100644
--- a/VectoCore/VectoCore/Models/SimulationComponent/Impl/IdleControllerSwitcher.cs
+++ b/VectoCore/VectoCore/Models/SimulationComponent/Impl/IdleControllerSwitcher.cs
@@ -96,15 +96,18 @@ namespace TUGraz.VectoCore.Models.SimulationComponent.Impl
 
 		public void CommitSimulationStep(IModalDataContainer container)
 		{
-			_ptoController.CommitSimulationStep(container);
+			if (_currentController == _ptoController) {
+				_ptoController.CommitSimulationStep(container);
+			}
 		}
 
 		public Second Duration
 		{
 			get
 			{
-				if (_ptoController != null)
+				if (_ptoController != null) {
 					return _ptoController.Duration;
+				}
 				return 0.SI<Second>();
 			}
 		}
diff --git a/VectoCore/VectoCore/Models/SimulationComponent/Impl/PTOCycleController.cs b/VectoCore/VectoCore/Models/SimulationComponent/Impl/PTOCycleController.cs
index 5ae1b9637b..14c3754d1c 100644
--- a/VectoCore/VectoCore/Models/SimulationComponent/Impl/PTOCycleController.cs
+++ b/VectoCore/VectoCore/Models/SimulationComponent/Impl/PTOCycleController.cs
@@ -88,7 +88,7 @@ namespace TUGraz.VectoCore.Models.SimulationComponent.Impl
 
 		public Second GetNextCycleTime()
 		{
-			if (CycleIterator.RightSample == null) {
+			if (CycleIterator.LastEntry && AbsTime.IsEqual(CycleIterator.RightSample.Time)) {
 				return null;
 			}
 
-- 
GitLab