2 #include "a/pid_neuro.h"
4 static JSClassID liba_pid_neuro_class_id
;
6 static void liba_pid_neuro_finalizer(JSRuntime
*rt
, JSValue val
)
8 js_free_rt(rt
, JS_GetOpaque(val
, liba_pid_neuro_class_id
));
11 static JSValue
liba_pid_neuro_ctor(JSContext
*ctx
, JSValueConst new_target
, int argc
, JSValueConst
*argv
)
13 JSValue proto
, clazz
= JS_UNDEFINED
;
14 a_pid_neuro
*const self
= (a_pid_neuro
*)js_mallocz(ctx
, sizeof(a_pid_neuro
));
15 if (!self
) { return JS_EXCEPTION
; }
16 self
->pid
.summax
= +A_FLOAT_INF
;
17 self
->pid
.summin
= -A_FLOAT_INF
;
18 self
->pid
.outmax
= +A_FLOAT_INF
;
19 self
->pid
.outmin
= -A_FLOAT_INF
;
20 self
->k
= self
->pid
.kp
= 1;
21 self
->wp
= A_FLOAT_C(0.1);
22 self
->wi
= A_FLOAT_C(0.1);
23 self
->wd
= A_FLOAT_C(0.1);
24 a_pid_neuro_init(self
);
25 proto
= JS_GetPropertyStr(ctx
, new_target
, "prototype");
26 if (JS_IsException(proto
)) { goto fail
; }
27 clazz
= JS_NewObjectProtoClass(ctx
, proto
, liba_pid_neuro_class_id
);
28 JS_FreeValue(ctx
, proto
);
29 if (JS_IsException(clazz
)) { goto fail
; }
30 JS_SetOpaque(clazz
, self
);
36 JS_FreeValue(ctx
, clazz
);
40 static JSValue
liba_pid_neuro_set_kpid(JSContext
*ctx
, JSValueConst this_val
, int argc
, JSValueConst
*argv
)
43 double arg
[] = {0, 0, 0, 0};
44 a_pid_neuro
*const self
= (a_pid_neuro
*)JS_GetOpaque2(ctx
, this_val
, liba_pid_neuro_class_id
);
45 if (!self
) { return JS_EXCEPTION
; }
46 for (i
= 0; i
< A_LEN(arg
); ++i
)
48 if (JS_ToFloat64(ctx
, &arg
[i
], argv
[i
])) { return JS_EXCEPTION
; }
50 a_pid_neuro_set_kpid(self
, (a_float
)arg
[0], (a_float
)arg
[1], (a_float
)arg
[2], (a_float
)arg
[3]);
55 static JSValue
liba_pid_neuro_set_wpid(JSContext
*ctx
, JSValueConst this_val
, int argc
, JSValueConst
*argv
)
58 double arg
[] = {0, 0, 0};
59 a_pid_neuro
*const self
= (a_pid_neuro
*)JS_GetOpaque2(ctx
, this_val
, liba_pid_neuro_class_id
);
60 if (!self
) { return JS_EXCEPTION
; }
61 for (i
= 0; i
< A_LEN(arg
); ++i
)
63 if (JS_ToFloat64(ctx
, &arg
[i
], argv
[i
])) { return JS_EXCEPTION
; }
65 a_pid_neuro_set_wpid(self
, (a_float
)arg
[0], (a_float
)arg
[1], (a_float
)arg
[2]);
70 static JSValue
liba_pid_neuro_zero(JSContext
*ctx
, JSValueConst this_val
, int argc
, JSValueConst
*argv
)
72 a_pid_neuro
*const self
= (a_pid_neuro
*)JS_GetOpaque2(ctx
, this_val
, liba_pid_neuro_class_id
);
73 if (!self
) { return JS_EXCEPTION
; }
74 a_pid_neuro_zero(self
);
80 static JSValue
liba_pid_neuro_run(JSContext
*ctx
, JSValueConst this_val
, int argc
, JSValueConst
*argv
)
83 double arg
[] = {0, 0};
84 a_pid_neuro
*const self
= (a_pid_neuro
*)JS_GetOpaque2(ctx
, this_val
, liba_pid_neuro_class_id
);
85 if (!self
) { return JS_EXCEPTION
; }
86 for (i
= 0; i
< A_LEN(arg
); ++i
)
88 if (JS_ToFloat64(ctx
, &arg
[i
], argv
[i
])) { return JS_EXCEPTION
; }
91 return JS_NewFloat64(ctx
, (double)a_pid_neuro_run(self
, (a_float
)arg
[0], (a_float
)arg
[1]));
94 static JSValue
liba_pid_neuro_inc(JSContext
*ctx
, JSValueConst this_val
, int argc
, JSValueConst
*argv
)
97 double arg
[] = {0, 0};
98 a_pid_neuro
*const self
= (a_pid_neuro
*)JS_GetOpaque2(ctx
, this_val
, liba_pid_neuro_class_id
);
99 if (!self
) { return JS_EXCEPTION
; }
100 for (i
= 0; i
< A_LEN(arg
); ++i
)
102 if (JS_ToFloat64(ctx
, &arg
[i
], argv
[i
])) { return JS_EXCEPTION
; }
105 return JS_NewFloat64(ctx
, (double)a_pid_neuro_inc(self
, (a_float
)arg
[0], (a_float
)arg
[1]));
125 static JSValue
liba_pid_neuro_get(JSContext
*ctx
, JSValueConst this_val
, int magic
)
128 a_pid_neuro
*const self
= (a_pid_neuro
*)JS_GetOpaque2(ctx
, this_val
, liba_pid_neuro_class_id
);
129 if (!self
) { return JS_EXCEPTION
; }
132 case self_k
: x
= (double)self
->k
; break;
133 case self_kp
: x
= (double)self
->pid
.kp
; break;
134 case self_ki
: x
= (double)self
->pid
.ki
; break;
135 case self_kd
: x
= (double)self
->pid
.kd
; break;
136 case self_wp
: x
= (double)self
->wp
; break;
137 case self_wi
: x
= (double)self
->wi
; break;
138 case self_wd
: x
= (double)self
->wd
; break;
139 case self_outmax
: x
= (double)self
->pid
.outmax
; break;
140 case self_outmin
: x
= (double)self
->pid
.outmin
; break;
141 case self_out
: x
= (double)self
->pid
.out
; break;
142 case self_fdb
: x
= (double)self
->pid
.fdb
; break;
143 case self_err
: x
= (double)self
->pid
.err
; break;
144 case self_ec
: x
= (double)self
->ec
; break;
145 default: return JS_UNDEFINED
;
147 return JS_NewFloat64(ctx
, x
);
150 static JSValue
liba_pid_neuro_set(JSContext
*ctx
, JSValueConst this_val
, JSValueConst val
, int magic
)
153 a_pid_neuro
*const self
= (a_pid_neuro
*)JS_GetOpaque2(ctx
, this_val
, liba_pid_neuro_class_id
);
154 if (!self
) { return JS_EXCEPTION
; }
155 if (JS_ToFloat64(ctx
, &x
, val
)) { return JS_EXCEPTION
; }
158 case self_k
: self
->k
= (a_float
)x
; break;
159 case self_kp
: self
->pid
.kp
= (a_float
)x
; break;
160 case self_ki
: self
->pid
.ki
= (a_float
)x
; break;
161 case self_kd
: self
->pid
.kd
= (a_float
)x
; break;
162 case self_wp
: self
->wp
= (a_float
)x
; break;
163 case self_wi
: self
->wi
= (a_float
)x
; break;
164 case self_wd
: self
->wd
= (a_float
)x
; break;
165 case self_outmax
: self
->pid
.outmax
= (a_float
)x
; break;
166 case self_outmin
: self
->pid
.outmin
= (a_float
)x
; break;
172 static JSClassDef liba_pid_neuro_class
;
173 static JSCFunctionListEntry
const liba_pid_neuro_proto
[] = {
174 JS_PROP_STRING_DEF("[Symbol.toStringTag]", "a.pid.neuron", 0),
175 JS_CGETSET_MAGIC_DEF("k", liba_pid_neuro_get
, liba_pid_neuro_set
, self_k
),
176 JS_CGETSET_MAGIC_DEF("kp", liba_pid_neuro_get
, liba_pid_neuro_set
, self_kp
),
177 JS_CGETSET_MAGIC_DEF("ki", liba_pid_neuro_get
, liba_pid_neuro_set
, self_ki
),
178 JS_CGETSET_MAGIC_DEF("kd", liba_pid_neuro_get
, liba_pid_neuro_set
, self_kd
),
179 JS_CGETSET_MAGIC_DEF("wp", liba_pid_neuro_get
, liba_pid_neuro_set
, self_wp
),
180 JS_CGETSET_MAGIC_DEF("wi", liba_pid_neuro_get
, liba_pid_neuro_set
, self_wi
),
181 JS_CGETSET_MAGIC_DEF("wd", liba_pid_neuro_get
, liba_pid_neuro_set
, self_wd
),
182 JS_CGETSET_MAGIC_DEF("outmax", liba_pid_neuro_get
, liba_pid_neuro_set
, self_outmax
),
183 JS_CGETSET_MAGIC_DEF("outmin", liba_pid_neuro_get
, liba_pid_neuro_set
, self_outmin
),
184 JS_CGETSET_MAGIC_DEF("out", liba_pid_neuro_get
, NULL
, self_out
),
185 JS_CGETSET_MAGIC_DEF("fdb", liba_pid_neuro_get
, NULL
, self_fdb
),
186 JS_CGETSET_MAGIC_DEF("err", liba_pid_neuro_get
, NULL
, self_err
),
187 JS_CGETSET_MAGIC_DEF("ec", liba_pid_neuro_get
, NULL
, self_ec
),
188 JS_CFUNC_DEF("set_kpid", 4, liba_pid_neuro_set_kpid
),
189 JS_CFUNC_DEF("set_wpid", 3, liba_pid_neuro_set_wpid
),
190 JS_CFUNC_DEF("zero", 0, liba_pid_neuro_zero
),
191 JS_CFUNC_DEF("run", 2, liba_pid_neuro_run
),
192 JS_CFUNC_DEF("inc", 2, liba_pid_neuro_inc
),
195 int js_liba_pid_neuro_init(JSContext
*ctx
, JSModuleDef
*m
)
197 JSValue proto
, clazz
;
198 liba_pid_neuro_class
.class_name
= "pid_neuro";
199 liba_pid_neuro_class
.finalizer
= liba_pid_neuro_finalizer
;
201 JS_NewClassID(&liba_pid_neuro_class_id
);
202 JS_NewClass(JS_GetRuntime(ctx
), liba_pid_neuro_class_id
, &liba_pid_neuro_class
);
204 proto
= JS_NewObject(ctx
);
205 JS_SetPropertyFunctionList(ctx
, proto
, liba_pid_neuro_proto
, A_LEN(liba_pid_neuro_proto
));
207 clazz
= JS_NewCFunction2(ctx
, liba_pid_neuro_ctor
, "pid_neuro", 3, JS_CFUNC_constructor
, 0);
208 JS_SetClassProto(ctx
, liba_pid_neuro_class_id
, proto
);
209 JS_SetConstructor(ctx
, clazz
, proto
);
211 return JS_SetModuleExport(ctx
, m
, "pid_neuro", clazz
);