SensorBikeController.cs 3.9 KB

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