From cf5e79fc69420eb790fdd41986e47366f9cc1e87 Mon Sep 17 00:00:00 2001 From: "ucart@co3050-12" <dawehr@iastate.edu> Date: Wed, 1 Feb 2017 12:34:28 -0600 Subject: [PATCH] Changed logic in callbacks to reduce indentation. see cb_setparam, cb_getparam --- quad/sw/modular_quad_pid/src/callbacks.c | 144 ++++++++++++----------- 1 file changed, 74 insertions(+), 70 deletions(-) diff --git a/quad/sw/modular_quad_pid/src/callbacks.c b/quad/sw/modular_quad_pid/src/callbacks.c index 8c6661dad..3555fb458 100644 --- a/quad/sw/modular_quad_pid/src/callbacks.c +++ b/quad/sw/modular_quad_pid/src/callbacks.c @@ -112,35 +112,37 @@ int cb_setparam(modular_structs_t *structs) // Get some of the meta data u16 data_len = uart_buff_get_u16(6); // Check if the data length is correct - if (data_len == 6) + if (data_len != 6) { - // Get the controller ID, parameter ID, parameter value - u8 controller_id = uart_buff_data_get_u8(0); - u8 param_id = uart_buff_data_get_u8(1); - float param_val = uart_buff_data_get_float(3); - - // Check to make sure the IDs are in bounds - if (controller_id < MAX_CONTROLLER_ID && - param_id < MAX_CONTROL_PARAM_ID) - { - // Set the param_val into the controller by controller_id, param_id - switch(param_id) - { - case KP_ID: - structs->parameter_struct.pid_controllers[controller_id].Kp = param_val; - break; - case KI_ID: - structs->parameter_struct.pid_controllers[controller_id].Ki = param_val; - break; - case KD_ID: - structs->parameter_struct.pid_controllers[controller_id].Kd = param_val; - break; - case SP_ID: - structs->parameter_struct.pid_controllers[controller_id].setpoint = param_val; - break; - } - } + return -1; + } + + // Get the controller ID, parameter ID, parameter value + u8 controller_id = uart_buff_data_get_u8(0); + u8 param_id = uart_buff_data_get_u8(1); + float param_val = uart_buff_data_get_float(3); + // Check to make sure the IDs are in bounds + if (controller_id >= MAX_CONTROLLER_ID || + param_id >= MAX_CONTROL_PARAM_ID) + { + return -1; + } + // Set the param_val into the controller by controller_id, param_id + switch(param_id) + { + case KP_ID: + structs->parameter_struct.pid_controllers[controller_id].Kp = param_val; + break; + case KI_ID: + structs->parameter_struct.pid_controllers[controller_id].Ki = param_val; + break; + case KD_ID: + structs->parameter_struct.pid_controllers[controller_id].Kd = param_val; + break; + case SP_ID: + structs->parameter_struct.pid_controllers[controller_id].setpoint = param_val; + break; } return 0; @@ -176,51 +178,53 @@ int cb_getparam(modular_structs_t* structs) u16 data_len = uart_buff_get_u16(6); u16 msg_id = uart_buff_get_u16(3); // Check if the data length is correct - if (data_len == 2) + if (data_len != 2) + { + return -1; + } + + // Get the controller ID, parameter ID + u8 controller_id = uart_buff_data_get_u8(0); + u8 param_id = uart_buff_data_get_u8(1); + // Check to make sure the IDs are in bounds + if (controller_id >= MAX_CONTROLLER_ID || + param_id >= MAX_CONTROL_PARAM_ID) + { + return -1; + } + + // Make the variable to send + float param_val; + // Set the param_val equal to the parameter value stored in the controller by + // controller_id, param_id + switch(param_id) { - // Get the controller ID, parameter ID - u8 controller_id = uart_buff_data_get_u8(0); - u8 param_id = uart_buff_data_get_u8(1); - - // Check to make sure the IDs are in bounds - if (controller_id < MAX_CONTROLLER_ID && - param_id < MAX_CONTROL_PARAM_ID) - { - // Make the variable to send - float param_val; - // Set the param_val equal to the parameter value stored in the controller by - // controller_id, param_id - switch(param_id) - { - case KP_ID: - param_val = structs->parameter_struct.pid_controllers[controller_id].Kp; - break; - case KI_ID: - param_val = structs->parameter_struct.pid_controllers[controller_id].Ki; - break; - case KD_ID: - param_val = structs->parameter_struct.pid_controllers[controller_id].Kd; - break; - case SP_ID: - param_val = structs->parameter_struct.pid_controllers[controller_id].setpoint; - break; - } - - // Format the response data - char resp_data[6]; - - // Controller ID - resp_data[0] = controller_id; - // Parameter ID - resp_data[1] = param_id; - // Parameter value (4 byte float) - // TODO set a strict byte ordering for communication between the ground station and the quad - memcpy(&resp_data[2], ¶m_val, sizeof(param_val)); - - // Send the response - send_data(RESPPARAM_ID, msg_id, resp_data, sizeof(resp_data)); - } + case KP_ID: + param_val = structs->parameter_struct.pid_controllers[controller_id].Kp; + break; + case KI_ID: + param_val = structs->parameter_struct.pid_controllers[controller_id].Ki; + break; + case KD_ID: + param_val = structs->parameter_struct.pid_controllers[controller_id].Kd; + break; + case SP_ID: + param_val = structs->parameter_struct.pid_controllers[controller_id].setpoint; + break; } + // Format the response data + char resp_data[6]; + // Controller ID + resp_data[0] = controller_id; + // Parameter ID + resp_data[1] = param_id; + // Parameter value (4 byte float) + // TODO set a strict byte ordering for communication between the ground station and the quad + memcpy(&resp_data[2], ¶m_val, sizeof(param_val)); + + // Send the response + send_data(RESPPARAM_ID, msg_id, resp_data, sizeof(resp_data)); + return 0; } -- GitLab