|
| 1 | +#include <stdio.h> |
| 2 | +#include <stdlib.h> |
| 3 | +#include <dlfcn.h> |
| 4 | +// #include <julia.h> |
| 5 | + |
| 6 | +// Path to julia binary folder |
| 7 | +#define JULIA_PATH "/home/fredrikb/repos/julia/usr/bin/" // NOTE: modify this path |
| 8 | + |
| 9 | +// Path to juliac compiled shared object file |
| 10 | +#define LIB_PATH "/home/fredrikb/.julia/dev/DiscretePIDs/examples/juliac/juliac_pid.so" // NOTE: modify this path |
| 11 | + |
| 12 | + |
| 13 | + |
| 14 | +// Define the types of the julia @ccallable functions |
| 15 | +typedef void (*jl_init_with_image_t)(const char *bindir, const char *sysimage); |
| 16 | +typedef double (*calculate_control_t)(double r, double y, double uff); |
| 17 | +typedef void (*set_K_t)(double K, double r, double y); |
| 18 | +typedef void (*set_Ti_t)(double Ti); |
| 19 | +typedef void (*set_Td_t)(double Td); |
| 20 | +typedef void (*reset_state_t)(); |
| 21 | + |
| 22 | + |
| 23 | +int main() { |
| 24 | + |
| 25 | + // Load the shared library |
| 26 | + printf("Loading juliac_pid.so\n"); |
| 27 | + void *lib_handle = dlopen(LIB_PATH, RTLD_LAZY); |
| 28 | + if (!lib_handle) { |
| 29 | + fprintf(stderr, "Error: Unable to load library %s\n", dlerror()); |
| 30 | + exit(EXIT_FAILURE); |
| 31 | + } |
| 32 | + printf("Loaded juliac_pid.so\n"); |
| 33 | + |
| 34 | + // Locate the julia functions function |
| 35 | + printf("Finding symbols\n"); |
| 36 | + jl_init_with_image_t jl_init_with_image = (jl_init_with_image_t)dlsym(lib_handle, "jl_init_with_image"); |
| 37 | + |
| 38 | + calculate_control_t calculate_control = (calculate_control_t)dlsym(lib_handle, "calculate_control!"); |
| 39 | + set_K_t set_K = (set_K_t)dlsym(lib_handle, "set_K!"); |
| 40 | + set_Ti_t set_Ti = (set_Ti_t)dlsym(lib_handle, "set_Ti!"); |
| 41 | + set_Td_t set_Td = (set_Td_t)dlsym(lib_handle, "set_Td!"); |
| 42 | + reset_state_t reset_state = (reset_state_t)dlsym(lib_handle, "reset_state!"); |
| 43 | + |
| 44 | + |
| 45 | + if (jl_init_with_image == NULL || calculate_control == NULL) { |
| 46 | + char *error = dlerror(); |
| 47 | + fprintf(stderr, "Error: Unable to find symbol: %s\n", error); |
| 48 | + exit(EXIT_FAILURE); |
| 49 | + } |
| 50 | + printf("Found all symbols!\n"); |
| 51 | + |
| 52 | + // Init julia |
| 53 | + jl_init_with_image(JULIA_PATH, LIB_PATH); |
| 54 | + |
| 55 | + // Trivial test program that computes a few control outputs and modifies K |
| 56 | + double r = 1.0, y = 0.0, uff = 0.0; |
| 57 | + double result = calculate_control(r, y, uff); |
| 58 | + printf("calculate_control! returned: %f\n", result); |
| 59 | + result = calculate_control(r, y, uff); |
| 60 | + printf("calculate_control! returned: %f\n", result); |
| 61 | + set_K(0.0, r, y); |
| 62 | + for (int i = 0; i < 3; ++i) { |
| 63 | + result = calculate_control(r, y, uff); |
| 64 | + printf("calculate_control! returned: %f\n", result); |
| 65 | + } |
| 66 | + |
| 67 | + // jl_atexit_hook(0); |
| 68 | + return 0; |
| 69 | +} |
| 70 | + |
| 71 | + |
| 72 | +// Compile this C program using a command like the one above, modified to suit your paths |
| 73 | +// export LD_LIBRARY_PATH=/home/fredrikb/repos/julia/usr/lib:$LD_LIBRARY_PATH |
| 74 | +// gcc -o pid_program test_juliac_pid.c -I /home/fredrikb/repos/julia/usr/include/julia -L/home/fredrikb/repos/julia/usr/lib -ljulia -ldl |
0 commit comments