-
Notifications
You must be signed in to change notification settings - Fork 0
/
dock.ks
135 lines (101 loc) · 2.97 KB
/
dock.ks
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
@lazyGlobal off.
// God this script is a mess
deletePath("pid.csv").
clearVecDraws().
local mydock is ship:dockingports[0].
local shipdock is 0.
function finddock {
parameter ports.
for port in ports {
if port:nodetype = mydock:nodetype
and not(port:haspartner) {
return port.
}
}
return 0.
}
if target:istype("Part") {
set shipdock to target.
} else {
set shipdock to finddock(target:dockingports).
}
print "My docks name is: " + mydock:name.
print "The ships dock name is: " + shipdock:name.
mydock:controlfrom().
lock steering to lookDirUp(-shipdock:portfacing:vector, up:forevector). // Ironically `up` in this case means sideways
// I hope I never have to touch vectors ever again
// PID controller part
// PID controller for y axis
local ykp is 1.
local yki is 0.
local ykd is 0.75.
local ypid is pidLoop(ykp, yki, ykd, -1, 1).
set ypid:setpoint to 0.
// PID controller for x axis
local xkp is 1.
local xki is 0.
local xkd is 0.75.
local xpid is pidLoop(xkp, xki, xkd, -1, 1).
set xpid:setpoint to 0.
lock rotatedDock to shipdock:nodePosition * -ship:facing.
local dockVector is vecDraw(
V(0,0,0),
{ return shipdock:nodePosition. },
rgb(1,0,1),
"Dock is this way",
1.0,
true,
0.2,
true,
true
).
function pidreadouts {
clearScreen.
print "Node y position: " + rotatedDock:y.
print "Node x position: " + rotatedDock:x.
print "Node z position: " + rotatedDock:z.
print "-------- y PID loop -------".
print "y PID loop error: " + ypid:error.
print "y PID loop output: " + ypid:output.
print "y P: " + ypid:pterm.
print "y I: " + ypid:iterm.
print "y D: " + ypid:dterm.
print "-------- x PID loop -------".
print "x PID loop error: " + xpid:error.
print "x PID loop output: " + xpid:output.
print "x P: " + xpid:pterm.
print "x I: " + xpid:iterm.
print "x D: " + xpid:dterm.
print "Sample: " + i.
}
function logpiddata {
parameter pid.
local elapsedtime is time:seconds - startime.
log elapsedtime + "," + pid:input + "," + pid:output to pid.csv.
}
local i is 0.
local samplecount is 2000.
local startime is time:seconds.
until i = samplecount {
if mydock:haspartner { break. }
ypid:update(time:seconds, rotatedDock:y).
xpid:update(time:seconds, rotatedDock:x).
set ship:control:starboard to -xpid:output.
set ship:control:top to -ypid:output.
logpiddata(xpid).
pidreadouts().
// This massive if statement checks if the two ships are lined up and their difference in velocity is
// less than 0.25 m/s
if abs(rotatedDock:y) < 0.1 and
abs(rotatedDock:x) < 0.1 and
abs(ship:velocity:orbit:mag - shipdock:ship:velocity:orbit:mag) < 0.25 {
set ship:control:fore to 0.1. // I hope this wont cause any issues.
}
// set i to i + 1.
wait 0.
}
set ship:control:starboard to 0.
set ship:control:top to 0.
set ship:control:fore to 0.
print "Done!".
clearVecDraws().