@@ -225,6 +225,113 @@ function sim_turn!(
225225end
226226
227227
228+ """
229+ sim_reposition!(sam; dt, total_time, reposition_interval_s, target_elevation_deg,
230+ target_azimuth_deg, prn)
231+
232+ Run a simulation that periodically resets the kite's elevation and azimuth.
233+
234+ This function simulates the AWE model and, at a specified time interval, calls
235+ `reposition!` to reposition the kite to a target elevation and azimuth. It logs
236+ the entire simulation and returns a `SysLog`.
237+
238+ # Arguments
239+ - `sam::SymbolicAWEModel`: Initialized AWE model.
240+
241+ # Keywords
242+ - `dt::Float64`: Time step [s]. Defaults to `1/sam.set.sample_freq`.
243+ - `total_time::Float64`: Total simulation duration [s]. Defaults to 20.0.
244+ - `reposition_interval_s::Float64`: The interval in seconds at which to reset the pose. Defaults to 5.0.
245+ - `target_elevation::Float64`: The target elevation in rad for repositioning. Defaults to deg2rad(45.0).
246+ - `target_azimuth::Float64`: The target azimuth in rad for repositioning. Defaults to 0.0.
247+ - `target_heading::Float64`: The target heading in rad for repositioning. Defaults to 0.0.
248+ - `prn::Bool`: If true, prints status messages during the simulation. Defaults to true.
249+
250+ # Returns
251+ - `SysLog`: A log of the complete simulation.
252+ """
253+ function sim_reposition!(
254+ sam:: SymbolicAWEModel ;
255+ dt= 1 / sam. set. sample_freq,
256+ total_time= 20.0 ,
257+ reposition_interval_s= 5.0 ,
258+ target_elevation= deg2rad(45.0 ),
259+ target_azimuth= 0.0 ,
260+ target_heading= 0.0 ,
261+ prn= true
262+ )
263+ # 1. --- Initialization ---
264+ sys_struct = sam. sys_struct
265+ steps = Int(round(total_time / dt))
266+ reposition_interval_steps = Int(round(reposition_interval_s / dt))
267+ set_values = zeros(Float64, steps, length(sys_struct. winches))
268+ vsm_interval = 1 ÷ dt
269+
270+ logger = Logger(length(sys_struct. points), steps)
271+ sys_state = SysState(sam)
272+
273+ if prn
274+ println(" --- Starting simulation with periodic repositioning ---" )
275+ println(" Total time: $(total_time) s, Reposition interval: $(reposition_interval_s) s" )
276+ end
277+
278+ # 2. --- Simulation Loop ---
279+ time = @elapsed for step in 1 : steps
280+ t = (step- 1 ) * dt
281+
282+ # Hold the kite in place by countering the tether forces with winch torques
283+ set_values[step, :] = - sam. set. drum_radius .* [norm(winch. force) for winch in sys_struct. winches]
284+
285+ # --- Repositioning Logic ---
286+ if step > 1 && (step - 1 ) % reposition_interval_steps == 0
287+ if prn
288+ println(" \n >>> Time: $(round(t, digits= 2 )) s. Repositioning kite..." )
289+ println(" >>> Target Elevation: $(rad2deg(target_elevation)) °," *
290+ " Target Azimuth: $(rad2deg(target_azimuth)) °" )
291+ end
292+
293+ # Update the transform with the new target pose
294+ sys_struct. transforms[1 ]. elevation = target_elevation
295+ sys_struct. transforms[1 ]. azimuth = target_azimuth
296+ sys_struct. transforms[1 ]. heading = target_heading - sys_struct. wings[1 ]. heading
297+
298+ # Apply the transformation without changing velocities
299+ SymbolicAWEModels. reposition!(sys_struct. transforms, sys_struct)
300+
301+ # Reinitialize the solver to handle the state discontinuity
302+ SymbolicAWEModels. reinit!(sam, sam. prob, FBDF())
303+
304+ if prn
305+ # Verify the new pose after one step
306+ next_step!(sam; dt= dt, set_values= set_values[step, :], vsm_interval)
307+ updated_elevation_deg = rad2deg(sys_struct. wings[1 ]. elevation)
308+ println(" >>> Pose updated. New Elevation is now: " *
309+ " $(round(updated_elevation_deg, digits= 2 )) degrees.\n " )
310+ end
311+ else
312+ # --- Normal simulation step ---
313+ next_step!(sam; dt= dt, set_values= set_values[step, :], vsm_interval)
314+ end
315+
316+ # Log the state at the current time step
317+ update_sys_state!(sys_state, sam)
318+ sys_state. time = t
319+ log!(logger, sys_state)
320+ end
321+
322+ if prn
323+ println(" --- Simulation Finished ---" )
324+ println(" Runtime: $time " )
325+ println(" Times realtime: $(dt* steps/ time) " )
326+ end
327+
328+ # Save and return the log
329+ mkpath(get_data_path())
330+ save_log(logger, " tmp_reposition_run" )
331+ return load_log(" tmp_reposition_run" )
332+ end
333+
334+
228335"""
229336 SysState(y::AbstractVector, sam::SymbolicAWEModel, t::Real; zoom=1.0)
230337
0 commit comments