SensorBikeController.cs 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. using System;
  2. using System.Collections.Generic;
  3. using Controller.Bicycle;
  4. using Controller.Lean;
  5. using Study;
  6. using Sensors;
  7. using Sensors.ANT;
  8. using Sensors.Bluetooth;
  9. using Tracking;
  10. using System.Linq;
  11. using UnityEngine;
  12. // Declare outisde for public visibility
  13. //public enum SteeringMode { frontWheel, Leaning, HMD };
  14. namespace Controller
  15. {
  16. [Serializable]
  17. public struct FrontWheelTrackerConfig
  18. {
  19. public FrontWheelTracker frontWheelTracker;
  20. public float multiplicator;
  21. public float AdjustedRotation => frontWheelTracker.SteerRotation * multiplicator;
  22. }
  23. [Serializable]
  24. public struct HMDTrackerConfig
  25. {
  26. public HMDTracker cameraTracker;
  27. public float multiplicator;
  28. public float steeringOffset;
  29. public float AdjustedRotation => cameraTracker.SteerRotation * multiplicator;
  30. public float AdjustedLean => cameraTracker.LeanRotation * multiplicator;
  31. }
  32. [RequireComponent(typeof(IBicycleController))]
  33. public class SensorBikeController : MonoBehaviour
  34. {
  35. public PolarRotationMapping polarRotationMapping;
  36. public FrontWheelTrackerConfig frontWheelTrackerConfig;
  37. public HMDTrackerConfig hmdTrackerConfig;
  38. private float leanFactor;
  39. public bool steer = true;
  40. public bool accelerate = true;
  41. public bool lean = true;
  42. public Study.SteeringMode steeringSelection;
  43. private IBicycleController bicycleController;
  44. private bool isFrontWheelTrackerNotNull;
  45. private bool isHMDTrackerNotNull;
  46. private BikeSensorData sensorData;
  47. private Queue<float> previousLeanValues;
  48. public int framesToConsider = 10;
  49. private void Start()
  50. {
  51. isFrontWheelTrackerNotNull = frontWheelTrackerConfig.frontWheelTracker != null;
  52. isHMDTrackerNotNull = hmdTrackerConfig.cameraTracker != null;
  53. bicycleController = GetComponent<IBicycleController>();
  54. sensorData = BikeSensorData.Instance;
  55. leanFactor = 90f / (polarRotationMapping.maxRight - polarRotationMapping.center);
  56. if (framesToConsider > 1) previousLeanValues = new Queue<float>(framesToConsider);
  57. }
  58. private void Update()
  59. {
  60. Debug.Log("Bike Sensor Controller called");
  61. var speedData = sensorData.SpeedData;
  62. if (speedData != null && accelerate) SetSpeed(speedData.Value);
  63. if (steer) SetSteer();
  64. if (lean) SetLeaningAngle();
  65. }
  66. public void SetCondition(Study.SteeringMode mode)
  67. {
  68. Debug.Log("Setting Condition");
  69. steeringSelection = mode;
  70. }
  71. private void SetSteer()
  72. {
  73. Debug.Log("Updating Steering");
  74. switch (steeringSelection) {
  75. case Study.SteeringMode.frontWheel:
  76. if (isFrontWheelTrackerNotNull) {
  77. bicycleController.CurrentSteerAngle = frontWheelTrackerConfig.AdjustedRotation * 2f;
  78. }
  79. break;
  80. case Study.SteeringMode.Leaning:
  81. var polarData = sensorData.BleData;
  82. if (polarData != null)
  83. {
  84. Debug.Log("Updating Polar Data");
  85. Debug.Log("Polar Y: " + polarData.Value.Acc);
  86. var polarLean = CalculateLeanRotationMultiFrame(polarData);
  87. Debug.Log("Ploar Post-Processed: " + polarLean);
  88. bicycleController.CurrentLeaningAngle = polarLean;
  89. bicycleController.CurrentSteerAngle = polarLean;
  90. // Activate below in case we also need the steering angle
  91. //bicycleController.CurrentSteerAngle = (polarData.Value.Acc.y - polarRotationMapping.center) * leanFactor;
  92. }
  93. break;
  94. case Study.SteeringMode.HMD:
  95. if (isHMDTrackerNotNull)
  96. {
  97. // We emprirically observed a right-drift
  98. // We subtract a constant to counteract this
  99. bicycleController.CurrentSteerAngle = hmdTrackerConfig.AdjustedRotation;
  100. }
  101. break;
  102. }
  103. Debug.Log("Updating Steering Angle to " + bicycleController.CurrentSteerAngle);
  104. }
  105. private void SetLeaningAngle()
  106. {
  107. //bicycleController.CurrentLeaningAngle =
  108. }
  109. private void SetSpeed(SpeedSensorData speedData)
  110. {
  111. bicycleController.CurrentSpeed = speedData.Speed;
  112. }
  113. // Helpers for Lean Steering
  114. private float CalculateLeanRotationSingleFrame(BleSensorData? polarData)
  115. {
  116. return (-1f) * (polarData.Value.Acc.y - polarRotationMapping.center) * leanFactor;
  117. }
  118. private float CalculateLeanRotationMultiFrame(BleSensorData? polarData)
  119. {
  120. var angleThisFrame = CalculateLeanRotationSingleFrame(polarData);
  121. if (previousLeanValues.Count > framesToConsider) previousLeanValues.Dequeue();
  122. previousLeanValues.Enqueue(angleThisFrame);
  123. //easiest way -> avg; TODO maybe in future, remove outliers
  124. return previousLeanValues.Average();
  125. }
  126. }
  127. }