diff --git a/examples/fuzzypid/fuzzypid1.py b/examples/fuzzypid/fuzzypid1.py index 51d018992..a3d65f8c5 100644 --- a/examples/fuzzypid/fuzzypid1.py +++ b/examples/fuzzypid/fuzzypid1.py @@ -41,19 +41,16 @@ fuzzy_pid_params = FuzzyPIDParams([ [0.65, 0, 0, 0, 0, 0, 1], [-0.34, 0, 0, 0, 0, 0, 1], [-1.1, 0, 0, 0, 0, 0, 1], - [-2.4, 0, 0, 0, 0, 0, 1], - [1.2, 0, 0, 0, 0, 0, 1], - [1.2, 0.05, 0.1, 0, 0, 0, 1], ]) -direct = PIDDirect([True, False, False, False, True, True]) +direct = PIDDirect([True, False, False]) fuzzy_pid_controller = FuzzyPIDController( rule_base, mf_params, fuzzy_pid_params, direct=direct) max_error = 100 middle_pwm_output = 500 -control_id = 5 +control_id = 2 real = 0 idea = max_error * 0.9 print("idea value: ", idea) diff --git a/package/fuzzypid/_fuzzypid.c b/package/fuzzypid/_fuzzypid.c index 6d64d9e24..3c588391d 100644 --- a/package/fuzzypid/_fuzzypid.c +++ b/package/fuzzypid/_fuzzypid.c @@ -168,12 +168,19 @@ void _fuzzypid_FuzzyPIDController___init__(PikaObj* self, int* rules = obj_getPtr(rule_base, "rules"); int* mf_params_mat = obj_getPtr(mf_params, "mf_params"); float* fuzzy_pid_params = obj_getPtr(pid_params, "fuzzy_pid_params"); - int DOF = obj_getInt(pid_params, "DOF"); + int DOF_pid_parms = obj_getInt(pid_params, "DOF"); + int DOF_direct = obj_getInt(direct, "DOF"); + if (DOF_pid_parms != DOF_direct) { + obj_setSysOut(self, + "Error: pid_params and direct must have the same length"); + obj_setErrorCode(self, -__LINE__); + return; + } struct PID** pid_vector = fuzzy_pid_vector_init( (float(*)[pid_params_count])fuzzy_pid_params, delta_k, mf_type, fo_type, - df_type, mf_params_mat, (int(*)[pid_params_count])rules, DOF); + df_type, mf_params_mat, (int(*)[pid_params_count])rules, DOF_pid_parms); obj_setPtr(self, "pid_vector", pid_vector); - obj_setInt(self, "DOF", DOF); + obj_setInt(self, "DOF", DOF_pid_parms); obj_setPtr(self, "direct_vector", obj_getPtr(direct, "direct_vector")); } @@ -191,6 +198,12 @@ pika_float _fuzzypid_FuzzyPIDController_compute_output(PikaObj* self, pika_float input) { struct PID** pid_vector = obj_getPtr(self, "pid_vector"); pika_bool* direct_vector = obj_getPtr(self, "direct_vector"); + int DOF = obj_getInt(self, "DOF"); + if (control_id < 0 || control_id >= DOF) { + obj_setSysOut(self, "Error: control_id out of range"); + obj_setErrorCode(self, -__LINE__); + return 0; + } return fuzzy_pid_motor_pwd_output(real, input, direct_vector[control_id], pid_vector[control_id]); } diff --git a/port/linux/package/pikascript/pikascript-lib/fuzzypid/_fuzzypid.c b/port/linux/package/pikascript/pikascript-lib/fuzzypid/_fuzzypid.c index 6d64d9e24..3c588391d 100644 --- a/port/linux/package/pikascript/pikascript-lib/fuzzypid/_fuzzypid.c +++ b/port/linux/package/pikascript/pikascript-lib/fuzzypid/_fuzzypid.c @@ -168,12 +168,19 @@ void _fuzzypid_FuzzyPIDController___init__(PikaObj* self, int* rules = obj_getPtr(rule_base, "rules"); int* mf_params_mat = obj_getPtr(mf_params, "mf_params"); float* fuzzy_pid_params = obj_getPtr(pid_params, "fuzzy_pid_params"); - int DOF = obj_getInt(pid_params, "DOF"); + int DOF_pid_parms = obj_getInt(pid_params, "DOF"); + int DOF_direct = obj_getInt(direct, "DOF"); + if (DOF_pid_parms != DOF_direct) { + obj_setSysOut(self, + "Error: pid_params and direct must have the same length"); + obj_setErrorCode(self, -__LINE__); + return; + } struct PID** pid_vector = fuzzy_pid_vector_init( (float(*)[pid_params_count])fuzzy_pid_params, delta_k, mf_type, fo_type, - df_type, mf_params_mat, (int(*)[pid_params_count])rules, DOF); + df_type, mf_params_mat, (int(*)[pid_params_count])rules, DOF_pid_parms); obj_setPtr(self, "pid_vector", pid_vector); - obj_setInt(self, "DOF", DOF); + obj_setInt(self, "DOF", DOF_pid_parms); obj_setPtr(self, "direct_vector", obj_getPtr(direct, "direct_vector")); } @@ -191,6 +198,12 @@ pika_float _fuzzypid_FuzzyPIDController_compute_output(PikaObj* self, pika_float input) { struct PID** pid_vector = obj_getPtr(self, "pid_vector"); pika_bool* direct_vector = obj_getPtr(self, "direct_vector"); + int DOF = obj_getInt(self, "DOF"); + if (control_id < 0 || control_id >= DOF) { + obj_setSysOut(self, "Error: control_id out of range"); + obj_setErrorCode(self, -__LINE__); + return 0; + } return fuzzy_pid_motor_pwd_output(real, input, direct_vector[control_id], pid_vector[control_id]); } diff --git a/port/linux/test/python/fuzzypid/fuzzypid1.py b/port/linux/test/python/fuzzypid/fuzzypid1.py index 51d018992..a3d65f8c5 100644 --- a/port/linux/test/python/fuzzypid/fuzzypid1.py +++ b/port/linux/test/python/fuzzypid/fuzzypid1.py @@ -41,19 +41,16 @@ fuzzy_pid_params = FuzzyPIDParams([ [0.65, 0, 0, 0, 0, 0, 1], [-0.34, 0, 0, 0, 0, 0, 1], [-1.1, 0, 0, 0, 0, 0, 1], - [-2.4, 0, 0, 0, 0, 0, 1], - [1.2, 0, 0, 0, 0, 0, 1], - [1.2, 0.05, 0.1, 0, 0, 0, 1], ]) -direct = PIDDirect([True, False, False, False, True, True]) +direct = PIDDirect([True, False, False]) fuzzy_pid_controller = FuzzyPIDController( rule_base, mf_params, fuzzy_pid_params, direct=direct) max_error = 100 middle_pwm_output = 500 -control_id = 5 +control_id = 2 real = 0 idea = max_error * 0.9 print("idea value: ", idea)