VibrationController.cs 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. using System;
  2. using System.Threading.Tasks;
  3. using Sensors;
  4. using UnityEngine;
  5. namespace SicknessReduction.Haptic
  6. {
  7. public enum VibrationControllerMode
  8. {
  9. Continuous,
  10. AlternatingFixed,
  11. AlternatingCadenceBased
  12. }
  13. [RequireComponent(typeof(DynamicReductionSource))]
  14. public class VibrationController : EspController
  15. {
  16. private const string TOPIC_CYCLE = "Vibration/Control/Cycle";
  17. private const string TOPIC_CADENCE = "Vibration/Control/Cadence";
  18. private const int THRES_CADENCE_CHANGE = 4;
  19. public int minCycle = 100;
  20. public int maxCycle = 178; //vibro motor specs: 11000 ± 3,000rpm = 133.33 - 183.33 Hz
  21. public float reductionValueThreshold = 0.05f;
  22. public VibrationControllerMode mode;
  23. public int fixedCycleRpm;
  24. private int currentCycle;
  25. private bool initialCyclePublished;
  26. private int previousCadence = -1;
  27. private DynamicReductionSource reductionSource;
  28. protected override void Start()
  29. {
  30. base.Start();
  31. reductionSource = GetComponent<DynamicReductionSource>();
  32. }
  33. protected override async void Update()
  34. {
  35. base.Update();
  36. // > 0 while cornering
  37. var reductionValue = reductionSource.CurrentValue;
  38. var cycleValue = reductionValue < reductionValueThreshold
  39. ? 0
  40. : (int) Mathf.Lerp(minCycle, maxCycle, reductionValue);
  41. if (!DoUpdate || PreviousUpdateActive) return;
  42. if (reductionValue > 0)
  43. switch (mode)
  44. {
  45. case VibrationControllerMode.Continuous:
  46. await VibrateContinuous(cycleValue);
  47. break;
  48. case VibrationControllerMode.AlternatingFixed:
  49. await VibrateAlternatingFixed(cycleValue);
  50. break;
  51. case VibrationControllerMode.AlternatingCadenceBased:
  52. await VibrateAlternatingCadenceBased();
  53. break;
  54. default:
  55. throw new ArgumentOutOfRangeException();
  56. }
  57. else
  58. await StopVibrating();
  59. PreviousUpdateActive = false;
  60. }
  61. private async Task StopVibrating()
  62. {
  63. await Broker.Publish(TOPIC_CYCLE, "0");
  64. currentCycle = 0;
  65. }
  66. private async Task VibrateAlternatingCadenceBased()
  67. {
  68. var cadence = BikeSensorData.Instance.PowermeterData?.InstantaneousCadence;
  69. if (!cadence.HasValue) return;
  70. var c = cadence.Value;
  71. PreviousUpdateActive = true; //flag to avoid concurrent updates
  72. //as soon as we have a cadence, we want the motors to vibrate
  73. if (!initialCyclePublished)
  74. {
  75. await Broker.Publish(TOPIC_CYCLE, $"{maxCycle}");
  76. initialCyclePublished = true;
  77. }
  78. //if the cadence changes to 0, we have to switch off vibration
  79. if (c == 0)
  80. {
  81. await Broker.Publish(TOPIC_CYCLE, "0");
  82. previousCadence = c;
  83. }
  84. //as soon as we have cadence again, we want to switch on vibration again, and then immediately set cadence again
  85. else if (previousCadence == 0)
  86. {
  87. await Broker.Publish(TOPIC_CYCLE, $"{maxCycle}");
  88. await PublishCadence(c);
  89. }
  90. //if we have never set cadence, or the change of cadence is high enough, we tell the ESP to change cadence
  91. else if (previousCadence < 0 || c - previousCadence > THRES_CADENCE_CHANGE)
  92. {
  93. await PublishCadence(c);
  94. }
  95. }
  96. private async Task VibrateAlternatingFixed(int cycleValue)
  97. {
  98. if (Math.Abs(cycleValue - currentCycle) > 0)
  99. {
  100. await Broker.Publish(TOPIC_CYCLE, $"{cycleValue}");
  101. await PublishCadence(fixedCycleRpm);
  102. }
  103. }
  104. private async Task VibrateContinuous(int cycleValue)
  105. {
  106. if (Math.Abs(cycleValue - currentCycle) > 0)
  107. //Debug.Log($"Sending Cycle {cycleValue}");
  108. await Broker.Publish(TOPIC_CYCLE, $"{cycleValue}");
  109. }
  110. private async Task PublishCadence(int cadence)
  111. {
  112. //Debug.Log($"Sending Cadence {cadence}");
  113. await Broker.Publish(TOPIC_CADENCE, $"{cadence}");
  114. previousCadence = cadence;
  115. }
  116. }
  117. }