|
1 | 1 | #include <stdio.h>
|
2 | 2 | #include <stdlib.h>
|
3 | 3 | #include <dlfcn.h>
|
4 |
| -#include <julia.h> |
| 4 | +// #include <julia.h> |
5 | 5 |
|
6 |
| -// Define the type of the function calculate_control |
| 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); |
7 | 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)(); |
8 | 21 |
|
9 |
| -int main() { |
10 |
| - jl_init(); |
11 | 22 |
|
12 |
| - const char *lib_path = "/home/fredrikb/.julia/dev/DiscretePIDs/examples/juliac/juliac_pid.so"; |
| 23 | +int main() { |
13 | 24 |
|
14 | 25 | // Load the shared library
|
15 | 26 | printf("Loading juliac_pid.so\n");
|
16 |
| - void *lib_handle = dlopen(lib_path, RTLD_LAZY); |
| 27 | + void *lib_handle = dlopen(LIB_PATH, RTLD_LAZY); |
17 | 28 | if (!lib_handle) {
|
18 | 29 | fprintf(stderr, "Error: Unable to load library %s\n", dlerror());
|
19 | 30 | exit(EXIT_FAILURE);
|
20 | 31 | }
|
21 | 32 | printf("Loaded juliac_pid.so\n");
|
22 | 33 |
|
23 |
| - // Locate the calculate_control function |
24 |
| - printf("Finding calculate_control!\n"); |
| 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 | + |
25 | 38 | calculate_control_t calculate_control = (calculate_control_t)dlsym(lib_handle, "calculate_control!");
|
26 |
| - char *error = dlerror(); |
27 |
| - if (error != NULL) { |
28 |
| - fprintf(stderr, "Error: Unable to find symbol calculate_control!: %s\n", error); |
29 |
| - dlclose(lib_handle); |
| 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); |
30 | 48 | exit(EXIT_FAILURE);
|
31 | 49 | }
|
32 |
| - printf("Found calculate_control!\n"); |
| 50 | + printf("Found all symbols!\n"); |
| 51 | + |
| 52 | + // Init julia |
| 53 | + jl_init_with_image(JULIA_PATH, LIB_PATH); |
33 | 54 |
|
34 |
| - // Call the function |
35 |
| - double r = 0.0, y = 0.0, uff = 0.0; |
| 55 | + // Trivial test program that computes a few control outputs and modifies K |
| 56 | + double r = 1.0, y = 0.0, uff = 0.0; |
36 | 57 | double result = calculate_control(r, y, uff);
|
37 | 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 | + } |
38 | 66 |
|
39 |
| - // Close the library |
40 |
| - dlclose(lib_handle); |
41 |
| - |
42 |
| - jl_atexit_hook(0); |
| 67 | + // jl_atexit_hook(0); |
43 | 68 | return 0;
|
44 | 69 | }
|
45 | 70 |
|
46 | 71 |
|
| 72 | +// Compile this C program using a command like the one above, modified to suit your paths |
47 | 73 | // export LD_LIBRARY_PATH=/home/fredrikb/repos/julia/usr/lib:$LD_LIBRARY_PATH
|
48 | 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