From f2405e23b401bc7ccadc6dc2bed863b297fe6edb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mario=20H=C3=BCttel?= Date: Sun, 8 Nov 2020 21:35:41 +0100 Subject: [PATCH] Add settings load function for PID parameters --- stm-firmware/config-parser/config-parser.c | 11 +++- .../include/config-parser/config-parser.h | 2 +- .../settings/settings-sd-card.h | 5 +- .../reflow-controller/settings/settings.h | 19 +++++++ stm-firmware/settings/settings-sd-card.c | 57 ++++++++++++++++++- stm-firmware/settings/settings.c | 5 ++ 6 files changed, 94 insertions(+), 5 deletions(-) diff --git a/stm-firmware/config-parser/config-parser.c b/stm-firmware/config-parser/config-parser.c index db15072..d372ef5 100644 --- a/stm-firmware/config-parser/config-parser.c +++ b/stm-firmware/config-parser/config-parser.c @@ -96,7 +96,7 @@ exit: return 0; } -enum config_parser_ret config_parser_get_line(config_parser_handle_t handle, struct config_parser_entry *entry) +enum config_parser_ret config_parser_get_line(config_parser_handle_t handle, struct config_parser_entry *entry, bool force_float) { struct config_parser *p; config_parser_check_handle(handle); @@ -142,6 +142,15 @@ enum config_parser_ret config_parser_get_line(config_parser_handle_t handle, str token = strtok(NULL, token_delim); } + if (force_float) { + if (entry->type == CONFIG_PARSER_TYPE_INT) + entry->value.float_val = (float)entry->value.int_val; + if (entry->type == CONFIG_PARSER_TYPE_UINT) + entry->value.float_val = (float)entry->value.uint_val; + + entry->type = CONFIG_PARSER_TYPE_FLOAT; + } + return CONFIG_PARSER_OK; } diff --git a/stm-firmware/config-parser/include/config-parser/config-parser.h b/stm-firmware/config-parser/include/config-parser/config-parser.h index e228363..df9181b 100644 --- a/stm-firmware/config-parser/include/config-parser/config-parser.h +++ b/stm-firmware/config-parser/include/config-parser/config-parser.h @@ -82,7 +82,7 @@ config_parser_handle_t config_parser_open_file(struct config_parser *config_pars * have to be copied * @return Config parser error */ -enum config_parser_ret config_parser_get_line(config_parser_handle_t handle, struct config_parser_entry *entry); +enum config_parser_ret config_parser_get_line(config_parser_handle_t handle, struct config_parser_entry *entry, bool force_float); enum config_parser_ret config_parser_reset_to_start(config_parser_handle_t handle); diff --git a/stm-firmware/include/reflow-controller/settings/settings-sd-card.h b/stm-firmware/include/reflow-controller/settings/settings-sd-card.h index 30fb4df..a5619d0 100644 --- a/stm-firmware/include/reflow-controller/settings/settings-sd-card.h +++ b/stm-firmware/include/reflow-controller/settings/settings-sd-card.h @@ -22,8 +22,7 @@ #define __SETTINGS_SETTINGS_SD_CARD_H__ #include - -#define CALIBRATION_FILE_NAME "settings.ini" +#include int sd_card_settings_save_calibration(float sens_deviation, float offset, bool active); @@ -35,4 +34,6 @@ int sd_card_settings_save_calibration(float sens_deviation, float offset, bool a */ int sd_card_settings_try_load_calibration(float *sens_deviation, float *offset); +enum settings_load_result sd_card_settings_load_pid_oven_parameters(struct oven_pid_settings *settings); + #endif /* __SETTINGS_SETTINGS_SD_CARD_H__ */ diff --git a/stm-firmware/include/reflow-controller/settings/settings.h b/stm-firmware/include/reflow-controller/settings/settings.h index 5d7c8b2..8fe4c9c 100644 --- a/stm-firmware/include/reflow-controller/settings/settings.h +++ b/stm-firmware/include/reflow-controller/settings/settings.h @@ -24,6 +24,23 @@ #include +struct oven_pid_settings { + float kd; + float kp; + float ki; + float t_sample; + float max_integral; +}; + +enum settings_load_result { + SETT_LOAD_SUCCESS = 0, + SETT_LOAD_FILE_NOT_FOUND, + SETT_LOAD_ERR, + SETT_LOAD_DISK_ERR +}; + +#define SETTINGS_PID_PARAMETER_FILE "pid.conf" + /** * @brief Save the calibration * @param sens_deviation @@ -34,4 +51,6 @@ int settings_save_calibration(float sens_deviation, float offset, bool active); int settings_load_calibration(float *sens_dev, float *offset); +enum settings_load_result settings_load_pid_oven_parameters(struct oven_pid_settings *settings); + #endif /* __SETTINGS_SETTINGS_H__ */ diff --git a/stm-firmware/settings/settings-sd-card.c b/stm-firmware/settings/settings-sd-card.c index 2713627..f3b1a19 100644 --- a/stm-firmware/settings/settings-sd-card.c +++ b/stm-firmware/settings/settings-sd-card.c @@ -138,7 +138,7 @@ int sd_card_settings_try_load_calibration(float *sens_deviation, float *offset) p = config_parser_open_file(&parser, false, path, workbuff, sizeof(workbuff)); status = 0; do { - res = config_parser_get_line(p, &entry); + res = config_parser_get_line(p, &entry, true); if (res == CONFIG_PARSER_OK) { if (!strcmp(entry.name, "offset") && entry.type == CONFIG_PARSER_TYPE_FLOAT) { offset_loaded = true; @@ -161,3 +161,58 @@ int sd_card_settings_try_load_calibration(float *sens_deviation, float *offset) return status; } + +enum settings_load_result sd_card_settings_load_pid_oven_parameters(struct oven_pid_settings *settings) +{ + enum settings_load_result ret = SETT_LOAD_ERR; + const char * const file_name = SETTINGS_PID_PARAMETER_FILE; + struct config_parser parser; + config_parser_handle_t p; + enum config_parser_ret parse_result; + struct config_parser_entry entry; + bool kp_loaded = false, kd_loaded = false, ki_loaded = false, int_max_loaded = false, t_sample = false; + + if (!settings) { + ret = SETT_LOAD_ERR; + goto exit; + } + + workbuff[0] = 0; + p = config_parser_open_file(&parser, false, file_name, workbuff, sizeof(workbuff)); + if (!p) + goto exit; + do { + parse_result = config_parser_get_line(p, &entry, true); + if (parse_result == CONFIG_PARSER_OK) { + if (!strcmp(entry.name, "kp")) { + kp_loaded = true; + settings->kp = entry.value.float_val; + } else if (!strcmp(entry.name, "kd")) { + kd_loaded = true; + settings->kd = entry.value.float_val; + } else if (!strcmp(entry.name, "ki")) { + ki_loaded = true; + settings->ki = entry.value.float_val; + } else if (!strcmp(entry.name, "max_int")) { + int_max_loaded = true; + settings->max_integral = entry.value.float_val; + } else if (!strcmp(entry.name, "sample_period")) { + t_sample = true; + settings->t_sample = entry.value.float_val / 1000.0f; + } + } + + } while (parse_result != CONFIG_PARSER_END_REACHED && + parse_result != CONFIG_PARSER_GENERIC_ERR && + parse_result != CONFIG_PARSER_IOERR && + parse_result != CONFIG_PARSER_PARAM_ERR); + + + if (kp_loaded && kd_loaded && ki_loaded && t_sample && int_max_loaded) + ret = SETT_LOAD_SUCCESS; + + config_parser_close_file(p); + +exit: + return ret; +} diff --git a/stm-firmware/settings/settings.c b/stm-firmware/settings/settings.c index 99b4b2c..91db21c 100644 --- a/stm-firmware/settings/settings.c +++ b/stm-firmware/settings/settings.c @@ -31,3 +31,8 @@ int settings_load_calibration(float *sens_dev, float *offset) { return sd_card_settings_try_load_calibration(sens_dev, offset); } + +enum settings_load_result settings_load_pid_oven_parameters(struct oven_pid_settings *settings) +{ + return sd_card_settings_load_pid_oven_parameters(settings); +}