In Julia, I am receiving the error message:
MethodError: no method matching connect(::ODESystem, ::ODESystem)
Closest candidates are:
connect(::Base.LibuvStream, ::Any…)
Below is the code:
using CairoMakie, AlgebraOfGraphics
using ModelingToolkit, ModelingToolkitStandardLibrary
using DifferentialEquations
set_aog_theme!()
@parameters t
const B = ModelingToolkitStandardLibrary.Blocks
@component function Thruster(; name, thrust, lever_arm)
@named ctrl_input = B.RealInput()
@named torque_out = B.RealOutput()
sts = @variables u(t) M(t)
ps = @parameters thrust=thrust lever_arm=lever_arm
eqs = [
M ~ u * lever_arm * thrust,
u ~ ctrl_input.u,
torque_out.u ~ M
]
ODESystem(eqs, t, sts, ps; systems=[ctrl_input, torque_out], name = name)
end
@component function SimpleSpacecraftPlant(; name, J=100.0, ϕ0=0.0, ω0=0.0)
@named torque_in = B.RealInput()
@named ϕ_out = B.RealOutput()
@named ω_out = B.RealOutput()
sts = @variables ϕ(t)=ϕ0 ω(t)=ω0
ps = @parameters J=J ϕ0=ϕ0 ω0=ω0
D = Differential(t)
eqs = [
D(ϕ) ~ ω,
D(ω) ~ torque_in.u / J,
ϕ_out.u ~ ϕ,
ω_out.u ~ ω
]
compose(
ODESystem(eqs, t, sts, ps; name = name), torque_in, ϕ_out, ω_out
)
end
@component function BBController(; name, thruster_torque)
@named ref_signal = B.RealInput()
@named ctrl_output = B.RealOutput()
sts = @variables ref(t) u(t)
ps = @parameters thruster_torque=thruster_torque
eqs = [
u ~ thruster_torque*sign(ref),
ref ~ ref_signal.u,
ctrl_output.u ~ u
]
compose(
ODESystem(eqs, t, sts, ps; name = name), ref_signal, ctrl_output
)
end
setpoint = deg2rad(10)
@named θ_ref = B.Constant(k=setpoint)
J = 100
ωn = 0.5
ζ = 1.3
Kp = J*ωn^2
Kd = 2*J*ωn*ζ
@named ref_controller = B.LimPID(k=Kp, Td=Kd, Ti=1, gains=true)
F = 1
L = 1
@named thruster = Thruster(thrust=F, lever_arm=L)
@named plant = SimpleSpacecraftPlant(J=J)
F = 1
L = 1
@named bangbang_controller = BBController(thruster_torque=L*F)
function simulate_system(controller; tspan=[0.0, 120.0], solver_kwargs...)
system_eqs = [
connect(θ_ref.output, ref_controller.reference),
connect(ref_controller.ctr_output, controller.ref_signal),
connect(controller.ctrl_output, thruster.ctrl_input),
connect(thruster.torque_out, plant.torque_in),
connect(plant.ϕ_out, ref_controller.measurement),
]
@named model = ODESystem(
system_eqs, t; systems = [
θ_ref, ref_controller, thruster, plant, controller
]
)
sys = structural_simplify(model)
prob = ODEProblem(sys, [], tspan, [])
sol = solve(prob; solver_kwargs...)
end
tspan=[0.0, 180.0]
bb_sol = simulate_system(bangbang_controller; tspan=tspan)
times = 0:0.1:tspan[2]
interp = bb_sol(times)
fig1 = Figure()
ax11 = Axis(fig1[1,1], xlabel="Time (s)", ylabel="Angle (°)")
lines!(ax11, times, rad2deg.(interp[plant.ϕ]))
hlines!(ax11, [rad2deg(setpoint)], linestyle=:dash)
bracket!(120, 10, 180, 10, offset=5, text="Inset Area", style=:square, orientation=:down)
ax12 = Axis(fig1, bbox = BBox(400, 750, 200, 450))
lines!(ax12, 120:0.1:180, rad2deg.(bb_sol(120:0.1:180)[plant.ϕ]))
hlines!(ax12, [rad2deg(setpoint)], linestyle=:dash)
fig1
I believe the problem is to do with connectors. I tried looking up documentation but could not find which connector may not be working. Any suggestions?
New contributor
AndyDufresne is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.