AntSetup.cs 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. using TMPro;
  2. using UnityEngine;
  3. public class AntSetup : MonoBehaviour
  4. {
  5. [Header("Speed")] public int speedSensorId = 0;
  6. public float wheelCircumference = 2.096f;
  7. public TextMeshProUGUI speedDisplay;
  8. [Header("Powermeter")]
  9. public int powerMeterId = 0;
  10. public TextMeshProUGUI powerDisplay;
  11. public TextMeshProUGUI cadenceDisplay;
  12. public TextMeshProUGUI torqueDisplay;
  13. private SpeedSensorReceiver speedSensorReceiver;
  14. private PowerMeterReceiver powerMeterReceiver;
  15. private bool speedDisplayAvailable;
  16. private bool powerDisplayAvailable;
  17. private bool cadenceDisplayAvailable;
  18. private bool torqueDisplayAvailable;
  19. private BicycleController bicycleController;
  20. // Start is called before the first frame update
  21. void Start()
  22. {
  23. bicycleController = GetComponent<BicycleController>();
  24. speedDisplayAvailable = speedDisplay != null;
  25. powerDisplayAvailable = powerDisplay != null;
  26. torqueDisplayAvailable = torqueDisplay != null;
  27. cadenceDisplayAvailable = cadenceDisplay != null;
  28. speedSensorReceiver = new SpeedSensorReceiver(speedSensorId, wheelCircumference);
  29. powerMeterReceiver = new PowerMeterReceiver(powerMeterId);
  30. speedSensorReceiver.StartScan();
  31. powerMeterReceiver.StartScan();
  32. }
  33. // Update is called once per frame
  34. void Update()
  35. {
  36. //transform.Translate(Vector3.forward * (Time.deltaTime * (speedSensorReceiver?.Speed ?? 0)));
  37. bicycleController.SetSpeed(speedSensorReceiver?.Speed ?? 0);
  38. //bicycleController.SetTorque(powerMeterReceiver?.CrankTorque ?? 0); //TODO crank to wheel torque;
  39. if (speedDisplayAvailable)
  40. {
  41. speedDisplay.text = $"{(speedSensorReceiver?.SpeedKmh ?? 0f):n2} km/h";
  42. }
  43. if (powerDisplayAvailable)
  44. {
  45. powerDisplay.text = $"{(powerMeterReceiver?.InstantaneousPower ?? 0f):0} W";
  46. }
  47. if (cadenceDisplayAvailable)
  48. {
  49. cadenceDisplay.text = $"{(powerMeterReceiver?.InstantaneousCadence ?? 0f):0} rpm";
  50. }
  51. if (torqueDisplayAvailable)
  52. {
  53. torqueDisplay.text = $"{(powerMeterReceiver?.CrankTorque ?? 0f):F2} Nm";
  54. }
  55. }
  56. }