diff --git a/Core/Inc/git_rev.h b/Core/Inc/git_rev.h index c9cd8ba..63e6bd7 100644 --- a/Core/Inc/git_rev.h +++ b/Core/Inc/git_rev.h @@ -7,6 +7,6 @@ // Auto generated header file containing the last git revision -#define GIT_HASH "ff0aed1" +#define GIT_HASH "56a7d25" #endif //FLOATPUMP_GIT_REVISION_TEMPLATE_H \ No newline at end of file diff --git a/Core/Src/main.cpp b/Core/Src/main.cpp index f994082..317afd0 100644 --- a/Core/Src/main.cpp +++ b/Core/Src/main.cpp @@ -14,6 +14,7 @@ #include "Menu_Entry_Type_Time.h" #include "Config_Store.h" #include "Menu_Controller.h" +#include "PressureChannel.h" #define SLAVE_ADDRESS_LCD 0x4e @@ -87,12 +88,10 @@ int main(void) { Menu mainmenu("Hauptmenu"); Menu_Entry_Type_Checkable entry1bool(false), entry2bool(false); - bool l_calib = false, h_calib = false; - uint16_t lcb = 0, hcb = 4096; - uint8_t perc = 50; + bool l_calib, h_calib = false; uint8_t cur_perc = 0; - uint32_t current = 0; + uint16_t prescaler = 0; entry1bool.linkConfig(&l_calib); entry2bool.linkConfig(&h_calib); @@ -122,14 +121,14 @@ int main(void) { HAL_GPIO_WritePin(MPWR0_GPIO_Port, MPWR0_Pin, GPIO_PIN_SET); HAL_GPIO_WritePin(MPWR2_GPIO_Port, MPWR2_Pin, GPIO_PIN_SET); - uint16_t timed_tp = 0; - uint16_t averaging[25]; - int index_avg = 0; + + floatpump::io::PressureChannel channel(&hadc1, MPWR0_GPIO_Port, MPWR0_Pin); static int old_pos = 0; while (1) { + //render menu controller.execute(); if (rot_button) { rot_button = false; @@ -145,42 +144,27 @@ int main(void) { } HAL_Delay(100); - timed_tp++; - //Execute each second + if(l_calib) + channel.calibrateLow(); - uint32_t measured, avg = 0; + if(h_calib) + channel.calibrateHigh(); - HAL_ADC_Start(&hadc1); - HAL_ADC_PollForConversion(&hadc1, 1); - measured = HAL_ADC_GetValue(&hadc1); + l_calib = false; + h_calib = false; - averaging[index_avg % 25] = measured; + prescaler++; - for(int i = 0; i < 25; i++) { - avg += averaging[i]; - } + if(prescaler % 100 == 0) + channel.poll(); - current = avg/25; - index_avg++; + current = channel.getRaw(); + cur_perc = channel.getPercent(); - //calibrate - - if(l_calib) { - l_calib = false; - lcb = current - 10; - } - - if(h_calib) { - h_calib = false; - hcb = current + 10; - } - - cur_perc = ((current - lcb) *100)/(hcb-lcb); - - if(cur_perc > perc + 5) { + if(cur_perc > 50) { HAL_GPIO_WritePin(OCHAN0_GPIO_Port, OCHAN0_Pin, GPIO_PIN_SET); - } else if(cur_perc < perc - 5) { + } else if(cur_perc < 45) { HAL_GPIO_WritePin(OCHAN0_GPIO_Port, OCHAN0_Pin, GPIO_PIN_RESET); } } diff --git a/Middlewares/floatpump/Inc/InputChannel.h b/Middlewares/floatpump/Inc/InputChannel.h deleted file mode 100644 index 8d967c2..0000000 --- a/Middlewares/floatpump/Inc/InputChannel.h +++ /dev/null @@ -1,67 +0,0 @@ -// -// Created by robtor on 10.01.23. -// - -#ifndef FLOATPUMP_INPUTCHANNEL_H -#define FLOATPUMP_INPUTCHANNEL_H - -#include -#include "stm32f4xx_hal.h" - -namespace floatpump { - namespace io { - - class InputChannel { - - public: - InputChannel(ADC_HandleTypeDef *adc, GPIO_TypeDef *gpio, uint16_t port) : m_adc(adc), m_gpio(gpio), m_gpio_port(port) {}; - - void poll() { - //Enable measuring power - HAL_GPIO_WritePin(m_gpio, m_gpio_port, GPIO_PIN_SET); - HAL_Delay(m_cooldown); - - uint16_t average = 0; - - for(int i = 0; i < m_avg_size; i++) { - HAL_Delay(m_avg_delay); - HAL_ADC_Start(m_adc); - HAL_ADC_PollForConversion(m_adc, m_avg_delay); - average += HAL_ADC_GetValue(m_adc); - } - - m_raw = average / m_avg_size; - - m_percent = ((m_raw - m_lowcalib)*100)/(m_highcalib-m_lowcalib); - } - - uint16_t getRaw() { - return m_raw; - } - - int8_t getPercent() { - return m_percent; - } - - - private: - const uint16_t m_avg_size = 50; - const uint16_t m_avg_delay = 10; - const uint16_t m_cooldown = 500; - - uint16_t m_lowcalib = 0; - uint16_t m_highcalib = 65535; - - uint16_t m_raw = 0; - int8_t m_percent = 0; - - ADC_HandleTypeDef *m_adc; - GPIO_TypeDef *m_gpio; - uint16_t m_gpio_port; - - }; - - } // floatpump -} // io - -#endif //FLOATPUMP_INPUTCHANNEL_H diff --git a/Middlewares/floatpump/Inc/PressureChannel.h b/Middlewares/floatpump/Inc/PressureChannel.h new file mode 100644 index 0000000..91cac40 --- /dev/null +++ b/Middlewares/floatpump/Inc/PressureChannel.h @@ -0,0 +1,50 @@ +// +// Created by robtor on 10.01.23. +// + +#ifndef FLOATPUMP_PRESSURECHANNEL_H +#define FLOATPUMP_PRESSURECHANNEL_H + +#include +#include "stm32f4xx_hal.h" + +namespace floatpump::io { + + class PressureChannel { + + public: + PressureChannel(ADC_HandleTypeDef *adc, GPIO_TypeDef *gpio, uint16_t port, const uint16_t avg_size = 50);; + + void poll(); + + [[nodiscard]] uint16_t getRaw() const; + + [[nodiscard]] int8_t getPercent() const; + + [[maybe_unused]] void calibrateManualLow(uint16_t low); + + [[maybe_unused]] void calibrateManualHigh(uint16_t high); + + void calibrateLow(); + + void calibrateHigh(); + + private: + const uint16_t m_avg_size = 50; + const uint16_t m_avg_delay = 10; + const uint16_t m_cooldown = 500; + + uint16_t m_lowcalib = 0; + uint16_t m_highcalib = 65535; + + uint16_t m_raw = 0; + int8_t m_percent = 0; + + ADC_HandleTypeDef *m_adc; + GPIO_TypeDef *m_gpio; + uint16_t m_gpio_port; + }; + +} // io + +#endif //FLOATPUMP_PRESSURECHANNEL_H diff --git a/Middlewares/floatpump/Src/InputChannel.cpp b/Middlewares/floatpump/Src/InputChannel.cpp deleted file mode 100644 index 970692f..0000000 --- a/Middlewares/floatpump/Src/InputChannel.cpp +++ /dev/null @@ -1,11 +0,0 @@ -// -// Created by robtor on 10.01.23. -// - -#include "InputChannel.h" - -namespace floatpump { - namespace io { - - } // floatpump -} // io \ No newline at end of file diff --git a/Middlewares/floatpump/Src/PressureChannel.cpp b/Middlewares/floatpump/Src/PressureChannel.cpp new file mode 100644 index 0000000..8b3ec7e --- /dev/null +++ b/Middlewares/floatpump/Src/PressureChannel.cpp @@ -0,0 +1,61 @@ +// +// Created by robtor on 10.01.23. +// + +#include "PressureChannel.h" + +namespace floatpump::io { + + void PressureChannel::poll() { + //Enable measuring power + HAL_GPIO_WritePin(m_gpio, m_gpio_port, GPIO_PIN_SET); + HAL_Delay(m_cooldown); + + uint32_t average = 0; + + for (int i = 0; i < m_avg_size; i++) { + HAL_Delay(m_avg_delay); + HAL_ADC_Start(m_adc); + HAL_ADC_PollForConversion(m_adc, m_avg_delay); + average += HAL_ADC_GetValue(m_adc); + } + + HAL_GPIO_WritePin(m_gpio, m_gpio_port, GPIO_PIN_RESET); + + + m_raw = average / m_avg_size; + + m_percent = (int8_t) (((m_raw - m_lowcalib) * 100) / (m_highcalib - m_lowcalib)); + + } + + PressureChannel::PressureChannel(ADC_HandleTypeDef *adc, GPIO_TypeDef *gpio, uint16_t port, + const uint16_t avg_size) : m_adc(adc), m_gpio(gpio), m_gpio_port(port), + m_avg_size(avg_size) {} + + uint16_t PressureChannel::getRaw() const { + return m_raw; + } + + int8_t PressureChannel::getPercent() const { + return m_percent; + } + + void PressureChannel::calibrateManualLow(uint16_t low) { + m_lowcalib = low; + } + + void PressureChannel::calibrateManualHigh(uint16_t high) { + m_highcalib = high; + } + + void PressureChannel::calibrateLow() { + poll(); + m_lowcalib = m_raw; + } + + void PressureChannel::calibrateHigh() { + poll(); + m_highcalib = m_raw; + } +} \ No newline at end of file