FanController.cs 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. using System;
  2. using System.Threading.Tasks;
  3. using Controller.Bicycle;
  4. using UnityEngine;
  5. namespace SicknessReduction.Haptic
  6. {
  7. public class FanController : EspController
  8. {
  9. private const int MAX_FAN_CYCLE = 256; //for RES=8
  10. private const float MAX_CYCLE_AT_SPEED = 6.94f; //25km/h
  11. private const int THRES_CYCLE_CHANGE = 6;
  12. private const string TOPIC = "Fan/CHANNEL_NUMBER/Control";
  13. private const string CHANNEL_NUMBER_PLACEHOLDER = "CHANNEL_NUMBER";
  14. public RbBicycleController bicycleController;
  15. [Header("ESP Channels")] public int channelTopLeft = 0;
  16. public int channelBottomLeft = 1;
  17. public int channelTopRight = 2;
  18. public int channelBottomRight = 3;
  19. private Vector2 deviationFromCenter;
  20. private Transform cameraTransform;
  21. private bool cameraTransformAvailable;
  22. private Vector4 previousFanCycles;
  23. private string channelTL;
  24. private string channelBL;
  25. private string channelTR;
  26. private string channelBR;
  27. protected override void Start()
  28. {
  29. base.Start();
  30. if (Camera.main != null)
  31. {
  32. cameraTransform = Camera.main.transform;
  33. cameraTransformAvailable = true;
  34. }
  35. channelTL = TOPIC.Replace(CHANNEL_NUMBER_PLACEHOLDER, channelTopLeft.ToString());
  36. channelBL = TOPIC.Replace(CHANNEL_NUMBER_PLACEHOLDER, channelBottomLeft.ToString());
  37. channelTR = TOPIC.Replace(CHANNEL_NUMBER_PLACEHOLDER, channelTopRight.ToString());
  38. channelBR = TOPIC.Replace(CHANNEL_NUMBER_PLACEHOLDER, channelBottomRight.ToString());
  39. }
  40. protected override async void Update()
  41. {
  42. base.Update();
  43. if (!DoUpdate || PreviousUpdateActive) return;
  44. PreviousUpdateActive = true;
  45. var cycle = FanCycleForSpeed();
  46. CalculateDeviation();
  47. await SetFanSpeeds(cycle);
  48. PreviousUpdateActive = false;
  49. }
  50. private async Task SetFanSpeeds(int maxCycle)
  51. {
  52. var minCycle = (int) (maxCycle * (1f - Mathf.Abs(deviationFromCenter.x)));
  53. var leftCycle = deviationFromCenter.x >= 0 ? minCycle : maxCycle;
  54. var rightCycle = deviationFromCenter.x <0 ? minCycle: maxCycle;
  55. minCycle = (int) (maxCycle * (1f - Mathf.Abs(deviationFromCenter.y)));
  56. var bottomCycle = deviationFromCenter.y >= 0 ? minCycle : maxCycle;
  57. var topCycle = deviationFromCenter.y < 0 ? minCycle : maxCycle;
  58. var cycleTopLeft = Math.Min(topCycle, leftCycle);
  59. var cycleBottomLeft = Math.Min(bottomCycle, leftCycle);
  60. var cycleTopRight = Math.Min(topCycle, rightCycle);
  61. var cycleBottomRight = Math.Min(bottomCycle, rightCycle);
  62. await SendFanCycles(cycleTopLeft, cycleBottomLeft, cycleTopRight, cycleBottomRight);
  63. }
  64. private async Task SendFanCycles(int tl, int bl, int tr, int br)
  65. {
  66. var currentCycles = new Vector4(tl, bl, tr, br);
  67. if (currentCycles.magnitude <= 0.2f && previousFanCycles.magnitude >0.2f || (currentCycles - previousFanCycles).magnitude > THRES_CYCLE_CHANGE)
  68. {
  69. Debug.Log($"Sending FanCycles (tl = {tl}, bl = {bl}, tr = {tr}, br = {br}");
  70. previousFanCycles = currentCycles;
  71. await Broker.Publish(channelTL, tl.ToString());
  72. await Broker.Publish(channelBL, bl.ToString());
  73. await Broker.Publish(channelTR, tr.ToString());
  74. await Broker.Publish(channelBR, br.ToString());
  75. }
  76. }
  77. private void CalculateDeviation()
  78. {
  79. if (cameraTransformAvailable)
  80. {
  81. var hmdForward = cameraTransform.forward;
  82. var bikeForward = bicycleController.Forward;
  83. //Debug.DrawLine(cameraTransform.position, cameraTransform.position + hmdForward * 5, Color.blue);
  84. var hmdOnForwardPlane = Vector3.ProjectOnPlane(hmdForward, bicycleController.Up);
  85. //Debug.DrawLine(cameraTransform.position, cameraTransform.position + hmdOnForwardPlane * 4, Color.magenta);
  86. var hmdOnUpPlane = Vector3.ProjectOnPlane(hmdForward, bicycleController.Right);
  87. //Debug.DrawLine(cameraTransform.position, cameraTransform.position + hmdOnUpPlane * 8, Color.green);
  88. // Debug.DrawLine(cameraTransform.position, cameraTransform.position + bicycleController.Forward * 10, Color.white);
  89. var devLeftRight = Mathf.Clamp(Vector3.Angle(bikeForward, hmdOnForwardPlane), -90f, 90f);
  90. var devUpDown = Mathf.Clamp(Vector3.Angle(bikeForward, hmdOnUpPlane), -90f, 90f);
  91. deviationFromCenter = new Vector2(devLeftRight, devUpDown)/90f;
  92. }
  93. else
  94. {
  95. deviationFromCenter = Vector2.zero;
  96. }
  97. }
  98. private int FanCycleForSpeed()
  99. {
  100. return (int) Mathf.Lerp(0f, MAX_FAN_CYCLE, bicycleController.CurrentSpeed/MAX_CYCLE_AT_SPEED);
  101. }
  102. }
  103. }