From dffd8d12eac5da70c2307d2fdff191f6c992ab83 Mon Sep 17 00:00:00 2001
From: Markus Quaritsch <markus.quaritsch@tugraz.at>
Date: Mon, 14 Sep 2015 15:54:20 +0200
Subject: [PATCH] work on testcases for driving strategy

---
 .../Impl/DefaultDriverStrategy.cs             |  5 +++
 .../Models/SimulationComponent/Impl/Driver.cs | 35 +++++++++++--------
 .../DriverStrategy/DriverStrategyTest.cs      | 35 +++++++++++++++----
 3 files changed, 54 insertions(+), 21 deletions(-)

diff --git a/VectoCore/Models/SimulationComponent/Impl/DefaultDriverStrategy.cs b/VectoCore/Models/SimulationComponent/Impl/DefaultDriverStrategy.cs
index 49001b9d27..3bf6c5c3a5 100644
--- a/VectoCore/Models/SimulationComponent/Impl/DefaultDriverStrategy.cs
+++ b/VectoCore/Models/SimulationComponent/Impl/DefaultDriverStrategy.cs
@@ -158,6 +158,9 @@ namespace TUGraz.VectoCore.Models.SimulationComponent.Impl
 							Case<ResponseUnderload>(() => {
 								// overload may happen if driver limits acceleration when rolling downhill
 								response = DriverStrategy.Driver.DrivingActionBrake(absTime, ds, targetVelocity, gradient);
+							}).
+							Case<ResponseSpeedLimitExceeded>(() => {
+								response = DriverStrategy.Driver.DrivingActionBrake(absTime, ds, targetVelocity, gradient);
 							});
 					}).
 					Case<ResponseUnderload>(r => {
@@ -168,6 +171,8 @@ namespace TUGraz.VectoCore.Models.SimulationComponent.Impl
 				response.Switch().
 					Case<ResponseUnderload>(r => {
 						response = DriverStrategy.Driver.DrivingActionBrake(absTime, ds, targetVelocity, gradient, r);
+					}).Case<ResponseSpeedLimitExceeded>(() => {
+						response = DriverStrategy.Driver.DrivingActionBrake(absTime, ds, targetVelocity, gradient);
 					});
 			}
 			return response;
diff --git a/VectoCore/Models/SimulationComponent/Impl/Driver.cs b/VectoCore/Models/SimulationComponent/Impl/Driver.cs
index 6184a7c99b..1344c0e30c 100644
--- a/VectoCore/Models/SimulationComponent/Impl/Driver.cs
+++ b/VectoCore/Models/SimulationComponent/Impl/Driver.cs
@@ -240,7 +240,7 @@ namespace TUGraz.VectoCore.Models.SimulationComponent.Impl
 			// compute speed at the end of the simulation interval. if it exceeds the limit -> return
 			var v2 = DataBus.VehicleSpeed + operatingPoint.Acceleration * operatingPoint.SimulationInterval;
 			if (v2 > maxVelocity) {
-				return new ResponseSpeedLimitExceeded();
+				return new ResponseSpeedLimitExceeded() { Source = this };
 			}
 
 			var retVal = NextComponent.Request(absTime, CurrentState.dt, CurrentState.Acceleration, gradient);
@@ -275,16 +275,18 @@ namespace TUGraz.VectoCore.Models.SimulationComponent.Impl
 
 			var operatingPoint = ComputeAcceleration(ds, nextTargetSpeed);
 
-			var v2 = DataBus.VehicleSpeed + operatingPoint.Acceleration * operatingPoint.SimulationInterval;
-			var nextAcceleration = DriverData.AccelerationCurve.Lookup(v2).Deceleration;
-			var tmp = ComputeTimeInterval(VectoMath.Min(operatingPoint.Acceleration, nextAcceleration),
-				operatingPoint.SimulationDistance);
-			if (operatingPoint.SimulationDistance.Equals(tmp.SimulationDistance)) {
-				// only decelerate more of the simulation interval is not modified
-				// i.e., braking to the next sample point
-				Log.Debug("adjusting acceleration from {0} to {1}", operatingPoint.Acceleration, tmp.Acceleration);
-				operatingPoint = tmp;
-				// ComputeTimeInterval((operatingPoint.Acceleration + tmp.Acceleration) / 2, operatingPoint.SimulationDistance);
+			if (operatingPoint.Acceleration < 0) {
+				var v2 = DataBus.VehicleSpeed + operatingPoint.Acceleration * operatingPoint.SimulationInterval;
+				var nextAcceleration = DriverData.AccelerationCurve.Lookup(v2).Deceleration;
+				var tmp = ComputeTimeInterval(VectoMath.Min(operatingPoint.Acceleration, nextAcceleration),
+					operatingPoint.SimulationDistance);
+				if (operatingPoint.SimulationDistance.Equals(tmp.SimulationDistance)) {
+					// only decelerate more of the simulation interval is not modified
+					// i.e., braking to the next sample point
+					Log.Debug("adjusting acceleration from {0} to {1}", operatingPoint.Acceleration, tmp.Acceleration);
+					operatingPoint = tmp;
+					// ComputeTimeInterval((operatingPoint.Acceleration + tmp.Acceleration) / 2, operatingPoint.SimulationDistance);
+				}
 			}
 
 
@@ -403,7 +405,10 @@ namespace TUGraz.VectoCore.Models.SimulationComponent.Impl
 				Case<ResponseGearShift>(r => origDelta = r.GearboxPowerRequest).
 				Case<ResponseUnderload>(r => origDelta = DataBus.ClutchClosed(absTime)
 					? r.Delta
-					: r.GearboxPowerRequest);
+					: r.GearboxPowerRequest).
+				Default(r => {
+					throw new UnexpectedResponseException("cannot use response for searching braking power!", r);
+				});
 			var delta = origDelta;
 
 			debug.Add(new { brakingPower = 0.SI<Watt>(), searchInterval, delta });
@@ -517,9 +522,9 @@ namespace TUGraz.VectoCore.Models.SimulationComponent.Impl
 					LogManager.EnableLogging();
 					Log.Warn("Operating Point outside driver acceleration limits: a: {0}", retVal.Acceleration);
 					LogManager.DisableLogging();
-					throw new VectoSimulationException(
-						"Could not find an operating point: operating point outside of driver acceleration limits. a: {0}",
-						retVal.Acceleration);
+					//throw new VectoSimulationException(
+					//	"Could not find an operating point: operating point outside of driver acceleration limits. a: {0}",
+					//	retVal.Acceleration);
 				}
 
 				// TODO: move to driving mode
diff --git a/VectoCoreTest/Integration/DriverStrategy/DriverStrategyTest.cs b/VectoCoreTest/Integration/DriverStrategy/DriverStrategyTest.cs
index c1141ddf26..8c61fba198 100644
--- a/VectoCoreTest/Integration/DriverStrategy/DriverStrategyTest.cs
+++ b/VectoCoreTest/Integration/DriverStrategy/DriverStrategyTest.cs
@@ -101,7 +101,8 @@ namespace TUGraz.VectoCore.Tests.Integration.DriverStrategy
 		{
 			var cycle = CreateCycleData(new[] {
 				// <s>,<v>,<grad>,<stop>
-				"  0,  20, -15,     0",
+				"   0,  20, -15,     0",
+				"  10, 60, -15,     0",
 				"1000, 60, -15,     0",
 			});
 			CreatePowerTrain(cycle, "DriverStrategy_Accelerate_20_60_downhill_15.vmod").Run();
@@ -234,7 +235,7 @@ namespace TUGraz.VectoCore.Tests.Integration.DriverStrategy
 				// <s>,<v>,<grad>,<stop>
 				"  0,  85, 0,     0",
 				"1000, 85, 0,     0",
-				"  0,  0,  0,     2",
+				"1000,  0,  0,     2",
 			});
 			CreatePowerTrain(cycle, "DriverStrategy_Accelerate_85_0_level_stop.vmod").Run();
 		}
@@ -435,14 +436,36 @@ namespace TUGraz.VectoCore.Tests.Integration.DriverStrategy
 		}
 
 		[TestMethod]
-		public void Drive_80_downhill_25()
+		public void Drive_20_downhill_15()
+		{
+			var cycle = CreateCycleData(new[] {
+				// <s>, <v>, <grad>, <stop>
+				"   0,  20,  -15,    0",
+				" 500,  20,  -25,    0",
+			});
+			CreatePowerTrain(cycle, "DriverStrategy_Drive_20_downhill_15.vmod").Run();
+		}
+
+		[TestMethod]
+		public void Drive_30_downhill_15()
+		{
+			var cycle = CreateCycleData(new[] {
+				// <s>, <v>, <grad>, <stop>
+				"   0,  30,  -15,    0",
+				" 500,  30,  -15,    0",
+			});
+			CreatePowerTrain(cycle, "DriverStrategy_Drive_30_downhill_15.vmod").Run();
+		}
+
+		[TestMethod]
+		public void Drive_50_downhill_15()
 		{
 			var cycle = CreateCycleData(new[] {
 				// <s>, <v>, <grad>, <stop>
-				"   0,  80,  -25,    0",
-				" 500,  80,  -25,    0",
+				"   0,  50,  -15,    0",
+				" 500,  50,  -15,    0",
 			});
-			CreatePowerTrain(cycle, "DriverStrategy_Drive_80_downhill_25.vmod").Run();
+			CreatePowerTrain(cycle, "DriverStrategy_Drive_50_downhill_15.vmod").Run();
 		}
 
 		[TestMethod]
-- 
GitLab