Move main loop implementation torwards target
This commit is contained in:
		@@ -21,4 +21,10 @@
 | 
			
		||||
#ifndef __OVEN_DRIVER_H__
 | 
			
		||||
#define __OVEN_DRIVER_H__
 | 
			
		||||
 | 
			
		||||
#include <stdint.h>
 | 
			
		||||
 | 
			
		||||
void oven_driver_init(void);
 | 
			
		||||
 | 
			
		||||
void oven_driver_set_power(uint8_t power);
 | 
			
		||||
 | 
			
		||||
#endif /* __OVEN_DRIVER_H__ */
 | 
			
		||||
 
 | 
			
		||||
@@ -34,18 +34,19 @@
 | 
			
		||||
#include <reflow-controller/systick.h>
 | 
			
		||||
#include <reflow-controller/adc-meas.h>
 | 
			
		||||
#include <reflow-controller/shell.h>
 | 
			
		||||
#include <reflow-controller/pid-controller.h>
 | 
			
		||||
#include <reflow-controller/ui/lcd.h>
 | 
			
		||||
#include <reflow-controller/digio.h>
 | 
			
		||||
#include "fatfs/shimatta_sdio_driver/shimatta_sdio.h"
 | 
			
		||||
#include <reflow-controller/temp-converter.h>
 | 
			
		||||
#include <reflow-controller/rotary-encoder.h>
 | 
			
		||||
#include <reflow-controller/pid-controller.h>
 | 
			
		||||
#include <stm-periph/stm32-gpio-macros.h>
 | 
			
		||||
#include <stm-periph/clock-enable-manager.h>
 | 
			
		||||
#include <stm-periph/uart.h>
 | 
			
		||||
#include <reflow-controller/shell-uart-config.h>
 | 
			
		||||
#include <helper-macros/helper-macros.h>
 | 
			
		||||
#include <reflow-controller/button.h>
 | 
			
		||||
#include <reflow-controller/oven-driver.h>
 | 
			
		||||
#include <fatfs/ff.h>
 | 
			
		||||
 | 
			
		||||
static void setup_nvic_priorities()
 | 
			
		||||
@@ -59,11 +60,13 @@ static void setup_nvic_priorities()
 | 
			
		||||
	NVIC_SetPriority(DMA2_Stream7_IRQn, 3);
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
/* Process parameters are defined static globally to be watched in debugger from any context */
 | 
			
		||||
static float pt1000_value;
 | 
			
		||||
static volatile int pt1000_value_status;
 | 
			
		||||
static uint32_t rot;
 | 
			
		||||
static volatile float pid_out;
 | 
			
		||||
static volatile float current_temperature;
 | 
			
		||||
static float target_temperature;
 | 
			
		||||
static struct pid_controller pid;
 | 
			
		||||
static volatile enum button_state button;
 | 
			
		||||
 | 
			
		||||
FATFS fs;
 | 
			
		||||
FATFS *fs_ptr = &fs;
 | 
			
		||||
@@ -81,7 +84,7 @@ static inline void uart_gpio_config()
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
static char shell_uart_tx_buff[128];
 | 
			
		||||
static char shell_uart_rx_buff[32];
 | 
			
		||||
static char shell_uart_rx_buff[48];
 | 
			
		||||
struct stm_uart shell_uart;
 | 
			
		||||
 | 
			
		||||
static shellmatta_retCode_t write_shell_callback(const char *data, uint32_t len)
 | 
			
		||||
@@ -133,31 +136,31 @@ static bool mount_sd_card_if_avail(bool mounted)
 | 
			
		||||
	return mounted;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
const char *oven_controller_hello_world = "Hello world :)\n";
 | 
			
		||||
static volatile enum button_state button;
 | 
			
		||||
static volatile uint32_t main_loops_per_ms;
 | 
			
		||||
int main()
 | 
			
		||||
static inline int32_t handle_pid_controller(struct pid_controller *pid, float target_temperature,
 | 
			
		||||
					    float current_pt1000_resistance)
 | 
			
		||||
{
 | 
			
		||||
	char disp[4][21] = {0};
 | 
			
		||||
	bool sd_card_mounted = false;
 | 
			
		||||
	FIL test_file;
 | 
			
		||||
	const char *uart_input;
 | 
			
		||||
	size_t uart_input_len;
 | 
			
		||||
	shellmatta_handle_t shell_handle;
 | 
			
		||||
	int uart_receive_status;
 | 
			
		||||
	float current_temperature;
 | 
			
		||||
	int32_t pid_out;
 | 
			
		||||
 | 
			
		||||
	uint32_t main_loop_cnt = 0UL;
 | 
			
		||||
	uint64_t ms_stamp = 0ULL;
 | 
			
		||||
	(void)temp_converter_convert_resistance_to_temp(current_pt1000_resistance, (float *)¤t_temperature);
 | 
			
		||||
	pid_out = (int32_t)pid_sample(pid, target_temperature - current_temperature);
 | 
			
		||||
 | 
			
		||||
	static struct pid_controller pid;
 | 
			
		||||
	uint64_t pid_timestamp = 0ULL;
 | 
			
		||||
	uint64_t display_timestamp = 0ULL;
 | 
			
		||||
	/* Blink green LED */
 | 
			
		||||
	led_set(1, !led_get(1));
 | 
			
		||||
 | 
			
		||||
	return pid_out;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
const char *oven_controller_hello_world = "Hello world :)\n";
 | 
			
		||||
 | 
			
		||||
static inline void setup_system()
 | 
			
		||||
{
 | 
			
		||||
 | 
			
		||||
	setup_nvic_priorities();
 | 
			
		||||
	systick_setup();
 | 
			
		||||
 | 
			
		||||
	adc_pt1000_setup_meas();
 | 
			
		||||
 | 
			
		||||
	oven_driver_init();
 | 
			
		||||
	digio_setup_default_all();
 | 
			
		||||
	led_setup();
 | 
			
		||||
	loudspeaker_setup();
 | 
			
		||||
@@ -167,9 +170,26 @@ int main()
 | 
			
		||||
 | 
			
		||||
	uart_gpio_config();
 | 
			
		||||
	setup_sell_uart(&shell_uart);
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
	lcd_home();
 | 
			
		||||
	lcd_string("Reflow");
 | 
			
		||||
int main()
 | 
			
		||||
{
 | 
			
		||||
	bool sd_card_mounted = false;
 | 
			
		||||
	FIL test_file;
 | 
			
		||||
	const char *uart_input;
 | 
			
		||||
	size_t uart_input_len;
 | 
			
		||||
	shellmatta_handle_t shell_handle;
 | 
			
		||||
	int uart_receive_status;
 | 
			
		||||
 | 
			
		||||
	uint64_t pid_timestamp = 0ULL;
 | 
			
		||||
	bool pid_controller_active = false;
 | 
			
		||||
	int32_t pid_controller_output;
 | 
			
		||||
	uint64_t display_timestamp = 0ULL;
 | 
			
		||||
	char disp[4][21] = {0};
 | 
			
		||||
 | 
			
		||||
	target_temperature = 25.0f;
 | 
			
		||||
 | 
			
		||||
	setup_system();
 | 
			
		||||
 | 
			
		||||
	shell_handle = shell_init(write_shell_callback);
 | 
			
		||||
	shell_print_motd(shell_handle);
 | 
			
		||||
@@ -185,42 +205,43 @@ int main()
 | 
			
		||||
	pid_zero(&pid);
 | 
			
		||||
 | 
			
		||||
	while (1) {
 | 
			
		||||
		if (systick_get_global_tick() - ms_stamp >= 1) {
 | 
			
		||||
			ms_stamp = systick_get_global_tick();
 | 
			
		||||
			main_loops_per_ms = main_loop_cnt;
 | 
			
		||||
			main_loop_cnt = 0UL;
 | 
			
		||||
		}
 | 
			
		||||
		sd_card_mounted = mount_sd_card_if_avail(sd_card_mounted);
 | 
			
		||||
 | 
			
		||||
		pt1000_value_status = adc_pt1000_get_current_resistance(&pt1000_value);
 | 
			
		||||
 | 
			
		||||
		if (pt1000_value_status >= 0) {
 | 
			
		||||
			(void)temp_converter_convert_resistance_to_temp(pt1000_value, (float *)¤t_temperature);
 | 
			
		||||
			if (systick_ticks_have_passed(pid_timestamp, 250)) {
 | 
			
		||||
 | 
			
		||||
				pid_out = pid_sample(&pid, 100.0 - current_temperature);
 | 
			
		||||
				pid_timestamp = systick_get_global_tick();
 | 
			
		||||
				snprintf(&disp[2][0], 21, "Temp: %.1f C", current_temperature);
 | 
			
		||||
				led_set(1, !led_get(1));
 | 
			
		||||
			}
 | 
			
		||||
		if (systick_ticks_have_passed(pid_timestamp, 250)) {
 | 
			
		||||
			pid_timestamp = systick_get_global_tick();
 | 
			
		||||
			if (pt1000_value_status >= 0 && pid_controller_active)
 | 
			
		||||
				pid_controller_output = handle_pid_controller(&pid, target_temperature, pt1000_value);
 | 
			
		||||
 | 
			
		||||
			/* Blink red led in case of temp error */
 | 
			
		||||
			if (pt1000_value_status < 0)
 | 
			
		||||
				led_set(0, !led_get(0));
 | 
			
		||||
		}
 | 
			
		||||
 | 
			
		||||
		//pid_timestamp = systick_get_global_tick();
 | 
			
		||||
		/* Handle error in case PID controller should be active, but temperature measurement failed */
 | 
			
		||||
		if (pid_controller_active && pt1000_value_status < 0) {
 | 
			
		||||
			/* Disable the oven controller */
 | 
			
		||||
			oven_driver_set_power(0U);
 | 
			
		||||
 | 
			
		||||
			/* Activate loundspeaker permanently */
 | 
			
		||||
			loudspeaker_set(1);
 | 
			
		||||
		} else if (pid_controller_active) {
 | 
			
		||||
			/* In case temperature measuremnt is okay and controlelr is working, write output power */
 | 
			
		||||
			oven_driver_set_power(pid_controller_output < 0 ? 0U : pid_controller_output);
 | 
			
		||||
		}
 | 
			
		||||
 | 
			
		||||
		button = button_read_event();
 | 
			
		||||
		rot = rotary_encoder_get_abs_val();
 | 
			
		||||
 | 
			
		||||
		/* TODO: handle gui */
 | 
			
		||||
 | 
			
		||||
		/* Handle uart input for shell */
 | 
			
		||||
		uart_receive_status = uart_receive_data_with_dma(&shell_uart, &uart_input, &uart_input_len);
 | 
			
		||||
		if (uart_receive_status >= 1)
 | 
			
		||||
			shell_handle_input(shell_handle, uart_input, uart_input_len);
 | 
			
		||||
 | 
			
		||||
		main_loop_cnt++;
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
		rot = rotary_encoder_get_abs_val();
 | 
			
		||||
		snprintf(&disp[0][0], 21U, "Rot-Enc: %" PRIu32, rot);
 | 
			
		||||
		strcpy(&disp[1][0], "Line 2");
 | 
			
		||||
		strcpy(&disp[3][0], "Shimatta Reflow");
 | 
			
		||||
 | 
			
		||||
		if (systick_ticks_have_passed(display_timestamp, 1)) {
 | 
			
		||||
			display_timestamp = systick_get_global_tick();
 | 
			
		||||
			lcd_fsm_write_buffer(disp);
 | 
			
		||||
 
 | 
			
		||||
@@ -19,3 +19,13 @@
 | 
			
		||||
*/
 | 
			
		||||
 | 
			
		||||
#include <reflow-controller/oven-driver.h>
 | 
			
		||||
 | 
			
		||||
void oven_driver_init()
 | 
			
		||||
{
 | 
			
		||||
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
void oven_driver_set_power(uint8_t power)
 | 
			
		||||
{
 | 
			
		||||
 | 
			
		||||
}
 | 
			
		||||
 
 | 
			
		||||
		Reference in New Issue
	
	Block a user