2 #include "a/pid_fuzzy.h"
3 #include "liba_pid_fuzzy.h"
4 #if A_PREREQ_GNUC(3, 0) || __has_warning("-Wfloat-equal")
5 #pragma GCC diagnostic ignored "-Wfloat-equal"
6 #endif /* -Wfloat-equal */
9 #define L Java_liba_pid_1fuzzy
20 } L
= {NULL
, NULL
, NULL
, NULL
, NULL
, NULL
, NULL
, NULL
};
22 JNIEXPORT
void JNICALL
Java_liba_pid_1fuzzy_clinit(JNIEnv
*_env
, jclass _cls
)
24 jclass _bb
= (*_env
)->FindClass(_env
, "Ljava/nio/ByteBuffer;");
25 L
.ctx
= (*_env
)->GetFieldID(_env
, _cls
, "ctx", "Ljava/nio/ByteBuffer;");
26 L
.me
= (*_env
)->GetFieldID(_env
, _cls
, "me", "Ljava/nio/ByteBuffer;");
27 L
.mec
= (*_env
)->GetFieldID(_env
, _cls
, "mec", "Ljava/nio/ByteBuffer;");
28 L
.mkp
= (*_env
)->GetFieldID(_env
, _cls
, "mkp", "Ljava/nio/ByteBuffer;");
29 L
.mki
= (*_env
)->GetFieldID(_env
, _cls
, "mki", "Ljava/nio/ByteBuffer;");
30 L
.mkd
= (*_env
)->GetFieldID(_env
, _cls
, "mkd", "Ljava/nio/ByteBuffer;");
31 L
.block
= (*_env
)->GetFieldID(_env
, _cls
, "block", "Ljava/nio/ByteBuffer;");
32 L
.alloc
= (*_env
)->GetStaticMethodID(_env
, _bb
, "allocateDirect", "(I)Ljava/nio/ByteBuffer;");
33 (*_env
)->SetStaticIntField(_env
, _cls
, (*_env
)->GetStaticFieldID(_env
, _cls
, "CAP", "I"), A_PID_FUZZY_CAP
);
34 (*_env
)->SetStaticIntField(_env
, _cls
, (*_env
)->GetStaticFieldID(_env
, _cls
, "CAP_ALGEBRA", "I"), A_PID_FUZZY_CAP_ALGEBRA
);
35 (*_env
)->SetStaticIntField(_env
, _cls
, (*_env
)->GetStaticFieldID(_env
, _cls
, "CAP_BOUNDED", "I"), A_PID_FUZZY_CAP_BOUNDED
);
36 (*_env
)->SetStaticIntField(_env
, _cls
, (*_env
)->GetStaticFieldID(_env
, _cls
, "CUP", "I"), A_PID_FUZZY_CUP
);
37 (*_env
)->SetStaticIntField(_env
, _cls
, (*_env
)->GetStaticFieldID(_env
, _cls
, "CUP_ALGEBRA", "I"), A_PID_FUZZY_CUP_ALGEBRA
);
38 (*_env
)->SetStaticIntField(_env
, _cls
, (*_env
)->GetStaticFieldID(_env
, _cls
, "CUP_BOUNDED", "I"), A_PID_FUZZY_CUP_BOUNDED
);
39 (*_env
)->SetStaticIntField(_env
, _cls
, (*_env
)->GetStaticFieldID(_env
, _cls
, "EQU", "I"), A_PID_FUZZY_EQU
);
42 JNIEXPORT
void JNICALL
Java_liba_pid_1fuzzy_init(JNIEnv
*_env
, jobject _obj
)
44 jobject _ctx
= (*_env
)->CallObjectMethod(_env
, _obj
, L
.alloc
, (jint
)sizeof(a_pid_fuzzy
));
45 a_pid_fuzzy
*ctx
= (a_pid_fuzzy
*)(*_env
)->GetDirectBufferAddress(_env
, _ctx
);
46 (*_env
)->SetObjectField(_env
, _obj
, L
.ctx
, _ctx
);
47 ctx
->pid
.summax
= +A_FLOAT_INF
;
48 ctx
->pid
.summin
= -A_FLOAT_INF
;
49 ctx
->pid
.outmax
= +A_FLOAT_INF
;
50 ctx
->pid
.outmin
= -A_FLOAT_INF
;
51 ctx
->kp
= ctx
->pid
.kp
= 1;
52 ctx
->op
= a_fuzzy_equ
;
53 a_pid_fuzzy_init(ctx
);
56 JNIEXPORT jdouble JNICALL
Java_liba_pid_1fuzzy_kp__(JNIEnv
*_env
, jobject _obj
)
58 jobject _ctx
= (*_env
)->GetObjectField(_env
, _obj
, L
.ctx
);
59 a_pid_fuzzy
*ctx
= (a_pid_fuzzy
*)(*_env
)->GetDirectBufferAddress(_env
, _ctx
);
63 JNIEXPORT jobject JNICALL
Java_liba_pid_1fuzzy_kp__D(JNIEnv
*_env
, jobject _obj
, jdouble kp
)
65 jobject _ctx
= (*_env
)->GetObjectField(_env
, _obj
, L
.ctx
);
66 a_pid_fuzzy
*ctx
= (a_pid_fuzzy
*)(*_env
)->GetDirectBufferAddress(_env
, _ctx
);
67 ctx
->pid
.kp
= ctx
->kp
= kp
;
71 JNIEXPORT jdouble JNICALL
Java_liba_pid_1fuzzy_ki__(JNIEnv
*_env
, jobject _obj
)
73 jobject _ctx
= (*_env
)->GetObjectField(_env
, _obj
, L
.ctx
);
74 a_pid_fuzzy
*ctx
= (a_pid_fuzzy
*)(*_env
)->GetDirectBufferAddress(_env
, _ctx
);
78 JNIEXPORT jobject JNICALL
Java_liba_pid_1fuzzy_ki__D(JNIEnv
*_env
, jobject _obj
, jdouble ki
)
80 jobject _ctx
= (*_env
)->GetObjectField(_env
, _obj
, L
.ctx
);
81 a_pid_fuzzy
*ctx
= (a_pid_fuzzy
*)(*_env
)->GetDirectBufferAddress(_env
, _ctx
);
82 ctx
->pid
.ki
= ctx
->ki
= ki
;
86 JNIEXPORT jdouble JNICALL
Java_liba_pid_1fuzzy_kd__(JNIEnv
*_env
, jobject _obj
)
88 jobject _ctx
= (*_env
)->GetObjectField(_env
, _obj
, L
.ctx
);
89 a_pid_fuzzy
*ctx
= (a_pid_fuzzy
*)(*_env
)->GetDirectBufferAddress(_env
, _ctx
);
93 JNIEXPORT jobject JNICALL
Java_liba_pid_1fuzzy_kd__D(JNIEnv
*_env
, jobject _obj
, jdouble kd
)
95 jobject _ctx
= (*_env
)->GetObjectField(_env
, _obj
, L
.ctx
);
96 a_pid_fuzzy
*ctx
= (a_pid_fuzzy
*)(*_env
)->GetDirectBufferAddress(_env
, _ctx
);
97 ctx
->pid
.kd
= ctx
->kd
= kd
;
101 JNIEXPORT jdouble JNICALL
Java_liba_pid_1fuzzy_summax__(JNIEnv
*_env
, jobject _obj
)
103 jobject _ctx
= (*_env
)->GetObjectField(_env
, _obj
, L
.ctx
);
104 a_pid_fuzzy
*ctx
= (a_pid_fuzzy
*)(*_env
)->GetDirectBufferAddress(_env
, _ctx
);
105 return ctx
->pid
.summax
;
108 JNIEXPORT jobject JNICALL
Java_liba_pid_1fuzzy_summax__D(JNIEnv
*_env
, jobject _obj
, jdouble summax
)
110 jobject _ctx
= (*_env
)->GetObjectField(_env
, _obj
, L
.ctx
);
111 a_pid_fuzzy
*ctx
= (a_pid_fuzzy
*)(*_env
)->GetDirectBufferAddress(_env
, _ctx
);
112 ctx
->pid
.summax
= summax
;
116 JNIEXPORT jdouble JNICALL
Java_liba_pid_1fuzzy_summin__(JNIEnv
*_env
, jobject _obj
)
118 jobject _ctx
= (*_env
)->GetObjectField(_env
, _obj
, L
.ctx
);
119 a_pid_fuzzy
*ctx
= (a_pid_fuzzy
*)(*_env
)->GetDirectBufferAddress(_env
, _ctx
);
120 return ctx
->pid
.summin
;
123 JNIEXPORT jobject JNICALL
Java_liba_pid_1fuzzy_summin__D(JNIEnv
*_env
, jobject _obj
, jdouble summin
)
125 jobject _ctx
= (*_env
)->GetObjectField(_env
, _obj
, L
.ctx
);
126 a_pid_fuzzy
*ctx
= (a_pid_fuzzy
*)(*_env
)->GetDirectBufferAddress(_env
, _ctx
);
127 ctx
->pid
.summin
= summin
;
131 JNIEXPORT jdouble JNICALL
Java_liba_pid_1fuzzy_sum(JNIEnv
*_env
, jobject _obj
)
133 jobject _ctx
= (*_env
)->GetObjectField(_env
, _obj
, L
.ctx
);
134 a_pid_fuzzy
*ctx
= (a_pid_fuzzy
*)(*_env
)->GetDirectBufferAddress(_env
, _ctx
);
138 JNIEXPORT jdouble JNICALL
Java_liba_pid_1fuzzy_outmax__(JNIEnv
*_env
, jobject _obj
)
140 jobject _ctx
= (*_env
)->GetObjectField(_env
, _obj
, L
.ctx
);
141 a_pid_fuzzy
*ctx
= (a_pid_fuzzy
*)(*_env
)->GetDirectBufferAddress(_env
, _ctx
);
142 return ctx
->pid
.outmax
;
145 JNIEXPORT jobject JNICALL
Java_liba_pid_1fuzzy_outmax__D(JNIEnv
*_env
, jobject _obj
, jdouble outmax
)
147 jobject _ctx
= (*_env
)->GetObjectField(_env
, _obj
, L
.ctx
);
148 a_pid_fuzzy
*ctx
= (a_pid_fuzzy
*)(*_env
)->GetDirectBufferAddress(_env
, _ctx
);
149 ctx
->pid
.outmax
= outmax
;
153 JNIEXPORT jdouble JNICALL
Java_liba_pid_1fuzzy_outmin__(JNIEnv
*_env
, jobject _obj
)
155 jobject _ctx
= (*_env
)->GetObjectField(_env
, _obj
, L
.ctx
);
156 a_pid_fuzzy
*ctx
= (a_pid_fuzzy
*)(*_env
)->GetDirectBufferAddress(_env
, _ctx
);
157 return ctx
->pid
.outmin
;
160 JNIEXPORT jobject JNICALL
Java_liba_pid_1fuzzy_outmin__D(JNIEnv
*_env
, jobject _obj
, jdouble outmin
)
162 jobject _ctx
= (*_env
)->GetObjectField(_env
, _obj
, L
.ctx
);
163 a_pid_fuzzy
*ctx
= (a_pid_fuzzy
*)(*_env
)->GetDirectBufferAddress(_env
, _ctx
);
164 ctx
->pid
.outmin
= outmin
;
168 JNIEXPORT jdouble JNICALL
Java_liba_pid_1fuzzy_out(JNIEnv
*_env
, jobject _obj
)
170 jobject _ctx
= (*_env
)->GetObjectField(_env
, _obj
, L
.ctx
);
171 a_pid_fuzzy
*ctx
= (a_pid_fuzzy
*)(*_env
)->GetDirectBufferAddress(_env
, _ctx
);
175 JNIEXPORT jdouble JNICALL
Java_liba_pid_1fuzzy_fdb(JNIEnv
*_env
, jobject _obj
)
177 jobject _ctx
= (*_env
)->GetObjectField(_env
, _obj
, L
.ctx
);
178 a_pid_fuzzy
*ctx
= (a_pid_fuzzy
*)(*_env
)->GetDirectBufferAddress(_env
, _ctx
);
182 JNIEXPORT jdouble JNICALL
Java_liba_pid_1fuzzy_err(JNIEnv
*_env
, jobject _obj
)
184 jobject _ctx
= (*_env
)->GetObjectField(_env
, _obj
, L
.ctx
);
185 a_pid_fuzzy
*ctx
= (a_pid_fuzzy
*)(*_env
)->GetDirectBufferAddress(_env
, _ctx
);
189 JNIEXPORT jint JNICALL
Java_liba_pid_1fuzzy_order(JNIEnv
*_env
, jobject _obj
)
191 jobject _ctx
= (*_env
)->GetObjectField(_env
, _obj
, L
.ctx
);
192 a_pid_fuzzy
*ctx
= (a_pid_fuzzy
*)(*_env
)->GetDirectBufferAddress(_env
, _ctx
);
193 return (jint
)ctx
->order
;
196 JNIEXPORT jobject JNICALL
Java_liba_pid_1fuzzy_op(JNIEnv
*_env
, jobject _obj
, jint op
)
198 jobject _ctx
= (*_env
)->GetObjectField(_env
, _obj
, L
.ctx
);
199 a_pid_fuzzy
*ctx
= (a_pid_fuzzy
*)(*_env
)->GetDirectBufferAddress(_env
, _ctx
);
200 a_pid_fuzzy_set_op(ctx
, (unsigned int)op
);
204 static jobject
concat(JNIEnv
*_env
, jobject _obj
, jobjectArray _val
, jdouble
const **out
)
207 jsize order
= (*_env
)->GetArrayLength(_env
, _val
);
208 for (jsize idx
= 0; idx
!= order
; ++idx
)
210 jobject o
= (*_env
)->GetObjectArrayElement(_env
, _val
, idx
);
211 length
+= (*_env
)->GetArrayLength(_env
, o
);
212 (*_env
)->DeleteLocalRef(_env
, o
);
214 jobject obj
= (*_env
)->CallObjectMethod(_env
, _obj
, L
.alloc
, (jint
)length
* 8);
215 jdouble
*ptr
= (jdouble
*)(*_env
)->GetDirectBufferAddress(_env
, obj
);
217 for (jsize idx
= 0, num
= 0; idx
!= order
; ++idx
, ptr
+= num
)
219 jobject o
= (*_env
)->GetObjectArrayElement(_env
, _val
, idx
);
220 num
= (*_env
)->GetArrayLength(_env
, o
);
221 (*_env
)->GetDoubleArrayRegion(_env
, o
, 0, num
, ptr
);
222 (*_env
)->DeleteLocalRef(_env
, o
);
227 JNIEXPORT jobject JNICALL
Java_liba_pid_1fuzzy_rule(JNIEnv
*_env
, jobject _obj
, jobjectArray me
, jobjectArray mec
,
228 jobjectArray mkp
, jobjectArray mki
, jobjectArray mkd
)
230 jobject _ctx
= (*_env
)->GetObjectField(_env
, _obj
, L
.ctx
);
231 a_pid_fuzzy
*ctx
= (a_pid_fuzzy
*)(*_env
)->GetDirectBufferAddress(_env
, _ctx
);
232 ctx
->order
= (unsigned int)(*_env
)->GetArrayLength(_env
, me
);
233 jobject _me
= concat(_env
, _obj
, me
, &ctx
->me
);
234 jobject _mec
= concat(_env
, _obj
, mec
, &ctx
->mec
);
235 jobject _mkp
= concat(_env
, _obj
, mkp
, &ctx
->mkp
);
236 jobject _mki
= concat(_env
, _obj
, mki
, &ctx
->mki
);
237 jobject _mkd
= concat(_env
, _obj
, mkd
, &ctx
->mkd
);
238 (*_env
)->SetObjectField(_env
, _obj
, L
.me
, _me
);
239 (*_env
)->SetObjectField(_env
, _obj
, L
.mec
, _mec
);
240 (*_env
)->SetObjectField(_env
, _obj
, L
.mkp
, _mkp
);
241 (*_env
)->SetObjectField(_env
, _obj
, L
.mki
, _mki
);
242 (*_env
)->SetObjectField(_env
, _obj
, L
.mkd
, _mkd
);
246 JNIEXPORT jint JNICALL
Java_liba_pid_1fuzzy_block__(JNIEnv
*_env
, jobject _obj
)
248 jobject _ctx
= (*_env
)->GetObjectField(_env
, _obj
, L
.ctx
);
249 a_pid_fuzzy
*ctx
= (a_pid_fuzzy
*)(*_env
)->GetDirectBufferAddress(_env
, _ctx
);
250 return (jint
)ctx
->block
;
253 JNIEXPORT jobject JNICALL
Java_liba_pid_1fuzzy_block__I(JNIEnv
*_env
, jobject _obj
, jint num
)
255 jobject _ctx
= (*_env
)->GetObjectField(_env
, _obj
, L
.ctx
);
256 a_pid_fuzzy
*ctx
= (a_pid_fuzzy
*)(*_env
)->GetDirectBufferAddress(_env
, _ctx
);
257 if (num
> (jint
)ctx
->block
)
259 jobject _block
= (*_env
)->CallObjectMethod(_env
, _obj
, L
.alloc
, (jint
)A_PID_FUZZY_BLOCK((unsigned int)num
));
260 a_pid_fuzzy_set_block(ctx
, (*_env
)->GetDirectBufferAddress(_env
, _block
), (a_size
)num
);
261 (*_env
)->SetObjectField(_env
, _obj
, L
.block
, _block
);
266 JNIEXPORT jobject JNICALL
Java_liba_pid_1fuzzy_kpid(JNIEnv
*_env
, jobject _obj
, jdouble kp
, jdouble ki
, jdouble kd
)
268 jobject _ctx
= (*_env
)->GetObjectField(_env
, _obj
, L
.ctx
);
269 a_pid_fuzzy
*ctx
= (a_pid_fuzzy
*)(*_env
)->GetDirectBufferAddress(_env
, _ctx
);
270 a_pid_fuzzy_kpid(ctx
, kp
, ki
, kd
);
274 JNIEXPORT jdouble JNICALL
Java_liba_pid_1fuzzy_run(JNIEnv
*_env
, jobject _obj
, jdouble set
, jdouble fdb
)
276 jobject _ctx
= (*_env
)->GetObjectField(_env
, _obj
, L
.ctx
);
277 a_pid_fuzzy
*ctx
= (a_pid_fuzzy
*)(*_env
)->GetDirectBufferAddress(_env
, _ctx
);
278 return a_pid_fuzzy_run(ctx
, set
, fdb
);
281 JNIEXPORT jdouble JNICALL
Java_liba_pid_1fuzzy_pos(JNIEnv
*_env
, jobject _obj
, jdouble set
, jdouble fdb
)
283 jobject _ctx
= (*_env
)->GetObjectField(_env
, _obj
, L
.ctx
);
284 a_pid_fuzzy
*ctx
= (a_pid_fuzzy
*)(*_env
)->GetDirectBufferAddress(_env
, _ctx
);
285 return a_pid_fuzzy_pos(ctx
, set
, fdb
);
288 JNIEXPORT jdouble JNICALL
Java_liba_pid_1fuzzy_inc(JNIEnv
*_env
, jobject _obj
, jdouble set
, jdouble fdb
)
290 jobject _ctx
= (*_env
)->GetObjectField(_env
, _obj
, L
.ctx
);
291 a_pid_fuzzy
*ctx
= (a_pid_fuzzy
*)(*_env
)->GetDirectBufferAddress(_env
, _ctx
);
292 return a_pid_fuzzy_inc(ctx
, set
, fdb
);
295 JNIEXPORT jobject JNICALL
Java_liba_pid_1fuzzy_zero(JNIEnv
*_env
, jobject _obj
)
297 jobject _ctx
= (*_env
)->GetObjectField(_env
, _obj
, L
.ctx
);
298 a_pid_fuzzy
*ctx
= (a_pid_fuzzy
*)(*_env
)->GetDirectBufferAddress(_env
, _ctx
);
299 a_pid_fuzzy_zero(ctx
);