create a_regress_simple_evar
[liba.git] / lua / src / pid_neuro.c
blob6607aa2cff53b0d94d4d814bdfa292bd7a63bdbd
1 /***
2 single neuron proportional integral derivative controller
3 @module liba.pid_neuro
4 */
6 #include "pid_neuro.h"
7 #include "a/pid_neuro.h"
9 /***
10 constructor for single neuron PID controller
11 @treturn a.pid_neuro single neuron PID controller userdata
12 @function new
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;
24 ctx->pid.ki = 0;
25 ctx->pid.kd = 0;
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);
30 return 1;
33 /***
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
37 @function init
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);
43 ctx->pid.kp = 1;
44 ctx->pid.ki = 0;
45 ctx->pid.kd = 0;
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;
50 ctx->k = 1;
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);
55 return 1;
58 /***
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
66 @function set_kpid
68 int liba_pid_neuro_set_kpid(lua_State *L)
70 a_pid_neuro *const ctx = (a_pid_neuro *)lua_touserdata(L, 1);
71 if (ctx)
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);
78 lua_pushvalue(L, 1);
79 return 1;
81 return 0;
84 /***
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
91 @function set_wpid
93 int liba_pid_neuro_set_wpid(lua_State *L)
95 a_pid_neuro *const ctx = (a_pid_neuro *)lua_touserdata(L, 1);
96 if (ctx)
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);
102 lua_pushvalue(L, 1);
103 return 1;
105 return 0;
108 /***
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
114 @function run
116 int liba_pid_neuro_run(lua_State *L)
118 a_pid_neuro *const ctx = (a_pid_neuro *)lua_touserdata(L, 1);
119 if (ctx)
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));
124 return 1;
126 return 0;
129 /***
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
135 @function inc
137 int liba_pid_neuro_inc(lua_State *L)
139 a_pid_neuro *const ctx = (a_pid_neuro *)lua_touserdata(L, 1);
140 if (ctx)
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));
145 return 1;
147 return 0;
150 /***
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
154 @function zero
156 int liba_pid_neuro_zero(lua_State *L)
158 a_pid_neuro *const ctx = (a_pid_neuro *)lua_touserdata(L, 1);
159 if (ctx)
161 a_pid_neuro_zero(ctx);
162 return 1;
164 return 0;
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);
174 break;
175 case 0x00003731: // kp
176 ctx->pid.kp = (a_float)luaL_checknumber(L, 3);
177 break;
178 case 0x0000372A: // ki
179 ctx->pid.ki = (a_float)luaL_checknumber(L, 3);
180 break;
181 case 0x00003725: // kd
182 ctx->pid.kd = (a_float)luaL_checknumber(L, 3);
183 break;
184 case 0x00003D55: // wp
185 ctx->wp = (a_float)luaL_checknumber(L, 3);
186 break;
187 case 0x00003D4E: // wi
188 ctx->wi = (a_float)luaL_checknumber(L, 3);
189 break;
190 case 0x00003D49: // wd
191 ctx->wd = (a_float)luaL_checknumber(L, 3);
192 break;
193 case 0x23C8F10E: // outmax
194 ctx->pid.outmax = (a_float)luaL_checknumber(L, 3);
195 break;
196 case 0x23C8F51C: // outmin
197 ctx->pid.outmin = (a_float)luaL_checknumber(L, 3);
198 break;
199 case 0xE8859EEB: // __name
200 case 0xE70C48C6: // __call
201 case 0xA65758B2: // __index
202 case 0xAEB551C6: // __newindex
203 break;
204 default:
205 lua_getmetatable(L, 1);
206 lua_replace(L, 1);
207 lua_rawset(L, 1);
209 return 0;
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);
219 break;
220 case 0x00003731: // kp
221 lua_pushnumber(L, (lua_Number)ctx->pid.kp);
222 break;
223 case 0x0000372A: // ki
224 lua_pushnumber(L, (lua_Number)ctx->pid.ki);
225 break;
226 case 0x00003725: // kd
227 lua_pushnumber(L, (lua_Number)ctx->pid.kd);
228 break;
229 case 0x00003D55: // wp
230 lua_pushnumber(L, (lua_Number)ctx->wp);
231 break;
232 case 0x00003D4E: // wi
233 lua_pushnumber(L, (lua_Number)ctx->wi);
234 break;
235 case 0x00003D49: // wd
236 lua_pushnumber(L, (lua_Number)ctx->wd);
237 break;
238 case 0x23C8F10E: // outmax
239 lua_pushnumber(L, (lua_Number)ctx->pid.outmax);
240 break;
241 case 0x23C8F51C: // outmin
242 lua_pushnumber(L, (lua_Number)ctx->pid.outmin);
243 break;
244 case 0x001D4D3A: // out
245 lua_pushnumber(L, (lua_Number)ctx->pid.out);
246 break;
247 case 0x001AE924: // fdb
248 lua_pushnumber(L, (lua_Number)ctx->pid.fdb);
249 break;
250 case 0x001AAD55: // err
251 lua_pushnumber(L, (lua_Number)ctx->pid.err);
252 break;
253 case 0x00003412: // ec
254 lua_pushnumber(L, (lua_Number)ctx->ec);
255 break;
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);
273 break;
274 default:
275 lua_getmetatable(L, 1);
276 lua_replace(L, 1);
277 lua_rawget(L, 1);
279 return 1;
282 static int liba_pid_neuro_(lua_State *L)
284 lua_pushcfunction(L, liba_pid_neuro_new);
285 lua_replace(L, 1);
286 lua_call(L, lua_gettop(L) - 1, 1);
287 return 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);
317 return 1;