/
VesselControl.cs
320 lines (253 loc) · 10.1 KB
/
VesselControl.cs
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
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
using System;
using System.Collections;
using System.Collections.Generic;
using System.IO;
using System.Runtime.InteropServices;
using System.Runtime.Serialization.Formatters.Binary;
using UnityEngine;
using Winterdom.IO.FileMap;
public class VesselControl : MonoBehaviour
{
public enum FlightControlMode { RawStick,Assist,Automous}
public FlightControlMode controlMode;
public struct Vessel { public Transform transform; public List<Part> parts; public Transform meshOffset; }
public Vessel vessel;
MemoryMappedFile vesselFile;
MemoryBridge memoryBridge;
public Vector3 PIDvaluesRollHigh, PIDvaluesRollLow;
public Vector3 PIDvaluesPitchHigh, PIDvaluesPitchLow;
public Vector3 PIDvaluesYawHigh, PIDvaluesYawLow;
public List<Part> vesselParts;
public Vector3 gimbalOffset, vesselOffset;
public Transform COM;
Material mirrorMat, targetMat, inRangeMat;
public Transform adjustedGimbal;
public VesselSerializer targetVessel, mirrorVessel;
PID PIDyaw, PIDpitch, PIDroll;
public void ControlAwake(MemoryBridge memoryBridge)
{
this.memoryBridge = memoryBridge;
ToggleAutoPilot(true);
mirrorMat = Resources.Load("MirrorMat", typeof(Material)) as Material;
targetMat = Resources.Load("TargetMat", typeof(Material)) as Material;
inRangeMat = Resources.Load("InRangeMat", typeof(Material)) as Material;
// gimbalOffset = Vector3.zero;
mirrorVessel = gameObject.AddComponent(typeof(VesselSerializer)) as VesselSerializer;
vessel = mirrorVessel.DrawVessel(this.memoryBridge, mirrorMat, "Mirror Vessel COM");
Debug.Log("Mirror Vessel Built with Part Count : " + vessel.parts.Count);
targetVessel = gameObject.AddComponent(typeof(VesselSerializer)) as VesselSerializer;
targetVessel.DrawVessel(this.memoryBridge, targetMat, "Target Vessel COM");
// Debug.Log("Target Vessel Copied from Mirror");
vessel.transform.SetParent(transform.Find("Gimbal").transform);
PIDpitch = new PID(PIDvaluesPitchHigh, PIDvaluesPitchLow);
PIDyaw = new PID(PIDvaluesYawHigh, PIDvaluesYawLow);
PIDroll = new PID(PIDvaluesRollHigh, PIDvaluesRollLow);
GameObject gimbalObject = new GameObject();
gimbalObject.name = "Adjusted Gimbal";
adjustedGimbal = gimbalObject.transform;
adjustedGimbal.SetParent(GameObject.Find("Gimbal").transform);
// DebugVector.DrawVector(adjustedGimbal);
targetVessel.vessel.SetParent(transform.Find("Gimbal").transform);
memoryBridge.SetFloat("VesselYaw", 1.1f);
memoryBridge.SetFloat("VesselPitch", 1.1f);
memoryBridge.SetFloat("VesselRoll", 1.1f);
//memoryBridge.SetFloat("testnull" , null);
// targetVessel.gameObject.SetActive(false);
// GameObject COMobject = new GameObject();
// COMobject.name = "COM";
// COM = COMobject.transform;
// var vesselCOM = memoryBridge.LocalCOM;
// var convertedCOM = new Vector3(vesselCOM.x, -vesselCOM.z, vesselCOM.y);
// COM.localPosition = convertedCOM;
//COM.SetParent(transform.parent);
//transform.SetParent(COM);
//COM.position = Vector3.zero;
if(controlMode == FlightControlMode.RawStick)
{
targetVessel.vessel.gameObject.SetActive(false);
}
}
bool autoPilot = false;
bool vesselInRange = false;
public void VesselUpdate()
{
var vesselOffst = memoryBridge.GetVector3("AdjustedVesselCOMoffset" + memoryBridge.fileName);
vessel.meshOffset.localPosition = vesselOffst;
targetVessel.vesselOffset.localPosition = vesselOffst;
var kspOffset = memoryBridge.GetQuaternion("VesselGimbleOffset" + memoryBridge.fileName);
vessel.transform.rotation = kspOffset;
adjustedGimbal.rotation = vessel.transform.rotation;
var tempRot = adjustedGimbal.localRotation;
tempRot.x = 0;
tempRot.z = 0;
adjustedGimbal.localRotation = tempRot;
if(controlMode == FlightControlMode.RawStick)
{
targetVessel.vessel.rotation = adjustedGimbal.rotation;
System.TimeSpan deltaTime = TimeSpan.FromSeconds(Time.fixedDeltaTime);
bool recievingPitchInput = false;
bool recievingRollInput = false;
bool recievingYawInput = false;
var rollInput = Input.GetAxis("Roll");
var pitchInput = Input.GetAxis("Pitch");
var yawInput = Input.GetAxis("Yaw");
// Debug.Log(pitcInput);
if (rollInput != 0)
recievingRollInput = true;
if (pitchInput !=0)
recievingPitchInput = true;
if (yawInput !=0)
recievingYawInput = true;
// var tempVesselRot = targetVessel.vessel.transform.rotation;
if (recievingYawInput)
{
memoryBridge.SetFloat("VesselYaw", yawInput);
// tempVesselRot.y = mirrorVessel.vessel.transform.rotation.y;
}
else
{
memoryBridge.SetFloat("VesselYaw", 1.1f);
}
if (recievingPitchInput)
{
memoryBridge.SetFloat("VesselPitch", pitchInput);
// tempVesselRot.x = mirrorVessel.vessel.transform.rotation.x;
}
else
{
memoryBridge.SetFloat("VesselPitch", 1.1f);
}
if (recievingRollInput)
{
memoryBridge.SetFloat("VesselRoll", rollInput);
// tempVesselRot.z = mirrorVessel.vessel.transform.rotation.z;
}
else
{
memoryBridge.SetFloat("VesselRoll", 1.1f);
}
// targetVessel.vessel.transform.rotation = tempVesselRot;
var targetVesselOffset = Quaternion.Inverse(targetVessel.vessel.rotation) * vessel.transform.rotation;
//if (rollInput > 0 & Math.Abs(rollInput) <= 1)
//{
// memoryBridge.SetFloat("VesselRoll", rollInput);
//}
//else
//{
// memoryBridge.SetFloat("VesselRoll", PIDroll.CalculateResult(targetVesselOffset.z * 10, deltaTime));
//}
//memoryBridge.SetFloat("VesselPitch", pitchInput);
//memoryBridge.SetFloat("VesselYaw", yawInput);
// var targetVesselOffset = targetVessel.vessel.localEulerAngles - mirrorVessel.vessel.localEulerAngles;
// Debug.Log(targetVesselOffset);
// memoryBridge.SetFloat("VesselPitch", PIDpitch.CalculateResult(targetVesselOffset.x * 10, deltaTime));
}
// memoryBridge.SetFloat("VesselYaw", PIDyaw.CalculateResult(targetVesselOffset.y, deltaTime));
//var rawRoll = targetVesselOffset.x;
//float rollError = 0;
//if (rawRoll > 180)
//{
// rollError = 360 - rawRoll;
//}
//else
//{
// rollError = -rawRoll;
//}
//if (autoPilot)
//{
// //Debug.Log(Quaternion.Angle(adjustedGimbal.rotation, transform.rotation));
// var targetAngle = Quaternion.Angle(adjustedGimbal.rotation, transform.rotation);
// if (targetAngle < 3 & !vesselInRange)
// {
// OnVesselEnterTargerRange();
// }
// if (targetAngle >= 3 & vesselInRange)
// {
// OnVesselExitTargetRange();
// }
//}
}
class ProximityPID
{
PidController trgtPID;
public ProximityPID(double P, double I, double D, double max, double min)
{
trgtPID = new PidController(P, I, D, max, min);
trgtPID.SetPoint = 0;
}
public double FixedUpdate(double processVariable, System.TimeSpan deltaTime)
{
trgtPID.ProcessVariable = processVariable;
double trgtAngle = trgtPID.ControlVariable(deltaTime);
return trgtAngle;
}
}
class PID
{
PidController PIDlow, PIDhigh;
public enum PIDMode { High, Low }
public PIDMode mode;
public Quaternion lockVector;
// Vessel activeVessel;
public bool pidRunning = false;
public PID(Vector3 PIDvaluesHigh, Vector3 PIDvaluesLow)
{
PIDhigh = new PidController(PIDvaluesHigh.x, PIDvaluesHigh.y, PIDvaluesHigh.z, 1, -1);
PIDlow = new PidController(PIDvaluesLow.x, PIDvaluesLow.y, PIDvaluesLow.z, 1, -1);
PIDhigh.SetPoint = 0;
PIDlow.SetPoint = 0;
mode = PIDMode.Low;
}
public void StartPID()
{
pidRunning = true;
}
public float CalculateResult(float error, System.TimeSpan deltaTime)
{
float result = 0;
if (pidRunning)
{
switch (mode)
{
case PIDMode.Low:
PIDlow.ProcessVariable = error;
result = (float)PIDlow.ControlVariable(deltaTime);
break;
case PIDMode.High:
PIDhigh.ProcessVariable = error;
result = (float)PIDhigh.ControlVariable(deltaTime);
break;
}
}
return result;
}
}
public void ToggleAutoPilot(bool active)
{
if (active)
{
memoryBridge.SetBool("ClientAutoPilotActive", true);
}
else
{
memoryBridge.SetBool("ClientAutoPilotActive", false);
}
autoPilot = active;
}
void OnVesselEnterTargerRange()
{
Debug.Log("Acceptable range");
vesselInRange = true;
mirrorVessel.gameObject.SetActive(false);
targetVessel.SetVesselMaterial(inRangeMat);
// targetVessel.set
}
void OnVesselExitTargetRange()
{
Debug.Log("Left Acceptable range");
vesselInRange = false;
mirrorVessel.gameObject.SetActive(true);
targetVessel.SetVesselMaterial(targetMat);
}
}