From 5f9480d102f742a3399ce91939432741431e29b4 Mon Sep 17 00:00:00 2001
From: Michael Krisper <michael.krisper@tugraz.at>
Date: Wed, 4 Nov 2015 13:55:44 +0100
Subject: [PATCH] pe_clutch, tq_clutch, pbrake

---
 .../Simulation/Impl/VehicleContainer.cs       |  6 +--
 .../Models/SimulationComponent/IBrakes.cs     |  2 +-
 .../Models/SimulationComponent/Impl/Brakes.cs | 26 ++++++-------
 .../Models/SimulationComponent/Impl/Clutch.cs | 38 ++++++++++---------
 .../Models/SimulationComponent/Impl/Driver.cs |  9 ++---
 5 files changed, 39 insertions(+), 42 deletions(-)

diff --git a/VectoCore/Models/Simulation/Impl/VehicleContainer.cs b/VectoCore/Models/Simulation/Impl/VehicleContainer.cs
index 38a81a5d05..59b9a6d872 100644
--- a/VectoCore/Models/Simulation/Impl/VehicleContainer.cs
+++ b/VectoCore/Models/Simulation/Impl/VehicleContainer.cs
@@ -245,10 +245,10 @@ namespace TUGraz.VectoCore.Models.Simulation.Impl
 			return Road.LookAhead(time);
 		}
 
-		public Watt BreakPower
+		public Watt BrakePower
 		{
-			get { return Brakes.BreakPower; }
-			set { Brakes.BreakPower = value; }
+			get { return Brakes.BrakePower; }
+			set { Brakes.BrakePower = value; }
 		}
 
 		public bool ClutchClosed(Second absTime)
diff --git a/VectoCore/Models/SimulationComponent/IBrakes.cs b/VectoCore/Models/SimulationComponent/IBrakes.cs
index f00f90dc8f..4f5bcf8b69 100644
--- a/VectoCore/Models/SimulationComponent/IBrakes.cs
+++ b/VectoCore/Models/SimulationComponent/IBrakes.cs
@@ -4,6 +4,6 @@ namespace TUGraz.VectoCore.Models.SimulationComponent
 {
 	public interface IBrakes
 	{
-		Watt BreakPower { get; set; }
+		Watt BrakePower { get; set; }
 	}
 }
\ No newline at end of file
diff --git a/VectoCore/Models/SimulationComponent/Impl/Brakes.cs b/VectoCore/Models/SimulationComponent/Impl/Brakes.cs
index e643cea4dc..6ab0edf518 100644
--- a/VectoCore/Models/SimulationComponent/Impl/Brakes.cs
+++ b/VectoCore/Models/SimulationComponent/Impl/Brakes.cs
@@ -11,8 +11,6 @@ namespace TUGraz.VectoCore.Models.SimulationComponent.Impl
 	{
 		protected ITnOutPort NextComponent;
 
-		protected NewtonMeter BreakTorque;
-
 		public Brakes(IVehicleContainer dataBus) : base(dataBus) {}
 
 
@@ -26,30 +24,29 @@ namespace TUGraz.VectoCore.Models.SimulationComponent.Impl
 			return this;
 		}
 
-		public Watt BreakPower { get; set; }
+		public Watt BrakePower { get; set; }
 
 		public IResponse Request(Second absTime, Second dt, NewtonMeter torque, PerSecond angularVelocity, bool dryRun = false)
 		{
-			if (!BreakPower.IsEqual(0)) {
+			var brakeTorque = 0.SI<NewtonMeter>();
+			if (!BrakePower.IsEqual(0)) {
 				if (angularVelocity.IsEqual(0)) {
-					BreakTorque = torque;
+					brakeTorque = torque;
 				} else {
-					BreakTorque = BreakPower / angularVelocity;
+					brakeTorque = BrakePower / angularVelocity;
 				}
 			}
-			if (!dryRun && BreakPower < 0) {
+			if (!dryRun && BrakePower < 0) {
 				throw new VectoSimulationException("Negative Braking Power is not allowed!");
 			}
-//			var retVal = NextComponent.Request(absTime, dt, torque - torque.Sign() *  BreakTorque, angularVelocity, dryRun);
-			var retVal = NextComponent.Request(absTime, dt, torque + BreakTorque, angularVelocity, dryRun);
-			retVal.BrakePower = BreakPower;
+			var retVal = NextComponent.Request(absTime, dt, torque + brakeTorque, angularVelocity, dryRun);
+			retVal.BrakePower = brakeTorque * angularVelocity;
 			return retVal;
 		}
 
 		public IResponse Initialize(NewtonMeter torque, PerSecond angularVelocity)
 		{
-			BreakPower = 0.SI<Watt>();
-			BreakTorque = 0.SI<NewtonMeter>();
+			BrakePower = 0.SI<Watt>();
 			return DataBus.VehicleStopped
 				? NextComponent.Initialize(0.SI<NewtonMeter>(), 0.SI<PerSecond>())
 				: NextComponent.Initialize(torque, angularVelocity);
@@ -63,13 +60,12 @@ namespace TUGraz.VectoCore.Models.SimulationComponent.Impl
 
 		protected override void DoWriteModalResults(IModalDataWriter writer)
 		{
-			writer[ModalResultField.Pbrake] = BreakPower;
+			writer[ModalResultField.Pbrake] = BrakePower;
 		}
 
 		protected override void DoCommitSimulationStep()
 		{
-			BreakPower = 0.SI<Watt>();
-			BreakTorque = 0.SI<NewtonMeter>();
+			BrakePower = 0.SI<Watt>();
 		}
 	}
 }
\ No newline at end of file
diff --git a/VectoCore/Models/SimulationComponent/Impl/Clutch.cs b/VectoCore/Models/SimulationComponent/Impl/Clutch.cs
index 410adf931e..84fb25be73 100644
--- a/VectoCore/Models/SimulationComponent/Impl/Clutch.cs
+++ b/VectoCore/Models/SimulationComponent/Impl/Clutch.cs
@@ -1,9 +1,7 @@
-using System;
-using TUGraz.VectoCore.Configuration;
+using TUGraz.VectoCore.Configuration;
 using TUGraz.VectoCore.Models.Connector.Ports;
 using TUGraz.VectoCore.Models.Simulation;
 using TUGraz.VectoCore.Models.Simulation.Data;
-using TUGraz.VectoCore.Models.Simulation.DataBus;
 using TUGraz.VectoCore.Models.SimulationComponent.Data;
 using TUGraz.VectoCore.Utils;
 
@@ -15,9 +13,11 @@ namespace TUGraz.VectoCore.Models.SimulationComponent.Impl
 		private readonly PerSecond _ratedSpeed;
 		protected ITnOutPort NextComponent;
 		private const double ClutchEff = 1;
-		private ClutchState _clutchState = SimulationComponent.ClutchState.ClutchSlipping;
+		private ClutchState _clutchState = ClutchState.ClutchSlipping;
 
 		protected ICombustionEngineIdleController IdleController;
+		private Watt _RequiredPower;
+		private NewtonMeter _RequiredTorque;
 
 
 		public Clutch(IVehicleContainer cockpit, CombustionEngineData engineData,
@@ -36,14 +36,14 @@ namespace TUGraz.VectoCore.Models.SimulationComponent.Impl
 
 		protected override void DoWriteModalResults(IModalDataWriter writer)
 		{
-			// TODO: @@@
-			writer[ModalResultField.Pe_clutch] = 0.SI<Watt>();
+			writer[ModalResultField.Pe_clutch] = _RequiredPower;
+			writer[ModalResultField.Tq_clutch] = _RequiredTorque;
 		}
 
 		protected override void DoCommitSimulationStep()
 		{
-			//todo: implement!
-			//throw new NotImplementedException();
+			_RequiredPower = 0.SI<Watt>();
+			_RequiredTorque = 0.SI<NewtonMeter>();
 		}
 
 		public ITnInPort InPort()
@@ -65,20 +65,24 @@ namespace TUGraz.VectoCore.Models.SimulationComponent.Impl
 		public IResponse Request(Second absTime, Second dt, NewtonMeter torque, PerSecond angularVelocity, bool dryRun = false)
 		{
 			if (angularVelocity == null) {
-				//var retval = NextComponent.Request(absTime, dt, torque, null, dryRun);
 				Log.Debug("Invoking IdleController...");
+
+				_RequiredPower = 0.SI<Watt>();
+				_RequiredTorque = 0.SI<NewtonMeter>();
 				var retval = IdleController.Request(absTime, dt, torque, null, dryRun);
-				retval.ClutchPowerRequest = 0.SI<Watt>();
+				retval.ClutchPowerRequest = _RequiredPower;
 				return retval;
 			}
 			if (IdleController != null) {
 				IdleController.Reset();
 			}
 			NewtonMeter torqueIn;
-			PerSecond engineSpeedIn;
-			AddClutchLoss(torque, angularVelocity, out torqueIn, out engineSpeedIn);
+			PerSecond angularVelocityIn;
+			AddClutchLoss(torque, angularVelocity, out torqueIn, out angularVelocityIn);
+			_RequiredPower = torqueIn * angularVelocityIn;
+			_RequiredTorque = torqueIn;
 
-			var retVal = NextComponent.Request(absTime, dt, torqueIn, engineSpeedIn, dryRun);
+			var retVal = NextComponent.Request(absTime, dt, torqueIn, angularVelocityIn, dryRun);
 			retVal.ClutchPowerRequest = torque * angularVelocity;
 			return retVal;
 		}
@@ -107,17 +111,15 @@ namespace TUGraz.VectoCore.Models.SimulationComponent.Impl
 			torqueIn = torque;
 			engineSpeedIn = angularVelocity;
 
-
-			// @@@quam
 			if (DataBus.VehicleStopped) {
-				_clutchState = SimulationComponent.ClutchState.ClutchOpened;
+				_clutchState = ClutchState.ClutchOpened;
 				engineSpeedIn = _idleSpeed;
 				torqueIn = 0.SI<NewtonMeter>();
 			} else {
 				var engineSpeedNorm = (angularVelocity - _idleSpeed) /
 									(_ratedSpeed - _idleSpeed);
 				if (engineSpeedNorm < Constants.SimulationSettings.CluchNormSpeed) {
-					_clutchState = SimulationComponent.ClutchState.ClutchSlipping;
+					_clutchState = ClutchState.ClutchSlipping;
 
 					var engineSpeed0 = VectoMath.Max(_idleSpeed, angularVelocity);
 					var clutchSpeedNorm = Constants.SimulationSettings.CluchNormSpeed /
@@ -128,7 +130,7 @@ namespace TUGraz.VectoCore.Models.SimulationComponent.Impl
 
 					torqueIn = ((torque * angularVelocity) / ClutchEff / engineSpeedIn);
 				} else {
-					_clutchState = SimulationComponent.ClutchState.ClutchClosed;
+					_clutchState = ClutchState.ClutchClosed;
 				}
 			}
 			Log.Debug("to Engine:   torque: {0}, angularVelocity: {1}, power {2}", torqueIn, engineSpeedIn,
diff --git a/VectoCore/Models/SimulationComponent/Impl/Driver.cs b/VectoCore/Models/SimulationComponent/Impl/Driver.cs
index 876fb23016..a0417b4cca 100644
--- a/VectoCore/Models/SimulationComponent/Impl/Driver.cs
+++ b/VectoCore/Models/SimulationComponent/Impl/Driver.cs
@@ -395,9 +395,9 @@ namespace TUGraz.VectoCore.Models.SimulationComponent.Impl
 
 			Log.Debug("Found operating point for breaking. dt: {0}, acceleration: {1} brakingPower: {2}",
 				operatingPoint.SimulationInterval,
-				operatingPoint.Acceleration, DataBus.BreakPower);
-			if (DataBus.BreakPower < 0) {
-				DataBus.BreakPower = 0.SI<Watt>();
+				operatingPoint.Acceleration, DataBus.BrakePower);
+			if (DataBus.BrakePower < 0) {
+				DataBus.BrakePower = 0.SI<Watt>();
 				return new ResponseOverload { Source = this };
 			}
 
@@ -503,7 +503,7 @@ namespace TUGraz.VectoCore.Models.SimulationComponent.Impl
 
 			do {
 				operatingPoint = ComputeTimeInterval(operatingPoint.Acceleration, ds);
-				DataBus.BreakPower = brakePower;
+				DataBus.BrakePower = brakePower;
 				var response =
 					(ResponseDryRun)
 						NextComponent.Request(absTime, operatingPoint.SimulationInterval, operatingPoint.Acceleration, gradient, true);
@@ -781,7 +781,6 @@ namespace TUGraz.VectoCore.Models.SimulationComponent.Impl
 					"TargetVelocity or VehicleVelocity is not zero! v: {0} target: {1}", DataBus.VehicleSpeed.Value(),
 					targetVelocity.Value()));
 			}
-			DataBus.BreakPower = Double.PositiveInfinity.SI<Watt>();
 			var retVal = NextComponent.Request(absTime, dt, 0.SI<MeterPerSquareSecond>(), gradient);
 			retVal.Switch().
 				Case<ResponseGearShift>(r => {
-- 
GitLab