From 53716998ac5628853e3980ca050841149e76293b Mon Sep 17 00:00:00 2001
From: Michael Krisper <michael.krisper@tugraz.at>
Date: Tue, 12 Apr 2022 15:39:31 +0200
Subject: [PATCH] BatteryElectricMotorController: Corrected Bug when
 GearboxModelData is null (e.g. E3)

---
 .../Impl/BatteryElectricMotorController.cs    | 20 +++++++++----------
 1 file changed, 10 insertions(+), 10 deletions(-)

diff --git a/VectoCore/VectoCore/Models/SimulationComponent/Impl/BatteryElectricMotorController.cs b/VectoCore/VectoCore/Models/SimulationComponent/Impl/BatteryElectricMotorController.cs
index 0b76e0af92..4f0075c7c4 100644
--- a/VectoCore/VectoCore/Models/SimulationComponent/Impl/BatteryElectricMotorController.cs
+++ b/VectoCore/VectoCore/Models/SimulationComponent/Impl/BatteryElectricMotorController.cs
@@ -1,8 +1,6 @@
 using System.Linq;
 using TUGraz.VectoCommon.InputData;
 using TUGraz.VectoCommon.Utils;
-using TUGraz.VectoCore.Configuration;
-using TUGraz.VectoCore.Models.Declaration;
 using TUGraz.VectoCore.Models.Simulation.Impl;
 using TUGraz.VectoCore.Models.SimulationComponent.Data;
 using TUGraz.VectoCore.Utils;
@@ -26,16 +24,16 @@ namespace TUGraz.VectoCore.Models.SimulationComponent.Impl {
 
 		#region Implementation of IElectricMotorControl
 
-		public NewtonMeter MechanicalAssistPower(
-			Second absTime, Second dt, NewtonMeter outTorque, PerSecond prevOutAngularVelocity, PerSecond currOutAngularVelocity,
-			NewtonMeter maxDriveTorque, NewtonMeter maxRecuperationTorque,
-			PowertrainPosition position, bool dryRun)
+		public NewtonMeter MechanicalAssistPower(Second absTime, Second dt, NewtonMeter outTorque, 
+			PerSecond prevOutAngularVelocity, PerSecond currOutAngularVelocity, NewtonMeter maxDriveTorque, 
+			NewtonMeter maxRecuperationTorque, PowertrainPosition position, bool dryRun)
 		{
 			if (!DataBus.GearboxInfo.GearEngaged(absTime) && DataBus.DriverInfo.DrivingAction == DrivingAction.Roll) {
 				var avgSpeed = (prevOutAngularVelocity + currOutAngularVelocity) / 2;
 				var inertiaTorqueLoss = avgSpeed.IsEqual(0)
 					? 0.SI<NewtonMeter>()
-					: Formulas.InertiaPower(currOutAngularVelocity, prevOutAngularVelocity, ElectricMotorData.Inertia, dt) / avgSpeed;
+					: Formulas.InertiaPower(currOutAngularVelocity, prevOutAngularVelocity, 
+						ElectricMotorData.Inertia, dt) / avgSpeed;
 				//var dragTorque = ElectricMotorData.DragCurve.Lookup()
 				return (-inertiaTorqueLoss); //.LimitTo(maxDriveTorque, maxRecuperationTorque);
 			}
@@ -43,8 +41,10 @@ namespace TUGraz.VectoCore.Models.SimulationComponent.Impl {
                 DataBus.DriverInfo.DrivingAction == DrivingAction.Roll) {
                 return null;
             }
-
-            if (DataBus.VehicleInfo.VehicleSpeed.IsSmallerOrEqual(GearboxModelData.DisengageWhenHaltingSpeed) && outTorque.IsSmaller(0)) {
+			
+            if (GearboxModelData != null 
+				&& DataBus.VehicleInfo.VehicleSpeed.IsSmallerOrEqual(GearboxModelData.DisengageWhenHaltingSpeed) 
+				&& outTorque.IsSmaller(0)) {
                 return null;
             }
 
@@ -52,7 +52,7 @@ namespace TUGraz.VectoCore.Models.SimulationComponent.Impl {
 				return null;
 			}
 
-			return (-outTorque).LimitTo(maxDriveTorque,  maxRecuperationTorque ?? VectoMath.Max(maxDriveTorque, 0.SI<NewtonMeter>()));
+			return (-outTorque).LimitTo(maxDriveTorque, maxRecuperationTorque ?? VectoMath.Max(maxDriveTorque, 0.SI<NewtonMeter>()));
 		}
 
 		#endregion
-- 
GitLab