Skip to content
This repository has been archived by the owner on Jul 4, 2023. It is now read-only.

Commit

Permalink
closes #90 : Provide Kick Start for the Locomotives in AutoMode
Browse files Browse the repository at this point in the history
  • Loading branch information
Christian Benjamin Ries committed Apr 7, 2021
1 parent 83e14f9 commit ae75459
Show file tree
Hide file tree
Showing 4 changed files with 54 additions and 2 deletions.
25 changes: 24 additions & 1 deletion railessentials/AutoMode/AutoModeTaskBase.Accelerate.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ private async Task AccelerateLocomotiveCurve(
if (ecosLoc == null) return;
if (speedCurve == null) return;

KickStart(currentSpeed, ecosLoc);

if (maxSeconds <= -1)
maxSeconds = speedCurve.MaxTime;

Expand Down Expand Up @@ -96,11 +98,13 @@ private async Task AccelerateLocomotive(

await Task.Run(() =>
{
KickStart(currentSpeed, ecosLoc);
var hasCanceled = false;
var newCurrentSpeed = currentSpeed;
var sw = Stopwatch.StartNew();
for (var i = currentSpeed; i <= targetSpeed; ++i)
{
Ctx.GetClientHandler()?.LocomotiveChangeSpeedstep(ecosLoc, i);
Expand Down Expand Up @@ -156,5 +160,24 @@ private bool __delayAccelerate(

return false;
}

private void KickStart(int currentSpeed, Locomotive ecosLoc)
{
var maxSpeedSteps = ecosLoc.GetNumberOfSpeedsteps();
if (currentSpeed > 2) return;
var previousSpeed = currentSpeed;
var kickStartSpeed = previousSpeed;

if (maxSpeedSteps <= 14)
kickStartSpeed = Globals.DccKickStartM14;
else if (maxSpeedSteps <= 28)
kickStartSpeed = Globals.DccKickStartM28;
else if (maxSpeedSteps <= 128)
kickStartSpeed = Globals.DccKickStartM128;

Ctx.GetClientHandler()?.LocomotiveChangeSpeedstep(ecosLoc, kickStartSpeed);
System.Threading.Thread.Sleep(Globals.DccKickStartDelayMsecs);
Ctx.GetClientHandler()?.LocomotiveChangeSpeedstep(ecosLoc, previousSpeed);
}
}
}
7 changes: 6 additions & 1 deletion railessentials/AutoMode/AutoModeTaskBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -201,9 +201,14 @@ public async override Task Run()
var maxSpeed = Route.Locomotive.GetNumberOfSpeedsteps();
var speedCurve = Route.LocomotivesData?.SpeedCurve;

var targetSpeed = (int)(maxSpeed / 3.0);
var targetSpeed = (int)(maxSpeed / 3.0));
if (speedCurve != null)
targetSpeed = speedCurve.MaxSpeed;
else
{
if(Route.LocomotivesData != null)
targetSpeed = Route.LocomotivesData.GetLevel("level").Value;
}

#endregion

Expand Down
5 changes: 5 additions & 0 deletions railessentials/Globals.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,11 @@ public static class Globals
public const string ApplicationName = "RailEssentials";
public const string ApplicationDescription = "RailEssentials is a software for controlling your Model Trains especially when ESU's ECoS 50210/50200 is used.";

public const int DccKickStartM14 = 3;
public const int DccKickStartM28 = 3;
public const int DccKickStartM128 = 5;
public const int DccKickStartDelayMsecs = 100;

public static Dictionary<string, string> GetCfgDataPath()
{
var dict = new Dictionary<string, string>
Expand Down
19 changes: 19 additions & 0 deletions railessentials/Locomotives/Data.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
// Licensed under the MIT License
// File: Data.cs

using System;
using System.Collections.Generic;

namespace railessentials.Locomotives
Expand All @@ -14,5 +15,23 @@ public class Data
public string EnterBlockSide { get; set; }
public SpeedCurve SpeedCurve { get; set; }
public Dictionary<string, SpeedLevel> SpeedLevels { get; set; } = new();

public SpeedLevel GetLevel(string level)
{
var def = new SpeedLevel { Value = 0 };

if (string.IsNullOrEmpty(level))
return def;

if (SpeedLevels == null) return def;

foreach(var it in SpeedLevels)
{
if (it.Key.IndexOf(level, StringComparison.OrdinalIgnoreCase) != -1)
return it.Value;
}

return def;
}
}
}

0 comments on commit ae75459

Please sign in to comment.