-
Notifications
You must be signed in to change notification settings - Fork 0
/
pid_py_bindings.py
72 lines (51 loc) · 2.38 KB
/
pid_py_bindings.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
# Copyright (C) 2021 Jos de Koning - All Rights Reserved
#
# You may use, distribute and modify this code under the
# terms of the BSD-3-Clause license.
#
# SPDX-License-Identifier: BSD-3-Clause
import ctypes
libc = ctypes.CDLL('pid.so')
def wrap_function(lib, funcname, restype, argtypes):
func = lib.__getattr__(funcname)
func.restype = restype
func.argtypes = argtypes
return func
def newStructType(name=None, fields=None):
return type(name, (ctypes.Structure, ), { "_fields_" : fields } )
# Create template of pid_gains_t struct
pid_gains_t_config = { "name" : "pid_gains_t",
"fields" : [ ('p', ctypes.c_float),
('i', ctypes.c_float),
('d', ctypes.c_float) ] }
# Create pid_gains_t type
pid_gains_t = newStructType(**pid_gains_t_config)
# Create template of pid_handle_t struct
pid_handle_t_config = { "name" : "pid_handle_t",
"fields" : [ ('k', pid_gains_t),
('p', ctypes.c_float),
('i', ctypes.c_float),
('d', ctypes.c_float),
('pv_previous', ctypes.c_float),
('sp', ctypes.c_float),
('pv', ctypes.c_float),
('cv', ctypes.c_float),
('cv_min', ctypes.c_float),
('cv_max', ctypes.c_float) ] }
# Create pid_handle_t type
pid_handle_t = newStructType(**pid_handle_t_config)
if __name__ == '__main__':
# # # Example implementation # # #
# Create pid_calc python interface
pid_calc = wrap_function(libc, 'pid_calc', ctypes.c_float, [ctypes.POINTER(pid_handle_t), ctypes.c_float, ctypes.c_float])
# Create gain struct
pid_gains_s = pid_gains_t(p=5, i=0.5, d=15)
# Add the gains to the pid handle
pid_handle_s = pid_handle_t(k=pid_gains_s)
# Set the saturation limits for the control value
pid_handle_s.cv_max = 100
pid_handle_s.cv_min = 0
pid_handle_s.i = 0
pid_handle_s.pv_previous = 20
# Calculate the next control value
cv = pid_calc(pid_handle_s, 20, 100) # (handle, pv, sp)