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 Nbb
= (*Env
)->FindClass(Env
, "Ljava/nio/ByteBuffer;");
25 L
.New
= (*Env
)->GetStaticMethodID(Env
, Nbb
, "allocateDirect", "(I)Ljava/nio/ByteBuffer;");
26 L
.ctx
= (*Env
)->GetFieldID(Env
, Cls
, "ctx", "Ljava/nio/ByteBuffer;");
27 L
.me
= (*Env
)->GetFieldID(Env
, Cls
, "me", "Ljava/nio/ByteBuffer;");
28 L
.mec
= (*Env
)->GetFieldID(Env
, Cls
, "mec", "Ljava/nio/ByteBuffer;");
29 L
.mkp
= (*Env
)->GetFieldID(Env
, Cls
, "mkp", "Ljava/nio/ByteBuffer;");
30 L
.mki
= (*Env
)->GetFieldID(Env
, Cls
, "mki", "Ljava/nio/ByteBuffer;");
31 L
.mkd
= (*Env
)->GetFieldID(Env
, Cls
, "mkd", "Ljava/nio/ByteBuffer;");
32 L
.bfuzz
= (*Env
)->GetFieldID(Env
, Cls
, "bfuzz", "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
.New
, (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
->opr
= 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_set_1kp(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_set_1ki(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_set_1kd(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_set_1summax(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_set_1summin(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_set_1outmax(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_set_1outmin(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_nrule(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
->nrule
;
196 JNIEXPORT jobject JNICALL
Java_liba_pid_1fuzzy_opr(JNIEnv
*Env
, jobject Obj
, jint opr
)
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_opr(ctx
, (unsigned int)opr
);
204 JNIEXPORT jint JNICALL
Java_liba_pid_1fuzzy_nfuzz(JNIEnv
*Env
, jobject Obj
)
206 jobject Ctx
= (*Env
)->GetObjectField(Env
, Obj
, L
.ctx
);
207 a_pid_fuzzy
*ctx
= (a_pid_fuzzy
*)(*Env
)->GetDirectBufferAddress(Env
, Ctx
);
208 return (jint
)ctx
->nfuzz
;
211 JNIEXPORT jobject JNICALL
Java_liba_pid_1fuzzy_set_1nfuzz(JNIEnv
*Env
, jobject Obj
, jint num
)
213 jobject Ctx
= (*Env
)->GetObjectField(Env
, Obj
, L
.ctx
);
214 a_pid_fuzzy
*ctx
= (a_pid_fuzzy
*)(*Env
)->GetDirectBufferAddress(Env
, Ctx
);
215 if (num
> (jint
)ctx
->nfuzz
)
217 jobject Bfuzz
= (*Env
)->CallObjectMethod(Env
, Obj
, L
.New
, (jint
)A_PID_FUZZY_BFUZZ((unsigned int)num
));
218 a_pid_fuzzy_set_bfuzz(ctx
, (*Env
)->GetDirectBufferAddress(Env
, Bfuzz
), (a_size
)num
);
219 (*Env
)->SetObjectField(Env
, Obj
, L
.bfuzz
, Bfuzz
);
224 static jobject
concat(JNIEnv
*Env
, jobject Obj
, jobjectArray val
, jdouble
const **out
)
227 jsize nrule
= (*Env
)->GetArrayLength(Env
, val
);
228 for (jsize idx
= 0; idx
!= nrule
; ++idx
)
230 jobject o
= (*Env
)->GetObjectArrayElement(Env
, val
, idx
);
231 length
+= (*Env
)->GetArrayLength(Env
, o
);
232 (*Env
)->DeleteLocalRef(Env
, o
);
234 jobject obj
= (*Env
)->CallObjectMethod(Env
, Obj
, L
.New
, (jint
)length
* 8);
235 jdouble
*ptr
= (jdouble
*)(*Env
)->GetDirectBufferAddress(Env
, obj
);
237 for (jsize idx
= 0, num
= 0; idx
!= nrule
; ++idx
, ptr
+= num
)
239 jobject o
= (*Env
)->GetObjectArrayElement(Env
, val
, idx
);
240 num
= (*Env
)->GetArrayLength(Env
, o
);
241 (*Env
)->GetDoubleArrayRegion(Env
, o
, 0, num
, ptr
);
242 (*Env
)->DeleteLocalRef(Env
, o
);
247 JNIEXPORT jobject JNICALL
Java_liba_pid_1fuzzy_set_1rule(JNIEnv
*Env
, jobject Obj
, jobjectArray me
, jobjectArray mec
,
248 jobjectArray mkp
, jobjectArray mki
, jobjectArray mkd
)
250 jobject Ctx
= (*Env
)->GetObjectField(Env
, Obj
, L
.ctx
);
251 a_pid_fuzzy
*ctx
= (a_pid_fuzzy
*)(*Env
)->GetDirectBufferAddress(Env
, Ctx
);
252 ctx
->nrule
= (unsigned int)(*Env
)->GetArrayLength(Env
, me
);
253 jobject Me
= concat(Env
, Obj
, me
, &ctx
->me
);
254 jobject Mec
= concat(Env
, Obj
, mec
, &ctx
->mec
);
255 jobject Mkp
= concat(Env
, Obj
, mkp
, &ctx
->mkp
);
256 jobject Mki
= concat(Env
, Obj
, mki
, &ctx
->mki
);
257 jobject Mkd
= concat(Env
, Obj
, mkd
, &ctx
->mkd
);
258 (*Env
)->SetObjectField(Env
, Obj
, L
.me
, Me
);
259 (*Env
)->SetObjectField(Env
, Obj
, L
.mec
, Mec
);
260 (*Env
)->SetObjectField(Env
, Obj
, L
.mkp
, Mkp
);
261 (*Env
)->SetObjectField(Env
, Obj
, L
.mki
, Mki
);
262 (*Env
)->SetObjectField(Env
, Obj
, L
.mkd
, Mkd
);
266 JNIEXPORT jobject JNICALL
Java_liba_pid_1fuzzy_set_1kpid(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_set_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
);