Merge pull request #42 from vas-dav/thread-relay

Thread relay
This commit is contained in:
RedHawk 2023-05-13 02:05:15 +03:00 committed by GitHub
commit ccce95abcd
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 219 additions and 0 deletions

View File

@ -43,6 +43,7 @@
<option id="com.crt.advproject.cpp.fpu.1211390952" name="Floating point" superClass="com.crt.advproject.cpp.fpu" useByScannerDiscovery="true"/> <option id="com.crt.advproject.cpp.fpu.1211390952" name="Floating point" superClass="com.crt.advproject.cpp.fpu" useByScannerDiscovery="true"/>
<option IS_BUILTIN_EMPTY="false" IS_VALUE_EMPTY="false" id="gnu.cpp.compiler.option.include.paths.417443680" name="Include paths (-I)" superClass="gnu.cpp.compiler.option.include.paths" useByScannerDiscovery="false" valueType="includePath"> <option IS_BUILTIN_EMPTY="false" IS_VALUE_EMPTY="false" id="gnu.cpp.compiler.option.include.paths.417443680" name="Include paths (-I)" superClass="gnu.cpp.compiler.option.include.paths" useByScannerDiscovery="false" valueType="includePath">
<listOptionValue builtIn="false" value="&quot;${workspace_loc:/lpc_board_nxp_lpcxpresso_11u68/inc}&quot;"/> <listOptionValue builtIn="false" value="&quot;${workspace_loc:/lpc_board_nxp_lpcxpresso_11u68/inc}&quot;"/>
<listOptionValue builtIn="false" value="&quot;${workspace_loc:/${ProjName}/src/threads/relay}&quot;"/>
<listOptionValue builtIn="false" value="&quot;${workspace_loc:/${ProjName}/src}&quot;"/> <listOptionValue builtIn="false" value="&quot;${workspace_loc:/${ProjName}/src}&quot;"/>
<listOptionValue builtIn="false" value="&quot;${workspace_loc:/lpc_chip_11u6x/inc}&quot;"/> <listOptionValue builtIn="false" value="&quot;${workspace_loc:/lpc_chip_11u6x/inc}&quot;"/>
<listOptionValue builtIn="false" value="&quot;${workspace_loc:/shoh/freertos}&quot;"/> <listOptionValue builtIn="false" value="&quot;${workspace_loc:/shoh/freertos}&quot;"/>

View File

@ -9,6 +9,7 @@
#include "Log.h" #include "Log.h"
#include "ThreadCommon.h" #include "ThreadCommon.h"
#include "Rotary.h" #include "Rotary.h"
#include "Relay.h"
#include "Manager.h" #include "Manager.h"
#include "Logging.h" #include "Logging.h"
#include "UserInterface.h" #include "UserInterface.h"
@ -108,6 +109,9 @@ void thread_master(void* pvParams) {
manager->qm->createQueue(20, manager->qm->createQueue(20,
sizeof(UserInterface::InterfaceWithData), sizeof(UserInterface::InterfaceWithData),
ThreadCommon::QueueManager::ui_event_manager); ThreadCommon::QueueManager::ui_event_manager);
manager->qm->createQueue(10,
sizeof(Event),
ThreadCommon::QueueManager::relay_event_master);
LOG_INFO("Master created queues"); LOG_INFO("Master created queues");
@ -121,6 +125,9 @@ void thread_master(void* pvParams) {
manager->tm->createTask(thread_user_interface, "user_interface", manager->tm->createTask(thread_user_interface, "user_interface",
configMINIMAL_STACK_SIZE * 10,tskIDLE_PRIORITY + 1UL, configMINIMAL_STACK_SIZE * 10,tskIDLE_PRIORITY + 1UL,
static_cast<void*>(manager)); static_cast<void*>(manager));
manager->tm->createTask(thread_relay, "relay",
configMINIMAL_STACK_SIZE * 10,tskIDLE_PRIORITY + 1UL,
static_cast<void*>(manager));
LOG_INFO("Master created tasks"); LOG_INFO("Master created tasks");
m.taskFunction(); m.taskFunction();
} }

View File

@ -0,0 +1,151 @@
/*
* Relay.cpp
*
* Created on: 12 May 2023
* Author: tylen
*/
#include "Relay.h"
#include "Event.h"
#include "Log.h"
RelayDevice::RelayDevice(uint8_t pha_pin,
uint8_t pha_port,
uint8_t relay_device_index,
bool heat_output_1
) :
heat_output_one(heat_output_1)
{
pha = new DigitalIoPin(pha_pin, pha_port, false);
LOG_DEBUG("Creating RelayDevice with output %d",
heat_output_one ? 1 : 2);
pha->write(heat_output_one);
}
RelayDevice::~RelayDevice()
{
LOG_ERROR("Deleting RelayDevice");
}
void inline RelayDevice::RelayOn()
{
pha->write(!heat_output_one);
}
void inline RelayDevice::RelayOff()
{
pha->write(heat_output_one);
}
Relay::Relay(ThreadCommon::QueueManager* qm): _qm(qm)
{
LOG_DEBUG("Creating Relay");
}
Relay::~Relay()
{
LOG_ERROR("Deleting Relay");
}
void Relay::setPowerMode(PowerMode pm)
{
switch (pm)
{
case PowerMode::POWER_0:
relays[INF_RELAY].RelayOff();
relays[SUP_RELAY].RelayOff();
LOG_INFO("Heater is turned OFF");
case PowerMode::POWER_1:
relays[INF_RELAY].RelayOn();
relays[SUP_RELAY].RelayOff();
LOG_INFO("Heater using power mode 1");
break;
case PowerMode::POWER_2:
relays[INF_RELAY].RelayOff();
relays[SUP_RELAY].RelayOn();
LOG_INFO("Heater using power mode 2");
break;
case PowerMode::POWER_3:
relays[INF_RELAY].RelayOn();
relays[SUP_RELAY].RelayOn();
LOG_INFO("Heater using power mode 3");
break;
default:
break;
}
}
void Relay::taskFunction()
{
Event data(Event::Null, 0);
for(;;)
{
_qm->receive<Event>(ThreadCommon::QueueManager::relay_event_master, &data, portMAX_DELAY);
parseEvent(&data);
utilizeEventData();
}
}
void Relay::parseEvent(Event* d)
{
for (uint8_t i = Event::ExternalTemp; i <= Event::SetPoint; i++)
{
EventRawData rd = d->getDataOf(static_cast<Event::EventType>(i));
if(rd == ERROR_RETURN)
{
continue;
}
switch(i /* EventType */)
{
case Event::ExternalTemp:
ext_temp = rd;
break;
case Event::SetPoint:
setpoint = rd;
break;
default:
assert(0);
break;
}
}
}
void Relay::utilizeEventData()
{
PowerMode pm = POWER_0;
/* If setpoint is lower than ext_temp,
* none of below checks will pass and function
* shall turn OFF the heater.
* When setpoint is always higher than ext_temp,
* we can use up to three stages determine how
* powerful the output of the signal should be.
*/
int8_t diff = setpoint - ext_temp;
if (diff >= 10)
{
pm = POWER_3;
}
else if (diff >= 5)
{
pm = POWER_2;
}
else if (diff >= 1)
{
pm = POWER_1;
}
setPowerMode(pm);
return;
}
void thread_relay(void * pvParams)
{
ThreadCommon::CommonManagers * manager = static_cast<ThreadCommon::CommonManagers*>(pvParams);
Relay r(manager->qm);
r.taskFunction();
}

View File

@ -0,0 +1,60 @@
/*
* Relay.h
*
* Created on: 12 May 2023
* Author: tylen
*/
#ifndef THREADS_RELAY_RELAY_H_
#define THREADS_RELAY_RELAY_H_
#include "ThreadCommon.h"
#include "DigitalIoPin.h"
#include "Counter.h"
#include "Event.h"
class RelayDevice {
public:
RelayDevice(uint8_t pha_pin,
uint8_t pha_port,
uint8_t relay_device_index,
bool heat_output_1 = true);
virtual ~RelayDevice();
void RelayOn();
void RelayOff();
private:
DigitalIoPin * pha;
bool heat_output_one;
};
class Relay {
enum PowerMode
{
POWER_0,
POWER_1,
POWER_2,
POWER_3
};
enum RelayType
{
INF_RELAY,
SUP_RELAY
};
public:
Relay(ThreadCommon::QueueManager* qm);
virtual ~Relay();
void taskFunction();
void setPowerMode(PowerMode pm);
void utilizeEventData();
private:
ThreadCommon::QueueManager* _qm;
RelayDevice relays [2] = {{0, 24, 0},
{0, 26, 1}};
void parseEvent(Event * d);
int8_t setpoint, ext_temp;
};
void thread_relay(void * pvParams);
#endif /* THREADS_RELAY_RELAY_H_ */