This repository has been archived by the owner on Mar 14, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 11
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
229 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,97 @@ | ||
# TODO: DOC | ||
function propagate_timestep( | ||
ac, controls, env, state, aerostate, tspan, dynamic_system, solver; | ||
solver_options=Dict(), | ||
) | ||
|
||
# Controls, Forces, moments, mass, inertia and gyro effects are assumed | ||
# to be constant for the time step | ||
ac = calculate_aircraft(ac, controls, aerostate, state, get_gravity(env)) | ||
|
||
# Unpack properties to match OrdinaryDiffEq interface | ||
ac_mass_props = get_mass_props(ac) | ||
ac_pfm = get_pfm(ac) | ||
h = get_gyro_effects(get_propulsion(ac)) | ||
|
||
mass = get_mass(ac_mass_props) | ||
inertia = get_inertia(ac_mass_props) | ||
forces = get_forces(ac_pfm) | ||
moments = get_moments(ac_pfm) | ||
|
||
p = [mass, inertia, forces, moments, h] | ||
|
||
x0 = get_x(dynamic_system, state) | ||
|
||
# Create ODE problem | ||
prob = ODEProblem( | ||
get_state_equation_ode_wrapper(dynamic_system), | ||
x0, | ||
tspan, | ||
p, | ||
) | ||
sol = solve(prob, solver, save_everystep = false, save_start = false; solver_options...) | ||
|
||
if ~(sol.retcode == :Success) | ||
error("Propagate time step did not converge from $(tspan[1]) to $(tspan[2])") | ||
end | ||
|
||
dynamic_system.x[:] = sol.u[1] | ||
# TODO: is it possible to obtain this from solve? | ||
dynamic_system.x_dot[:] = get_state_equation(dynamic_system)(sol.u[1], p...) | ||
|
||
# Obtain state from sol | ||
state = convert(state, dynamic_system) | ||
|
||
# As the position has changed, environment changes (must be updated) | ||
# and aircraft changes (must be updated) | ||
# Calculate environment and aerostate for the current state | ||
env = calculate_environment(env, get_position(state)) | ||
aerostate = AeroState(state, env) | ||
# Calculate aircraft | ||
ac = calculate_aircraft(ac, controls, aerostate, state, get_gravity(env)) | ||
|
||
return sol, ac, env, state, aerostate | ||
end | ||
|
||
|
||
|
||
function propagate( | ||
ac, env, state, controls_stream, tini, tfin, dt, dynamic_system, solver; | ||
solver_options=Dict() | ||
) | ||
|
||
# Prepare initial time step | ||
t0 = tini | ||
t1 = t0 + dt | ||
|
||
# Create results object | ||
aerostate = AeroState(state, env) | ||
results = ResultsStore(tini, ac, env, state, aerostate) | ||
|
||
while t1 < tfin + 0.5*dt | ||
# Get controls | ||
controls = get_controls(controls_stream, t0) | ||
|
||
tspan = (t0, t1) | ||
sol, ac, env, state, aerostate = propagate_timestep( | ||
ac, | ||
controls, | ||
env, | ||
state, | ||
aerostate, | ||
tspan, | ||
dynamic_system, | ||
solver; | ||
solver_options=solver_options, | ||
) | ||
|
||
# Store | ||
push!(results, t1, ac, env, state, aerostate) | ||
|
||
# Prepare for next time step | ||
t0 = t1 | ||
t1 = t1 + dt | ||
end | ||
|
||
return results | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,108 @@ | ||
using LinearAlgebra | ||
using OrdinaryDiffEq | ||
using FlightMechanics.Aircrafts | ||
using FlightMechanics.Models | ||
|
||
|
||
# Choose aircraft | ||
ac = F16() | ||
# Choose Flight Control System | ||
fcs = F16FCS() | ||
# Choose dynamic system | ||
dynamic_system = SixDOFEulerFixedMass() | ||
# Choose solver | ||
solver = RK4() | ||
solver_options = Dict() | ||
|
||
# Initilizations | ||
h = 1000.0 * M2FT | ||
tas = 250 * KT2MS | ||
psi = 0.0 # rad | ||
gamma = 0.0 | ||
turn_rate = 0.0 | ||
|
||
pos = EarthPosition(0, 0, -h) | ||
|
||
# Initilize environment | ||
env = Environment(pos, atmos = "ISAF16", wind = "NoWind", grav = "const") | ||
|
||
att = Attitude(-1, 0.1, 0.0) | ||
|
||
# Initilize FCS | ||
controls_0 = StickPedalsLeverControls(0.0, 0.5, 0.5, 0.8) | ||
|
||
# Trim a/c | ||
ac, aerostate, state, controls = steady_state_trim( | ||
ac, | ||
controls_0, | ||
env, | ||
tas, | ||
pos, | ||
psi, | ||
gamma, | ||
turn_rate, | ||
0.0, | ||
0.0, | ||
show_trace = false, | ||
) | ||
|
||
controls_stream = convert(ControlsStream, controls) | ||
|
||
times_to_test = [0.1, 1.0, 10.0, 100.0] # seconds | ||
|
||
@testset "Simulation time $t s" for t=times_to_test | ||
tini = 0.0 | ||
tfin = t | ||
dt = 0.01 | ||
|
||
results = propagate( | ||
ac, | ||
env, | ||
state, | ||
controls_stream, | ||
tini, | ||
tfin, | ||
dt, | ||
dynamic_system, | ||
solver; | ||
solver_options=solver_options | ||
) | ||
|
||
@test isapprox( | ||
results.state[1].attitude, | ||
results.state[end].attitude, | ||
) | ||
|
||
@test isapprox( | ||
results.state[1].velocity, | ||
results.state[end].velocity, | ||
) | ||
|
||
@test isapprox( | ||
results.state[1].angular_velocity, | ||
results.state[end].angular_velocity, | ||
atol=1e-11 | ||
) | ||
|
||
@test isapprox( | ||
results.state[1].acceleration, | ||
results.state[end].acceleration, | ||
atol=1e-10 | ||
) | ||
|
||
@test isapprox( | ||
results.state[1].angular_acceleration, | ||
results.state[end].angular_acceleration, | ||
atol=1e-12 | ||
) | ||
|
||
@test isapprox( | ||
get_height(results.state[1]), | ||
get_height(results.state[end]), | ||
) | ||
|
||
@test isapprox( | ||
norm(get_xyz_earth(results.state[1])[1:2] - get_xyz_earth(results.state[end])[1:2]), | ||
tas * (tfin - tini), | ||
) | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters