Restructuring and comments

This commit is contained in:
2021-05-22 16:40:08 +02:00
parent 7e3d2d1d0b
commit fdb3ceb3e9
10 changed files with 116 additions and 60 deletions

View File

@@ -0,0 +1,297 @@
/* Reflow Oven Controller
*
* Copyright (C) 2021 Mario Hüttel <mario.huettel@gmx.net>
*
* This file is part of the Reflow Oven Controller Project.
*
* The reflow oven controller is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License version 2 as
* published by the Free Software Foundation.
*
* The Reflow Oven Control Firmware is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with the reflow oven controller project.
* If not, see <http://www.gnu.org/licenses/>.
*/
/**
* @addtogroup temp-profile
* @{
*/
#include <reflow-controller/temp-profile/temp-profile-executer.h>
#include <reflow-controller/systick.h>
#include <helper-macros/helper-macros.h>
#include <reflow-controller/oven-driver.h>
#include <reflow-controller/temp-converter.h>
#include <reflow-controller/adc-meas.h>
#include <reflow-controller/digio.h>
#include <reflow-controller/safety/safety-controller.h>
static struct tpe_current_state IN_SECTION(.ccm.data) state = {
.status = TPE_OFF,
.start_timestamp = 0,
};
static bool IN_SECTION(.ccm.bss) pid_should_run;
struct pid_controller IN_SECTION(.ccm.bss) pid;
bool IN_SECTION(.ccm.bss) cmd_continue;
static SlList *command_list = NULL;
static void tpe_abort(void)
{
temp_profile_executer_stop();
state.status = TPE_ABORT;
}
enum pl_ret_val temp_profile_executer_start(const char *filename)
{
uint32_t parsed_count = 0;
enum pl_ret_val res;
state.setpoint = 0.0f;
state.start_timestamp = 0ULL;
state.setpoint = 0.0f;
state.step = 0;
state.profile_steps = 0;
oven_pid_stop();
pid_should_run = false;
state.status = TPE_OFF;
state.profile_steps = 0;
cmd_continue = false;
/* This should never happen... But who knows */
if (command_list) {
temp_profile_free_command_list(&command_list);
}
res = temp_profile_parse_from_file(filename, &command_list, MAX_PROFILE_LENGTH, &parsed_count);
if (res == PL_RET_SUCCESS) {
state.profile_steps = parsed_count;
state.status = TPE_RUNNING;
state.start_timestamp = systick_get_global_tick();
} else {
if (command_list)
temp_profile_free_command_list(&command_list);
}
return res;
}
static bool cmd_wait_temp(struct pl_command *cmd, bool cmd_continue)
{
(void)cmd_continue;
float resistance;
int res;
float temp;
res = adc_pt1000_get_current_resistance(&resistance);
if (res < 0) {
tpe_abort();
return false;
}
(void)temp_converter_convert_resistance_to_temp(resistance, &temp);
if (ABS(cmd->params[0] - temp) < 3.0f)
return true;
return false;
}
static bool cmd_wait_time(struct pl_command *cmd, bool cmd_continue)
{
static uint64_t temp_tick = 0UL;
if (cmd_continue) {
if (systick_ticks_have_passed(temp_tick,
(uint64_t)(cmd->params[0] * 1000.0f)))
return true;
} else {
temp_tick = systick_get_global_tick();
}
return false;
}
static void reactivate_pid_if_suspended(void)
{
if (oven_pid_get_status() == OVEN_PID_DEACTIVATED)
oven_pid_init(&pid);
pid_should_run = true;
}
static bool cmd_set_temp(struct pl_command *cmd, bool cmd_continue)
{
(void)cmd_continue;
reactivate_pid_if_suspended();
oven_pid_set_target_temperature(cmd->params[0]);
state.setpoint = cmd->params[0];
return true;
}
static bool cmd_ramp(struct pl_command *cmd, bool cmd_continue)
{
static uint64_t IN_SECTION(.ccm.bss) start_timestamp;
static float IN_SECTION(.ccm.bss) start_temp;
static float IN_SECTION(.ccm.bss) slope;
float secs_passed;
bool ret = false;
if (!cmd_continue) {
/* Init of command */
start_temp = state.setpoint;
slope = (cmd->params[0] - start_temp) / cmd->params[1];
reactivate_pid_if_suspended();
oven_pid_set_target_temperature(start_temp);
start_timestamp = systick_get_global_tick();
} else {
secs_passed = ((float)(systick_get_global_tick() - start_timestamp)) / 1000.0f;
if ((state.setpoint <= cmd->params[0] && start_temp < cmd->params[0]) ||
(state.setpoint >= cmd->params[0] && start_temp > cmd->params[0])) {
state.setpoint = start_temp + secs_passed * slope;
} else {
state.setpoint = cmd->params[0];
ret = true;
}
oven_pid_set_target_temperature(state.setpoint);
}
return ret;
}
static void cmd_ack_flags(void)
{
uint32_t flag_cnt;
uint32_t i;
enum safety_flag flag_enum;
bool status;
flag_cnt = safety_controller_get_flag_count();
for (i = 0; i < flag_cnt; i++) {
safety_controller_get_flag_by_index(i, &status, &flag_enum);
if (status)
(void)safety_controller_ack_flag(flag_enum);
}
}
int temp_profile_executer_handle(void)
{
struct pl_command *current_cmd;
static uint64_t last_tick = 0UL;
bool advance;
uint32_t next_step;
/* Return if no profile is currently executed */
if (state.status != TPE_RUNNING)
return -1;
/* Abort profile execution if oven PID is aborted. This is most likely due to some error flags */
if (oven_pid_get_status() == OVEN_PID_ABORTED && pid_should_run) {
tpe_abort();
oven_pid_stop();
return -1;
}
/* Execute Temp Profile every 100 ms. If not yet due, return */
if (!systick_ticks_have_passed(last_tick, 100))
return 0;
current_cmd = (struct pl_command *)sl_list_nth(command_list, state.step)->data;
next_step = state.step;
switch (current_cmd->cmd) {
case PL_WAIT_FOR_TIME:
advance = cmd_wait_time(current_cmd, cmd_continue);
break;
case PL_WAIT_FOR_TEMP:
advance = cmd_wait_temp(current_cmd, cmd_continue);
break;
case PL_SET_TEMP:
advance = cmd_set_temp(current_cmd, cmd_continue);
break;
case PL_LOUDSPEAKER_SET:
loudspeaker_set((uint16_t)current_cmd->params[0]);
advance = true;
break;
case PL_OFF:
oven_pid_stop();
pid_should_run = false;
advance = true;
break;
case PL_PID_CONF:
pid_init(&pid, current_cmd->params[0], /* Kd */
current_cmd->params[1], /* Ki */
current_cmd->params[2], /* Kp */
0.0f, 0.0f,
current_cmd->params[3], /* Int max */
current_cmd->params[4], /* Kd tau */
current_cmd->params[5]); /* Period */
oven_pid_init(&pid);
advance = true;
pid_should_run = true;
break;
case PL_SET_RAMP:
advance = cmd_ramp(current_cmd, cmd_continue);
break;
case PL_CLEAR_FLAGS:
cmd_ack_flags();
advance = true;
break;
default:
tpe_abort();
advance = true;
break;
}
if (advance)
next_step++;
if (next_step != state.step) {
state.step = next_step;
if (next_step >= state.profile_steps) {
(void)temp_profile_executer_stop();
} else {
cmd_continue = false;
}
} else {
cmd_continue = true;
}
last_tick = systick_get_global_tick();
return 0;
}
const struct tpe_current_state *temp_profile_executer_status(void)
{
return &state;
}
int temp_profile_executer_stop(void)
{
if (state.status == TPE_RUNNING) {
state.status = TPE_OFF;
oven_pid_stop();
}
/* Free the command list */
if (command_list)
temp_profile_free_command_list(&command_list);
loudspeaker_set(0);
return 0;
}
/** @} */

View File

@@ -0,0 +1,250 @@
/* Reflow Oven Controller
*
* Copyright (C) 2021 Mario Hüttel <mario.huettel@gmx.net>
*
* This file is part of the Reflow Oven Controller Project.
*
* The reflow oven controller is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License version 2 as
* published by the Free Software Foundation.
*
* The Reflow Oven Control Firmware is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with the reflow oven controller project.
* If not, see <http://www.gnu.org/licenses/>.
*/
/**
* @addtogroup temp-profile
* @{
*/
#include <reflow-controller/temp-profile/temp-profile-parser.h>
#include <stdint.h>
#include <string.h>
#include <stdlib.h>
#include <fatfs/ff.h>
#include <linklist-lib/singly-linked-list.h>
struct pl_command_list_map {
enum pl_command_type command;
const char * const token;
uint8_t expected_param_count;
};
/**
* @brief This list stores the command tokens and the expected number of arguments for each command
*/
static const struct pl_command_list_map cmd_list_map[_PL_NUM_CMDS] = {
{PL_PID_CONF, "pid_conf", 6u},
{PL_SET_TEMP, "temp_set", 1u},
{PL_WAIT_FOR_TEMP, "wait_temp", 1u},
{PL_WAIT_FOR_TIME, "wait_time", 1u},
{PL_SET_RAMP, "temp_ramp", 2u},
{PL_LOUDSPEAKER_SET, "beep", 1u},
{PL_OFF, "temp_off", 0u},
{PL_CLEAR_FLAGS, "clear_flags", 0u},
};
/**
* @brief Read a line in the file until a line break is detected or a comment is started.
*
* In case a comment is detected, it is still read form the file to ensure
* the next line is read afterwards
*
* @param f File to read from
* @param buffer Buffer to read in
* @param buffsize buffer size
* @return 0 if successful, -1 if disk error,
*/
static int read_line_until_comment(FIL *f, char *buffer, uint32_t buffsize)
{
char *ptr;
uint32_t i;
if (!f || !buffsize || !buffer)
return -1000;
buffer[0] = 0;
/* Read the line from file */
ptr = f_gets(buffer, (int)buffsize, f);
if (!ptr) {
buffer[0] = 0;
return -2;
}
/* Go through the line until we encounter a # sign or the end of line*/
for (i = 0; i < buffsize; i++) {
if (buffer[i] == '\n' || buffer[i] == '#') {
buffer[i] = 0;
break;
} else if (buffer[i] == 0) {
break;
}
}
return 0;
}
static const struct pl_command_list_map *string_to_command(const char *str)
{
uint32_t i;
const struct pl_command_list_map *ret = NULL;
if (!str)
return NULL;
for (i = 0u; i < _PL_NUM_CMDS; i++) {
if (!strcmp(str, cmd_list_map[i].token)) {
ret = &cmd_list_map[i];
break;
}
}
return ret;
}
static int parse_line(char *line, struct pl_command *cmd)
{
uint8_t token_idx = 0;
char *token;
const char * const delim = " \t";
const struct pl_command_list_map *map = NULL;
char *endptr;
struct pl_command c;
if (!line || !cmd)
return -1000;
token = strtok(line, delim);
if (!token) {
/* Empty line or command line */
return 1;
}
while (token && token_idx <= PROFILE_LANG_MAX_NUM_ARGS) {
switch (token_idx) {
case 0:
map = string_to_command(token);
c.cmd = map->command;
break;
default:
if (!map) {
/* No valid command found */
return -1;
}
c.params[token_idx - 1] = strtof(token, &endptr);
if (endptr == token) {
/* Invalid parameter */
return -2;
}
if (token_idx > map->expected_param_count)
return -3;
break;
}
token = strtok(NULL, delim);
token_idx++;
}
if (!map || (token_idx - 1 < map->expected_param_count)) {
return -3;
}
memcpy(cmd, &c, sizeof(struct pl_command));
return 0;
}
static SlList *copy_and_append_command_to_list(SlList *list, const struct pl_command *cmd)
{
struct pl_command *alloced_cmd;
alloced_cmd = (struct pl_command *)malloc(sizeof(struct pl_command));
memcpy(alloced_cmd, cmd, sizeof(struct pl_command));
list = sl_list_append(list, alloced_cmd);
return list;
}
enum pl_ret_val temp_profile_parse_from_file(const char *filename,
SlList **command_list,
uint32_t max_len,
uint32_t *cmds_parsed)
{
FIL script_file;
FRESULT fres;
int res;
enum pl_ret_val ret = PL_RET_SUCCESS;
char workbuff[256];
struct pl_command temp_command;
uint32_t cmd_idx;
if (!filename || !command_list || !max_len || !cmds_parsed)
return PL_RET_PARAM_ERR;
fres = f_open(&script_file, filename, FA_READ);
if (fres != FR_OK) {
ret = PL_RET_DISK_ERR;
goto exit;
}
cmd_idx = 0;
*cmds_parsed = 0;
do {
/* read in the line */
res = read_line_until_comment(&script_file, workbuff, sizeof(workbuff));
if (res < 0) {
ret = PL_RET_DISK_ERR;
goto exit_close;
}
/* Check if list already full */
if (cmd_idx >= max_len) {
ret = PL_RET_LIST_FULL;
goto exit_close;
}
/* Parse the line */
res = parse_line(workbuff, &temp_command);
if (res < 0) {
ret = PL_RET_SCRIPT_ERR;
goto exit_close;
} else if (res == 0) {
cmd_idx++;
*cmds_parsed = cmd_idx;
/* Append the temp_command to the list */
*command_list = copy_and_append_command_to_list(*command_list, &temp_command);
}
} while (!f_eof(&script_file));
exit_close:
(void)f_close(&script_file);
exit:
return ret;
}
static void delete_pl_command(void *cmd)
{
if (cmd)
free(cmd);
}
void temp_profile_free_command_list(SlList **list)
{
sl_list_free_full(*list, delete_pl_command);
*list = NULL;
}
/** @} */