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 JSClassDef liba_pid_neuro_class
= {"pid_neuro", .finalizer
= liba_pid_neuro_finalizer
};
13 static JSValue
liba_pid_neuro_ctor(JSContext
*ctx
, JSValueConst new_target
, int argc
, JSValueConst
*argv
)
17 JSValue proto
, clazz
= JS_UNDEFINED
;
18 a_pid_neuro
*const self
= (a_pid_neuro
*)js_mallocz(ctx
, sizeof(a_pid_neuro
));
19 if (!self
) { return JS_EXCEPTION
; }
20 self
->pid
.summax
= +A_FLOAT_INF
;
21 self
->pid
.summin
= -A_FLOAT_INF
;
22 self
->pid
.outmax
= +A_FLOAT_INF
;
23 self
->pid
.outmin
= -A_FLOAT_INF
;
24 self
->k
= self
->pid
.kp
= 1;
25 self
->wp
= A_FLOAT_C(0.1);
26 self
->wi
= A_FLOAT_C(0.1);
27 self
->wd
= A_FLOAT_C(0.1);
28 a_pid_neuro_init(self
);
29 proto
= JS_GetPropertyStr(ctx
, new_target
, "prototype");
30 if (JS_IsException(proto
)) { goto fail
; }
31 clazz
= JS_NewObjectProtoClass(ctx
, proto
, liba_pid_neuro_class_id
);
32 JS_FreeValue(ctx
, proto
);
33 if (JS_IsException(clazz
)) { goto fail
; }
34 JS_SetOpaque(clazz
, self
);
38 JS_FreeValue(ctx
, clazz
);
42 static JSValue
liba_pid_neuro_kpid(JSContext
*ctx
, JSValueConst this_val
, int argc
, JSValueConst
*argv
)
45 a_pid_neuro
*const self
= (a_pid_neuro
*)JS_GetOpaque2(ctx
, this_val
, liba_pid_neuro_class_id
);
46 if (!self
) { return JS_EXCEPTION
; }
47 double args
[] = {0, 0, 0, 0};
48 for (unsigned int i
= 0; i
< A_LEN(args
); ++i
)
50 if (JS_ToFloat64(ctx
, &args
[i
], argv
[i
])) { return JS_EXCEPTION
; }
52 a_pid_neuro_kpid(self
, (a_float
)args
[0], (a_float
)args
[1], (a_float
)args
[2], (a_float
)args
[3]);
56 static JSValue
liba_pid_neuro_wpid(JSContext
*ctx
, JSValueConst this_val
, int argc
, JSValueConst
*argv
)
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 double args
[] = {0, 0, 0};
62 for (unsigned int i
= 0; i
< A_LEN(args
); ++i
)
64 if (JS_ToFloat64(ctx
, &args
[i
], argv
[i
])) { return JS_EXCEPTION
; }
66 a_pid_neuro_wpid(self
, (a_float
)args
[0], (a_float
)args
[1], (a_float
)args
[2]);
70 static JSValue
liba_pid_neuro_zero(JSContext
*ctx
, JSValueConst this_val
, int argc
, JSValueConst
*argv
)
74 a_pid_neuro
*const self
= (a_pid_neuro
*)JS_GetOpaque2(ctx
, this_val
, liba_pid_neuro_class_id
);
75 if (!self
) { return JS_EXCEPTION
; }
76 a_pid_neuro_zero(self
);
80 static JSValue
liba_pid_neuro_run(JSContext
*ctx
, JSValueConst this_val
, int argc
, JSValueConst
*argv
)
83 a_pid_neuro
*const self
= (a_pid_neuro
*)JS_GetOpaque2(ctx
, this_val
, liba_pid_neuro_class_id
);
84 if (!self
) { return JS_EXCEPTION
; }
85 double args
[] = {0, 0};
86 for (unsigned int i
= 0; i
< A_LEN(args
); ++i
)
88 if (JS_ToFloat64(ctx
, &args
[i
], argv
[i
])) { return JS_EXCEPTION
; }
90 return JS_NewFloat64(ctx
, (double)a_pid_neuro_run(self
, (a_float
)args
[0], (a_float
)args
[1]));
93 static JSValue
liba_pid_neuro_inc(JSContext
*ctx
, JSValueConst this_val
, int argc
, JSValueConst
*argv
)
96 a_pid_neuro
*const self
= (a_pid_neuro
*)JS_GetOpaque2(ctx
, this_val
, liba_pid_neuro_class_id
);
97 if (!self
) { return JS_EXCEPTION
; }
98 double args
[] = {0, 0};
99 for (unsigned int i
= 0; i
< A_LEN(args
); ++i
)
101 if (JS_ToFloat64(ctx
, &args
[i
], argv
[i
])) { return JS_EXCEPTION
; }
103 return JS_NewFloat64(ctx
, (double)a_pid_neuro_inc(self
, (a_float
)args
[0], (a_float
)args
[1]));
123 static JSValue
liba_pid_neuro_get(JSContext
*ctx
, JSValueConst this_val
, int magic
)
125 a_pid_neuro
*const self
= (a_pid_neuro
*)JS_GetOpaque2(ctx
, this_val
, liba_pid_neuro_class_id
);
126 if (!self
) { return JS_EXCEPTION
; }
130 case self_k_
: x
= (double)self
->k
; break;
131 case self_kp_
: x
= (double)self
->pid
.kp
; break;
132 case self_ki_
: x
= (double)self
->pid
.ki
; break;
133 case self_kd_
: x
= (double)self
->pid
.kd
; break;
134 case self_wp_
: x
= (double)self
->wp
; break;
135 case self_wi_
: x
= (double)self
->wi
; break;
136 case self_wd_
: x
= (double)self
->wd
; break;
137 case self_outmax_
: x
= (double)self
->pid
.outmax
; break;
138 case self_outmin_
: x
= (double)self
->pid
.outmin
; break;
139 case self_out_
: x
= (double)self
->pid
.out
; break;
140 case self_fdb_
: x
= (double)self
->pid
.fdb
; break;
141 case self_err_
: x
= (double)self
->pid
.err
; break;
142 case self_ec_
: x
= (double)self
->ec
; break;
143 default: return JS_UNDEFINED
;
145 return JS_NewFloat64(ctx
, x
);
148 static JSValue
liba_pid_neuro_set(JSContext
*ctx
, JSValueConst this_val
, JSValueConst val
, int magic
)
150 a_pid_neuro
*const self
= (a_pid_neuro
*)JS_GetOpaque2(ctx
, this_val
, liba_pid_neuro_class_id
);
151 if (!self
) { return JS_EXCEPTION
; }
153 if (JS_ToFloat64(ctx
, &x
, val
)) { return JS_EXCEPTION
; }
156 case self_k_
: self
->k
= (a_float
)x
; break;
157 case self_kp_
: self
->pid
.kp
= (a_float
)x
; break;
158 case self_ki_
: self
->pid
.ki
= (a_float
)x
; break;
159 case self_kd_
: self
->pid
.kd
= (a_float
)x
; break;
160 case self_wp_
: self
->wp
= (a_float
)x
; break;
161 case self_wi_
: self
->wi
= (a_float
)x
; break;
162 case self_wd_
: self
->wd
= (a_float
)x
; break;
163 case self_outmax_
: self
->pid
.outmax
= (a_float
)x
; break;
164 case self_outmin_
: self
->pid
.outmin
= (a_float
)x
; break;
170 static JSCFunctionListEntry
const liba_pid_neuro_proto
[] = {
171 JS_PROP_STRING_DEF("[Symbol.toStringTag]", "a.pid.neuron", 0),
172 JS_CGETSET_MAGIC_DEF("k", liba_pid_neuro_get
, liba_pid_neuro_set
, self_k_
),
173 JS_CGETSET_MAGIC_DEF("kp", liba_pid_neuro_get
, liba_pid_neuro_set
, self_kp_
),
174 JS_CGETSET_MAGIC_DEF("ki", liba_pid_neuro_get
, liba_pid_neuro_set
, self_ki_
),
175 JS_CGETSET_MAGIC_DEF("kd", liba_pid_neuro_get
, liba_pid_neuro_set
, self_kd_
),
176 JS_CGETSET_MAGIC_DEF("wp", liba_pid_neuro_get
, liba_pid_neuro_set
, self_wp_
),
177 JS_CGETSET_MAGIC_DEF("wi", liba_pid_neuro_get
, liba_pid_neuro_set
, self_wi_
),
178 JS_CGETSET_MAGIC_DEF("wd", liba_pid_neuro_get
, liba_pid_neuro_set
, self_wd_
),
179 JS_CGETSET_MAGIC_DEF("outmax", liba_pid_neuro_get
, liba_pid_neuro_set
, self_outmax_
),
180 JS_CGETSET_MAGIC_DEF("outmin", liba_pid_neuro_get
, liba_pid_neuro_set
, self_outmin_
),
181 JS_CGETSET_MAGIC_DEF("out", liba_pid_neuro_get
, NULL
, self_out_
),
182 JS_CGETSET_MAGIC_DEF("fdb", liba_pid_neuro_get
, NULL
, self_fdb_
),
183 JS_CGETSET_MAGIC_DEF("err", liba_pid_neuro_get
, NULL
, self_err_
),
184 JS_CGETSET_MAGIC_DEF("ec", liba_pid_neuro_get
, NULL
, self_ec_
),
185 JS_CFUNC_DEF("kpid", 4, liba_pid_neuro_kpid
),
186 JS_CFUNC_DEF("wpid", 3, liba_pid_neuro_wpid
),
187 JS_CFUNC_DEF("zero", 0, liba_pid_neuro_zero
),
188 JS_CFUNC_DEF("run", 2, liba_pid_neuro_run
),
189 JS_CFUNC_DEF("inc", 2, liba_pid_neuro_inc
),
192 int js_liba_pid_neuro_init(JSContext
*ctx
, JSModuleDef
*m
)
194 JS_NewClassID(&liba_pid_neuro_class_id
);
195 JS_NewClass(JS_GetRuntime(ctx
), liba_pid_neuro_class_id
, &liba_pid_neuro_class
);
197 JSValue
const proto
= JS_NewObject(ctx
);
198 JS_SetPropertyFunctionList(ctx
, proto
, liba_pid_neuro_proto
, A_LEN(liba_pid_neuro_proto
));
200 JSValue
const clazz
= JS_NewCFunction2(ctx
, liba_pid_neuro_ctor
, "pid_neuro", 3, JS_CFUNC_constructor
, 0);
201 JS_SetConstructor(ctx
, clazz
, proto
);
202 JS_SetClassProto(ctx
, liba_pid_neuro_class_id
, proto
);
204 return JS_SetModuleExport(ctx
, m
, "pid_neuro", clazz
);