FEATURE: Implemented Output relay channel with cooldown function
This commit is contained in:
parent
68d6346d72
commit
6fb5c3ff79
@ -7,6 +7,6 @@
|
||||
|
||||
// Auto generated header file containing the last git revision
|
||||
|
||||
#define GIT_HASH "56a7d25"
|
||||
#define GIT_HASH "a7d18ec"
|
||||
|
||||
#endif //FLOATPUMP_GIT_REVISION_TEMPLATE_H
|
@ -15,6 +15,7 @@
|
||||
#include "Config_Store.h"
|
||||
#include "Menu_Controller.h"
|
||||
#include "PressureChannel.h"
|
||||
#include "RelayChannel.h"
|
||||
|
||||
#define SLAVE_ADDRESS_LCD 0x4e
|
||||
|
||||
@ -92,6 +93,7 @@ int main(void) {
|
||||
uint8_t cur_perc = 0;
|
||||
uint32_t current = 0;
|
||||
uint16_t prescaler = 0;
|
||||
uint32_t curCooldown = 0;
|
||||
|
||||
entry1bool.linkConfig(&l_calib);
|
||||
entry2bool.linkConfig(&h_calib);
|
||||
@ -116,6 +118,13 @@ int main(void) {
|
||||
mainmenu.addEntry(entry4);
|
||||
mainmenu.addEntry(entry5);
|
||||
|
||||
Menu_Entry_Type_ReadOnly<uint32_t> cdd(0);
|
||||
cdd.linkConfig(&curCooldown);
|
||||
Menu_Entry entry6(cdd, "Cooldown");
|
||||
|
||||
mainmenu.addEntry(entry6);
|
||||
|
||||
|
||||
Menu_Controller controller(&mainmenu, display);
|
||||
|
||||
HAL_GPIO_WritePin(MPWR0_GPIO_Port, MPWR0_Pin, GPIO_PIN_SET);
|
||||
@ -123,7 +132,8 @@ int main(void) {
|
||||
|
||||
|
||||
floatpump::io::PressureChannel channel(&hadc1, MPWR0_GPIO_Port, MPWR0_Pin);
|
||||
|
||||
floatpump::io::RelayChannel relay(OCHAN0_GPIO_Port, OCHAN0_Pin, false);
|
||||
relay.setCooldown(10000);
|
||||
|
||||
static int old_pos = 0;
|
||||
while (1) {
|
||||
@ -145,15 +155,21 @@ int main(void) {
|
||||
HAL_Delay(100);
|
||||
|
||||
|
||||
if(l_calib)
|
||||
channel.calibrateLow();
|
||||
if(l_calib) {
|
||||
relay.switchRelay(floatpump::io::RelayChannel::state::ON);
|
||||
}
|
||||
//channel.calibrateLow();
|
||||
|
||||
if(h_calib)
|
||||
channel.calibrateHigh();
|
||||
if(h_calib) {
|
||||
relay.switchRelay(floatpump::io::RelayChannel::state::OFF);
|
||||
}
|
||||
//channel.calibrateHigh();
|
||||
|
||||
l_calib = false;
|
||||
h_calib = false;
|
||||
|
||||
curCooldown = relay.getRemainingCooldown();
|
||||
|
||||
prescaler++;
|
||||
|
||||
if(prescaler % 100 == 0)
|
||||
@ -162,11 +178,11 @@ int main(void) {
|
||||
current = channel.getRaw();
|
||||
cur_perc = channel.getPercent();
|
||||
|
||||
if(cur_perc > 50) {
|
||||
HAL_GPIO_WritePin(OCHAN0_GPIO_Port, OCHAN0_Pin, GPIO_PIN_SET);
|
||||
/*if(cur_perc > 50) {
|
||||
relay.switchRelay(floatpump::io::RelayChannel::state::ON);
|
||||
} else if(cur_perc < 45) {
|
||||
HAL_GPIO_WritePin(OCHAN0_GPIO_Port, OCHAN0_Pin, GPIO_PIN_RESET);
|
||||
}
|
||||
relay.switchRelay(floatpump::io::RelayChannel::state::OFF);
|
||||
}*/
|
||||
}
|
||||
}
|
||||
|
||||
|
70
Middlewares/floatpump/Inc/RelayChannel.h
Normal file
70
Middlewares/floatpump/Inc/RelayChannel.h
Normal file
@ -0,0 +1,70 @@
|
||||
//
|
||||
// Created by robtor on 10.01.23.
|
||||
//
|
||||
|
||||
#ifndef FLOATPUMP_RELAYCHANNEL_H
|
||||
#define FLOATPUMP_RELAYCHANNEL_H
|
||||
|
||||
#include "stm32f4xx_hal.h"
|
||||
|
||||
namespace floatpump {
|
||||
namespace io {
|
||||
|
||||
class RelayChannel {
|
||||
public:
|
||||
|
||||
enum state {
|
||||
ON, OFF
|
||||
};
|
||||
|
||||
RelayChannel(GPIO_TypeDef *gpio, uint16_t pin, bool inverted = false, state initial = state::OFF): m_gpio(gpio), m_gpio_pin(pin), m_inverted(inverted) {
|
||||
if(initial == state::ON)
|
||||
switchRelay(state::ON);
|
||||
};
|
||||
|
||||
void switchRelay(state st) {
|
||||
uint32_t elapesd = HAL_GetTick() - m_lastTick;
|
||||
|
||||
if(st == state::ON && m_state == state::OFF && getRemainingCooldown() == 0) {
|
||||
//Check if cooldown expired and enable Relay if true
|
||||
HAL_GPIO_WritePin(m_gpio, m_gpio_pin, (m_inverted) ? GPIO_PIN_RESET : GPIO_PIN_SET);
|
||||
m_state = state::ON;
|
||||
} else if(st == state::OFF && m_state == state::ON) {
|
||||
//Store last tick value when turning off
|
||||
//Always offer turning off!
|
||||
m_lastTick = HAL_GetTick();
|
||||
HAL_GPIO_WritePin(m_gpio, m_gpio_pin, (m_inverted) ? GPIO_PIN_SET : GPIO_PIN_RESET);
|
||||
m_state = state::OFF;
|
||||
}
|
||||
}
|
||||
|
||||
void setCooldown(uint16_t ms) {
|
||||
m_cooldown = ms;
|
||||
}
|
||||
|
||||
uint16_t getRemainingCooldown() {
|
||||
uint32_t elapsed = (HAL_GetTick() - m_lastTick);
|
||||
|
||||
if(elapsed <= m_cooldown)
|
||||
return m_cooldown - (HAL_GetTick() - m_lastTick);
|
||||
else
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
||||
private:
|
||||
GPIO_TypeDef *m_gpio;
|
||||
uint16_t m_gpio_pin;
|
||||
|
||||
uint16_t m_cooldown = 0;
|
||||
|
||||
uint32_t m_lastTick = 0;
|
||||
|
||||
bool m_inverted = false;
|
||||
state m_state = state::OFF;
|
||||
};
|
||||
|
||||
} // floatpump
|
||||
} // io
|
||||
|
||||
#endif //FLOATPUMP_RELAYCHANNEL_H
|
10
Middlewares/floatpump/Src/RelayChannel.cpp
Normal file
10
Middlewares/floatpump/Src/RelayChannel.cpp
Normal file
@ -0,0 +1,10 @@
|
||||
//
|
||||
// Created by robtor on 10.01.23.
|
||||
//
|
||||
|
||||
#include "RelayChannel.h"
|
||||
|
||||
namespace floatpump {
|
||||
namespace io {
|
||||
} // floatpump
|
||||
} // io
|
Loading…
Reference in New Issue
Block a user