mirror of
https://github.com/eddyem/stm32samples.git
synced 2026-03-20 00:30:57 +03:00
Chiller works
This commit is contained in:
Binary file not shown.
@@ -20,10 +20,58 @@
|
|||||||
#define HARDWARE_H
|
#define HARDWARE_H
|
||||||
#include "stm32f0.h"
|
#include "stm32f0.h"
|
||||||
|
|
||||||
// measure flow sensor data each 5 seconds
|
// measure flow sensor data each 1 second
|
||||||
#define FLOW_RATE_MS 4999
|
#define FLOW_RATE_MS (999)
|
||||||
extern uint16_t flow_rate, flow_cntr;
|
// previous as string constant
|
||||||
|
#define FLOWRATESTR "1"
|
||||||
|
|
||||||
|
// each TMEASURE_MS ms calculate temperatures & check them
|
||||||
|
#define TMEASURE_MS (1000)
|
||||||
|
// each TCHECK_MS ms check cooler state and regulate temperature
|
||||||
|
#define TCHECK_MS (10000)
|
||||||
|
|
||||||
|
/*
|
||||||
|
temperature limits and tolerances
|
||||||
|
*/
|
||||||
|
// tolerance: +-1.5degrC
|
||||||
|
#define TEMP_TOLERANCE (15)
|
||||||
|
// dT tolerance: +-0.5degrC
|
||||||
|
#define DT_TOLERANCE (5)
|
||||||
|
// maximal heater temperature - 80degrC; normal - <60
|
||||||
|
#define MAX_HEATER_T (800)
|
||||||
|
#define NORMAL_HEATER_T (600)
|
||||||
|
// maximal output temperature - 45degrC; minimal - 10
|
||||||
|
#define MAX_OUTPUT_T (450)
|
||||||
|
#define MIN_OUTPUT_T (100)
|
||||||
|
// temperature working values: from 15 to 30degrC
|
||||||
|
#define OUTPUT_T_H (300)
|
||||||
|
#define OUTPUT_T_L (150)
|
||||||
|
|
||||||
|
/*
|
||||||
|
other limits & tolerances
|
||||||
|
*/
|
||||||
|
// minimal flow rate - 0.2l per minute
|
||||||
|
#define MIN_FLOW_RATE (20)
|
||||||
|
// normal flow rate
|
||||||
|
#define NORMAL_FLOW_RATE (30)
|
||||||
|
// minimal PWM values when motors should work
|
||||||
|
#define MIN_PUMP_PWM (90)
|
||||||
|
#define MIN_COOLER_PWM (90)
|
||||||
|
|
||||||
|
// PWM setters and getters
|
||||||
|
#define SET_COOLER_PWM(N) do{TIM14->CCR1 = (uint32_t)N;}while(0)
|
||||||
|
#define GET_COOLER_PWM() (uint16_t)(TIM14->CCR1)
|
||||||
|
#define SET_HEATER_PWM(N) do{TIM16->CCR1 = (uint32_t)N;}while(0)
|
||||||
|
#define GET_HEATER_PWM() (uint16_t)(TIM16->CCR1)
|
||||||
|
#define SET_PUMP_PWM(N) do{TIM17->CCR1 = (uint32_t)N;}while(0)
|
||||||
|
#define GET_PUMP_PWM() (uint16_t)(TIM17->CCR1)
|
||||||
|
|
||||||
|
// ext. alarm states
|
||||||
|
#define ALARM_ON() pin_set(GPIOF, 2)
|
||||||
|
#define ALARM_OFF() pin_clear(GPIOF, 2)
|
||||||
|
#define ALARM_STATE() pin_read(GPIOF, 2)
|
||||||
|
|
||||||
|
extern volatile uint16_t flow_rate, flow_cntr;
|
||||||
extern volatile uint32_t Tms;
|
extern volatile uint32_t Tms;
|
||||||
|
|
||||||
void hw_setup(void);
|
void hw_setup(void);
|
||||||
|
|||||||
@@ -1,4 +1,4 @@
|
|||||||
update=Чт 20 дек 2018 22:32:48
|
update=Ср 16 янв 2019 16:46:34
|
||||||
version=1
|
version=1
|
||||||
last_client=kicad
|
last_client=kicad
|
||||||
[pcbnew]
|
[pcbnew]
|
||||||
|
|||||||
File diff suppressed because it is too large
Load Diff
@@ -18,33 +18,53 @@
|
|||||||
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
|
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
|
||||||
* MA 02110-1301, USA.
|
* MA 02110-1301, USA.
|
||||||
*/
|
*/
|
||||||
|
#include <string.h> // memcpy
|
||||||
#include "stm32f0.h"
|
#include "stm32f0.h"
|
||||||
#include "hardware.h"
|
#include "hardware.h"
|
||||||
#include "usart.h"
|
#include "usart.h"
|
||||||
#include "adc.h"
|
#include "adc.h"
|
||||||
#include "protocol.h"
|
#include "protocol.h"
|
||||||
|
#include "mainloop.h"
|
||||||
|
|
||||||
volatile uint32_t Tms = 0;
|
volatile uint32_t Tms = 0;
|
||||||
uint16_t flow_rate = 0; // flow sensor rate
|
volatile uint16_t flow_rate = 0; // flow sensor rate
|
||||||
uint16_t flow_cntr = 0; // flow sensor trigger counter
|
volatile uint16_t flow_cntr = 0; // flow sensor trigger counter
|
||||||
|
// this variable is global as user need to clear it in protocol.c
|
||||||
|
uint8_t crit_error = 0; // got critical error, need user acknowledgement
|
||||||
|
|
||||||
// Called when systick fires
|
// Called when systick fires
|
||||||
void sys_tick_handler(void){
|
void sys_tick_handler(void){
|
||||||
++Tms;
|
++Tms;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
static void print_state(uint8_t state){
|
||||||
|
if(state == ST_OK){
|
||||||
|
put_string("OK\n");
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
if(state & ST_CRITICAL) put_string("CRIT"); // add prefix "CRIT" for critical states
|
||||||
|
if(!(state & ST_OK)){ // something changed
|
||||||
|
if(state & ST_OFF) put_string("OFF");
|
||||||
|
else{
|
||||||
|
if(state & ST_FASTER) put_string("FASTER");
|
||||||
|
else put_string("SLOWER");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
put_char('\n');
|
||||||
|
}
|
||||||
|
|
||||||
int main(void){
|
int main(void){
|
||||||
uint32_t lastTflow = 0;
|
uint32_t lastTflow = 0; // last flow measurement time
|
||||||
|
chiller_state ost = {ST_OK, ST_OK, ST_OK, ST_OK}, *st; // old & current chiller states
|
||||||
char *txt;
|
char *txt;
|
||||||
hw_setup();
|
hw_setup();
|
||||||
SysTick_Config(6000, 1);
|
SysTick_Config(6000, 1);
|
||||||
usart1_send_blocking("Chiller controller v0.1\n", 0);
|
SEND_BLK("Chiller controller v0.1\n");
|
||||||
if(RCC->CSR & RCC_CSR_IWDGRSTF){ // watchdog reset occured
|
if(RCC->CSR & RCC_CSR_IWDGRSTF){ // watchdog reset occured
|
||||||
usart1_send("WDGRESET=1\n", 0);
|
SEND_BLK("WDGRESET=1");
|
||||||
}
|
}
|
||||||
if(RCC->CSR & RCC_CSR_SFTRSTF){ // software reset occured
|
if(RCC->CSR & RCC_CSR_SFTRSTF){ // software reset occured
|
||||||
usart1_send("SOFTRESET=1\n", 0);
|
SEND_BLK("SOFTRESET=1");
|
||||||
}
|
}
|
||||||
RCC->CSR |= RCC_CSR_RMVF; // remove reset flags
|
RCC->CSR |= RCC_CSR_RMVF; // remove reset flags
|
||||||
while (1){
|
while (1){
|
||||||
@@ -53,6 +73,7 @@ int main(void){
|
|||||||
lastTflow = Tms;
|
lastTflow = Tms;
|
||||||
flow_rate = flow_cntr;
|
flow_rate = flow_cntr;
|
||||||
flow_cntr = 0;
|
flow_cntr = 0;
|
||||||
|
if(crit_error) SEND("CRITICAL=1\n");
|
||||||
}
|
}
|
||||||
if(usart1_getline(&txt)){ // usart1 received command, process it
|
if(usart1_getline(&txt)){ // usart1 received command, process it
|
||||||
txt = process_command(txt);
|
txt = process_command(txt);
|
||||||
@@ -62,5 +83,29 @@ int main(void){
|
|||||||
IWDG->KR = IWDG_REFRESH;
|
IWDG->KR = IWDG_REFRESH;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
IWDG->KR = IWDG_REFRESH;
|
||||||
|
//usart1_sendbuf();
|
||||||
|
st = mainloop();
|
||||||
|
// process state values
|
||||||
|
if(st->common_state != ST_OK){
|
||||||
|
if(st->common_state & ST_CRITICAL) crit_error = 1;
|
||||||
|
put_string("STATE=");
|
||||||
|
print_state(st->common_state);
|
||||||
|
}
|
||||||
|
// other states
|
||||||
|
if(st->pump_state != ST_OK){
|
||||||
|
put_string("PUMP=");
|
||||||
|
print_state(st->pump_state);
|
||||||
|
}
|
||||||
|
if(st->cooler_state != ST_OK){
|
||||||
|
put_string("COOLER=");
|
||||||
|
print_state(st->cooler_state);
|
||||||
|
}
|
||||||
|
if(st->heater_state != ST_OK){
|
||||||
|
put_string("HEATER=");
|
||||||
|
print_state(st->heater_state);
|
||||||
|
}
|
||||||
|
memcpy(&ost, st, sizeof(chiller_state));
|
||||||
|
usart1_sendbuf();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
319
F0-nolib/Chiller/mainloop.c
Normal file
319
F0-nolib/Chiller/mainloop.c
Normal file
@@ -0,0 +1,319 @@
|
|||||||
|
/*
|
||||||
|
* This file is part of the Chiller project.
|
||||||
|
* Copyright 2019 Edward V. Emelianov <edward.emelianoff@gmail.com>.
|
||||||
|
*
|
||||||
|
* This program is free software: you can redistribute it and/or modify
|
||||||
|
* it under the terms of the GNU General Public License as published by
|
||||||
|
* the Free Software Foundation, either version 3 of the License, or
|
||||||
|
* (at your option) any later version.
|
||||||
|
*
|
||||||
|
* This program 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 this program. If not, see <http://www.gnu.org/licenses/>.
|
||||||
|
*/
|
||||||
|
|
||||||
|
#include "mainloop.h"
|
||||||
|
#include "hardware.h"
|
||||||
|
#include "adc.h"
|
||||||
|
|
||||||
|
int16_t Tset = 200; // temperature setpoint
|
||||||
|
int16_t NTCval[4] = {0,};
|
||||||
|
|
||||||
|
// common status for all functions from this file; pointer to this variable return @mainloop
|
||||||
|
static chiller_state retstatus = {
|
||||||
|
.common_state = ST_OK,
|
||||||
|
.heater_state = ST_OK,
|
||||||
|
.cooler_state = ST_OK,
|
||||||
|
.pump_state = ST_OK
|
||||||
|
};
|
||||||
|
|
||||||
|
/* error bit fields: */
|
||||||
|
// chiller_error==0 - no errors
|
||||||
|
#define CE_NOERROR (0)
|
||||||
|
// heater error (overheating)
|
||||||
|
#define CE_HEATER (1<<0)
|
||||||
|
// output was too hot
|
||||||
|
#define CE_OUTHOT (1<<1)
|
||||||
|
// output was too cool
|
||||||
|
#define CE_OUTCOOL (1<<2)
|
||||||
|
// no flow sensor pulses detected
|
||||||
|
#define CE_NOFLOW (1<<3)
|
||||||
|
|
||||||
|
// error code explaining why alarm is working
|
||||||
|
static uint8_t chiller_error;
|
||||||
|
|
||||||
|
static inline void increase_pump_pwm(){
|
||||||
|
uint16_t pwm = GET_PUMP_PWM();
|
||||||
|
if(pwm < 246){
|
||||||
|
SET_PUMP_PWM(pwm+10);
|
||||||
|
retstatus.pump_state = ST_FASTER;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
static inline void decrease_pump_pwm(){
|
||||||
|
uint16_t pwm = GET_PUMP_PWM();
|
||||||
|
if(pwm > MIN_PUMP_PWM+9){
|
||||||
|
SET_PUMP_PWM(pwm-10);
|
||||||
|
retstatus.pump_state = 0; // "ST_SLOWER"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @brief binsrch - binary search for new good value
|
||||||
|
* @param oldval - previuos value
|
||||||
|
* @param curval - current value
|
||||||
|
* @param dir - direction (1 - increase, 0 - decrease)
|
||||||
|
* @return new value
|
||||||
|
*/
|
||||||
|
static inline uint16_t binsrch(uint16_t oldval, uint16_t curval, uint8_t dir){
|
||||||
|
if(oldval == curval){
|
||||||
|
if(dir) oldval = 256;
|
||||||
|
else oldval = 0;
|
||||||
|
}else{
|
||||||
|
if(dir){ // increase
|
||||||
|
if(oldval == 0) oldval = 256;
|
||||||
|
else if(oldval < curval){
|
||||||
|
oldval = 2*curval - oldval;
|
||||||
|
}
|
||||||
|
}else{ // decrease
|
||||||
|
if(curval == 0) oldval = 0;
|
||||||
|
else if(oldval > curval){
|
||||||
|
oldval = 2*curval - oldval;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
oldval = (oldval + curval) / 2;
|
||||||
|
if(oldval > 255) oldval = 0;
|
||||||
|
return oldval;
|
||||||
|
}
|
||||||
|
|
||||||
|
// change PWM according to dir (1->up, 0->down)
|
||||||
|
static void change_heater_pwm(uint8_t dir){
|
||||||
|
static uint16_t oldpwm = 0;
|
||||||
|
uint16_t pwm = binsrch(oldpwm, GET_HEATER_PWM(), dir);
|
||||||
|
if(pwm != GET_HEATER_PWM()){
|
||||||
|
oldpwm = GET_HEATER_PWM();
|
||||||
|
SET_HEATER_PWM(pwm);
|
||||||
|
if(dir){ // up
|
||||||
|
retstatus.heater_state = ST_FASTER;
|
||||||
|
}else{ // down
|
||||||
|
if(pwm == 0) retstatus.heater_state = ST_OFF;
|
||||||
|
else retstatus.heater_state = 0; // "ST_SLOWER"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
static void change_cooler_pwm(uint8_t dir){
|
||||||
|
uint16_t pwm = GET_COOLER_PWM();
|
||||||
|
if(dir){ // up
|
||||||
|
if(pwm < 224) SET_COOLER_PWM(pwm + 32);
|
||||||
|
else SET_COOLER_PWM(255);
|
||||||
|
if(pwm != GET_COOLER_PWM())
|
||||||
|
retstatus.cooler_state = ST_FASTER;
|
||||||
|
}else{ // down
|
||||||
|
if(pwm > MIN_COOLER_PWM + 31) SET_COOLER_PWM(pwm - 32);
|
||||||
|
else SET_COOLER_PWM(0);
|
||||||
|
if(pwm != GET_COOLER_PWM())
|
||||||
|
retstatus.cooler_state = GET_COOLER_PWM() ? 0 : ST_OFF; // "ST_SLOWER" / ST_OFF
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @brief get_critical - check device for critical errors
|
||||||
|
* @return 1 if critical error occured
|
||||||
|
*/
|
||||||
|
static inline uint8_t get_critical(){
|
||||||
|
uint8_t ret = 0;
|
||||||
|
// critical state: heater can burn out!
|
||||||
|
// turn off heater & make signal
|
||||||
|
if(HEATER_TEMPERATURE > MAX_HEATER_T){
|
||||||
|
// change heater state to CRIT_HOFF
|
||||||
|
retstatus.heater_state = ST_CRITICAL;
|
||||||
|
if(GET_HEATER_PWM()){
|
||||||
|
SET_HEATER_PWM(0);
|
||||||
|
retstatus.heater_state |= ST_OFF;
|
||||||
|
}
|
||||||
|
chiller_error |= CE_HEATER;
|
||||||
|
ret = 1;
|
||||||
|
}
|
||||||
|
// very hot output: turn off heater, turn on cooler & make signal
|
||||||
|
if(OUTPUT_TEMPERATURE > MAX_OUTPUT_T){
|
||||||
|
// change chiller state to CRIT_HOT
|
||||||
|
retstatus.common_state = ST_CRITICAL;
|
||||||
|
if(GET_HEATER_PWM()){
|
||||||
|
SET_HEATER_PWM(0);
|
||||||
|
retstatus.heater_state = ST_OFF;
|
||||||
|
}
|
||||||
|
if(GET_COOLER_PWM() < 255){
|
||||||
|
SET_COOLER_PWM(255);
|
||||||
|
retstatus.cooler_state = ST_FASTER;
|
||||||
|
}
|
||||||
|
// if water @input is also too hot, turn pump to max speed
|
||||||
|
if(INPUT_TEMPERATURE > MAX_OUTPUT_T){
|
||||||
|
increase_pump_pwm();
|
||||||
|
}
|
||||||
|
chiller_error |= CE_OUTHOT;
|
||||||
|
ret = 1;
|
||||||
|
}
|
||||||
|
// very cool output: turn on heater (max power), turn off cooler & make signal
|
||||||
|
if(OUTPUT_TEMPERATURE < MIN_OUTPUT_T){
|
||||||
|
retstatus.common_state = ST_CRITICAL|ST_FASTER;
|
||||||
|
if(GET_HEATER_PWM() < 255){
|
||||||
|
SET_HEATER_PWM(255);
|
||||||
|
retstatus.heater_state = ST_FASTER;
|
||||||
|
}
|
||||||
|
if(GET_COOLER_PWM()){
|
||||||
|
SET_COOLER_PWM(0);
|
||||||
|
retstatus.cooler_state = ST_OFF;
|
||||||
|
}
|
||||||
|
chiller_error |= CE_OUTCOOL;
|
||||||
|
ret = 1;
|
||||||
|
}
|
||||||
|
// check flow rate & pump working
|
||||||
|
if(GET_PUMP_PWM() >= MIN_PUMP_PWM){ // pump working
|
||||||
|
if(flow_rate < MIN_FLOW_RATE){ // check pump
|
||||||
|
// change chiller state to CRIT_NOFLOW
|
||||||
|
retstatus.common_state = ST_CRITICAL|ST_OK;
|
||||||
|
// increase pump speed
|
||||||
|
//increase_pump_pwm();
|
||||||
|
retstatus.pump_state = ST_CRITICAL;
|
||||||
|
chiller_error |= CE_NOFLOW;
|
||||||
|
ret = 1;
|
||||||
|
}
|
||||||
|
}else{
|
||||||
|
// turn ON pump if PWM < minimal
|
||||||
|
// (pump should be never off!)
|
||||||
|
SET_PUMP_PWM(MIN_PUMP_PWM);
|
||||||
|
retstatus.pump_state = ST_FASTER;
|
||||||
|
}
|
||||||
|
return ret;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @brief check_alarm - check device status and turn off alarm if it is on
|
||||||
|
*/
|
||||||
|
static inline void check_alarm(){
|
||||||
|
if(!ALARM_STATE()) return;
|
||||||
|
// check errors & turn alarm OFF if there's no critical situations
|
||||||
|
if(chiller_error == CE_NOERROR){
|
||||||
|
// turn off alarm if there's no more errors
|
||||||
|
ALARM_OFF();
|
||||||
|
}else{
|
||||||
|
if(chiller_error & CE_HEATER){ // clear CE_HEATER if heater T is normal
|
||||||
|
if(HEATER_TEMPERATURE < NORMAL_HEATER_T){
|
||||||
|
chiller_error &= ~CE_HEATER;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if(chiller_error & CE_OUTHOT){ // clear CE_OUTHOT if Tout is normal
|
||||||
|
if(OUTPUT_TEMPERATURE < OUTPUT_T_H){
|
||||||
|
chiller_error &= ~CE_OUTHOT;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if(chiller_error & CE_OUTCOOL){ // clear CE_OUTCOOL if Tout is normal
|
||||||
|
if(OUTPUT_TEMPERATURE > OUTPUT_T_L){
|
||||||
|
chiller_error &= ~CE_OUTCOOL;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if(chiller_error & CE_NOFLOW){ // clear CE_NOFLOW if there's flow pulses
|
||||||
|
if(flow_rate > NORMAL_FLOW_RATE){
|
||||||
|
chiller_error &= ~CE_NOFLOW;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
static inline void checkOutT(){
|
||||||
|
// check that T is between limits
|
||||||
|
int8_t hc = 0; // need heating or cooling?
|
||||||
|
if(OUTPUT_TEMPERATURE > Tset + TEMP_TOLERANCE) hc = -1; // need cooling
|
||||||
|
else if(OUTPUT_TEMPERATURE < Tset - TEMP_TOLERANCE) hc = 1; // need heating
|
||||||
|
if(hc){// out of limits -> check
|
||||||
|
if(hc > 0){ // need heating: turn off cooler & turn on heater
|
||||||
|
if(GET_COOLER_PWM()){
|
||||||
|
SET_COOLER_PWM(0);
|
||||||
|
retstatus.cooler_state = ST_OFF;
|
||||||
|
}
|
||||||
|
if(GET_HEATER_PWM() < 255){
|
||||||
|
SET_HEATER_PWM(255);
|
||||||
|
retstatus.heater_state = ST_FASTER;
|
||||||
|
}else{
|
||||||
|
// bad situation: need MORE heating!
|
||||||
|
}
|
||||||
|
}else{ // need cooling: turn off heater & turn on cooler
|
||||||
|
if(GET_HEATER_PWM()){
|
||||||
|
SET_HEATER_PWM(0);
|
||||||
|
retstatus.heater_state = ST_OFF;
|
||||||
|
}
|
||||||
|
if(GET_COOLER_PWM() < 255){
|
||||||
|
SET_COOLER_PWM(255);
|
||||||
|
retstatus.cooler_state = ST_FASTER;
|
||||||
|
}else{
|
||||||
|
// bad situation: need MORE cooling!
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}else{ // T inside borders -> correct heating/cooling speed
|
||||||
|
// Tout > Tset -> heater PWM up & cooler PWM down
|
||||||
|
// else -> vice versa
|
||||||
|
uint8_t ht = 2; // don't need heater/cooler change
|
||||||
|
if(OUTPUT_TEMPERATURE < Tset - DT_TOLERANCE) ht = 1; // need heating
|
||||||
|
else if(OUTPUT_TEMPERATURE > Tset + DT_TOLERANCE) ht = 0; // need cooling
|
||||||
|
if(ht != 2){
|
||||||
|
change_heater_pwm(ht);
|
||||||
|
change_cooler_pwm(!ht);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
// if all OK, make pump slower
|
||||||
|
if(retstatus.pump_state == ST_OK){
|
||||||
|
decrease_pump_pwm();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @brief mainloop - the main chiller loop
|
||||||
|
* by timer check current states & change them
|
||||||
|
*/
|
||||||
|
chiller_state *mainloop(){
|
||||||
|
static uint32_t lastTmeas = 0xffff; // Temperatures measurement time
|
||||||
|
static uint32_t lastTchk = 0xffff; // last state checking time
|
||||||
|
retstatus.common_state = ST_OK;
|
||||||
|
retstatus.heater_state = ST_OK;
|
||||||
|
retstatus.cooler_state = ST_OK;
|
||||||
|
retstatus.pump_state = ST_OK;
|
||||||
|
// 1. Get temperatures and check critical situations
|
||||||
|
if(Tms - lastTmeas < TMEASURE_MS) return &retstatus;
|
||||||
|
lastTmeas = Tms;
|
||||||
|
for(int i = 0; i < 4; ++i) // refresh NTC values
|
||||||
|
NTCval[i] = getNTC(i);
|
||||||
|
uint8_t alrm = get_critical();
|
||||||
|
// check cooler
|
||||||
|
if(GET_COOLER_PWM() > MIN_COOLER_PWM){ // cooler working
|
||||||
|
// air temperature is very hot - cooler useless
|
||||||
|
if(AIR_TEMPERATURE > OUTPUT_TEMPERATURE + TEMP_TOLERANCE){
|
||||||
|
// change cooler state to OFF
|
||||||
|
if(GET_COOLER_PWM()){
|
||||||
|
SET_COOLER_PWM(0);
|
||||||
|
retstatus.cooler_state = ST_OFF;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}else{
|
||||||
|
if(GET_COOLER_PWM()){
|
||||||
|
SET_COOLER_PWM(0);
|
||||||
|
retstatus.cooler_state = ST_OFF;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
// check alarm
|
||||||
|
if(alrm){
|
||||||
|
ALARM_ON();
|
||||||
|
return &retstatus;
|
||||||
|
}
|
||||||
|
// there wasn't critical cases in this iteration, go further
|
||||||
|
check_alarm();
|
||||||
|
// Now check thermal data and decide what to do
|
||||||
|
if(Tms - lastTchk < TCHECK_MS) return &retstatus;
|
||||||
|
lastTchk = Tms;
|
||||||
|
checkOutT();
|
||||||
|
return &retstatus;
|
||||||
|
}
|
||||||
|
|
||||||
55
F0-nolib/Chiller/mainloop.h
Normal file
55
F0-nolib/Chiller/mainloop.h
Normal file
@@ -0,0 +1,55 @@
|
|||||||
|
/*
|
||||||
|
* This file is part of the Chiller project.
|
||||||
|
* Copyright 2019 Edward V. Emelianov <edward.emelianoff@gmail.com>.
|
||||||
|
*
|
||||||
|
* This program is free software: you can redistribute it and/or modify
|
||||||
|
* it under the terms of the GNU General Public License as published by
|
||||||
|
* the Free Software Foundation, either version 3 of the License, or
|
||||||
|
* (at your option) any later version.
|
||||||
|
*
|
||||||
|
* This program 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 this program. If not, see <http://www.gnu.org/licenses/>.
|
||||||
|
*/
|
||||||
|
#include "stm32f0.h"
|
||||||
|
|
||||||
|
// temperature setpoint
|
||||||
|
extern int16_t Tset;
|
||||||
|
|
||||||
|
// temperatures of NTC
|
||||||
|
extern int16_t NTCval[4];
|
||||||
|
// meaning of each array member: in/out, heater and air
|
||||||
|
#define TI_IDX (0)
|
||||||
|
#define TO_IDX (1)
|
||||||
|
#define TH_IDX (2)
|
||||||
|
#define TA_IDX (3)
|
||||||
|
#define INPUT_TEMPERATURE NTCval[TI_IDX]
|
||||||
|
#define OUTPUT_TEMPERATURE NTCval[TO_IDX]
|
||||||
|
#define HEATER_TEMPERATURE NTCval[TH_IDX]
|
||||||
|
#define AIR_TEMPERATURE NTCval[TA_IDX]
|
||||||
|
|
||||||
|
|
||||||
|
/* status bits */
|
||||||
|
// ==1 if no changes
|
||||||
|
#define ST_OK (1<<0)
|
||||||
|
// (ST_OK=0) == 1 if moving faster (or hotter), 0 if slower (or cooler)
|
||||||
|
#define ST_FASTER (1<<1)
|
||||||
|
// turn OFF
|
||||||
|
#define ST_OFF (1<<2)
|
||||||
|
// critical error
|
||||||
|
#define ST_CRITICAL (1<<7)
|
||||||
|
|
||||||
|
/* chiller status codes */
|
||||||
|
typedef struct{
|
||||||
|
uint8_t common_state; // common state != ST_OK if some other states changed
|
||||||
|
uint8_t heater_state;
|
||||||
|
uint8_t cooler_state;
|
||||||
|
uint8_t pump_state;
|
||||||
|
} chiller_state;
|
||||||
|
|
||||||
|
chiller_state *mainloop();
|
||||||
|
|
||||||
@@ -19,6 +19,9 @@
|
|||||||
#include "protocol.h"
|
#include "protocol.h"
|
||||||
#include "usart.h"
|
#include "usart.h"
|
||||||
#include "adc.h"
|
#include "adc.h"
|
||||||
|
#include "mainloop.h"
|
||||||
|
|
||||||
|
extern uint8_t crit_error;
|
||||||
|
|
||||||
#ifdef EBUG
|
#ifdef EBUG
|
||||||
/**
|
/**
|
||||||
@@ -71,16 +74,14 @@ static void debugging_proc(const char *command){
|
|||||||
static void get_ntc(const char *str){
|
static void get_ntc(const char *str){
|
||||||
uint8_t N = *str - '0';
|
uint8_t N = *str - '0';
|
||||||
if(N > 3) return;
|
if(N > 3) return;
|
||||||
int16_t NTC = getNTC(N);
|
|
||||||
put_string("NTC");
|
put_string("NTC");
|
||||||
put_char(*str);
|
put_char(*str);
|
||||||
put_char('=');
|
put_char('=');
|
||||||
put_int(NTC);
|
put_int(NTCval[N]);
|
||||||
}
|
}
|
||||||
|
|
||||||
#define SEND(x) usart1_send_blocking(x, 0)
|
#define STR(a) XSTR(a)
|
||||||
#define STR(a) XSTR(a)
|
#define XSTR(a) #a
|
||||||
#define XSTR(a) #a
|
|
||||||
/**
|
/**
|
||||||
* @brief process_command - command parser
|
* @brief process_command - command parser
|
||||||
* @param command - command text (all inside [] without spaces)
|
* @param command - command text (all inside [] without spaces)
|
||||||
@@ -93,20 +94,22 @@ char *process_command(const char *command){
|
|||||||
usart1_sendbuf(); // send buffer (if it is already filled)
|
usart1_sendbuf(); // send buffer (if it is already filled)
|
||||||
switch(*ptr++){
|
switch(*ptr++){
|
||||||
case '?': // help
|
case '?': // help
|
||||||
SEND(
|
SEND_BLK(
|
||||||
"Ax - alarm on(1)/off(0)\n"
|
"Ax - alarm on(1)/off(0)\n"
|
||||||
"Cx - cooler PWM\n"
|
"Cx - cooler PWM\n"
|
||||||
"F - get flow sensor rate for " STR(FLOW_RATE_MS) "ms\n"
|
"CLR- clear critical error\n"
|
||||||
|
"F - get flow sensor rate for " FLOWRATESTR "s (5880 pulses per liter)\n"
|
||||||
"Hx - heater PWM\n"
|
"Hx - heater PWM\n"
|
||||||
"L - check water level\n"
|
"L - check water level\n"
|
||||||
"Px - pump PWM\n"
|
"Px - pump PWM\n"
|
||||||
"R - reset\n"
|
"R - reset\n"
|
||||||
"Tx - get NTC temp\n"
|
"Sx - change temperature setpoint\n"
|
||||||
"t - get MCU temp\n"
|
"Tx - get NTC[x] temperature\n"
|
||||||
"V - get Vdd"
|
"t - get MCU temperature (approx.)\n"
|
||||||
|
"V - get Vdd"
|
||||||
);
|
);
|
||||||
#ifdef EBUG
|
#ifdef EBUG
|
||||||
SEND("d -> goto debug:\n"
|
SEND_BLK("d -> goto debug:\n"
|
||||||
"\tAx - get raw ADCx value\n"
|
"\tAx - get raw ADCx value\n"
|
||||||
"\tF - get flow_cntr\n"
|
"\tF - get flow_cntr\n"
|
||||||
"\tT - show raw T values\n"
|
"\tT - show raw T values\n"
|
||||||
@@ -115,17 +118,21 @@ char *process_command(const char *command){
|
|||||||
#endif
|
#endif
|
||||||
break;
|
break;
|
||||||
case 'A': // turn alarm on/off
|
case 'A': // turn alarm on/off
|
||||||
if(*ptr == '1') pin_set(GPIOF, 2);
|
if(*ptr == '1') ALARM_ON();
|
||||||
else if(*ptr == '0')pin_clear(GPIOF, 2);
|
else if(*ptr == '0') ALARM_OFF();
|
||||||
put_string("ALRM=");
|
put_string("ALRM=");
|
||||||
put_char(pin_read(GPIOF, 2) + '0');
|
put_char(ALARM_STATE() + '0');
|
||||||
break;
|
break;
|
||||||
case 'C': // cooler PWM - TIM14CH1
|
case 'C': // "CLR" - clear critical error flag, 'C' - cooler PWM - TIM14CH1
|
||||||
|
if(ptr[0] == 'L' && ptr[1] == 'R' && ptr[2] == 0){
|
||||||
|
crit_error = 0;
|
||||||
|
return "CLRERR=1\n";
|
||||||
|
}
|
||||||
if(getnum(ptr, &N) && N > -1 && N < 256){
|
if(getnum(ptr, &N) && N > -1 && N < 256){
|
||||||
TIM14->CCR1 = N;
|
SET_COOLER_PWM(N);
|
||||||
}
|
}
|
||||||
put_string("COOLERPWM=");
|
put_string("COOLERPWM=");
|
||||||
put_int(TIM14->CCR1);
|
put_int(GET_COOLER_PWM());
|
||||||
break;
|
break;
|
||||||
case 'F':
|
case 'F':
|
||||||
put_string("FLOWRATE=");
|
put_string("FLOWRATE=");
|
||||||
@@ -133,10 +140,10 @@ char *process_command(const char *command){
|
|||||||
break;
|
break;
|
||||||
case 'H': // heater PWM - TIM16CH1
|
case 'H': // heater PWM - TIM16CH1
|
||||||
if(getnum(ptr, &N) && N > -1 && N < 256){
|
if(getnum(ptr, &N) && N > -1 && N < 256){
|
||||||
TIM16->CCR1 = N;
|
SET_HEATER_PWM(N);
|
||||||
}
|
}
|
||||||
put_string("HEATERPWM=");
|
put_string("HEATERPWM=");
|
||||||
put_int(TIM16->CCR1);
|
put_int(GET_HEATER_PWM());
|
||||||
break;
|
break;
|
||||||
case 'L': // water level
|
case 'L': // water level
|
||||||
put_string("WATERLEVEL=");
|
put_string("WATERLEVEL=");
|
||||||
@@ -144,14 +151,21 @@ char *process_command(const char *command){
|
|||||||
break;
|
break;
|
||||||
case 'P': // pump PWM - TIM17CH1
|
case 'P': // pump PWM - TIM17CH1
|
||||||
if(getnum(ptr, &N) && N > -1 && N < 256){
|
if(getnum(ptr, &N) && N > -1 && N < 256){
|
||||||
TIM17->CCR1 = N;
|
SET_PUMP_PWM(N);
|
||||||
}
|
}
|
||||||
put_string("PUMPPWM=");
|
put_string("PUMPPWM=");
|
||||||
put_int(TIM17->CCR1);
|
put_int(GET_PUMP_PWM());
|
||||||
break;
|
break;
|
||||||
case 'R': // reset MCU
|
case 'R': // reset MCU
|
||||||
NVIC_SystemReset();
|
NVIC_SystemReset();
|
||||||
break;
|
break;
|
||||||
|
case 'S':
|
||||||
|
if(getnum(ptr, &N) && N > OUTPUT_T_L + TEMP_TOLERANCE && N < OUTPUT_T_H - TEMP_TOLERANCE){
|
||||||
|
Tset = N;
|
||||||
|
}
|
||||||
|
put_string("TSET=");
|
||||||
|
put_int(Tset);
|
||||||
|
break;
|
||||||
case 'T': // get temperature of NTC(x)
|
case 'T': // get temperature of NTC(x)
|
||||||
get_ntc(ptr);
|
get_ntc(ptr);
|
||||||
break;
|
break;
|
||||||
|
|||||||
@@ -41,18 +41,17 @@ static char trbuf[UARTBUFSZ+1]; // auxiliary buffer for data transmission
|
|||||||
static int trbufidx = 0;
|
static int trbufidx = 0;
|
||||||
|
|
||||||
int put_char(char c){
|
int put_char(char c){
|
||||||
if(trbufidx > UARTBUFSZ - 1) return 1;
|
if(trbufidx >= UARTBUFSZ - 1){
|
||||||
|
if(ALL_OK != usart1_sendbuf()) return 1;
|
||||||
|
}
|
||||||
trbuf[trbufidx++] = c;
|
trbuf[trbufidx++] = c;
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
// write zero-terminated string
|
// write zero-terminated string
|
||||||
int put_string(const char *str){
|
int put_string(const char *str){
|
||||||
while(trbufidx < UARTBUFSZ - 1 && *str){
|
while(*str){
|
||||||
trbuf[trbufidx++] = *str++;
|
if(put_char(*str++)) return 1; //error! shouldn't be!!!
|
||||||
}
|
}
|
||||||
//error! shouldn't be!!!
|
|
||||||
if(*str) return 1; // buffer overfull
|
|
||||||
trbuf[trbufidx] = 0;
|
|
||||||
return 0; // all OK
|
return 0; // all OK
|
||||||
}
|
}
|
||||||
/**
|
/**
|
||||||
|
|||||||
@@ -37,6 +37,10 @@ typedef enum{
|
|||||||
|
|
||||||
#define usart1ovr() (bufovr)
|
#define usart1ovr() (bufovr)
|
||||||
|
|
||||||
|
// send constant string
|
||||||
|
#define SEND_BLK(x) do{while(LINE_BUSY == usart1_send_blocking(x, sizeof(x)-1));}while(0)
|
||||||
|
#define SEND(x) do{while(LINE_BUSY == usart1_send(x, sizeof(x)-1));}while(0)
|
||||||
|
|
||||||
extern uint8_t bufovr;
|
extern uint8_t bufovr;
|
||||||
|
|
||||||
void USART1_config();
|
void USART1_config();
|
||||||
|
|||||||
@@ -1,4 +1,4 @@
|
|||||||
update=Вт 13 ноя 2018 22:27:32
|
update=Вс 06 янв 2019 17:11:01
|
||||||
version=1
|
version=1
|
||||||
last_client=kicad
|
last_client=kicad
|
||||||
[pcbnew]
|
[pcbnew]
|
||||||
|
|||||||
Reference in New Issue
Block a user