Advertisement
Guest User

Untitled

a guest
Sep 27th, 2016
77
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.75 KB | None | 0 0
  1. private class ThrottleCalculator
  2. {
  3. private readonly int _throttle;
  4. private DateTime _lastCalculation = DateTime.Now;
  5. private int _count = 0;
  6. private int _interval = 0;
  7.  
  8. public ThrottleCalculator(int throttle)
  9. {
  10. this._throttle = throttle;
  11. }
  12.  
  13. public async Task CalculateThrottle()
  14. {
  15. this._count += 1;
  16. var elapsed = DateTime.Now.Subtract(this._lastCalculation).TotalMilliseconds;
  17. var tick = 50;
  18. if (elapsed > tick)
  19. {
  20. this._lastCalculation = DateTime.Now;
  21. int projection = this._count * (1000 / tick);
  22. var errorTerm = this._throttle - projection;
  23. this._interval = this._interval - errorTerm;
  24. if (this._interval < 0)
  25. this._interval = 0;
  26.  
  27. // this is often several thousand, so I have to limit.
  28. if (this._interval > 100)
  29. this._interval = 100;
  30. await Task.Delay(this._interval);
  31. this._count = 0;
  32. }
  33. }
  34. }
  35.  
  36. var throttle = new ThrottleCalculator(600); // 600/s
  37. while (message = getMessage())
  38. {
  39. ... // do stuff with message.
  40. if (throttle != null)
  41. await throttle.CalculateThrottle();
  42.  
  43. private class ThrottleCalculator
  44. {
  45. private readonly int _throttle;
  46. private DateTime _lastCalculationTime;
  47. private double _measured = 0;
  48. private double _totalError = 0;
  49. private double _integral = 0;
  50. private double _lastError = 0;
  51.  
  52. public ThrottleCalculator(int throttle)
  53. {
  54. this._throttle = throttle;
  55. this._lastCalculationTime = DateTime.MinValue;
  56. }
  57.  
  58. public async Task CalculateThrottle()
  59. {
  60. var kp = -.1d; // proportional gain
  61. var ki = -.1d; // integral gain
  62. var kd = -.1d; // derivative gain
  63. var dt = 30d; // rate of change of time. calculcations every ms;
  64.  
  65. this._measured += 1;
  66. if (this._lastCalculationTime == DateTime.MinValue)
  67. this._lastCalculationTime = DateTime.Now;
  68. var elapsed = (double)DateTime.Now.Subtract(this._lastCalculationTime)
  69. .TotalMilliseconds;
  70. if (elapsed > dt)
  71. {
  72. this._lastCalculationTime = DateTime.Now;
  73. var error = ((double)this._throttle / (1000d / dt)) - this._measured;
  74. this._totalError += error;
  75. var integral = this._totalError;
  76. var derivative = (error - this._lastError) / elapsed;
  77. var actual = (kp * error) + (ki * integral) + (kd * derivative);
  78. var output = actual;
  79. if (output < 1)
  80. output = 0;
  81.  
  82. // i don't like this, but it seems necessary
  83. // so that wild wait values are never used.
  84. if (output > dt * 4)
  85. output = dt * 4;
  86. if (output > 0)
  87. await Task.Delay((int)output);
  88. this._measured = 0;
  89. this._lastError = error;
  90. }
  91. }
  92. }
  93.  
  94. Actual: 19.2000 Output: 19.2000 Integral: -209 Derivative: .0000 Error: 17
  95. Actual: 17.5000 Output: 17.5000 Integral: -192 Derivative: .0000 Error: 17
  96. Actual: 15.8000 Output: 15.8000 Integral: -175 Derivative: .0000 Error: 17
  97. Actual: 33.8104 Output: 33.8104 Integral: -255 Derivative: -3.1040 Error: -80
  98. Actual: 21.8931 Output: 21.8931 Integral: -238 Derivative: 2.0686 Error: 17
  99. Actual: 20.4000 Output: 20.4000 Integral: -221 Derivative: .0000 Error: 17
  100. Actual: 18.7000 Output: 18.7000 Integral: -204 Derivative: .0000 Error: 17
  101. Actual: 17.0000 Output: 17.0000 Integral: -187 Derivative: .0000 Error: 17
  102. Actual: 15.3000 Output: 15.3000 Integral: -170 Derivative: .0000 Error: 17
  103. Actual: 31.0752 Output: 31.0752 Integral: -239 Derivative: -2.7520 Error: -69
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement