/
helicopter.cs
334 lines (274 loc) · 11.1 KB
/
helicopter.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
321
322
323
324
325
326
327
328
329
330
331
332
333
334
// Flight Assist
// Control your spaceships without thrusters in every direction
//
// Author: Naosyth
// naosyth@gmail.com
// ==================== //
// === Tuning Guide === //
// ==================== //
// Four main variables control how the script behaves
//
// MaxPitch and MaxRoll
// Sets the maximum angle in degrees that the ship will rotate to bring itself to a stop
// A high angle means you will stop faster, but also have more vertical movement.
//
// GyroResponsiveness
// This controls the curve of the angle the ship takes while coming to a stop.
// The curve looks something like this:
// http://www.wolframalpha.com/input/?i=arctan%28x%2F4%29+%2F+%283.14159+%2F+2%29+for+x+from+-104+to+104
// Where 4, in the above equation, is GyroResponsiveness
// Increasing GyroResponsiveness makes the angle start approaching zero earlier
// This can help to reduce wobble, but also means you will take a bit longer to slow down.
//
// Block Names
static string GyroName = "FA Gyro";
static string RemoteControlName = "FA Remote";
static string TextPanelName = "FA Screen";
// Program options
static int ScreenRefreshRate = 5; // How many times per second to redraw the screen
static bool AlwaysEnabledInGravity = false;
// Control constants
static double MaxPitch = 67.5;
static double MaxRoll = 67.5;
static int GyroResponsiveness = 8; // Larger = more gradual angle drop
static int GyroCount = 9; // Number of gyros to use for auto hover
static double minRPM = 0.015; // Min RPM setting for gyros. Values that are too low behave weird. You shouldn't need to adjust this.
const double HalfPi = Math.PI / 2;
AutoHoverController controller;
void Main(string arguments) {
if (controller == null)
controller = new AutoHoverController(GridTerminalSystem, Me);
if (arguments != "") {
var args = arguments.Split(' ');
var command = args[0].ToLower();
if (command == "mode") {
var mode = args[1];
controller.mode = mode;
if (args.Length >= 3)
controller.setSpeed = Int32.Parse(args[2]);
}
}
controller.Tick();
}
class AutoHoverController {
private IMyProgrammableBlock Me;
private IMyGridTerminalSystem GridTerminalSystem;
private IMyRemoteControl remote;
private IMyTextPanel screen;
private List<IMyGyro> gyros;
private IMyGyro gyro;
private Vector3 pos;
private Vector3 oldPos;
private Vector3 deltaPos;
private double speed;
private double speedForward;
private double speedRight;
private double speedUp;
private double controlSpeedForward;
private double controlSpeedRight;
private Matrix orientation;
private Vector3 forwardVec;
private Vector3 rightVec;
private Vector3 upVec;
private bool switchingGravity;
private bool inGravity;
private double gravity;
private double pitch;
private double desiredPitch;
private float pitchRate;
private double roll;
private double desiredRoll;
private float rollRate;
public string mode;
public int setSpeed;
private int tickCount;
public AutoHoverController(IMyGridTerminalSystem gts, IMyProgrammableBlock pb) {
Me = pb;
GridTerminalSystem = gts;
remote = GridTerminalSystem.GetBlockWithName(RemoteControlName) as IMyRemoteControl;
gyro = GridTerminalSystem.GetBlockWithName(GyroName) as IMyGyro;
if (!String.IsNullOrEmpty(TextPanelName))
screen = GridTerminalSystem.GetBlockWithName(TextPanelName) as IMyTextPanel;
var list = new List<IMyTerminalBlock>();
GridTerminalSystem.GetBlocksOfType<IMyGyro>(list, x => x.CubeGrid == Me.CubeGrid && x != gyro);
gyros = list.ConvertAll(x => (IMyGyro)x);
gyros.Insert(0, gyro);
gyros = gyros.GetRange(0, GyroCount);
mode = "Hover";
setSpeed = 0;
}
public void Tick() {
CalculateOrientation();
CalculateSpeed();
tickCount += 1;
if (tickCount % (60 / ScreenRefreshRate) == 0)
PrintStatus();
// Automatically enable if AlwaysEnabledInGravity = true and in gravity wih script disabled
if (AlwaysEnabledInGravity && inGravity && !gyro.GyroOverride)
ToggleGyros(true);
if (switchingGravity)
ToggleGyros(false);
if (!gyro.GyroOverride)
return;
if (inGravity)
PlanetaryManeuvers();
else
SpaceManeuvers();
}
private void CalculateSpeed() {
// Get current time and change in time
double dt = 1000 / 60;
// Get delta position
pos = remote.GetPosition();
deltaPos = pos - oldPos;
oldPos = pos;
speed = deltaPos.Length() / dt * 1000;
// Get orientation vectors
orientation = remote.WorldMatrix;
if (inGravity) {
forwardVec = orientation.Forward;
rightVec = orientation.Right;
upVec = orientation.Up;
} else {
forwardVec = orientation.Up;
rightVec = orientation.Right;
upVec = orientation.Forward;
}
// Determine speed forward and sideways in m/s
if (inGravity) {
Vector3 gravityVec = -Vector3.Normalize(remote.GetNaturalGravity());
speedForward = Vector3.Dot(deltaPos, Vector3.Cross(gravityVec, rightVec)) / dt * 1000;
speedRight = Vector3.Dot(deltaPos, -Vector3.Cross(gravityVec, forwardVec)) / dt * 1000;
speedUp = Vector3.Dot(deltaPos, gravityVec) / dt * 1000;
} else {
speedForward = Vector3.Dot(deltaPos, upVec) / dt * 1000;
speedRight = Vector3.Dot(deltaPos, rightVec) / dt * 1000;
speedUp = Vector3.Dot(deltaPos, forwardVec) / dt * 1000;
}
}
private void CalculateOrientation() {
// Get gravity vector
Vector3 gravityVec = -Vector3.Normalize(remote.GetNaturalGravity());
gravity = remote.GetNaturalGravity().Length() / 9.81;
switchingGravity = inGravity;
inGravity = !float.IsNaN(gravityVec.GetDim(0));
switchingGravity = inGravity != switchingGravity;
// Determine roll and pitch in degrees
if (inGravity) {
pitch = Vector3.Dot(gravityVec, forwardVec) / (gravityVec.Length() * forwardVec.Length()) * 90;
roll = Vector3.Dot(gravityVec, rightVec) / (gravityVec.Length() * rightVec.Length()) * 90;
} else {
pitch = Vector3.Dot(deltaPos, forwardVec) / (deltaPos.Length() * forwardVec.Length()) * 90;
roll = Vector3.Dot(deltaPos, rightVec) / (deltaPos.Length() * rightVec.Length()) * 90;
if (speed == 0) {
roll = 0;
pitch = 0;
}
}
}
private void PlanetaryManeuvers() {
switch (mode.ToLower()) {
case "glide":
desiredPitch = 0;
desiredRoll = Math.Atan(speedRight / GyroResponsiveness) / HalfPi * MaxRoll;
break;
case "freeglide":
desiredPitch = 0;
desiredRoll = 0;
break;
case "pitch":
desiredPitch = Math.Atan(speedForward / GyroResponsiveness) / HalfPi * MaxPitch;
desiredRoll = roll;
break;
case "roll":
desiredPitch = pitch;
desiredRoll = Math.Atan(speedRight / GyroResponsiveness) / HalfPi * MaxRoll;
break;
case "cruise":
desiredPitch = Math.Atan((speedForward - setSpeed) / GyroResponsiveness) / HalfPi * MaxPitch;
desiredRoll = Math.Atan(speedRight / GyroResponsiveness) / HalfPi * MaxRoll;
break;
default: // Stationary Hover
desiredPitch = Math.Atan(speedForward / GyroResponsiveness) / HalfPi * MaxPitch;
desiredRoll = Math.Atan(speedRight / GyroResponsiveness) / HalfPi * MaxRoll;
break;
}
// Scale gyro rate based on difference bewteen the current and desired angle
pitchRate = (float)(gyro.GetMaximum<float>("Pitch") * (desiredPitch - pitch) / 90);
if (pitchRate > -minRPM && pitchRate < minRPM)
pitchRate = (float)(Math.Sign(pitchRate) * minRPM);
rollRate = (float)(gyro.GetMaximum<float>("Roll") * (desiredRoll - roll) / 90);
if (rollRate > -minRPM && rollRate < minRPM)
rollRate = (float)(Math.Sign(rollRate) * minRPM);
// Transform rotation to match the remote control block's orientation rather than the "build" orientation
Matrix shipOrientation;
remote.Orientation.GetMatrix(out shipOrientation);
Vector3 rotationVec = new Vector3(pitchRate, 0, rollRate);
rotationVec = Vector3.Transform(rotationVec, shipOrientation);
SetGyros(rotationVec, gyro.GyroOverride);
}
private void SpaceManeuvers() {
pitchRate = (float)(gyro.GetMaximum<float>("Pitch") * -pitch / 90);
if (pitchRate > -minRPM && pitchRate < minRPM)
pitchRate = (float)(Math.Sign(pitchRate) * minRPM);
rollRate = (float)(gyro.GetMaximum<float>("Roll") * -roll / 90);
if (rollRate > -minRPM && rollRate < minRPM)
rollRate = (float)(Math.Sign(rollRate) * minRPM);
if (speed < 0.5) {
rollRate = 0;
pitchRate = 0;
}
// Transform rotation to match the remote control block's orientation rather than the "build" orientation
Matrix shipOrientation;
remote.Orientation.GetMatrix(out shipOrientation);
Vector3 rotationVec = new Vector3(pitchRate, -rollRate, 0);
rotationVec = Vector3.Transform(rotationVec, shipOrientation);
SetGyros(rotationVec, gyro.GyroOverride);
}
private void PrintStatus() {
if (screen != null) {
if (!inGravity) {
screen.WritePublicText(
"> Auto Hover [" + (gyro.GyroOverride ? "Active" : "Disabled") + "] [Space]" +
"\n----- Velocity ----------------------------------------" +
"\nTotal: " + String.Format("{0:000}", speed) + " m/s" +
"\n F/B: " + String.Format("{0:000}", speedForward) +
"\n R/L: " + String.Format("{0:000}", speedRight) +
"\n U/D: " + String.Format("{0:000}", speedUp) +
"\n\n----- Orientation ----------------------------------------" +
"\nPitch: " + String.Format("{0:00}", pitch) + "°" +
" | Yaw: " + String.Format("{0:00}", roll) + "°");
return;
}
screen.WritePublicText(
"> Auto Hover [" + (gyro.GyroOverride ? "Active" : "Disabled") + "] [" + mode + "]" +
"\n----- Velocity ----------------------------------------" +
"\nTotal: " + String.Format("{0:000}", speed) + " m/s" +
"\n F/B: " + String.Format("{0:000}", speedForward) +
"\n R/L: " + String.Format("{0:000}", speedRight) +
"\n U/D: " + String.Format("{0:000}", speedUp) +
"\n\n----- Orientation ----------------------------------------" +
"\nPitch: " + String.Format("{0:00}", pitch) + "°" +
" | Roll: " + String.Format("{0:00}", roll) + "°" +
"\n\n----- Status ----------------------------------------" +
"\nGravity: " + String.Format("{0:0.00}", gravity) + " g");
}
}
private void ToggleGyros(bool state) {
for (int i = 0; i < gyros.Count; i++)
gyros[i].SetValueBool("Override", state);
}
private void SetGyros(Vector3 rotationVector, bool overrideEnabled) {
for (int i = 0; i < gyros.Count; i++) {
var g = gyros[i];
// Adjust rotation for the gyro's local orientation
Matrix localOrientation;
g.Orientation.GetMatrix(out localOrientation);
var localRot = Vector3.Transform(rotationVector, MatrixD.Transpose(localOrientation));
g.SetValueFloat("Pitch", (float)localRot.GetDim(0));
g.SetValueFloat("Yaw", (float)-localRot.GetDim(1));
g.SetValueFloat("Roll", (float)-localRot.GetDim(2));
g.SetValueBool("Override", gyro.GyroOverride);
}
}
}