123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108 |
- using System;
- using Controller.Bicycle;
- using Controller.Lean;
- using Sensors;
- using Sensors.ANT;
- using Tracking;
- using UnityEngine;
- namespace Controller
- {
- [Serializable]
- public struct FrontWheelTrackerConfig
- {
- public FrontWheelTracker frontWheelTracker;
- public float multiplicator;
- public float AdjustedRotation => frontWheelTracker.SteerRotation * multiplicator;
- }
- [Serializable]
- public struct HMDTrackerConfig
- {
- public HMDTracker cameraTracker;
- public float multiplicator;
- public float steeringOffset;
- public float AdjustedRotation => cameraTracker.SteerRotation * multiplicator;
- }
- [RequireComponent(typeof(IBicycleController))]
- public class SensorBikeController : MonoBehaviour
- {
- public enum SteeringMode { frontWheel, Leaning, HMD };
- public PolarRotationMapping polarRotationMapping;
- public FrontWheelTrackerConfig frontWheelTrackerConfig;
- public HMDTrackerConfig hmdTrackerConfig;
- private float leanFactor;
- public bool steer = true;
- public bool accelerate = true;
- public bool lean = true;
- public SteeringMode steeringSelection;
- private IBicycleController bicycleController;
- private bool isFrontWheelTrackerNotNull;
- private bool isHMDTrackerNotNull;
- private BikeSensorData sensorData;
- private void Start()
- {
- isFrontWheelTrackerNotNull = frontWheelTrackerConfig.frontWheelTracker != null;
- isHMDTrackerNotNull = hmdTrackerConfig.cameraTracker != null;
- bicycleController = GetComponent<IBicycleController>();
- sensorData = BikeSensorData.Instance;
- leanFactor = 90f / (polarRotationMapping.maxRight - polarRotationMapping.center);
- }
- private void Update()
- {
- Debug.Log("Bike Sensor Controller called");
- var speedData = sensorData.SpeedData;
- if (speedData != null && accelerate) SetSpeed(speedData.Value);
- if (steer) SetSteer();
- if (lean) SetLeaningAngle();
- }
- private void SetSteer()
- {
- Debug.Log("Updating Steering");
- switch (steeringSelection) {
- case SteeringMode.frontWheel:
- if (isFrontWheelTrackerNotNull) {
- bicycleController.CurrentSteerAngle = frontWheelTrackerConfig.AdjustedRotation * 2f;
- }
- break;
- case SteeringMode.Leaning:
- var polarData = sensorData.BleData;
- if (polarData != null)
- {
- bicycleController.CurrentSteerAngle = (polarData.Value.Acc.y - polarRotationMapping.center) * leanFactor;
- }
- break;
- case SteeringMode.HMD:
- if (isHMDTrackerNotNull)
- {
- // We emprirically observed a right-drift
- // We subtract a constant to counteract this
- bicycleController.CurrentSteerAngle = hmdTrackerConfig.AdjustedRotation - 2f;
- }
- break;
- }
- Debug.Log("Updating Steering Angle to " + bicycleController.CurrentSteerAngle);
- }
- private void SetLeaningAngle()
- {
- //bicycleController.CurrentLeaningAngle =
- }
- private void SetSpeed(SpeedSensorData speedData)
- {
- bicycleController.CurrentSpeed = speedData.Speed;
- }
- }
- }
|