From b1e3d72ef05438bc99a8fe74543c176a6d319299 Mon Sep 17 00:00:00 2001
From: Markus Quaritsch <markus.quaritsch@tugraz.at>
Date: Thu, 3 Sep 2015 15:00:55 +0200
Subject: [PATCH] started with driver strategy, driver mode drive

---
 .../Impl/DefaultDriverStrategy.cs             | 63 ++++++++++++++++---
 .../SimulationComponent/Impl/Gearbox.cs       |  4 ++
 2 files changed, 60 insertions(+), 7 deletions(-)

diff --git a/VectoCore/Models/SimulationComponent/Impl/DefaultDriverStrategy.cs b/VectoCore/Models/SimulationComponent/Impl/DefaultDriverStrategy.cs
index 22ed6603f6..ae94a14b43 100644
--- a/VectoCore/Models/SimulationComponent/Impl/DefaultDriverStrategy.cs
+++ b/VectoCore/Models/SimulationComponent/Impl/DefaultDriverStrategy.cs
@@ -2,7 +2,10 @@
 using System.Collections.Generic;
 using System.Diagnostics;
 using System.Linq;
+using System.Runtime.InteropServices.WindowsRuntime;
+using TUGraz.VectoCore.Exceptions;
 using TUGraz.VectoCore.Models.Connector.Ports;
+using TUGraz.VectoCore.Models.Connector.Ports.Impl;
 using TUGraz.VectoCore.Models.Declaration;
 using TUGraz.VectoCore.Utils;
 
@@ -18,13 +21,10 @@ namespace TUGraz.VectoCore.Models.SimulationComponent.Impl
 
 		public enum DrivingBehavior
 		{
-			//Stopped,
 			Accelerating,
 			Drive,
 			Coasting,
 			Braking,
-			//EcoRoll,
-			//OverSpeed,
 		}
 
 		protected DrivingMode CurrentDrivingMode;
@@ -39,18 +39,44 @@ namespace TUGraz.VectoCore.Models.SimulationComponent.Impl
 
 		public IDriverActions Driver { get; set; }
 
+		protected Meter BrakeTriggerDistance { get; set; }
+
+
 		public IResponse Request(Second absTime, Meter ds, MeterPerSecond targetVelocity, Radian gradient)
 		{
-			throw new NotImplementedException();
+			switch (CurrentDrivingMode) {
+				case DrivingMode.DrivingModeDrive:
+					var currentDistance = Driver.DataBus.Distance();
+					var nextAction = GetNextDrivingAction(currentDistance);
+					if (currentDistance.IsEqual(nextAction.ActionDistance)) {
+						CurrentDrivingMode = DrivingMode.DrivingModeBrake;
+						BrakeTriggerDistance = nextAction.TriggerDistance;
+						break;
+					}
+					if (currentDistance + ds > nextAction.ActionDistance) {
+						return new ResponseDrivingCycleDistanceExceeded() {
+							MaxDistance = nextAction.ActionDistance - currentDistance
+						};
+					}
+					break;
+				case DrivingMode.DrivingModeBrake:
+					if (Driver.DataBus.Distance() >= BrakeTriggerDistance) {
+						CurrentDrivingMode = DrivingMode.DrivingModeDrive;
+					}
+					break;
+			}
+
+			return DrivingModes[CurrentDrivingMode].Request(absTime, ds, targetVelocity, gradient);
 		}
 
+
 		public IResponse Request(Second absTime, Second dt, MeterPerSecond targetVelocity, Radian gradient)
 		{
 			return Driver.DrivingActionHalt(absTime, dt, targetVelocity, gradient);
 		}
 
 
-		protected IList<DrivingBehaviorEntry> GetNextDrivingActions(Meter minDistance)
+		protected DrivingBehaviorEntry GetNextDrivingAction(Meter minDistance)
 		{
 			var currentSpeed = Driver.DataBus.VehicleSpeed();
 
@@ -97,7 +123,7 @@ namespace TUGraz.VectoCore.Models.SimulationComponent.Impl
 				}
 			}
 
-			return nextActions.OrderBy(x => x.ActionDistance).ToList();
+			return nextActions.OrderBy(x => x.ActionDistance).First();
 		}
 	}
 
@@ -119,7 +145,30 @@ namespace TUGraz.VectoCore.Models.SimulationComponent.Impl
 
 		public IResponse Request(Second absTime, Meter ds, MeterPerSecond targetVelocity, Radian gradient)
 		{
-			throw new System.NotImplementedException();
+			IResponse response = null;
+			if (DriverStrategy.Driver.DataBus.ClutchState() != ClutchState.ClutchOpened) {
+				// drive along
+				response = DriverStrategy.Driver.DrivingActionAccelerate(absTime, ds, targetVelocity, gradient);
+				response.Switch().
+					Case<ResponseGearShift>(() => {
+						response = DriverStrategy.Driver.DrivingActionRoll(absTime, ds, gradient);
+						response.Switch().
+							Case<ResponseOverload>(() => {
+								// overload may happen if driver limits acceleration when rolling downhill
+								response = DriverStrategy.Driver.DrivingActionBrake(absTime, ds, targetVelocity, gradient);
+							});
+					}).
+					Case<ResponseOverload>(() => {
+						response = DriverStrategy.Driver.DrivingActionBrake(absTime, ds, targetVelocity, gradient);
+					});
+			} else {
+				response = DriverStrategy.Driver.DrivingActionRoll(absTime, ds, gradient);
+				response.Switch().
+					Case<ResponseOverload>(() => {
+						response = DriverStrategy.Driver.DrivingActionBrake(absTime, ds, targetVelocity, gradient);
+					});
+			}
+			return response;
 		}
 	}
 
diff --git a/VectoCore/Models/SimulationComponent/Impl/Gearbox.cs b/VectoCore/Models/SimulationComponent/Impl/Gearbox.cs
index 74f2c6e2c3..9ad907db91 100644
--- a/VectoCore/Models/SimulationComponent/Impl/Gearbox.cs
+++ b/VectoCore/Models/SimulationComponent/Impl/Gearbox.cs
@@ -81,6 +81,10 @@ namespace TUGraz.VectoCore.Models.SimulationComponent.Impl
 					return new ResponseDryRun { GearboxPowerRequest = outTorque * outEngineSpeed };
 				}
 
+				if (!outTorque.IsEqual(0)) {
+					// if clutch is open the gearbox can't provide a torque
+					return new ResponseOverload() { Delta = outTorque * outEngineSpeed };
+				}
 				// if shiftTime still not reached (only happens during shifting): apply zero-request
 				if (_shiftTime > absTime) {
 					var duringShiftResponse = Next.Request(absTime, dt, 0.SI<NewtonMeter>(), 0.SI<PerSecond>());
-- 
GitLab