|
| 1 | +// COPYRIGHT 2025 by the Open Rails project. |
| 2 | +// |
| 3 | +// This file is part of Open Rails. |
| 4 | +// |
| 5 | +// Open Rails is free software: you can redistribute it and/or modify |
| 6 | +// it under the terms of the GNU General Public License as published by |
| 7 | +// the Free Software Foundation, either version 3 of the License, or |
| 8 | +// (at your option) any later version. |
| 9 | +// |
| 10 | +// Open Rails is distributed in the hope that it will be useful, |
| 11 | +// but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 | +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 13 | +// GNU General Public License for more details. |
| 14 | +// |
| 15 | +// You should have received a copy of the GNU General Public License |
| 16 | +// along with Open Rails. If not, see <http://www.gnu.org/licenses/>. |
| 17 | + |
| 18 | +using System; |
| 19 | + |
| 20 | +namespace ORTS.Common |
| 21 | +{ |
| 22 | + /// <summary> |
| 23 | + /// Modified PID controller that converts requested force/acceleration to throttle or brake percent |
| 24 | + /// </summary> |
| 25 | + public class AccelerationController |
| 26 | + { |
| 27 | + public float Percent { get; private set; } |
| 28 | + public float PPercent { get; private set; } |
| 29 | + public float IPercent { get; private set; } |
| 30 | + public float DPercent { get; private set; } |
| 31 | + private float TotalError; |
| 32 | + private float LastTarget; |
| 33 | + private float LastError; |
| 34 | + private bool active; |
| 35 | + private readonly float[] Coefficients; |
| 36 | + private float ProportionalFactor; |
| 37 | + private float IntegralFactor; |
| 38 | + private float DerivativeFactor; |
| 39 | + public float Tolerance; |
| 40 | + public bool Active |
| 41 | + { |
| 42 | + set |
| 43 | + { |
| 44 | + if (active != value) |
| 45 | + { |
| 46 | + LastTarget = 0; |
| 47 | + LastError = 0; |
| 48 | + TotalError = 0; |
| 49 | + Percent = 0; |
| 50 | + active = value; |
| 51 | + } |
| 52 | + } |
| 53 | + get |
| 54 | + { |
| 55 | + return active; |
| 56 | + } |
| 57 | + } |
| 58 | + public AccelerationController(float p, float i, float d = 0) |
| 59 | + { |
| 60 | + Coefficients = new float[] { 100 * p, 100 * i, 100 * d }; |
| 61 | + } |
| 62 | + protected AccelerationController(AccelerationController o) |
| 63 | + { |
| 64 | + Coefficients = o.Coefficients; |
| 65 | + } |
| 66 | + public AccelerationController Clone() |
| 67 | + { |
| 68 | + return new AccelerationController(this); |
| 69 | + } |
| 70 | + /// <summary> |
| 71 | + /// Adjust PID coefficients according to the maximum force/acceleration |
| 72 | + /// </summary> |
| 73 | + /// <param name="maxAccelerationMpSS">Maximum force or acceleration developed with controller at 100%</param> |
| 74 | + public void Adjust(float maxAccelerationMpSS) |
| 75 | + { |
| 76 | + ProportionalFactor = Coefficients[0] / maxAccelerationMpSS; |
| 77 | + IntegralFactor = Coefficients[1] / maxAccelerationMpSS; |
| 78 | + DerivativeFactor = Coefficients[2] / maxAccelerationMpSS; |
| 79 | + } |
| 80 | + public void Update(float elapsedClockSeconds, float targetAccelerationMpSS, float currentAccelerationMpSS, float minPercent = 0, float maxPercent = 100) |
| 81 | + { |
| 82 | + if (!Active) Active = true; |
| 83 | + float error = targetAccelerationMpSS - currentAccelerationMpSS; |
| 84 | + TotalError += (error + LastError) * elapsedClockSeconds / 2; |
| 85 | + PPercent = ProportionalFactor * targetAccelerationMpSS; |
| 86 | + IPercent = IntegralFactor * TotalError; |
| 87 | + DPercent = elapsedClockSeconds > 0 && DerivativeFactor > 0 ? DerivativeFactor * (error - LastError) / elapsedClockSeconds : 0; |
| 88 | + Percent = PPercent + IPercent + DPercent; |
| 89 | + if (Percent <= minPercent) |
| 90 | + { |
| 91 | + if (PPercent > minPercent && IntegralFactor > 0) TotalError = (minPercent - PPercent) / IntegralFactor; |
| 92 | + else if (TotalError < 0) TotalError = 0; |
| 93 | + Percent = minPercent; |
| 94 | + } |
| 95 | + if (Percent >= maxPercent) |
| 96 | + { |
| 97 | + if (PPercent < maxPercent && IntegralFactor > 0) TotalError = (maxPercent - PPercent) / IntegralFactor; |
| 98 | + else if (TotalError > 0) TotalError = 0; |
| 99 | + Percent = maxPercent; |
| 100 | + } |
| 101 | + LastTarget = targetAccelerationMpSS; |
| 102 | + LastError = error; |
| 103 | + } |
| 104 | + } |
| 105 | +} |
0 commit comments