SensorBikeController.cs 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. using System;
  2. using Controller.Bicycle;
  3. using Controller.Lean;
  4. using Sensors;
  5. using Sensors.ANT;
  6. using Tracking;
  7. using UnityEngine;
  8. // Declare outisde for public visibility
  9. public enum SteeringMode { frontWheel, Leaning, HMD };
  10. namespace Controller
  11. {
  12. [Serializable]
  13. public struct FrontWheelTrackerConfig
  14. {
  15. public FrontWheelTracker frontWheelTracker;
  16. public float multiplicator;
  17. public float AdjustedRotation => frontWheelTracker.SteerRotation * multiplicator;
  18. }
  19. [Serializable]
  20. public struct HMDTrackerConfig
  21. {
  22. public HMDTracker cameraTracker;
  23. public float multiplicator;
  24. public float steeringOffset;
  25. public float AdjustedRotation => cameraTracker.SteerRotation * multiplicator;
  26. }
  27. [RequireComponent(typeof(IBicycleController))]
  28. public class SensorBikeController : MonoBehaviour
  29. {
  30. public PolarRotationMapping polarRotationMapping;
  31. public FrontWheelTrackerConfig frontWheelTrackerConfig;
  32. public HMDTrackerConfig hmdTrackerConfig;
  33. private float leanFactor;
  34. public bool steer = true;
  35. public bool accelerate = true;
  36. public bool lean = true;
  37. public SteeringMode steeringSelection;
  38. private IBicycleController bicycleController;
  39. private bool isFrontWheelTrackerNotNull;
  40. private bool isHMDTrackerNotNull;
  41. private BikeSensorData sensorData;
  42. private void Start()
  43. {
  44. isFrontWheelTrackerNotNull = frontWheelTrackerConfig.frontWheelTracker != null;
  45. isHMDTrackerNotNull = hmdTrackerConfig.cameraTracker != null;
  46. bicycleController = GetComponent<IBicycleController>();
  47. sensorData = BikeSensorData.Instance;
  48. leanFactor = 90f / (polarRotationMapping.maxRight - polarRotationMapping.center);
  49. }
  50. private void Update()
  51. {
  52. Debug.Log("Bike Sensor Controller called");
  53. var speedData = sensorData.SpeedData;
  54. if (speedData != null && accelerate) SetSpeed(speedData.Value);
  55. if (steer) SetSteer();
  56. if (lean) SetLeaningAngle();
  57. }
  58. private void SetSteer()
  59. {
  60. Debug.Log("Updating Steering");
  61. switch (steeringSelection) {
  62. case SteeringMode.frontWheel:
  63. if (isFrontWheelTrackerNotNull) {
  64. bicycleController.CurrentSteerAngle = frontWheelTrackerConfig.AdjustedRotation * 2f;
  65. }
  66. break;
  67. case SteeringMode.Leaning:
  68. var polarData = sensorData.BleData;
  69. if (polarData != null)
  70. {
  71. bicycleController.CurrentLeaningAngle = (polarData.Value.Acc.y - polarRotationMapping.center) * leanFactor;
  72. // Activate below in case we also need the steering angle
  73. //bicycleController.CurrentSteerAngle = (polarData.Value.Acc.y - polarRotationMapping.center) * leanFactor;
  74. }
  75. break;
  76. case SteeringMode.HMD:
  77. if (isHMDTrackerNotNull)
  78. {
  79. // We emprirically observed a right-drift
  80. // We subtract a constant to counteract this
  81. bicycleController.CurrentSteerAngle = hmdTrackerConfig.AdjustedRotation - 2f;
  82. }
  83. break;
  84. }
  85. Debug.Log("Updating Steering Angle to " + bicycleController.CurrentSteerAngle);
  86. }
  87. private void SetLeaningAngle()
  88. {
  89. //bicycleController.CurrentLeaningAngle =
  90. }
  91. private void SetSpeed(SpeedSensorData speedData)
  92. {
  93. bicycleController.CurrentSpeed = speedData.Speed;
  94. }
  95. }
  96. }