Compare commits

...

2 Commits

Author SHA1 Message Date
Marc Riera 09fa50602a Devices: Add ATC Dimetronic 2024-11-24 22:10:10 +01:00
Marc Riera 927ebd567c ATC Bombardier: check track state 2024-11-24 22:09:28 +01:00
5 changed files with 453 additions and 3 deletions

View File

@ -45,6 +45,18 @@ namespace OpenbveFcmbTrainPlugin
/// <summary>The current state of the ATC control.</summary> /// <summary>The current state of the ATC control.</summary>
private AtcControlStates AtcControlState; private AtcControlStates AtcControlState;
/// <summary>Represents the state of the track.</summary>
private enum TrackStates
{
Unprotected,
Enable,
ATC,
Disable
}
/// <summary>The current state of the track.</summary>
private TrackStates TrackState;
/// <summary>The time needed by the ATC device to initialize, in seconds.</summary> /// <summary>The time needed by the ATC device to initialize, in seconds.</summary>
private double InitializationTime; private double InitializationTime;
@ -360,11 +372,30 @@ namespace OpenbveFcmbTrainPlugin
} }
} }
/// <summary>Is called to inform about signals.</summary>
/// <param name="signal">The signal data.</param>
internal override void SetSignal(SignalData[] signal)
{
}
/// <summary>Is called when a beacon is passed.</summary> /// <summary>Is called when a beacon is passed.</summary>
/// <param name="beacon">The beacon data.</param> /// <param name="beacon">The beacon data.</param>
internal override void SetBeacon(BeaconData beacon) internal override void SetBeacon(BeaconData beacon)
{ {
int type = beacon.Type;
switch (type)
{
case -16777215:
{
if (!(beacon.Optional >= 0 & beacon.Optional <= 3))
{
break;
}
TrackState = (TrackStates)beacon.Optional;
return;
}
}
} }
/// <summary>Is called when the device should perform the AI.</summary> /// <summary>Is called when the device should perform the AI.</summary>

View File

@ -0,0 +1,410 @@
using OpenBveApi.Runtime;
namespace OpenbveFcmbTrainPlugin
{
/// <summary>A device simulating ATC by Dimetronic.</summary>
internal class AtcDimetronic : Device
{
/// <summary>Represents the state of the device.</summary>
private enum DeviceStates
{
/// <summary>The device is being overriden.</summary>
Override,
/// <summary>The device has been initialized and no driving mode is selected.</summary>
NoMode,
/// <summary>The device is in YARD (M+25) driving mode.</summary>
YARD,
/// <summary>The device is in ATP (M+ATP) driving mode.</summary>
ATP,
/// <summary>The device is in ATO mode.</summary>
ATO,
}
/// <summary>The current state of the device.</summary>
private DeviceStates DeviceState;
/// <summary>Represents the state of the ATC control.</summary>
private enum AtcControlStates
{
/// <summary>The brakes are released.</summary>
Released,
/// <summary>The power is cut.</summary>
NoPower,
/// <summary>The emergency brake is applied.</summary>
BrakeEmergency,
}
/// <summary>The current state of the ATC control.</summary>
private AtcControlStates AtcControlState;
/// <summary>Represents the state of the track.</summary>
private enum TrackStates
{
Unprotected,
Enable,
ATC,
Disable
}
/// <summary>The current state of the track.</summary>
private TrackStates TrackState;
/// <summary>The maximum speed in YARD mode, in km/h.</summary>
private double YardMaximumSpeed;
/// <summary>Represents an ATC speed code.</summary>
private class SpeedCode
{
internal Speed CurrentLimit { get; private set; }
internal Speed WarningOn { get; private set; }
internal Speed WarningOff { get; private set; }
internal Speed TargetLimit { get; private set; }
internal SpeedCode(Speed limit, Speed target)
{
CurrentLimit = limit;
TargetLimit = target;
if (limit == target)
{
// Constant speed
WarningOn = new Speed(limit.MetersPerSecond - 3 / 3.6);
WarningOff = new Speed(limit.MetersPerSecond - 5 / 3.6);
}
else
{
// Reduce speed
WarningOn = target;
WarningOff = new Speed(target.MetersPerSecond - 2 / 3.6);
}
}
}
/// <summary>The current speed code received by the train.</summary>
private SpeedCode CurrentSpeedCode;
/// <summary>The ideal power notch for the AI.</summary>
private int AiPowerNotch;
/// <summary>The ideal brake notch for the AI.</summary>
private int AiBrakeNotch;
/// <summary>Creates an instance of the Bombardier ATC device.</summary>
/// <param name="yardMaxSpeed">The maximum speed in YARD mode, in km/h.</param>
internal AtcDimetronic(double yardMaxSpeed)
{
YardMaximumSpeed = yardMaxSpeed;
}
/// <summary>Is called when the device state should be updated.</summary>
/// <param name="train">The current train.</param>
/// <param name="route">The current route.</param>
/// <param name="init">Whether the device should initialize.</param>
/// <param name="elapsedTime">The time elapsed since the previous call.</param>
internal override void Update(Train train, Route route, bool init, Time elapsedTime)
{
if (init)
{
// Initialize device on start
DeviceState = DeviceStates.NoMode;
}
// Update ATC movement permission
UpdateSpeedCode();
double speed = train.State.Speed.KilometersPerHour;
bool stopped = speed < 0.05;
// Speed limit enforcement
if (DeviceState == DeviceStates.YARD || DeviceState == DeviceStates.ATP || DeviceState == DeviceStates.ATO)
{
if (AtcControlState == AtcControlStates.Released && speed > CurrentSpeedCode.WarningOn.KilometersPerHour)
{
// Cut power above warning on threshold
AtcControlState = AtcControlStates.NoPower;
}
if (AtcControlState == AtcControlStates.NoPower && speed < CurrentSpeedCode.WarningOff.KilometersPerHour)
{
// Stop cutting power below warning off threshold
AtcControlState = AtcControlStates.Released;
}
if (speed > CurrentSpeedCode.CurrentLimit.KilometersPerHour)
{
// Unselect driving mode to apply emergency brake if overspeeding
DeviceState = DeviceStates.NoMode;
}
}
// Brake application
switch (AtcControlState)
{
case AtcControlStates.Released:
RequestedBrakeNotch = -1;
RequestedPowerNotch = -1;
break;
case AtcControlStates.NoPower:
RequestedBrakeNotch = -1;
RequestedPowerNotch = 0;
break;
case AtcControlStates.BrakeEmergency:
RequestedBrakeNotch = train.Specs.BrakeNotches + 1;
RequestedPowerNotch = 0;
break;
}
switch (DeviceState)
{
// ATC device is disabled (also called "Special Mode")
case DeviceStates.Override:
train.ContinuousProtection = false;
train.VigilanceOverride = false;
// Release control of the brakes and power
AtcControlState = AtcControlStates.Released;
break;
// ATC device is initialized and a driving mode is required
case DeviceStates.NoMode:
train.ContinuousProtection = false;
train.VigilanceOverride = false;
// Apply service/emergency brake while no driving mode is selected
AtcControlState = AtcControlStates.BrakeEmergency;
// ATC movement permission
CurrentSpeedCode = new SpeedCode(new Speed(0), new Speed(0));
break;
// ATC device is in YARD (M+25) driving mode
case DeviceStates.YARD:
train.ContinuousProtection = false;
train.VigilanceOverride = false;
// Apply brake if any door opens
if (train.DoorState != DoorStates.None)
{
RequestedBrakeNotch = stopped ? train.Specs.BrakeNotches + 1 : train.ServiceBrakeNotch;
}
// If the train is not moving, brakes are released
if (stopped)
{
AtcControlState = AtcControlStates.Released;
}
break;
// ATC device is in ATP (M+ATP) driving mode
case DeviceStates.ATP:
train.ContinuousProtection = true;
train.VigilanceOverride = false;
break;
// ATC device is in ATO driving mode
case DeviceStates.ATO:
train.ContinuousProtection = true;
train.VigilanceOverride = true;
break;
}
// Panel indicators
train.Panel[25] = DeviceState == DeviceStates.Override ? 1 : 0;
train.Panel[26] = DeviceState == DeviceStates.YARD ? 1 : 0;
train.Panel[27] = DeviceState == DeviceStates.ATP ? 1 : 0;
train.Panel[34] = (int)CurrentSpeedCode.CurrentLimit.KilometersPerHour * 1000;
}
/// <summary>Is called when the state of a key changes.</summary>
/// <param name="key">The key.</param>
/// <param name="pressed">Whether the key is pressed or released.</param>
/// <param name="train">The current train.</param>
internal override void KeyChange(VirtualKeys key, bool pressed, Train train)
{
double speed = train.State.Speed.KilometersPerHour;
bool stopped = speed < 0.05;
if (pressed)
{
switch (key)
{
case VirtualKeys.I:
// YARD (M+25) mode selection button
if (!OpenbveFcmbTrainPlugin.KeysPressed[(int)key])
{
if (DeviceState == DeviceStates.NoMode && stopped)
{
DeviceState = DeviceStates.YARD;
}
}
break;
case VirtualKeys.J:
// ATP (M+ATP) mode selection button
if (!OpenbveFcmbTrainPlugin.KeysPressed[(int)key])
{
}
break;
case VirtualKeys.K:
// ATO mode selection button
if (!OpenbveFcmbTrainPlugin.KeysPressed[(int)key])
{
}
break;
case VirtualKeys.L:
// ATO start button
if (!OpenbveFcmbTrainPlugin.KeysPressed[(int)key])
{
}
break;
case VirtualKeys.B1:
// ATC alarm acknowledge
if (!OpenbveFcmbTrainPlugin.KeysPressed[(int)key])
{
}
break;
case VirtualKeys.C1:
// Override toggle
if (!OpenbveFcmbTrainPlugin.KeysPressed[(int)key])
{
switch (DeviceState)
{
case DeviceStates.NoMode:
case DeviceStates.YARD:
// Override device if possible
if (stopped && train.PhysicalHandles.PowerNotch == 0)
{
DeviceState = DeviceStates.Override;
}
break;
case DeviceStates.Override:
// Disable override if possible
if (stopped && train.PhysicalHandles.PowerNotch == 0)
{
DeviceState = DeviceStates.NoMode;
}
break;
}
}
break;
}
}
}
/// <summary>Is called to inform about signals.</summary>
/// <param name="signal">The signal data.</param>
internal override void SetSignal(SignalData[] signal)
{
}
/// <summary>Is called when a beacon is passed.</summary>
/// <param name="beacon">The beacon data.</param>
internal override void SetBeacon(BeaconData beacon)
{
int type = beacon.Type;
switch (type)
{
case -16777215:
{
if (!(beacon.Optional >= 0 & beacon.Optional <= 3))
{
break;
}
TrackState = (TrackStates)beacon.Optional;
return;
}
}
}
/// <summary>Is called when the device should perform the AI.</summary>
/// <param name="data">The AI data.</param>
/// <param name="train">The current train.</param>
/// <param name="route">The current route.</param>
internal override void PerformAI(AIData data, Train train, Route route)
{
// Set AI notches to what the AI is doing right now
AiBrakeNotch = data.Handles.BrakeNotch;
AiPowerNotch = data.Handles.PowerNotch;
double speed = train.State.Speed.KilometersPerHour;
bool stopped = speed < 0.05;
switch (DeviceState)
{
case DeviceStates.NoMode:
// Apply brake
data.Handles.BrakeNotch = train.Specs.BrakeNotches;
data.Response = AIResponse.Short;
// If the train is stopped, select YARD (M+25) mode
if (stopped)
{
KeyChange(VirtualKeys.I, true, train);
data.Response = AIResponse.Short;
KeyChange(VirtualKeys.I, false, train);
}
break;
case DeviceStates.YARD:
case DeviceStates.ATP:
// Calculate ideal notch for AI when approaching the speed limit
if (speed > CurrentSpeedCode.WarningOn.KilometersPerHour - 2 && train.Acceleration > 0.1)
{
if (data.Handles.PowerNotch > 0)
{
AiPowerNotch -= 1;
}
else if (train.Specs.HasHoldBrake)
{
data.Handles.HoldBrake = true;
}
else
{
AiBrakeNotch += 1;
}
}
else if (speed < CurrentSpeedCode.WarningOff.KilometersPerHour - 2 && train.Acceleration < 0.25)
{
if (data.Handles.BrakeNotch > 0)
{
AiBrakeNotch -= 1;
}
else
{
AiPowerNotch += 1;
}
}
// If still far from next station, take control of AI handles
if (speed / (route.CurrentStation.StopPosition - train.State.Location) < 0.2)
{
if (speed > CurrentSpeedCode.CurrentLimit.KilometersPerHour - 5)
{
data.Handles.PowerNotch = AiPowerNotch;
data.Handles.BrakeNotch = AiBrakeNotch;
data.Response = AIResponse.Medium;
}
}
break;
}
}
/// <summary>Updates the data for the current speed code.</summary>
internal void UpdateSpeedCode()
{
Speed zero = new Speed(0);
Speed yard = new Speed(YardMaximumSpeed / 3.6);
switch (DeviceState)
{
case DeviceStates.Override:
case DeviceStates.NoMode:
if (CurrentSpeedCode == null || CurrentSpeedCode.CurrentLimit != zero || CurrentSpeedCode.TargetLimit != zero)
{
CurrentSpeedCode = new SpeedCode(zero, zero);
}
break;
case DeviceStates.YARD:
if (CurrentSpeedCode == null || CurrentSpeedCode.CurrentLimit != yard || CurrentSpeedCode.TargetLimit != yard)
{
CurrentSpeedCode = new SpeedCode(yard, yard);
}
break;
case DeviceStates.ATP:
case DeviceStates.ATO:
if (CurrentSpeedCode == null || CurrentSpeedCode.CurrentLimit != zero || CurrentSpeedCode.TargetLimit != zero)
{
CurrentSpeedCode = new SpeedCode(zero, zero);
}
break;
}
}
}
}

View File

@ -118,6 +118,7 @@ namespace OpenbveFcmbTrainPlugin
/// <remarks>The signal array is guaranteed to have at least one element. When accessing elements other than index 0, you must check the bounds of the array first.</remarks> /// <remarks>The signal array is guaranteed to have at least one element. When accessing elements other than index 0, you must check the bounds of the array first.</remarks>
public void SetSignal(SignalData[] signal) public void SetSignal(SignalData[] signal)
{ {
Train.SetSignal(signal);
} }
/// <summary>Is called when the train passes a beacon.</summary> /// <summary>Is called when the train passes a beacon.</summary>

View File

@ -45,6 +45,7 @@
<Compile Include="Devices\AtcBombardier.cs" /> <Compile Include="Devices\AtcBombardier.cs" />
<Compile Include="Managers\SoundManager.cs" /> <Compile Include="Managers\SoundManager.cs" />
<Compile Include="Managers\MessageManager.cs" /> <Compile Include="Managers\MessageManager.cs" />
<Compile Include="Devices\AtcDimetronic.cs" />
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>
<Folder Include="Devices\" /> <Folder Include="Devices\" />

View File

@ -69,7 +69,8 @@ namespace OpenbveFcmbTrainPlugin
Devices.Add(new Doors(false, 5, 15, 10)); Devices.Add(new Doors(false, 5, 15, 10));
Devices.Add(new Deadman(5.0, false)); Devices.Add(new Deadman(5.0, false));
Devices.Add(new TrainStop()); Devices.Add(new TrainStop());
Devices.Add(new AtcBombardier(30, 60, 20, 500)); //Devices.Add(new AtcBombardier(30, 60, 20, 500));
Devices.Add(new AtcDimetronic(25));
} }
/// <summary>Is called when the train should initialize.</summary> /// <summary>Is called when the train should initialize.</summary>
@ -183,7 +184,13 @@ namespace OpenbveFcmbTrainPlugin
/// <summary>Is called to inform about signals.</summary> /// <summary>Is called to inform about signals.</summary>
/// <param name="signal">The signal data.</param> /// <param name="signal">The signal data.</param>
internal void SetSignal(SignalData[] signal) { } internal void SetSignal(SignalData[] signal)
{
foreach (Device dev in Devices)
{
dev.SetSignal(signal);
}
}
/// <summary>Is called when a beacon is passed.</summary> /// <summary>Is called when a beacon is passed.</summary>
/// <param name="beacon">The beacon data.</param> /// <param name="beacon">The beacon data.</param>