From 075d747a469cc25c055639eb40fcec3d8dd35437 Mon Sep 17 00:00:00 2001
From: Markus Quaritsch <markus.quaritsch@tugraz.at>
Date: Wed, 3 Aug 2022 09:08:15 +0200
Subject: [PATCH] butgix calculating inertia torque of EM, bugfix searching
 electric power for torque

---
 .../SimulationComponent/Data/ElectricMotor/EfficiencyMap.cs | 2 +-
 .../Models/SimulationComponent/Impl/ElectricMotor.cs        | 2 +-
 .../Models/SimulationComponent/Strategies/HybridStrategy.cs | 6 +++++-
 3 files changed, 7 insertions(+), 3 deletions(-)

diff --git a/VectoCore/VectoCore/Models/SimulationComponent/Data/ElectricMotor/EfficiencyMap.cs b/VectoCore/VectoCore/Models/SimulationComponent/Data/ElectricMotor/EfficiencyMap.cs
index 4df9c70a3c..f61e127a71 100644
--- a/VectoCore/VectoCore/Models/SimulationComponent/Data/ElectricMotor/EfficiencyMap.cs
+++ b/VectoCore/VectoCore/Models/SimulationComponent/Data/ElectricMotor/EfficiencyMap.cs
@@ -164,7 +164,7 @@ namespace TUGraz.VectoCore.Models.SimulationComponent.Data.ElectricMotor
 					},
 					searcher: this);
 				var tmp = LookupElectricPower(avgSpeed, retVal, true);
-				if ((tmp.ElectricalPower - batPower).IsGreater(Constants.SimulationSettings.InterpolateSearchTolerance)) {
+				if (VectoMath.Abs(tmp.ElectricalPower - batPower).IsGreater(Constants.SimulationSettings.InterpolateSearchTolerance)) {
 					// searched operating point is not accurate enough...
 					retVal = SearchAlgorithm.Search(
 						maxEmTorque, elPowerMaxEM.ElectricalPower - batPower,
diff --git a/VectoCore/VectoCore/Models/SimulationComponent/Impl/ElectricMotor.cs b/VectoCore/VectoCore/Models/SimulationComponent/Impl/ElectricMotor.cs
index 69ad5fe4a1..8e6eb79c42 100644
--- a/VectoCore/VectoCore/Models/SimulationComponent/Impl/ElectricMotor.cs
+++ b/VectoCore/VectoCore/Models/SimulationComponent/Impl/ElectricMotor.cs
@@ -161,7 +161,7 @@ namespace TUGraz.VectoCore.Models.SimulationComponent.Impl
 			var avgEmSpeed = (prevEmSpeed + emSpeed) / 2;
 			var inertiaTorqueEm = avgEmSpeed.IsEqual(0)
 				? 0.SI<NewtonMeter>()
-				: Formulas.InertiaPower(avgEmSpeed, PreviousState.EMSpeed, ModelData.Inertia, dt) / avgEmSpeed;
+				: Formulas.InertiaPower(emSpeed, PreviousState.EMSpeed, ModelData.Inertia, dt) / avgEmSpeed;
 
 			var maxDriveTorqueEmMap = GetMaxDriveTorque(voltage, dt, avgEmSpeed, gear);
 			var maxRecuperationTorqueEmMap = GetMaxRecuperationTorque(voltage, dt, avgEmSpeed, gear);
diff --git a/VectoCore/VectoCore/Models/SimulationComponent/Strategies/HybridStrategy.cs b/VectoCore/VectoCore/Models/SimulationComponent/Strategies/HybridStrategy.cs
index 38e36b8cf9..33b5db495e 100644
--- a/VectoCore/VectoCore/Models/SimulationComponent/Strategies/HybridStrategy.cs
+++ b/VectoCore/VectoCore/Models/SimulationComponent/Strategies/HybridStrategy.cs
@@ -1298,6 +1298,10 @@ namespace TUGraz.VectoCore.Models.SimulationComponent.Strategies
 					}
 				} else {
 					if (emRecuperationTq.IsGreater(0)) {
+						var voltage = DataBus.BatteryInfo.InternalVoltage;
+						var maxbatDragTq = DataBus.ElectricMotorInfo(emPos).GetTorqueForElectricPower(voltage,
+							response.ElectricSystem.MaxPowerDrag, response.ElectricMotor.AngularVelocity, dt, nextGear,
+							false);
 						eval.Add(
 							new HybridResultEntry {
 								ICEOff = !DataBus.EngineInfo.EngineOn,
@@ -1307,7 +1311,7 @@ namespace TUGraz.VectoCore.Models.SimulationComponent.Strategies
 									GearboxInNeutral = false,
 									NextGear = nextGear,
 									MechanicalAssistPower = new Dictionary<PowertrainPosition, Tuple<PerSecond, NewtonMeter>> {
-										{ emPos, Tuple.Create(response.ElectricMotor.AngularVelocity, response.ElectricMotor.MaxRecuperationTorque) }
+										{ emPos, Tuple.Create(response.ElectricMotor.AngularVelocity, VectoMath.Min(maxbatDragTq, response.ElectricMotor.MaxRecuperationTorque)) }
 									}
 								}
 							});
-- 
GitLab