Relay and Thermo tasks

This commit is contained in:
CarlWachter 2024-03-11 14:41:56 +01:00
parent 505c1c3c0a
commit e1692d35b2
4 changed files with 161 additions and 24 deletions

29
App/Inc/tasks/relay.hpp Normal file
View File

@ -0,0 +1,29 @@
/*
* can_task.hpp
*
* Created on: 10 Dec 2023
* Author: Carl
*/
#ifndef INC_TASKS_RELAYTASK_HPP_
#define INC_TASKS_RELAYTASK_HPP_
#include <sta/tacos/thread.hpp>
#include <sta/devices/stm32/can.hpp>
namespace tasks
{
class RelayTask : public sta::tacos::TacosThread {
public:
RelayTask(uint32_t canID);
void init() override;
void func() override;
private:
};
} // namespace demo
#endif /* INC_TASKS_RELAYTASK_HPP_ */

View File

@ -16,11 +16,11 @@
#include <gpio.h>
#include <spi.h>
namespace demo
namespace tasks
{
class ThermoTask : public sta::tacos::TacosThread {
public:
ThermoTask();
ThermoTask(uint32_t canID);
void init() override;
@ -28,12 +28,14 @@ namespace demo
private:
sta::STM32GpioPin cs_pin = sta::STM32GpioPin(GPIOE, GPIO_PIN_13);
sta::STM32GpioPin* cs_pin[5];
sta::RtosMutex* mutex;
sta::STM32SPI* spi2;
sta::STM32SPIDevice* device_;
sta::MAX31855* tc;
sta::STM32SPIDevice* device_[5];
sta::MAX31855* tc_[5];
};
} // namespace demo

71
App/Src/tasks/relay.cpp Normal file
View File

@ -0,0 +1,71 @@
#include <tasks/relay.hpp>
#include <sta/debug/debug.hpp>
#include <sta/rtos/debug/heap_stats.hpp>
#include "can.h"
#include <sta/tacos.hpp>
#include <cmsis_os2.h>
namespace tasks {
RelayTask::RelayTask(uint32_t canID) :
TacosThread("RELAY", osPriorityNormal) {
setCanID(canID);
}
void RelayTask::init() {
}
void RelayTask::func() {
// Receiving polling message
CanSysMsg msg;
if (CAN_queue_.get(&msg, osWaitForever)) {
uint8_t dev = msg.payload[0];
uint8_t value = msg.payload[1];
switch (dev) {
case 1:
if (value == 1) {
HAL_GPIO_WritePin(GPIOA, GPIO_PIN_2, GPIO_PIN_SET);
} else if (value == 0) {
HAL_GPIO_WritePin(GPIOA, GPIO_PIN_2, GPIO_PIN_RESET);
}
break;
case 2:
if (value == 1) {
HAL_GPIO_WritePin(GPIOA, GPIO_PIN_1, GPIO_PIN_SET);
} else if (value == 0) {
HAL_GPIO_WritePin(GPIOA, GPIO_PIN_1, GPIO_PIN_RESET);
}
break;
case 3:
if (value == 1) {
HAL_GPIO_WritePin(GPIOA, GPIO_PIN_0, GPIO_PIN_SET);
} else if (value == 0) {
HAL_GPIO_WritePin(GPIOA, GPIO_PIN_0, GPIO_PIN_RESET);
}
break;
case 4:
if (value == 1) {
HAL_GPIO_WritePin(GPIOA, GPIO_PIN_3, GPIO_PIN_SET);
} else if (value == 0) {
HAL_GPIO_WritePin(GPIOA, GPIO_PIN_3, GPIO_PIN_RESET);
}
break;
case 5:
if (value == 1) {
HAL_GPIO_WritePin(GPIOA, GPIO_PIN_6, GPIO_PIN_SET);
} else if (value == 0) {
HAL_GPIO_WritePin(GPIOA, GPIO_PIN_6, GPIO_PIN_RESET);
}
break;
}
}
}
} // namespace tasks

View File

@ -7,51 +7,86 @@
#include <tasks/thermo.hpp>
#include <sta/debug/debug.hpp>
#include <sta/rtos/debug/heap_stats.hpp>
#include <gpio.h>
#include <spi.h>
#include <sta/MAX31855.hpp>
#include <sta/devices/stm32/bus/spi.hpp>
#include <sta/tacos.hpp>
#include <sta/rtos/mutex.hpp>
#include <cmsis_os2.h>
namespace demo
namespace tasks
{
ThermoTask::ThermoTask()
ThermoTask::ThermoTask(uint32_t canID)
: TacosThread("Thermo", osPriorityNormal)
{
setCanID(canID);
}
void ThermoTask::init()
{
mutex = new sta::RtosMutex("spi2");
spi2 = new sta::STM32SPI(&hspi2, 16000000, mutex);
device_ = new sta::STM32SPIDevice(spi2, &cs_pin);
tc = new sta::MAX31855(device_); //create driver object
// Init cs pins
cs_pin[0] = new sta::STM32GpioPin(GPIOE, GPIO_PIN_12);
cs_pin[1] = new sta::STM32GpioPin(GPIOB, GPIO_PIN_10);
cs_pin[2] = new sta::STM32GpioPin(GPIOE, GPIO_PIN_15);
cs_pin[3] = new sta::STM32GpioPin(GPIOE, GPIO_PIN_14);
cs_pin[4] = new sta::STM32GpioPin(GPIOE, GPIO_PIN_13);
// init devices
for (uint8_t i = 0; i < 5; i++){
device_[i] = new sta::STM32SPIDevice(spi2, cs_pin[i]);
}
// init drivers
for (uint8_t i = 0; i < 5; i++){
tc_[i] = new sta::MAX31855(device_[i]); //create driver object
}
}
void ThermoTask::func()
{
//STA_DEBUG_HEAP_STATS();
//canController.sendFrame(txHeader, payload);
//STA_DEBUG_HEAP_STATS();
// Receiving polling message
CanSysMsg msg;
//fuck off other pins
HAL_GPIO_WritePin(GPIOE,GPIO_PIN_12, GPIO_PIN_SET);
HAL_GPIO_WritePin(GPIOB,GPIO_PIN_10, GPIO_PIN_SET);
HAL_GPIO_WritePin(GPIOE,GPIO_PIN_15, GPIO_PIN_SET);
HAL_GPIO_WritePin(GPIOE,GPIO_PIN_14, GPIO_PIN_SET);
if (CAN_queue_.get(&msg, osWaitForever))
{
tc->update(); // update internal values
float temperature = tc->getTemp(); //read out temperature in degrees Celsius
float referenceTemperature = tc->getReferenceTemp(); // read out reference temperature in degrees Celsius
uint8_t status = tc->getStatus(); //read out status of the update
//fuck off other pins
/*HAL_GPIO_WritePin(GPIOE,GPIO_PIN_12, GPIO_PIN_SET);
HAL_GPIO_WritePin(GPIOB,GPIO_PIN_10, GPIO_PIN_SET);
HAL_GPIO_WritePin(GPIOE,GPIO_PIN_15, GPIO_PIN_SET);
HAL_GPIO_WritePin(GPIOE,GPIO_PIN_14, GPIO_PIN_SET);*/
uint8_t dev = msg.payload[0] - 1; // -1 to offset start from 0
//float temp = tc_[dev]->measureTemp();
tc_[dev]->update(); // update internal values
float temp = tc_[dev]->getTemp(); //read out temperature in degrees Celsius
uint8_t* bytePointer = reinterpret_cast<uint8_t*>(&temp);
for(uint8_t i = 0; i < 4; i++){
msg.payload[i] = bytePointer[i]; // first four bytes are float of Temperature in Celcius
}
//msg.payload[4] = tc_[dev]->getStatus(); // Followed by status
msg.header.sid = getCanID();
msg.header.payloadLength = 5;
msg.header.eid = 0;
msg.header.format = 0;
// Send it out
sta::tacos::queueCanBusMsg(msg, 0);
}
HAL_Delay(1000);
}
} // namespace demo