Skip to content

Commit 46b5bcd

Browse files
committed
move typedefs into header file
1 parent b82044d commit 46b5bcd

File tree

3 files changed

+32
-18
lines changed

3 files changed

+32
-18
lines changed

README.md

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -196,6 +196,9 @@ plot([res, res_fp], plotu=true, lab=["Float64" "" string(T) ""]); ylabel!("u + d
196196
The fixed-point controller behaves roughly the same in this case, but artifacts are clearly visible. If the number of bits used for the fractional part is decreased, the controller will start to misbehave.
197197

198198
## Compilation using JuliaC
199+
> [!IMPORTANT]
200+
> At the time of writing, this requires a nightly version of julia
201+
199202
The file `examples/juliac/juliac_pid.jl` contains a JuliaC-compatible interface that can be compiled into a C-callable shared library using JuliaC. To compile the file, run the following from the `examples/juliac` folder:
200203
```bash
201204
julia +nightly --project <PATH_TO_JULIA_REPO>/julia/contrib/juliac.jl --output-lib juliac_pid --experimental --trim=unsafe-warn --compile-ccallable juliac_pid.jl
@@ -222,7 +225,9 @@ calculate_control! returned: 3.000000
222225
calculate_control! returned: 3.000000
223226
calculate_control! returned: 3.000000
224227
```
225-
At the time of writing, this requires a nightly version of julia
228+
229+
230+
226231

227232

228233
## See also

examples/juliac/juliac_pid.h

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
#ifndef JULIAC_PID_H
2+
#define JULIAC_PID_H
3+
4+
#ifdef __cplusplus
5+
extern "C" {
6+
#endif
7+
8+
// Type definitions for function pointers
9+
typedef void (*jl_init_with_image_t)(const char *bindir, const char *sysimage);
10+
typedef double (*calculate_control_t)(double r, double y, double uff);
11+
typedef void (*set_K_t)(double K, double r, double y);
12+
typedef void (*set_Ti_t)(double Ti);
13+
typedef void (*set_Td_t)(double Td);
14+
typedef void (*reset_state_t)();
15+
16+
#ifdef __cplusplus
17+
}
18+
#endif
19+
20+
#endif // JULIAC_PID_H

examples/juliac/test_juliac_pid.c

Lines changed: 6 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -2,24 +2,14 @@
22
#include <stdlib.h>
33
#include <dlfcn.h>
44
// #include <julia.h>
5+
#include "juliac_pid.h"
56

67
// Path to julia binary folder
78
#define JULIA_PATH "/home/fredrikb/repos/julia/usr/bin/" // NOTE: modify this path
89

910
// Path to juliac compiled shared object file
1011
#define LIB_PATH "/home/fredrikb/.julia/dev/DiscretePIDs/examples/juliac/juliac_pid.so" // NOTE: modify this path
1112

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-
2313
int main() {
2414

2515
// Load the shared library
@@ -35,11 +25,11 @@ int main() {
3525
printf("Finding symbols\n");
3626
jl_init_with_image_t jl_init_with_image = (jl_init_with_image_t)dlsym(lib_handle, "jl_init_with_image");
3727

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!");
28+
calculate_control_t calculate_control = (calculate_control_t) dlsym(lib_handle, "calculate_control!");
29+
set_K_t set_K = (set_K_t) dlsym(lib_handle, "set_K!");
30+
set_Ti_t set_Ti = (set_Ti_t) dlsym(lib_handle, "set_Ti!");
31+
set_Td_t set_Td = (set_Td_t) dlsym(lib_handle, "set_Td!");
32+
reset_state_t reset_state = (reset_state_t) dlsym(lib_handle, "reset_state!");
4333

4434

4535
if (jl_init_with_image == NULL || calculate_control == NULL) {
@@ -64,7 +54,6 @@ int main() {
6454
printf("calculate_control! returned: %f\n", result);
6555
}
6656

67-
// jl_atexit_hook(0);
6857
return 0;
6958
}
7059

0 commit comments

Comments
 (0)