@@ -12,8 +12,9 @@ maybe_apply(f, x, p, t) = f
1212
1313function apply_inputs (func; kwargs... )
1414 simfun (dx, x, p, t) = func (
15- dx, x, p, t; map (f-> maybe_apply (f, x, p, t), (; kwargs... ))... )
16- simfun (x, p, t) = func (x, p, t; map (f-> maybe_apply (f, x, p, t), (; kwargs... ))... )
15+ dx, x, p, t; map (f -> maybe_apply (f, x, p, t), (; kwargs... ))...
16+ )
17+ simfun (x, p, t) = func (x, p, t; map (f -> maybe_apply (f, x, p, t), (; kwargs... ))... )
1718 return simfun
1819end
1920
@@ -27,7 +28,7 @@ SISO_simulator(P::TransferFunction) = SISO_simulator(ss(P))
2728function SISO_simulator (P:: AbstractStateSpace )
2829 @unpack A, B, C, D = P
2930
30- if size (D)!= (1 , 1 )
31+ if size (D) != (1 , 1 )
3132 error (" This is not a SISO system" )
3233 end
3334
@@ -37,8 +38,8 @@ function SISO_simulator(P::AbstractStateSpace)
3738 DD = D[1 , 1 ]
3839
3940 return function sim! (dx, x, p, t; u = 0.0 )
40- dx .= A* x + BB* u
41- return CC* x + DD* u
41+ dx .= A * x + BB * u
42+ return CC * x + DD * u
4243 end
4344end
4445
@@ -60,13 +61,13 @@ nominal_sim! = SISO_simulator(nominal_plant)
6061
6162# To test robustness to uncertainty, we'll also include unmodeled dynamics with an entirely
6263# different structure than our nominal plant model.
63- unmodeled_dynamics = 229 / (s^ 2 + 30 s + 229 )
64+ unmodeled_dynamics = 229 / (s^ 2 + 30 s + 229 )
6465truth_plant = nominal_plant * unmodeled_dynamics
6566truth_sim! = SISO_simulator (truth_plant)
6667
6768# We'll make a first-order sensor as well so we can add noise to our measurement
6869τ = 0.005
69- sensor_plant = 1 / (τ* s + 1 )
70+ sensor_plant = 1 / (τ * s + 1 )
7071sensor_sim! = SISO_simulator (sensor_plant)
7172
7273# # Derivative functions
@@ -76,7 +77,7 @@ control(θ, w) = θ'w
7677
7778# We'll use a simple gradient descent adaptation law
7879function adapt! (Dθ, θ, γ, t; e, w)
79- Dθ .= - γ* e * w
80+ Dθ .= - γ * e * w
8081 return nothing
8182end
8283
@@ -98,7 +99,7 @@ function feedback_sys!(D, vars, p, t; ym, r, n)
9899 return yp
99100end
100101# Now the full system takes in an input signal `r`, feeds it through the reference model,
101- # and feeds the output of the reference model `ym` and the input signal to `feedback_sys`.
102+ # and feeds the output of the reference model `ym` and the input signal to `feedback_sys`.
102103function system! (D, vars, p, t; r = 0.0 , n = 0.0 )
103104 @unpack reference_model, feedback_loop = vars
104105
@@ -122,31 +123,33 @@ sensor_ic = zeros(1)
122123θ_est_ic = ComponentArray (θr = 0.0 , θy = 0.0 )
123124
124125# # Set up and run Simulation
125- function simulate (plant_fun, plant_ic;
126+ function simulate (
127+ plant_fun, plant_ic;
126128 tspan = tspan,
127129 input_signal = input_signal,
128130 adapt_gain = 1.5 ,
129131 noise_param = nothing ,
130- deterministic_noise = 0.0 )
132+ deterministic_noise = 0.0
133+ )
131134 noise (D, vars, p, t) = (D. feedback_loop. sensor[1 ] = noise_param)
132135
133136 # Truth control parameters
134- θ_truth = (r = bm/ bp, y = (ap- am)/ bp)
137+ θ_truth = (r = bm / bp, y = (ap - am) / bp)
135138
136139 # Initial conditions
137140 ic = ComponentArray (
138141 reference_model = ref_ic,
139142 feedback_loop = (
140143 parameter_estimates = θ_est_ic,
141144 sensor = sensor_ic,
142- plant_model = plant_ic
145+ plant_model = plant_ic,
143146 )
144147 )
145148
146149 # Model parameters
147150 p = (
148151 gamma = adapt_gain,
149- plant_fun = plant_fun
152+ plant_fun = plant_fun,
150153 )
151154
152155 sim_fun = apply_inputs (system!; r = input_signal, n = deterministic_noise)
@@ -172,9 +175,14 @@ function simulate(plant_fun, plant_ic;
172175 )
173176
174177 # Parameter estimate tracking
175- bottom = plot (sol,
176- vars = Symbol .([
177- " feedback_loop.parameter_estimates.θr" , " feedback_loop.parameter_estimates.θy" ]))
178+ bottom = plot (
179+ sol,
180+ vars = Symbol .(
181+ [
182+ " feedback_loop.parameter_estimates.θr" , " feedback_loop.parameter_estimates.θy" ,
183+ ]
184+ )
185+ )
178186 plot! (
179187 bottom,
180188 [tspan... ], [θ_truth. r θ_truth. y; θ_truth. r θ_truth. y],
@@ -184,8 +192,10 @@ function simulate(plant_fun, plant_ic;
184192 )
185193
186194 # Combine both plots
187- plot (top, bottom, layout = (2 , 1 ), size = (800 , 800 ))
195+ return plot (top, bottom, layout = (2 , 1 ), size = (800 , 800 ))
188196end
189197
190- simulate (truth_sim!, truth_ic; input_signal = 2.0 ,
191- deterministic_noise = (x, p, t)-> 0.5 sin (16.1 t), noise_param = nothing )
198+ simulate (
199+ truth_sim!, truth_ic; input_signal = 2.0 ,
200+ deterministic_noise = (x, p, t) -> 0.5 sin (16.1 t), noise_param = nothing
201+ )
0 commit comments