2 single neuron proportional integral derivative controller
7 #include "a/pid_neuro.h"
10 constructor for single neuron PID controller
11 @treturn a.pid_neuro single neuron PID controller userdata
14 int liba_pid_neuro_new(lua_State
*L
)
16 a_pid_neuro
*const ctx
= lua_newclass(L
, a_pid_neuro
);
17 lua_registry_get(L
, liba_pid_neuro_new
);
18 lua_setmetatable(L
, -2);
19 ctx
->pid
.summax
= +A_FLOAT_INF
;
20 ctx
->pid
.summin
= -A_FLOAT_INF
;
21 ctx
->pid
.outmax
= +A_FLOAT_INF
;
22 ctx
->pid
.outmin
= -A_FLOAT_INF
;
23 ctx
->pid
.kp
= ctx
->k
= 1;
26 ctx
->wp
= A_FLOAT_C(0.1);
27 ctx
->wi
= A_FLOAT_C(0.1);
28 ctx
->wd
= A_FLOAT_C(0.1);
29 a_pid_neuro_init(ctx
);
34 initialize for single neuron PID controller
35 @tparam a.pid_neuro ctx single neuron PID controller userdata
36 @treturn a.pid_neuro single neuron PID controller userdata
39 int liba_pid_neuro_init(lua_State
*L
)
41 luaL_checktype(L
, 1, LUA_TUSERDATA
);
42 a_pid_neuro
*const ctx
= (a_pid_neuro
*)lua_touserdata(L
, 1);
46 ctx
->pid
.summax
= +A_FLOAT_INF
;
47 ctx
->pid
.summin
= -A_FLOAT_INF
;
48 ctx
->pid
.outmax
= +A_FLOAT_INF
;
49 ctx
->pid
.outmin
= -A_FLOAT_INF
;
51 ctx
->wp
= A_FLOAT_C(0.1);
52 ctx
->wi
= A_FLOAT_C(0.1);
53 ctx
->wd
= A_FLOAT_C(0.1);
54 a_pid_neuro_init(ctx
);
59 set proportional integral derivative constant for single neuron PID controller
60 @tparam a.pid_neuro ctx single neuron PID controller userdata
61 @tparam number k proportional output coefficient
62 @tparam number kp proportional learning constant
63 @tparam number ki integral learning constant
64 @tparam number kd derivative learning constant
65 @treturn a.pid_neuro single neuron PID controller userdata
68 int liba_pid_neuro_set_kpid(lua_State
*L
)
70 a_pid_neuro
*const ctx
= (a_pid_neuro
*)lua_touserdata(L
, 1);
73 a_float
const k
= (a_float
)luaL_checknumber(L
, 2);
74 a_float
const kp
= (a_float
)luaL_checknumber(L
, 3);
75 a_float
const ki
= (a_float
)luaL_checknumber(L
, 4);
76 a_float
const kd
= (a_float
)luaL_checknumber(L
, 5);
77 a_pid_neuro_set_kpid(ctx
, k
, kp
, ki
, kd
);
85 set proportional integral derivative weight for single neuron PID controller
86 @tparam a.pid_neuro ctx single neuron PID controller userdata
87 @tparam number wp proportional weight
88 @tparam number wi integral weight
89 @tparam number wd derivative weight
90 @treturn a.pid_neuro single neuron PID controller userdata
93 int liba_pid_neuro_set_wpid(lua_State
*L
)
95 a_pid_neuro
*const ctx
= (a_pid_neuro
*)lua_touserdata(L
, 1);
98 a_float
const wp
= (a_float
)luaL_checknumber(L
, 2);
99 a_float
const wi
= (a_float
)luaL_checknumber(L
, 3);
100 a_float
const wd
= (a_float
)luaL_checknumber(L
, 4);
101 a_pid_neuro_set_wpid(ctx
, wp
, wi
, wd
);
109 calculate for single neuron PID controller
110 @tparam a.pid_neuro ctx single neuron PID controller userdata
111 @tparam number set setpoint value
112 @tparam number fdb feedback value
113 @treturn number setpoint value
116 int liba_pid_neuro_run(lua_State
*L
)
118 a_pid_neuro
*const ctx
= (a_pid_neuro
*)lua_touserdata(L
, 1);
121 a_float
const set
= (a_float
)luaL_checknumber(L
, 2);
122 a_float
const fdb
= (a_float
)luaL_checknumber(L
, 3);
123 lua_pushnumber(L
, (lua_Number
)a_pid_neuro_run(ctx
, set
, fdb
));
130 calculate for single neuron PID controller
131 @tparam a.pid_neuro ctx single neuron PID controller userdata
132 @tparam number set setpoint value
133 @tparam number fdb feedback value
134 @treturn number setpoint value
137 int liba_pid_neuro_inc(lua_State
*L
)
139 a_pid_neuro
*const ctx
= (a_pid_neuro
*)lua_touserdata(L
, 1);
142 a_float
const set
= (a_float
)luaL_checknumber(L
, 2);
143 a_float
const fdb
= (a_float
)luaL_checknumber(L
, 3);
144 lua_pushnumber(L
, (lua_Number
)a_pid_neuro_inc(ctx
, set
, fdb
));
151 zeroing for single neuron PID controller
152 @tparam a.pid_neuro ctx single neuron PID controller userdata
153 @treturn a.pid_neuro single neuron PID controller userdata
156 int liba_pid_neuro_zero(lua_State
*L
)
158 a_pid_neuro
*const ctx
= (a_pid_neuro
*)lua_touserdata(L
, 1);
161 a_pid_neuro_zero(ctx
);
167 static int liba_pid_neuro_set(lua_State
*L
)
169 a_pid_neuro
*const ctx
= (a_pid_neuro
*)lua_touserdata(L
, 1);
170 switch (a_hash_bkdr(lua_tostring(L
, 2), 0))
172 case 0x0000006B: // k
173 ctx
->k
= (a_float
)luaL_checknumber(L
, 3);
175 case 0x00003731: // kp
176 ctx
->pid
.kp
= (a_float
)luaL_checknumber(L
, 3);
178 case 0x0000372A: // ki
179 ctx
->pid
.ki
= (a_float
)luaL_checknumber(L
, 3);
181 case 0x00003725: // kd
182 ctx
->pid
.kd
= (a_float
)luaL_checknumber(L
, 3);
184 case 0x00003D55: // wp
185 ctx
->wp
= (a_float
)luaL_checknumber(L
, 3);
187 case 0x00003D4E: // wi
188 ctx
->wi
= (a_float
)luaL_checknumber(L
, 3);
190 case 0x00003D49: // wd
191 ctx
->wd
= (a_float
)luaL_checknumber(L
, 3);
193 case 0x23C8F10E: // outmax
194 ctx
->pid
.outmax
= (a_float
)luaL_checknumber(L
, 3);
196 case 0x23C8F51C: // outmin
197 ctx
->pid
.outmin
= (a_float
)luaL_checknumber(L
, 3);
199 case 0xE8859EEB: // __name
200 case 0xE70C48C6: // __call
201 case 0xA65758B2: // __index
202 case 0xAEB551C6: // __newindex
205 lua_getmetatable(L
, 1);
212 static int liba_pid_neuro_get(lua_State
*L
)
214 a_pid_neuro
const *const ctx
= (a_pid_neuro
const *)lua_touserdata(L
, 1);
215 switch (a_hash_bkdr(lua_tostring(L
, 2), 0))
217 case 0x0000006B: // k
218 lua_pushnumber(L
, (lua_Number
)ctx
->k
);
220 case 0x00003731: // kp
221 lua_pushnumber(L
, (lua_Number
)ctx
->pid
.kp
);
223 case 0x0000372A: // ki
224 lua_pushnumber(L
, (lua_Number
)ctx
->pid
.ki
);
226 case 0x00003725: // kd
227 lua_pushnumber(L
, (lua_Number
)ctx
->pid
.kd
);
229 case 0x00003D55: // wp
230 lua_pushnumber(L
, (lua_Number
)ctx
->wp
);
232 case 0x00003D4E: // wi
233 lua_pushnumber(L
, (lua_Number
)ctx
->wi
);
235 case 0x00003D49: // wd
236 lua_pushnumber(L
, (lua_Number
)ctx
->wd
);
238 case 0x23C8F10E: // outmax
239 lua_pushnumber(L
, (lua_Number
)ctx
->pid
.outmax
);
241 case 0x23C8F51C: // outmin
242 lua_pushnumber(L
, (lua_Number
)ctx
->pid
.outmin
);
244 case 0x001D4D3A: // out
245 lua_pushnumber(L
, (lua_Number
)ctx
->pid
.out
);
247 case 0x001AE924: // fdb
248 lua_pushnumber(L
, (lua_Number
)ctx
->pid
.fdb
);
250 case 0x001AAD55: // err
251 lua_pushnumber(L
, (lua_Number
)ctx
->pid
.err
);
253 case 0x00003412: // ec
254 lua_pushnumber(L
, (lua_Number
)ctx
->ec
);
256 case 0xA65758B2: // __index
257 lua_registry_get(L
, liba_pid_neuro_new
);
258 lua_num_set(L
, -1, "kp", ctx
->pid
.kp
);
259 lua_num_set(L
, -1, "ki", ctx
->pid
.ki
);
260 lua_num_set(L
, -1, "kd", ctx
->pid
.kd
);
261 lua_num_set(L
, -1, "summax", ctx
->pid
.summax
);
262 lua_num_set(L
, -1, "summin", ctx
->pid
.summin
);
263 lua_num_set(L
, -1, "outmax", ctx
->pid
.outmax
);
264 lua_num_set(L
, -1, "outmin", ctx
->pid
.outmin
);
265 lua_num_set(L
, -1, "out", ctx
->pid
.out
);
266 lua_num_set(L
, -1, "fdb", ctx
->pid
.fdb
);
267 lua_num_set(L
, -1, "err", ctx
->pid
.err
);
268 lua_num_set(L
, -1, "ec", ctx
->ec
);
269 lua_num_set(L
, -1, "wp", ctx
->wp
);
270 lua_num_set(L
, -1, "wi", ctx
->wi
);
271 lua_num_set(L
, -1, "wd", ctx
->wd
);
272 lua_num_set(L
, -1, "k", ctx
->k
);
275 lua_getmetatable(L
, 1);
282 static int liba_pid_neuro_(lua_State
*L
)
284 lua_pushcfunction(L
, liba_pid_neuro_new
);
286 lua_call(L
, lua_gettop(L
) - 1, 1);
290 int luaopen_liba_pid_neuro(lua_State
*L
)
292 static lua_fun
const funcs
[] = {
293 {"new", liba_pid_neuro_new
},
294 {"init", liba_pid_neuro_init
},
295 {"set_kpid", liba_pid_neuro_set_kpid
},
296 {"set_wpid", liba_pid_neuro_set_wpid
},
297 {"run", liba_pid_neuro_run
},
298 {"inc", liba_pid_neuro_inc
},
299 {"zero", liba_pid_neuro_zero
},
301 lua_createtable(L
, 0, A_LEN(funcs
));
302 lua_fun_reg(L
, -1, funcs
, A_LEN(funcs
));
303 lua_createtable(L
, 0, 1);
304 lua_fun_set(L
, -1, "__call", liba_pid_neuro_
);
305 lua_setmetatable(L
, -2);
307 static lua_fun
const metas
[] = {
308 {"__newindex", liba_pid_neuro_set
},
309 {"__index", liba_pid_neuro_get
},
311 lua_createtable(L
, 0, A_LEN(metas
) + A_LEN(funcs
) + 1);
312 lua_fun_reg(L
, -1, metas
, A_LEN(metas
));
313 lua_fun_reg(L
, -1, funcs
, A_LEN(funcs
));
314 lua_str_set(L
, -1, "__name", "a.pid_neuro");
315 lua_registry_set(L
, liba_pid_neuro_new
);