SensorBikeController.cs 3.8 KB

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