|
2 | 2 | using System.Collections.Generic; |
3 | 3 | using UnityEngine; |
4 | 4 | using ZO.Util; |
| 5 | +using ZO.Physics; |
| 6 | +using ZO.Sensors; |
5 | 7 |
|
6 | 8 | namespace ZO.Controllers { |
7 | 9 |
|
8 | 10 | public class ZOQuadCopterController : ZOGameObjectBase { |
9 | | - |
| 11 | + |
10 | 12 | public enum ZOQuadCopterMotorConfiguration { |
11 | 13 | XConfiguration, |
12 | 14 | CrossConfiguration |
13 | 15 | } |
14 | 16 |
|
15 | 17 | public ZOQuadCopterMotorConfiguration _quadCopterConfiguration = ZOQuadCopterMotorConfiguration.XConfiguration; |
16 | | - // Start is called before the first frame update |
17 | | - void Start() { |
| 18 | + public ZOQuadCopterMotorConfiguration QuadCopterConfiguration { |
| 19 | + get => _quadCopterConfiguration; |
| 20 | + } |
| 21 | + public float _rotorDistanceFromCenter = 0.2f; |
| 22 | + public float RotorDistanceFromCenter { |
| 23 | + get => _rotorDistanceFromCenter; |
| 24 | + } |
| 25 | + public float _maxPerRotorForce = 50.0f; |
| 26 | + |
| 27 | + public Rigidbody _baseRigidBody = null; |
| 28 | + public Rigidbody BaseRigidBody { |
| 29 | + get => _baseRigidBody; |
| 30 | + } |
| 31 | + |
| 32 | + [Header("Altitude Control")] |
| 33 | + public ZOAltimeter _altimeter; |
| 34 | + public ZOAltimeter Altimeter { |
| 35 | + get => _altimeter; |
| 36 | + } |
| 37 | + public ZOPIDController _altitudePID = new ZOPIDController { |
| 38 | + Kp = 10, |
| 39 | + Ki = 1, |
| 40 | + Kd = 1, |
| 41 | + MaximumOutputValue = 100.0f, |
| 42 | + DeadBandEpsilon = 0.01f |
| 43 | + }; |
| 44 | + public ZOPIDController AltitudePID { |
| 45 | + get => _altitudePID; |
| 46 | + } |
18 | 47 |
|
| 48 | + private float AltitudeSetPoint { |
| 49 | + get { |
| 50 | + return AltitudePID.SetPoint; |
| 51 | + } |
| 52 | + set { |
| 53 | + AltitudePID.SetPoint = value; |
| 54 | + } |
19 | 55 | } |
20 | 56 |
|
21 | | - // Update is called once per frame |
22 | | - void Update() { |
| 57 | + private float _currentThrottle = 0.0f; |
| 58 | + private float _throttleIncrement = 0.1f; |
| 59 | + |
| 60 | + protected class Motor { |
| 61 | + public Vector3 localPosition = new Vector3(0,0,0); |
| 62 | + public Vector3 globalPosition = new Vector3(0,0,0); |
| 63 | + |
| 64 | + public float force = 0; |
| 65 | + public Vector3 globalForceVector = new Vector3(0,0,0); |
| 66 | + } |
| 67 | + |
| 68 | + protected Motor[] _motors = new Motor[4]; |
| 69 | + protected Motor[] Motors { |
| 70 | + get => _motors; |
| 71 | + } |
| 72 | + |
| 73 | + protected override void ZOStart() { |
| 74 | + base.ZOStart(); |
| 75 | + |
| 76 | + // initialize the motors |
| 77 | + for (int i = 0; i < Motors.Length; i++) { |
| 78 | + Motors[i] = new Motor(); |
| 79 | + } |
| 80 | + |
| 81 | + // set the altitude setpoint |
| 82 | + if (Altimeter != null) { |
| 83 | + AltitudeSetPoint = Altimeter.AltitudeMeters; |
| 84 | + } |
| 85 | + } |
| 86 | + |
| 87 | + |
| 88 | + protected override void ZOUpdate() { |
| 89 | + base.ZOUpdate(); |
| 90 | + |
| 91 | + if (Input.GetKeyDown("i")) { |
| 92 | + AltitudeSetPoint += _throttleIncrement; |
| 93 | + } |
| 94 | + if (Input.GetKeyDown("k")) { |
| 95 | + AltitudeSetPoint -= _throttleIncrement; |
| 96 | + } |
| 97 | + |
| 98 | + } |
| 99 | + |
| 100 | + protected override void ZOFixedUpdate() { |
| 101 | + base.ZOFixedUpdate(); |
| 102 | + if (QuadCopterConfiguration == ZOQuadCopterMotorConfiguration.XConfiguration) { |
| 103 | + |
| 104 | + // calculate altitude control |
| 105 | + _currentThrottle = AltitudePID.Update(Altimeter.AltitudeMeters, Time.deltaTime); |
| 106 | + // forward right motor |
| 107 | + Vector3 forwardRightPosition = BaseRigidBody.transform.TransformPoint(new Vector3(RotorDistanceFromCenter, 0, RotorDistanceFromCenter)); |
| 108 | + Vector3 forwardRightForce = BaseRigidBody.transform.up * _currentThrottle; |
| 109 | + BaseRigidBody.AddForceAtPosition(forwardRightForce, forwardRightPosition); |
| 110 | + Motors[0].globalPosition = forwardRightPosition; |
| 111 | + Motors[0].globalForceVector = forwardRightForce; |
| 112 | + |
| 113 | + // forward left motor |
| 114 | + Vector3 forwardLeftPosition = BaseRigidBody.transform.TransformPoint(new Vector3(-RotorDistanceFromCenter, 0, RotorDistanceFromCenter)); |
| 115 | + Vector3 forwardLeftForce = BaseRigidBody.transform.up * _currentThrottle; |
| 116 | + BaseRigidBody.AddForceAtPosition(forwardLeftForce, forwardLeftPosition); |
| 117 | + Motors[1].globalPosition = forwardLeftPosition; |
| 118 | + Motors[1].globalForceVector = forwardLeftForce; |
| 119 | + |
| 120 | + // back right motor |
| 121 | + Vector3 backRightPosition = BaseRigidBody.transform.TransformPoint(new Vector3(RotorDistanceFromCenter, 0, -RotorDistanceFromCenter)); |
| 122 | + Vector3 backRightForce = BaseRigidBody.transform.up * _currentThrottle; |
| 123 | + BaseRigidBody.AddForceAtPosition(backRightForce, backRightPosition); |
| 124 | + Motors[2].globalPosition = backRightPosition; |
| 125 | + Motors[2].globalForceVector = backRightForce; |
| 126 | + |
| 127 | + // back left motor |
| 128 | + Vector3 backLeftPosition = BaseRigidBody.transform.TransformPoint(new Vector3(-RotorDistanceFromCenter, 0, -RotorDistanceFromCenter)); |
| 129 | + Vector3 backLeftForce = BaseRigidBody.transform.up * _currentThrottle; |
| 130 | + BaseRigidBody.AddForceAtPosition(backLeftForce, backLeftPosition); |
| 131 | + Motors[3].globalPosition = backLeftPosition; |
| 132 | + Motors[3].globalForceVector = backLeftForce; |
| 133 | + |
| 134 | + } else if (QuadCopterConfiguration == ZOQuadCopterMotorConfiguration.CrossConfiguration) { |
| 135 | + //TODO: |
| 136 | + } |
| 137 | + |
| 138 | + } |
| 139 | + |
| 140 | + |
| 141 | + |
| 142 | + private void OnGUI() { |
| 143 | + GUI.TextField(new Rect(10, 10, 300, 22), $"Altitude Set Point: {AltitudeSetPoint.ToString("R2")}"); |
| 144 | + GUI.TextField(new Rect(10, 25, 300, 22), $"Altitude: {Altimeter.AltitudeMeters.ToString("R2")}"); |
| 145 | + GUI.TextField(new Rect(10, 40, 300, 22), $"Altitude Throttle: {_currentThrottle.ToString("R2")}"); |
| 146 | + |
| 147 | + foreach (Motor motor in Motors) { |
| 148 | + Debug.DrawRay(motor.globalPosition, motor.globalForceVector, Color.green, 0.1f); |
| 149 | + } |
23 | 150 |
|
24 | 151 | } |
25 | 152 | } |
|
0 commit comments