-
Notifications
You must be signed in to change notification settings - Fork 0
/
jsat.jl
385 lines (322 loc) · 10.6 KB
/
jsat.jl
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
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
using ModelingToolkit, DifferentialEquations, LinearAlgebra, IfElse, UUIDs, Distributions, Makie, WGLMakie
using Symbolics: scalarize
includet("quaternion.jl")
WGLMakie.activate!()
@variables t
D = Differential(t)
"""
Component
...
# Abstract type for defining spacecraft components
...
"""
abstract type Component end
"""
Body
...
# Arguments
- `J::Matrix{Float64,3,3}`: Inertia tensor (J to not conflict with identity matrix function).
- `Φ0::Vector{Float64,3}`: Initial euler angles
- `ω0::Vector{Float64,3}`: Initial angular velocity
...
"""
Base.@kwdef mutable struct Body <: Component
name::Symbol
J::Matrix{Float64}
q::Vector{Float64}
ω::Vector{Float64}
r::Vector{Float64}
v::Vector{Float64}
sys::Union{ODESystem,Nothing} = nothing
end
function make(C::Body)
@variables Φ(t)[1:3] = zeros(3) [irreducible = true]#angles
@variables q(t)[1:4] = C.q #quaternion
@variables ω(t)[1:3] = C.ω #rates
@variables Hb(t)[1:3] = C.J * C.ω #body momentum
@variables Hi(t)[1:3] = zeros(3) #internal momentum
@variables Hs(t)[1:3] = zeros(3) #system momentum
@variables T(t)[1:3] = zeros(3)#torque
@variables r(t)[1:3] = C.r
@variables v(t)[1:3] = C.v
@variables g(t)[1:3] = zeros(3)
@parameters J[1:3, 1:3] = C.J #inertia
Q = [
q[4] -q[3] q[2]
q[3] q[4] -q[1]
-q[2] q[1] q[4]
-q[1] -q[2] -q[3]
]
#note: DAE form used so we can solve the unsimplified system
eqs = [
zeros(3) .~ ω - inv(scalarize(J)) * Hb
zeros(3) .~ Φ - qtoe(q)
zeros(3) .~ Hs - (Hb + Hi)
D.(q) .~ 0.5 * Q * ω
D.(Hb) .~ T - cross(ω, Hs)
D.(r) .~ v
D.(v) .~ g #+F from externals when you can
]
C.sys = ODESystem(eqs, t, name=C.name)
return C.sys
end
"""
Thruster
...
# Arguments
- `F::Float64`: Thruster force magnitude
- `r::Vector{Float64,3}`: Position vector to thruster in parent frame
- `R::Vector{Float64,3,3}`: Thruster line of action rotation in parent frame
...
"""
Base.@kwdef mutable struct Thruster <: Component
name::Symbol
F::Float64
r::Vector{Float64}
R::Matrix{Float64}
sys::Union{ODESystem,Nothing} = nothing
end
function make(C::Thruster)
@variables u(t) = 1
@variables T(t)[1:3] = zeros(3)
@parameters F = C.F
@parameters r[1:3] = C.r
@parameters R[1:3, 1:3] = C.R
F̄ = [0, 0, F]#assumed out thruster +z-axis
eqs = zeros(3) .~ T - u * (r × (scalarize(R) * F̄))
C.sys = ODESystem(eqs, t, name=C.name)
return C.sys
end
"""
ReactionWheel
...
# Arguments
- `J::Float64`: Inertia moment of inertia (J to not conflict with identity matrix function).
- `kt::Float64`: Motor constant
- `ω::Float64`: Initial wheel angular velocity
...
"""
Base.@kwdef mutable struct ReactionWheel <: Component
name::Symbol
J::Float64
kt::Float64
a::Vector{Float64}
ω::Float64
sys::Union{ODESystem,Nothing} = nothing
end
function make(C::ReactionWheel)
@variables u(t) = 0#input current command
@variables Tm(t) = 0 #wheel torque in wheel frame
@variables T(t)[1:3] = zeros(3)#wheel reaction torque in body frame
@variables H(t)[1:3] = zeros(3) #output internal momentum
@variables ω(t) = C.ω #wheel speed
@parameters a[1:3] = normalize(C.a) #axis of rotation
@parameters kt = C.kt #current to torque motor gain
@parameters J = C.J #wheel inertia
eqs = [
0 ~ Tm - kt * u
D.(ω) .~ Tm ./ J
zeros(3) .~ T - Tm * a
zeros(3) .~ H - J * ω * a
]
C.sys = ODESystem(eqs, t, name=C.name)
return C.sys
end
"""
RateGyro
...
# Arguments
- `σ::Float64`: RateGyro random noise generator power level
...
"""
Base.@kwdef mutable struct RateGyro <: Component
name::Symbol
σ::Float64 # noise power
sys::Union{ODESystem,Nothing} = nothing
#r::Vector{Float64} # position vector to rategyro in body frame, used to induce wxr errors (to be added)
end
function make(C::RateGyro)
@variables ω_truth(t)[1:3] = zeros(3)
@variables ω_sensed(t)[1:3] = zeros(3) [irreducible = true]
eqs = zeros(3) .~ ω_sensed - ω_truth # noise added through SDE
C.sys = ODESystem(eqs, t, name=C.name)
return C.sys
end
"""
Controller
...
# Arguments
This is just a FSW controller interface. The equations should set the FSW states to constant (D.(x) .~ 0)
Value of the controller should be set by callback in the ODEProblem
...
"""
Base.@kwdef mutable struct Controller <: Component
name::Symbol
sys::Union{ODESystem,Nothing} = nothing
end
function make(C::Controller)
@variables rw_commands(t)[1:3] = zeros(3)
@variables thruster_commands(t)[1:4] = zeros(4)
eqs = [
D.(rw_commands) .~ zeros(3)
D.(thruster_commands) .~ zeros(4)
]
C.sys = ODESystem(eqs, t, name=C.name)
return C.sys
end
abstract type Gravity end
"""
TwoBody
...
# Arguments
Can specify mu or R if needed but defaults to Vallado Earth with no input
...
"""
Base.@kwdef mutable struct TwoBody <: Gravity
name::Symbol = :TwoBody
μ::Float64 = 3.986004415e14
R::Float64 = 6378136.3
sys::Union{ODESystem,Nothing} = nothing
end
function make(C::TwoBody)
@variables r(t)[1:3] = ones(3) [input = true] #dont make zeros or instability
@variables g(t)[1:3] = zeros(3) [output = true]
@parameters μ = C.μ
rmag = sqrt(dot(r,r))
eqs = zeros(3) .~ g - (-μ*r/norm(r)^3)
C.sys = ODESystem(scalarize(eqs), t, name=C.name)
return C.sys
end
"""
Spacecraft
...
# Arguments
- `Components::Vector{Component}`: Vector of abstract components
...
"""
Base.@kwdef mutable struct Spacecraft
name::Symbol
body::Body
thrusters::Union{Vector{Thruster},Nothing} = nothing
reactionwheels::Union{Vector{ReactionWheel},Nothing} = nothing
iru::RateGyro
controller::Controller
gravity::Gravity
full_sys::Union{ODESystem,Nothing} = nothing
sys::Union{ODESystem,Nothing} = nothing #structural_simplify'd sys
end
function make!(C::Spacecraft)
body = make(C.body)
thrusters = make.(C.thrusters)
reactionwheels = make.(C.reactionwheels)
rategyro = make(C.iru)
controller = make(C.controller)
gravity = make(C.gravity)
eqs = [
zeros(3) .~ body.T - (sum(map(x -> x.T, thrusters)) - sum(map(x -> x.T, reactionwheels)))
zeros(3) .~ body.Hi - sum(map(x -> x.H, reactionwheels))
zeros(3) .~ rategyro.ω_truth - body.ω
zeros(length(reactionwheels)) .~ controller.rw_commands - map(x -> x.u, reactionwheels)
zeros(length(thrusters)) .~ controller.thruster_commands - map(x -> x.u, thrusters)
zeros(3) .~ body.r - gravity.r
zeros(3) .~ body.g - gravity.g
]
C.full_sys = compose(ODESystem([eqs...;], t, name=C.name), body, thrusters..., reactionwheels..., rategyro, controller,gravity)
C.sys = structural_simplify(C.full_sys)
end
function test(sys)
prob = ODEProblem(sys, [Disp], (0.0, 100.0))
solve(prob, Rodas4(), dt=0.1, adaptive=false, progress=true)
end
indexof(sym, syms) = findfirst(isequal(sym), syms)
function simulate(sc, tspan; dispersions=[], nruns=1)
i_Φ = [
indexof(Symbol("getindex(body₊Φ(t), 1)"), Symbol.(states(sc.sys))),
indexof(Symbol("getindex(body₊Φ(t), 2)"), Symbol.(states(sc.sys))),
indexof(Symbol("getindex(body₊Φ(t), 3)"), Symbol.(states(sc.sys))),
]
i_ω = [
indexof(Symbol("getindex(iru₊ω_sensed(t), 1)"), Symbol.(states(sc.sys))),
indexof(Symbol("getindex(iru₊ω_sensed(t), 2)"), Symbol.(states(sc.sys))),
indexof(Symbol("getindex(iru₊ω_sensed(t), 3)"), Symbol.(states(sc.sys)))
]
i_rw = [
indexof(Symbol("getindex(controller₊rw_commands(t), 1)"), Symbol.(states(sc.sys))),
indexof(Symbol("getindex(controller₊rw_commands(t), 2)"), Symbol.(states(sc.sys))),
indexof(Symbol("getindex(controller₊rw_commands(t), 3)"), Symbol.(states(sc.sys))),
]
""" FSW Callback """
function fsw!(integrator)
#DI
ω = @view integrator.u[i_ω]
Φ = @view integrator.u[i_Φ]
#AC
Φr = [pi / 2, 0, 0]
ωr = [0, 0, 0]
kp = 10
kd = 1000
rw_u = -kp * (Φr - Φ) - kd * (ωr - ω)
#DO
integrator.u[i_rw] .= rw_u
end
model_cb = PeriodicCallback(fsw!, 0.1, save_positions=(false, false))
prob = ODEProblem(sc.sys, [], tspan, callback=model_cb)
sol = solve(prob, Rodas4(), progress=true)
# monte carlo
if nruns > 1
# draw dispersions
dispersion_keys = keys(dispersions)
dispersion_values = rand.(getindex.(Ref(dispersions), dispersion_keys), nruns)
# update problem
function prob_func(prob, i, repeat)
print("Simulating run $i\n")
these_dispersions = Dict(dispersion_keys .=> map(x -> x[i], dispersion_values))
prob = remake(prob,
u0 = update_u0(these_dispersions, sc.sys),
p = update_p(these_dispersions, sc.sys)
)
end
eprob = EnsembleProblem(prob, prob_func=prob_func)
sim = solve(eprob, trajectories=nruns)
else
sim = nothing
end
(sol,sim)
end
function plot3(d::Vector{Vector{Float64}})
n = length(d[1])
f = lines(map(x -> x[1], d))
if n > 1
for i = 2:n
lines!(map(x -> x[i], d))
end
end
return f
end
function mcplot(f,sol::ODESolution,sims::EnsembleSolution,index = nothing)
!isnothing(index) ? f2 = x -> map(y->y[index],f(x)) : f2 = x -> f(x)
fig = Figure()
ax = Axis(fig[1,1])
for sim in sims
lines!(ax,sim.t,f2(sim),color = (:gray, 0.2),linewidth = 3)
end
lines!(ax,sol.t,f2(sol),color = :red, linewidth = 3)
fig
end
includet("animators.jl")
function update_u0(x_updates, sys)
defs = ModelingToolkit.get_defaults(sys)
x = states(sys)
def_x_values = [defs[i] for i in x]
def_x = Dict(x .=> def_x_values)
new_x = merge(def_x, x_updates)
ModelingToolkit.varmap_to_vars(new_x, x)
end
function update_p(p_updates, sys)
defs = ModelingToolkit.get_defaults(sys)
p = parameters(sys)
def_p_values = [defs[i] for i in p]
def_p = Dict(p .=> def_p_values)
new_p = merge(def_p, p_updates)
ModelingToolkit.varmap_to_vars(new_p, p)
end