Skip to content

Commit 6f0bc72

Browse files
committed
fixes
1 parent 5622f04 commit 6f0bc72

File tree

1 file changed

+19
-45
lines changed

1 file changed

+19
-45
lines changed

test/runtests.jl

Lines changed: 19 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -282,8 +282,8 @@ end
282282

283283
end
284284

285-
@testset "TNon-static C-code" begin
286-
@info "Testing Tustin and non-static C-code"
285+
@testset "Non-static C-code" begin
286+
@info "Testing Non-static C-code"
287287

288288
# Test TransferFunction with static=false
289289
@syms J c
@@ -313,55 +313,41 @@ end
313313
path = mktempdir()
314314
filename = joinpath(path, "code_nonstatic.c")
315315
outname = joinpath(path, "test_nonstatic.so")
316-
317-
# Create wrapper C code that manages external state
318-
wrapper_code = """
319-
#include <string.h>
320-
$code
321-
322-
// Wrapper function that manages state for testing
323-
double transfer_function_wrapper(double ui, double T, double d, double w, double *u_state, double *y_state) {
324-
return transfer_function(u_state, y_state, ui, T, d, w);
325-
}
326-
"""
327-
328-
write(joinpath(path, filename), wrapper_code)
316+
write(joinpath(path, filename), code)
317+
# Compile the C code to a shared library
329318
run(`gcc $filename -lm -shared -o $outname`)
330319

331320
# Test function with external state management
332321
function c_lsim_nonstatic(u_signal, T, d, w)
333-
u_state = zeros(3)
334-
y_state = zeros(3)
322+
u_state = zeros(4)
323+
y_state = zeros(4)
335324
Libc.Libdl.dlopen(outname) do lib
336-
fn = Libc.Libdl.dlsym(lib, :transfer_function_wrapper)
325+
fn = Libc.Libdl.dlsym(lib, :transfer_function)
337326
map(u_signal) do u
338-
@ccall $(fn)(u::Float64, T::Float64, d::Float64, w::Float64,
339-
u_state::Ref{Cdouble}, y_state::Ref{Cdouble})::Float64
327+
@ccall $(fn)(u_state::Ref{Cdouble}, y_state::Ref{Cdouble}, u::Float64, T::Float64, d::Float64, w::Float64)::Float64
340328
end
341329
end
342330
end
343331

344332
# Test with two independent filters
345333
function c_lsim_dual(u_signal, T, d, w)
346-
u_state1 = zeros(3)
347-
y_state1 = zeros(3)
348-
u_state2 = zeros(3)
349-
y_state2 = zeros(3)
334+
u_state1 = zeros(4)
335+
y_state1 = zeros(4)
336+
u_state2 = zeros(4)
337+
y_state2 = zeros(4)
350338

351339
result1 = Float64[]
352340
result2 = Float64[]
353341

354342
Libc.Libdl.dlopen(outname) do lib
355-
fn = Libc.Libdl.dlsym(lib, :transfer_function_wrapper)
343+
fn = Libc.Libdl.dlsym(lib, :transfer_function)
356344
for u in u_signal
357345
# First filter
358-
r1 = @ccall $(fn)(u::Float64, T::Float64, d::Float64, w::Float64,
359-
u_state1::Ref{Cdouble}, y_state1::Ref{Cdouble})::Float64
346+
r1 = @ccall $(fn)(u_state1::Ref{Cdouble}, y_state1::Ref{Cdouble}, u::Float64, T::Float64, d::Float64, w::Float64)::Float64
360347
push!(result1, r1)
361348

362349
# Second filter with same input
363-
r2 = @ccall $(fn)(u::Float64, T::Float64, d::Float64, w::Float64,
364-
u_state2::Ref{Cdouble}, y_state2::Ref{Cdouble})::Float64
350+
r2 = @ccall $(fn)(u_state2::Ref{Cdouble}, y_state2::Ref{Cdouble}, u::Float64, T::Float64, d::Float64, w::Float64)::Float64
365351
push!(result2, r2)
366352
end
367353
end
@@ -387,32 +373,20 @@ end
387373
# Test StateSpace with static=false
388374
code_ss = SymbolicControlSystems.ccode(ss(Gd), static=false)
389375

390-
# Create wrapper for state-space
391-
wrapper_code_ss = """
392-
#include <string.h>
393-
$code_ss
394-
395-
// Wrapper that manages state
396-
void transfer_function_ss_wrapper(double *y, double u, double T, double d, double w, double *x_state) {
397-
transfer_function(x_state, y, u, T, d, w);
398-
}
399-
"""
400376

401377
filename_ss = joinpath(path, "code_nonstatic_ss.c")
402378
outname_ss = joinpath(path, "test_nonstatic_ss.so")
403-
write(joinpath(path, filename_ss), wrapper_code_ss)
379+
write(joinpath(path, filename_ss), code_ss)
404380
run(`gcc $filename_ss -lm -shared -o $outname_ss`)
405381

406382
function c_lsim_ss_nonstatic(u_signal, T, d, w)
407-
nx = ControlSystemsBase.nstates(ss(Gd_))
408-
x_state = zeros(nx)
383+
x_state = zeros(ss(Gd_).nx)
409384
y = zeros(1)
410385

411386
Libc.Libdl.dlopen(outname_ss) do lib
412-
fn = Libc.Libdl.dlsym(lib, :transfer_function_ss_wrapper)
387+
fn = Libc.Libdl.dlsym(lib, :transfer_function)
413388
map(u_signal) do u
414-
@ccall $(fn)(y::Ref{Cdouble}, u::Float64, T::Float64, d::Float64, w::Float64,
415-
x_state::Ref{Cdouble})::Cvoid
389+
@ccall $(fn)(x_state::Ref{Cdouble}, y::Ref{Cdouble}, u::Float64, T::Float64, d::Float64, w::Float64)::Cvoid
416390
y[]
417391
end
418392
end

0 commit comments

Comments
 (0)