SensorBikeController.cs 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. using System;
  2. using UnityEngine;
  3. using UnityEngine.Serialization;
  4. [Serializable]
  5. public struct PolarRotationMapping
  6. {
  7. public float maxRight;
  8. public float center;
  9. }
  10. [Serializable]
  11. public struct FrontWheelTrackerConfig
  12. {
  13. public FrontWheelTracker frontWheelTracker;
  14. public float multiplicator;
  15. public Vector3 AdjustedRotation => frontWheelTracker.Rotation * multiplicator;
  16. }
  17. public class SensorBikeController : MonoBehaviour
  18. {
  19. public float maxSpeed = 40f;
  20. public float maxMotorTorque = 1000;
  21. public PolarRotationMapping polarRotationMapping;
  22. public SpeedSensorConfig speedSensorConfig;
  23. public PolarSensorConfig polarSensorConfig;
  24. public FrontWheelTrackerConfig frontWheelTrackerConfig;
  25. public bool steer = true;
  26. public bool accelerate = true;
  27. public bool lean = true;
  28. private BicycleController bicycleController;
  29. private BikeSensorData sensorData;
  30. private float leanFactor;
  31. private bool isFrontWheelTrackerNotNull;
  32. private async void Start()
  33. {
  34. isFrontWheelTrackerNotNull = frontWheelTrackerConfig.frontWheelTracker != null;
  35. bicycleController = GetComponent<BicycleController>();
  36. sensorData = BikeSensorData.Instance;
  37. await sensorData.StartListening(polarSensorConfig: polarSensorConfig, speedSensorConfig: speedSensorConfig);
  38. leanFactor = 90f / (polarRotationMapping.maxRight - polarRotationMapping.center);
  39. }
  40. private void Update()
  41. {
  42. var speedData = sensorData.SpeedData;
  43. var polarData = sensorData.PolarData;
  44. if (speedData != null && accelerate)
  45. {
  46. SetSpeed(speedData.Value);
  47. }
  48. if (isFrontWheelTrackerNotNull && steer)
  49. {
  50. SetSteer();
  51. }
  52. if (polarData != null && lean)
  53. {
  54. SetLeaningAngle(polarData.Value);
  55. }
  56. }
  57. private void SetSteer()
  58. {
  59. bicycleController.CurrentSteerAngle = frontWheelTrackerConfig.AdjustedRotation.y; //TODO: something a bit smarter
  60. }
  61. private void OnDestroy()
  62. {
  63. sensorData.Dispose();
  64. }
  65. private void SetLeaningAngle(PolarSensorData polarData)
  66. {
  67. //don't lean while standing / walking to bike
  68. if (bicycleController.rigidBody.velocity.magnitude > .5f)
  69. {
  70. bicycleController.CurrentLeaningAngle = (polarData.Acc.y - polarRotationMapping.center) * leanFactor;
  71. }
  72. }
  73. private void SetSpeed(SpeedSensorData speedData)
  74. {
  75. var currentSpeed = bicycleController.rigidBody.velocity.magnitude;
  76. var speedDif = speedData.Speed - currentSpeed;
  77. var ratio = speedDif / maxSpeed;
  78. var torque = maxMotorTorque * ratio;
  79. if (speedDif >= .1f) // 0.36 km/h
  80. {
  81. Debug.Log($"SpeedDif = {speedDif} -> applying Torque {torque} (Ratio: {ratio})");
  82. bicycleController.CurrentBrakeTorque = 0;
  83. bicycleController.CurrentMotorTorque = torque;
  84. }
  85. else if (speedDif <= -.1f)
  86. {
  87. Debug.Log($"SpeedDif = {speedDif} -> applying brake Torque {torque} (Ratio: {ratio})");
  88. bicycleController.CurrentMotorTorque = 0;
  89. bicycleController.CurrentBrakeTorque = -torque;
  90. }
  91. // without else the speed overshoots a bit, but is way closer to real speed
  92. /*else
  93. {
  94. bicycleController.CurrentMotorTorque = 0;
  95. bicycleController.CurrentBrakeTorque = 0;
  96. }*/
  97. }
  98. }