From 45d098554549f298a36fe444fcaed6a16fe76063 Mon Sep 17 00:00:00 2001
From: Markus Quaritsch <markus.quaritsch@tugraz.at>
Date: Thu, 15 Oct 2015 08:56:29 +0200
Subject: [PATCH] introduce driving behavior (databus) gearbox disengages when
 braking and engine-speed would drop below idle speed

---
 .../Models/SimulationComponent/Impl/Driver.cs |  6 ++---
 .../SimulationComponent/Impl/Gearbox.cs       |  5 +++-
 .../SimulationComponent/Impl/ShiftStrategy.cs | 25 ++++++++++---------
 3 files changed, 20 insertions(+), 16 deletions(-)

diff --git a/VectoCore/Models/SimulationComponent/Impl/Driver.cs b/VectoCore/Models/SimulationComponent/Impl/Driver.cs
index 1dfd6ed2b3..6e0320d3b6 100644
--- a/VectoCore/Models/SimulationComponent/Impl/Driver.cs
+++ b/VectoCore/Models/SimulationComponent/Impl/Driver.cs
@@ -305,7 +305,7 @@ namespace TUGraz.VectoCore.Models.SimulationComponent.Impl
 				Case<ResponseSuccess>().
 				Case<ResponseUnderload>(). // driver limits acceleration, operating point may be below engine's 
 				//drag load resp. below 0
-				//Case<ResponseOverload>(). // driver limits acceleration, operating point may be above 0 (GBX), use brakes
+				Case<ResponseOverload>(). // driver limits acceleration, operating point may be above 0 (GBX), use brakes
 				Case<ResponseGearShift>().
 				Case<ResponseFailTimeInterval>(r => {
 					retVal = new ResponseDrivingCycleDistanceExceeded() {
@@ -769,10 +769,10 @@ namespace TUGraz.VectoCore.Models.SimulationComponent.Impl
 		/// <returns></returns>
 		public IResponse DrivingActionHalt(Second absTime, Second dt, MeterPerSecond targetVelocity, Radian gradient)
 		{
-			if (!targetVelocity.IsEqual(0) || !DataBus.VehicleSpeed.IsEqual(0)) {
+			if (!targetVelocity.IsEqual(0) || !DataBus.VehicleSpeed.IsEqual(0, 1e-3)) {
 				throw new NotImplementedException("TargetVelocity or VehicleVelocity is not zero!");
 			}
-			DataBus.BreakPower = double.PositiveInfinity.SI<Watt>();
+			DataBus.BreakPower = Double.PositiveInfinity.SI<Watt>();
 			var retVal = NextComponent.Request(absTime, dt, 0.SI<MeterPerSquareSecond>(), gradient);
 			retVal.Switch().
 				Case<ResponseGearShift>(r => {
diff --git a/VectoCore/Models/SimulationComponent/Impl/Gearbox.cs b/VectoCore/Models/SimulationComponent/Impl/Gearbox.cs
index 2dafcc1493..b528cbe88a 100644
--- a/VectoCore/Models/SimulationComponent/Impl/Gearbox.cs
+++ b/VectoCore/Models/SimulationComponent/Impl/Gearbox.cs
@@ -186,6 +186,7 @@ namespace TUGraz.VectoCore.Models.SimulationComponent.Impl
 		/// </returns>
 		public IResponse Request(Second absTime, Second dt, NewtonMeter torque, PerSecond angularVelocity, bool dryRun)
 		{
+			Log.Debug("Gearbox Power Request: torque: {0}, angularVelocity: {1}", torque, angularVelocity);
 			if (DataBus.VehicleStopped) {
 				_shiftTime = absTime;
 			}
@@ -293,7 +294,9 @@ namespace TUGraz.VectoCore.Models.SimulationComponent.Impl
 			}
 
 			if (dryRun) {
-				if (inEngineSpeed < DataBus.EngineIdleSpeed && DataBus.VehicleSpeed < Constants.SimulationSettings.VehicleStopClutchDisengageSpeed) {
+				if ((DataBus.DrivingBehavior == DrivingBehavior.Braking || DataBus.DrivingBehavior == DrivingBehavior.Coasting) &&
+					inEngineSpeed < DataBus.EngineIdleSpeed &&
+					DataBus.VehicleSpeed < Constants.SimulationSettings.VehicleStopClutchDisengageSpeed) {
 					_disengaged = true;
 					_shiftTime = absTime + dt;
 					_strategy.Disengage(absTime, dt, outTorque, outAngularVelocity);
diff --git a/VectoCore/Models/SimulationComponent/Impl/ShiftStrategy.cs b/VectoCore/Models/SimulationComponent/Impl/ShiftStrategy.cs
index 7261ffba83..3c0cb58e8d 100644
--- a/VectoCore/Models/SimulationComponent/Impl/ShiftStrategy.cs
+++ b/VectoCore/Models/SimulationComponent/Impl/ShiftStrategy.cs
@@ -289,18 +289,19 @@ namespace TUGraz.VectoCore.Models.SimulationComponent.Impl
 				Gearbox.Gear = tryNextGear;
 				var response = (ResponseDryRun)Gearbox.Request(absTime, dt, outTorque, outAngularVelocity, true);
 				Gearbox.Gear = tmpGear;
-
-				inAngularVelocity = Data.Gears[tryNextGear].Ratio * outAngularVelocity;
-				inTorque = response.ClutchPowerRequest / inAngularVelocity;
-
-				// if next gear supplied enough power reserve: take it
-				// otherwise take
-				if (!IsBelowDownShiftCurve(tryNextGear, inTorque, inAngularVelocity)) {
-					var fullLoadPower = response.EnginePowerRequest - response.DeltaFullLoad;
-					var reserve = 1 - (response.EnginePowerRequest / fullLoadPower).Cast<Scalar>();
-
-					if (reserve >= Data.TorqueReserve) {
-						NextGear = tryNextGear;
+				if (!(response is ResponseEngineSpeedTooLow)) {
+					inAngularVelocity = Data.Gears[tryNextGear].Ratio * outAngularVelocity;
+					inTorque = response.ClutchPowerRequest / inAngularVelocity;
+
+					// if next gear supplied enough power reserve: take it
+					// otherwise take
+					if (!IsBelowDownShiftCurve(tryNextGear, inTorque, inAngularVelocity)) {
+						var fullLoadPower = response.EnginePowerRequest - response.DeltaFullLoad;
+						var reserve = 1 - (response.EnginePowerRequest / fullLoadPower).Cast<Scalar>();
+
+						if (reserve >= Data.TorqueReserve) {
+							NextGear = tryNextGear;
+						}
 					}
 				}
 			}
-- 
GitLab