SensorBikeController.cs 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. using System;
  2. using Controller.Bicycle;
  3. using Controller.Lean;
  4. using Sensors;
  5. using Sensors.ANT;
  6. using Sensors.USB;
  7. using Tracking;
  8. using UnityEngine;
  9. namespace Controller
  10. {
  11. [Serializable]
  12. public struct FrontWheelTrackerConfig
  13. {
  14. public FrontWheelTracker frontWheelTracker;
  15. public float multiplicator;
  16. public float AdjustedRotation => frontWheelTracker.SteerRotation * multiplicator;
  17. }
  18. [RequireComponent(typeof(IBicycleController))]
  19. public class SensorBikeController : MonoBehaviour
  20. {
  21. public PolarRotationMapping polarRotationMapping;
  22. public FrontWheelTrackerConfig frontWheelTrackerConfig;
  23. public bool steer = true;
  24. public bool accelerate = true;
  25. public bool lean = true;
  26. public bool breaking = true;
  27. public float brakeIncreasePerSecond = 2.5f;
  28. private IBicycleController bicycleController;
  29. private bool isFrontWheelTrackerNotNull;
  30. private BikeSensorData sensorData;
  31. private void Start()
  32. {
  33. isFrontWheelTrackerNotNull = frontWheelTrackerConfig.frontWheelTracker != null;
  34. bicycleController = GetComponent<IBicycleController>();
  35. sensorData = BikeSensorData.Instance;
  36. }
  37. private void Update()
  38. {
  39. var speedData = sensorData.SpeedData;
  40. if (speedData != null && accelerate) SetSpeed(speedData.Value);
  41. //SetSpeed(new SpeedSensorData());
  42. var breakData = sensorData.UsbData;
  43. if(breakData != null && breaking){
  44. SetBreaking(breakData.Value);
  45. }
  46. if (isFrontWheelTrackerNotNull && steer) SetSteer();
  47. if (lean) SetLeaningAngle();
  48. }
  49. private void SetSteer()
  50. {
  51. bicycleController.CurrentSteerAngle =
  52. frontWheelTrackerConfig.AdjustedRotation;
  53. }
  54. private void SetLeaningAngle()
  55. {
  56. //bicycleController.CurrentLeaningAngle =
  57. }
  58. private void SetSpeed(SpeedSensorData speedData)
  59. {
  60. bicycleController.CurrentSpeed = speedData.Speed;
  61. //bicycleController.CurrentSpeed = 16 / 3.6f;
  62. }
  63. private void SetBreaking(USBSensorData breakData)
  64. {
  65. if(breakData.isBreaking){
  66. Debug.Log("Currently breaking");
  67. bicycleController.CurrentBreakForce += brakeIncreasePerSecond * Time.deltaTime;
  68. //bicycleController.CurrentSpeed = Mathf.Max(0, bicycleController.CurrentSpeed - brakeIncreasePerSecond * Time.deltaTime);
  69. }
  70. else
  71. {
  72. bicycleController.CurrentBreakForce = 0;
  73. }
  74. }
  75. }
  76. }