FlightSims
FlightSims
is a general-purpose numerical simulator by defining nested environments.
This package can be used for any kind of numerical simulation with dynamical systems
although it was supposed to be dedicated only for flight simulations.
Features
Compatibility
- It is compatible with OrdinaryDiffEq.jl. Supporting full compatibility with DifferentialEquations.jl is not on the road map for now. If you want more functionality, please feel free to report an issue!
Nested Environments and Zoo
- One can generate user-defined nested environments (or, dynamical systems) for complex flight simulation.
Also, some predefined environments are provided for reusability (i.e., environment zoo).
Take a look at
src/environments
.
Utilities
- Some utilities are also provided, for example, calculation of polynomial basis and 3D rotation.
Take a look at
src/utils
.
APIs
Main APIs are provided in src/APIs
.
Make an environment
AbstractEnv
: an abstract type for user-defined and predefined environments. All environment structures should be sub-type ofAbstractEnv
.State(env::AbstractEnv)
: return a function that produces structured states.dynamics!(env::AbstractEnv)
anddynamics(env::AbstractEnv)
: return a function that maps in-place (recommended) and out-of-place dynamics (resp.), compatible withDifferentialEquations
. User can extend these methods or simply define other methods.apply_inputs(func; kwargs...)
: It is borrowed from an MRAC example. By using this, user can easily apply various kind of inputs into the dynamical system (environment).
Note that these interfaces are also provided for some integrated environments, e.g., State(system, controller)
.
Simulation and data saving & loading
sim
: returnprob::ODEProblem
andsol::ODESolution
.process
: processprob
andsol
to get simulation data.save
: saveenv
,prob
,sol
, and optionallyprocess
, in a.jld2
file.load
: loadenv
,prob
,sol
, and optionallyprocess
, from a.jld2
file.
Usage
Optimal Control and reinforcement learning
- For an example of infinite-horizon continuous-time linear quadratic regulator (LQR), see the following example code (
test/lqr.jl
).
using FlightSims
using LinearAlgebra
using ControlSystems: lqr
using Plots
function test()
# double integrator
n, m = 2, 1
A = [0 1;
0 0]
B = [0;
1]
Q = Matrix(I, n, n)
R = Matrix(I, m, m)
env = LinearSystemEnv(A, B, Q, R) # exported from FlightSims
K = lqr(A, B, Q, R)
x0 = State(env)([1, 2])
u_lqr(x, p, t) = -K*x
prob, sol = sim(
x0, # initial condition
apply_inputs(dynamics!(env); u=u_lqr); # dynamics with input of LQR
tf=10.0, # final time
)
df = process(env)(prob, sol; Δt=0.01) # DataFrame; `Δt` means data sampling period.
plot(df.times, hcat(df.states...)'; label=["x1" "x2"]) # Plots
end
- For an example of continuous-time value-iteration adaptive dynamic programming (CT-VI-ADP), take a look at
main/continuous_time_vi_adp.jl
.
Nonlinear control
- For an example of backstepping position tracking controller for quadcopters,
see the following example code (
main/backstepping_tracking.jl
).
using FlightSims
const FS = FlightSims
using UnPack, ComponentArrays
using Transducers
using Plots
function make_env()
multicopter = IslamQuadcopterEnv()
@unpack m, g = multicopter
x0_multicopter = State(multicopter)()
pos0 = copy(x0_multicopter.p)
vel0 = copy(x0_multicopter.v)
helixCG = FS.HelixCommandGenerator(pos0)
cg = command_generator(helixCG)
controller = BacksteppingPositionControllerEnv(m; x_cmd_func=cg)
x0_controller = State(controller)(pos0, m, g)
x0 = ComponentArray(multicopter=x0_multicopter, controller=x0_controller)
multicopter, controller, x0, cg
end
function main()
multicopter, controller, x0, cg = make_env()
prob, sol = sim(x0, dynamics!(multicopter, controller); tf=40.0)
df = process()(prob, sol; Δt=0.01)
ts = df.times
poss = df.states |> Map(state -> state.multicopter.p) |> collect
poss_true = ts |> Map(t -> cg(t)) |> collect
## plot
# time vs position
p_pos = plot(; title="position", legend=:outertopright)
plot!(p_pos, ts, hcat(poss...)'; label=["x" "y" "z"], color="red", ls=[:dash :dot :dashdot])
plot!(p_pos, ts, hcat(poss_true...)'; label=["x (true)" "y (true)" "z (true)"], color="black", ls=[:dash :dot :dashdot])
savefig(p_pos, "t_vs_pos.png")
# 3d traj
p_traj = plot3d(; title="position", legend=:outertopright)
plot!(p_traj, hcat(poss...)'[:, 1], hcat(poss...)'[:, 2], hcat(poss...)'[:, 3]; label="position", color="red")
plot!(p_traj, hcat(poss_true...)'[:, 1], hcat(poss_true...)'[:, 2], hcat(poss_true...)'[:, 3]; label="position (true)", color="black")
savefig(p_traj, "traj.png")
end
Scientific machine learning
- For an example usage of Flux.jl, see
main/flux_example.jl
. - For an example code of an imitation learning algorithm, behavioural cloning, see
main/behavioural_cloning.jl
.