Code development platform for open source projects from the European Union institutions :large_blue_circle: EU Login authentication by SMS has been phased out. To see alternatives please check here

Skip to content
Snippets Groups Projects
Select Git revision
  • 5d1ec14e13ef51e77dce1afe7a18d8c36b949fe1
  • stable default
  • feat-fchv-bus
  • fix-h2-ice-bus
  • powertrains-multiple-axles
  • amdm3/develop
  • issue-1039
  • amdm3/main
  • test/nuget_publish
  • IEPC-experiments
  • amdm2/main
  • amdm2/develop
  • aptngearbox-not-auto
  • playground
  • official/main
  • official/develop
  • issue-templates
  • pdf-reports
  • HEV-timeruns-dev
  • timerun-empower-hybrids
  • timerun-pwheel-hybrids
  • Release/v5.0.3
  • Release/v5.0.1
  • Release/5.0.0-RC
  • Nuget/v0.11.4-DEV
  • Release/v0.11.4-DEV
  • Release/4.3.4-DEV
  • Release/4.3.3
  • Release/4.3.2-RC
  • Release/v4.3.0-DEV
  • Release/4.2.7
  • XMLConverterTool/4.2.6.0
  • Release/4.2.6-RC
  • Release/v4.2.5
  • Release/v4.2.3
  • Release/v4.2.2.3539-RC
  • Release/v4.2.1.3469
  • Release/v0.11.2.3456-DEV
  • Release/v4.2.0.3448-RC
  • Release/v4.1.3.3415
  • Release/v4.1.1.3413
41 results

PowertrainDrivingCycle.cs

Blame
  • Forked from VECTO / VECTO Sim
    10762 commits behind the upstream repository.
    user avatar
    Michael KRISPER authored
    5d1ec14e
    History
    Code owners
    Assign users and groups as approvers for specific file changes. Learn more.
    PowertrainDrivingCycle.cs 6.57 KiB
    /*
    * Copyright 2015 European Union
    *
    * Licensed under the EUPL (the "Licence");
    * You may not use this work except in compliance with the Licence.
    * You may obtain a copy of the Licence at:
    *
    * http://ec.europa.eu/idabc/eupl5
    *
    * Unless required by applicable law or agreed to in writing, software 
    * distributed under the Licence is distributed on an "AS IS" basis,
    * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    * See the Licence for the specific language governing permissions and 
    * limitations under the Licence.
    */
    
    using System.Collections.Generic;
    using System.Linq;
    using TUGraz.VectoCore.Exceptions;
    using TUGraz.VectoCore.Models.Connector.Ports;
    using TUGraz.VectoCore.Models.Connector.Ports.Impl;
    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.OutputData;
    using TUGraz.VectoCore.Utils;
    
    namespace TUGraz.VectoCore.Models.SimulationComponent.Impl
    {
    	/// <summary>
    	/// Represents a driving cycle which directly is connected to the powertrain (e.g. engine, or axle gear).
    	/// </summary>
    	public class PowertrainDrivingCycle : VectoSimulationComponent, IPowertrainSimulation, ITnInPort,
    		ISimulationOutPort
    	{
    		protected DrivingCycleData Data;
    		protected ITnOutPort NextComponent;
    		protected IEnumerator<DrivingCycleData.DrivingCycleEntry> RightSample { get; set; }
    		protected IEnumerator<DrivingCycleData.DrivingCycleEntry> LeftSample { get; set; }
    
    		protected Second AbsTime { get; set; }
    
    		/// <summary>
    		/// Initializes a new instance of the <see cref="PowertrainDrivingCycle"/> class.
    		/// </summary>
    		/// <param name="container">The container.</param>
    		/// <param name="cycle">The cycle.</param>
    		public PowertrainDrivingCycle(IVehicleContainer container, DrivingCycleData cycle) : base(container)
    		{
    			Data = cycle;
    			LeftSample = Data.Entries.GetEnumerator();
    			LeftSample.MoveNext();
    
    			RightSample = Data.Entries.GetEnumerator();
    			RightSample.MoveNext();
    			RightSample.MoveNext();
    		}
    
    		#region ITnInProvider
    
    		public ITnInPort InPort()
    		{
    			return this;
    		}
    
    		#endregion
    
    		#region ISimulationOutProvider
    
    		public ISimulationOutPort OutPort()
    		{
    			return this;
    		}
    
    		#endregion
    
    		#region ISimulationOutPort
    
    		public IResponse Request(Second absTime, Meter ds)
    		{
    			throw new VectoSimulationException("Powertrain Only Simulation can not handle distance request.");
    		}
    
    		public virtual IResponse Request(Second absTime, Second dt)
    		{
    			// cycle finished (no more entries in cycle)
    			if (LeftSample.Current == null) {
    				return new ResponseCycleFinished { Source = this };
    			}
    
    			// interval exceeded
    			if (RightSample.Current != null && (absTime + dt).IsGreater(RightSample.Current.Time)) {
    				return new ResponseFailTimeInterval {
    					AbsTime = absTime,
    					Source = this,
    					DeltaT = RightSample.Current.Time - absTime
    				};
    			}
    
    			var response = NextComponent.Request(absTime, dt, LeftSample.Current.Torque,
    				DataBus.ClutchClosed(absTime) ? LeftSample.Current.AngularVelocity : 0.SI<PerSecond>());
    
    			AbsTime = absTime + dt;
    
    			response.Switch()
    				.Case<ResponseUnderload>(r => {
    					Log.Warn("PowertrainDrivingCycle got an underload response. Using PDrag.");
    					response = new ResponseSuccess();
    				})
    				.Case<ResponseOverload>(r => {
    					Log.Warn("PowertrainDrivingCycle got an underload response. Using PFullLoad.");
    					response = new ResponseSuccess();
    				})
    				.Case<ResponseSuccess>(() => { })
    				.Default(
    					r => { throw new UnexpectedResponseException("PowertrainDrivingCycle received an unexpected response.", r); });
    
    			return response;
    		}
    
    		public IResponse Initialize()
    		{
    			var first = Data.Entries.First();
    
    			AbsTime = first.Time;
    			var response = NextComponent.Initialize(first.Torque, first.AngularVelocity);
    			response.AbsTime = AbsTime;
    			return response;
    		}
    
    		public string CycleName
    		{
    			get { return Data.Name; }
    		}
    
    		public double Progress
    		{
    			get { return AbsTime.Value() / Data.Entries.Last().Time.Value(); }
    		}
    
    		#endregion
    
    		#region ITnInPort
    
    		void ITnInPort.Connect(ITnOutPort other)
    		{
    			NextComponent = other;
    		}
    
    		#endregion
    
    		#region VectoSimulationComponent
    
    		protected override void DoWriteModalResults(IModalDataContainer container) {}
    
    		protected override void DoCommitSimulationStep()
    		{
    			if ((RightSample.Current == null) || AbsTime.IsGreaterOrEqual(RightSample.Current.Time)) {
    				RightSample.MoveNext();
    				LeftSample.MoveNext();
    			}
    		}
    
    		#endregion
    
    		public CycleData CycleData
    		{
    			get
    			{
    				return new CycleData {
    					AbsTime = LeftSample.Current.Time,
    					AbsDistance = null,
    					LeftSample = LeftSample.Current,
    					RightSample = RightSample.Current,
    				};
    			}
    		}
    	}
    
    	/// <summary>
    	/// Driving Cycle for the PWheel driving cycle.
    	/// </summary>
    	public class PWheelCycle : PowertrainDrivingCycle, IDriverInfo, IClutchInfo
    	{
    		public Gearbox Gearbox { get; set; }
    
    		/// <summary>
    		/// Initializes a new instance of the <see cref="PWheelCycle"/> class.
    		/// </summary>
    		/// <param name="container">The container.</param>
    		/// <param name="cycle">The cycle.</param>
    		/// <param name="axleRatio">The axle ratio.</param>
    		/// <param name="gearbox"></param>
    		public PWheelCycle(IVehicleContainer container, DrivingCycleData cycle, double axleRatio, Gearbox gearbox)
    			: base(container, cycle)
    		{
    			Gearbox = gearbox;
    
    			foreach (var entry in Data.Entries) {
    				entry.AngularVelocity = entry.AngularVelocity /
    										(axleRatio * (entry.Gear == 0 ? 1 : Gearbox.Data.Gears[entry.Gear].Ratio));
    				entry.Torque = entry.PWheel / entry.AngularVelocity;
    			}
    		}
    
    		public override IResponse Request(Second absTime, Second dt)
    		{
    			if (RightSample.Current == null) {
    				return new ResponseCycleFinished { Source = this };
    			}
    
    			Gearbox.Gear = LeftSample.Current.Gear;
    			Gearbox.Disengaged = LeftSample.Current.Gear == 0;
    			return base.Request(absTime, dt);
    		}
    
    
    		protected override void DoWriteModalResults(IModalDataContainer container)
    		{
    			container[ModalResultField.Pwheel] = LeftSample.Current.PWheel;
    			base.DoWriteModalResults(container);
    		}
    
    		#region IDriverInfo
    
    		/// <summary>
    		/// True if the angularVelocity at the wheels is 0.
    		/// </summary>
    		public bool VehicleStopped
    		{
    			get { return LeftSample.Current.PWheel.Abs().IsEqual(0); }
    		}
    
    		/// <summary>
    		/// Always Driving.
    		/// </summary>
    		public DrivingBehavior DrivingBehavior
    		{
    			get { return DrivingBehavior.Driving; }
    		}
    
    		#endregion
    
    		public bool ClutchClosed(Second absTime)
    		{
    			return LeftSample.Current.Gear != 0;
    		}
    	}
    }