forked from RemoteTechnologiesGroup/RemoteTech
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRoverComputer.cs
More file actions
244 lines (218 loc) · 9.43 KB
/
RoverComputer.cs
File metadata and controls
244 lines (218 loc) · 9.43 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using UnityEngine;
namespace RemoteTech.FlightComputer
{
public class RoverComputer
{
private Vessel mVessel;
private RoverPIDController mThrottlePID, mWheelPID;
private float
mRoverAlt,
mRoverLat,
mRoverLon,
mTargetLat,
mTargetLon,
brakeDist;
private Quaternion mRoverRot;
private Vector3 ForwardAxis;
public float Delta { get; private set; }
public float DeltaT { get; private set; }
public RoverComputer()
{
mThrottlePID = new RoverPIDController(10f, 1e-5F, 1e-5F, -1f, 1f);
mWheelPID = new RoverPIDController(10f, 1e-5F, 1e-5F, -1f, 1f, 5f);
}
public void SetVessel(Vessel v)
{
mVessel = v;
}
private float RoverHDG
{
get
{
Vector3d up = (mVessel.CoM - mVessel.mainBody.position).normalized;
Vector3d north = Vector3d.Exclude(up, (mVessel.mainBody.position + mVessel.mainBody.transform.up * (float)mVessel.mainBody.Radius) - mVessel.CoM).normalized;
if (ForwardAxis == Vector3.zero)
return RTUtil.GetHeading(mVessel.srf_velocity.normalized, up, north);
else
return RTUtil.GetHeading(mVessel.ReferenceTransform.TransformDirection(ForwardAxis), up, north);
}
}
private float TargetHDG
{
get
{
Vector3d up = (mVessel.CoM - mVessel.mainBody.position).normalized;
Vector3d north = Vector3d.Exclude(up, (mVessel.mainBody.position + mVessel.mainBody.transform.up * (float)mVessel.mainBody.Radius) - mVessel.CoM).normalized;
return RTUtil.GetHeading((TargetPos - mVessel.CoM).normalized, up, north);
}
}
private Vector3 TargetPos
{
get
{
return mVessel.mainBody.GetWorldSurfacePosition(mTargetLat, mTargetLon, mVessel.altitude);
}
}
private Vector3 RoverOrigPos
{
get
{
return mVessel.mainBody.GetWorldSurfacePosition(mRoverLat, mRoverLon, mRoverAlt);
}
}
private float RoverSpeed
{
get
{
if (ForwardAxis == Vector3.zero)
return (float)mVessel.srf_velocity.magnitude;
else
return Vector3.Dot(mVessel.srf_velocity, mVessel.ReferenceTransform.TransformDirection(ForwardAxis));
}
}
public void InitMode(RemoteTech.FlightComputer.Commands.DriveCommand dc)
{
if (mVessel == null) {
MonoBehaviour.print("mVessel was null!!!!!!!!!!!!!!!!!!!!!!!!!!");
}
ForwardAxis = Vector3.zero;
mRoverAlt = (float)mVessel.altitude;
mRoverLat = (float)mVessel.latitude;
mRoverLon = (float)mVessel.longitude;
Delta = 0;
DeltaT = 0;
brakeDist = 0;
switch (dc.mode) {
case RemoteTech.FlightComputer.Commands.DriveCommand.DriveMode.Turn:
mRoverRot = mVessel.ReferenceTransform.rotation;
break;
case RemoteTech.FlightComputer.Commands.DriveCommand.DriveMode.Distance:
mWheelPID.setClamp(-1, 1);
break;
case RemoteTech.FlightComputer.Commands.DriveCommand.DriveMode.DistanceHeading:
mWheelPID.setClamp(-dc.steering, dc.steering);
break;
case RemoteTech.FlightComputer.Commands.DriveCommand.DriveMode.Coord:
mWheelPID.setClamp(-dc.steering, dc.steering);
mTargetLat = dc.target;
mTargetLon = dc.target2;
break;
}
mThrottlePID.Reset();
mWheelPID.Reset();
mVessel.ActionGroups.SetGroup(KSPActionGroup.Brakes, false);
}
public bool Drive(RemoteTech.FlightComputer.Commands.DriveCommand dc, FlightCtrlState fs)
{
if (dc != null) {
if (mVessel.srf_velocity.magnitude > 0.5) {
float degForward = Mathf.Abs(RTUtil.ClampDegrees90(Vector3.Angle(mVessel.srf_velocity, mVessel.ReferenceTransform.forward)));
float degUp = Mathf.Abs(RTUtil.ClampDegrees90(Vector3.Angle(mVessel.srf_velocity, mVessel.ReferenceTransform.up)));
float degRight = Mathf.Abs(RTUtil.ClampDegrees90(Vector3.Angle(mVessel.srf_velocity, mVessel.ReferenceTransform.right)));
if (degForward < degUp && degForward < degRight)
ForwardAxis = Vector3.forward;
else if (degRight < degUp && degRight < degForward)
ForwardAxis = Vector3.right;
else
ForwardAxis = Vector3.up;
}
switch (dc.mode) {
case RemoteTech.FlightComputer.Commands.DriveCommand.DriveMode.Turn:
return Turn(dc, fs);
case RemoteTech.FlightComputer.Commands.DriveCommand.DriveMode.Distance:
return Distance(dc, fs);
case RemoteTech.FlightComputer.Commands.DriveCommand.DriveMode.DistanceHeading:
return DistanceHeading(dc, fs);
case RemoteTech.FlightComputer.Commands.DriveCommand.DriveMode.Coord:
return Coord(dc, fs);
}
return true;
}
return true;
}
private bool Turn(RemoteTech.FlightComputer.Commands.DriveCommand dc, FlightCtrlState fs)
{
Delta = Math.Abs(Quaternion.Angle(mRoverRot, mVessel.ReferenceTransform.rotation));
DeltaT = Delta / mVessel.GetComponent<Rigidbody>().angularVelocity.magnitude;
if (Delta < dc.target) {
fs.wheelThrottle = mThrottlePID.Control(dc.speed - RoverSpeed);
fs.wheelSteer = dc.steering;
return false;
} else {
fs.wheelThrottle = 0;
fs.wheelSteer = 0;
mVessel.ActionGroups.SetGroup(KSPActionGroup.Brakes, true);
dc.mode = RemoteTech.FlightComputer.Commands.DriveCommand.DriveMode.Off;
return true;
}
}
private bool Distance(RemoteTech.FlightComputer.Commands.DriveCommand dc, FlightCtrlState fs)
{
float speed = RoverSpeed;
Delta = Math.Abs(dc.target) - Vector3.Distance(RoverOrigPos, mVessel.CoM);
DeltaT = Delta / Math.Abs(speed);
if (Delta > 0) {
fs.wheelThrottle = mThrottlePID.Control(BrakeSpeed(dc.speed, speed) - speed);
return false;
} else {
fs.wheelThrottle = 0;
fs.wheelSteer = 0;
mVessel.ActionGroups.SetGroup(KSPActionGroup.Brakes, true);
dc.mode = RemoteTech.FlightComputer.Commands.DriveCommand.DriveMode.Off;
return true;
}
}
private bool DistanceHeading(RemoteTech.FlightComputer.Commands.DriveCommand dc, FlightCtrlState fs)
{
float speed = RoverSpeed;
Delta = Math.Abs(dc.target) - Vector3.Distance(RoverOrigPos, mVessel.CoM);
DeltaT = Delta / speed;
if (Delta > 0) {
fs.wheelThrottle = mThrottlePID.Control(BrakeSpeed(dc.speed, speed) - speed);
if (ForwardAxis != Vector3.zero)
fs.wheelSteer = mWheelPID.Control(RTUtil.AngleBetween(RoverHDG, dc.target2));
return false;
} else {
fs.wheelThrottle = 0;
fs.wheelSteer = 0;
mVessel.ActionGroups.SetGroup(KSPActionGroup.Brakes, true);
dc.mode = RemoteTech.FlightComputer.Commands.DriveCommand.DriveMode.Off;
return true;
}
}
private float BrakeSpeed(float speed, float actualSpeed)
{
if (brakeDist == 0) {
if (DeltaT > actualSpeed / 2) {
return speed;
} else
brakeDist = Delta;
}
return Math.Max(Math.Min(speed, (float)Math.Sqrt(Delta / brakeDist) * speed), speed / 10);
}
private bool Coord(RemoteTech.FlightComputer.Commands.DriveCommand dc, FlightCtrlState fs)
{
float
deg = RTUtil.AngleBetween(RoverHDG, TargetHDG),
speed = RoverSpeed;
Delta = Vector3.Distance(mVessel.CoM, TargetPos);
DeltaT = Delta / speed;
if (Delta > Math.Abs(deg) / 36) {
fs.wheelThrottle = mThrottlePID.Control(BrakeSpeed(dc.speed, speed) - speed);
if (ForwardAxis != Vector3.zero)
fs.wheelSteer = mWheelPID.Control(deg);
return false;
} else {
fs.wheelThrottle = 0;
fs.wheelSteer = 0;
mVessel.ActionGroups.SetGroup(KSPActionGroup.Brakes, true);
dc.mode = RemoteTech.FlightComputer.Commands.DriveCommand.DriveMode.Off;
return true;
}
}
}
}