SensorBikeController.cs 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  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. private IBicycleController bicycleController;
  28. private bool isFrontWheelTrackerNotNull;
  29. private BikeSensorData sensorData;
  30. private float brakeIncreasePerSecond = 2.5f;
  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. }
  71. }
  72. }