SensorBikeController.cs 3.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  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. [RequireComponent(typeof(IBicycleController))]
  18. public class SensorBikeController : MonoBehaviour
  19. {
  20. public enum SteeringMode { frontWheel, Leaning, HMD };
  21. public PolarRotationMapping polarRotationMapping;
  22. public FrontWheelTrackerConfig frontWheelTrackerConfig;
  23. private float leanFactor;
  24. public bool steer = true;
  25. public bool accelerate = true;
  26. public bool lean = true;
  27. public SteeringMode steeringSelection;
  28. private IBicycleController bicycleController;
  29. private bool isFrontWheelTrackerNotNull;
  30. private BikeSensorData sensorData;
  31. private GameObject player;
  32. private void Start()
  33. {
  34. isFrontWheelTrackerNotNull = frontWheelTrackerConfig.frontWheelTracker != null;
  35. bicycleController = GetComponent<IBicycleController>();
  36. sensorData = BikeSensorData.Instance;
  37. player = GameObject.FindGameObjectWithTag("HMDTracker");
  38. // Dummy assignment
  39. steeringSelection = SteeringMode.frontWheel;
  40. leanFactor = 90f / (polarRotationMapping.maxRight - polarRotationMapping.center);
  41. }
  42. private void Update()
  43. {
  44. Debug.Log("Bike Sensor Controller called");
  45. var speedData = sensorData.SpeedData;
  46. if (speedData != null && accelerate) SetSpeed(speedData.Value);
  47. if (steer) SetSteer();
  48. if (lean) SetLeaningAngle();
  49. }
  50. private void SetSteer()
  51. {
  52. Debug.Log("Updating Steering");
  53. switch (steeringSelection) {
  54. case SteeringMode.frontWheel:
  55. if (isFrontWheelTrackerNotNull) {
  56. bicycleController.CurrentSteerAngle = frontWheelTrackerConfig.AdjustedRotation * 2f;
  57. }
  58. break;
  59. case SteeringMode.Leaning:
  60. var polarData = sensorData.BleData;
  61. if (polarData != null)
  62. {
  63. bicycleController.CurrentSteerAngle = (polarData.Value.Acc.y - polarRotationMapping.center) * leanFactor;
  64. }
  65. break;
  66. case SteeringMode.HMD:
  67. // TODO: Convert steering relative to bike
  68. Debug.Log("HMD Rotation " + player.transform.eulerAngles);
  69. var rot = player.transform.rotation;
  70. var yaw = Mathf.Asin(2 * rot.x * rot.y + 2 * rot.z * rot.w);
  71. bicycleController.CurrentSteerAngle = yaw;
  72. break;
  73. }
  74. Debug.Log("Updating Steering Angle to " + bicycleController.CurrentSteerAngle);
  75. }
  76. private void SetLeaningAngle()
  77. {
  78. //bicycleController.CurrentLeaningAngle =
  79. }
  80. private void SetSpeed(SpeedSensorData speedData)
  81. {
  82. bicycleController.CurrentSpeed = speedData.Speed;
  83. }
  84. }
  85. }