After running this script I have a file libmixer.so.1.0.1 , but how do I now call
this function from Julia?
And is it necessary to create a shared object file, or can Julia call c functions
in a more easy way?
println("Testing mixer.c ...")
#=
struct actuator_output {
REAL32 front_motor;
REAL32 back_motor;
};
void mixer(REAL32 total_thrust, REAL32 pid_out, struct actuator_output *output);
=#
type Actuator_output
front_motor::Float32 # electrical generator power in kW
back_motor::Float32 # ratio of the drum diameter and the tether diameter
end
total_thrust = Float32(0.5)
pid_out = Float32(0.1)
output = Actuator_output(0.0, 0.0)
ccall(:mixer, "lib/libmixer.so", (total_thrust, pid_out, output))
println(output)
But executing it gives the following error:
ufechner@TUD277255:~/uavtalk$ ./test_mixer.sh
Testing mixer.c ...
ERROR: LoadError: TypeError: ccall: expected Type{T}, got String
in include_from_node1(::String) at ./loading.jl:488
in process_options(::Base.JLOptions) at ./client.jl:262
in _start() at ./client.jl:318
while loading /home/ufechner/uavtalk/scripts/test_mixer.jl, in expression starting on line 22
println("Testing mixer.c ...")
#=
struct actuator_output {
REAL32 front_motor;
REAL32 back_motor;
};
void mixer(REAL32 total_thrust, REAL32 pid_out, struct actuator_output *output);
=#
type Actuator_output
front_motor::Float32 # electrical generator power in kW
back_motor::Float32 # ratio of the drum diameter and the tether diameter
end
total_thrust = Float32(0.5)
pid_out = Float32(0.1)
output = Actuator_output(0.0, 0.0)
ccall((:mixer, "lib/libmixer.so"), Void, (Float32, Float32, Ptr(Actuator_output)), total_thrust, pid_out, output)
println(output)
Error message:
Testing mixer.c ...
ERROR: LoadError: MethodError: Cannot `convert` an object of type Type{Actuator_output} to an object of type Ptr{T}
This may have arisen from a call to the constructor Ptr{T}(...),
since type constructors fall back to convert methods.
in anonymous at ./<missing>:?
in include_from_node1(::String) at ./loading.jl:488
in process_options(::Base.JLOptions) at ./client.jl:262
in _start() at ./client.jl:318
while loading /home/ufechner/uavtalk/scripts/test_mixer.jl, in expression starting on line 22
You don’t need the Ref. This is already covered in the table entry for T* which stated that you just need to use Ref{T} as argument type and nothing else.
Thanks for the info.
For the record: Today I wrote 16 unit tests in Julia for my C code and found and fixed more than one non-trivial bug in my C code. Julia provides efficient means to write tests for C functions.
Uwe
Well, currently I am writing security critical, real-time flight control software for very small embedded systems. Julia is not a good choice for this kind of applications.