thermometer stuff
This commit is contained in:
@ -49,6 +49,8 @@ void Config::initialize() {
|
|||||||
setFloat(ADC1START + 4, 1.0);
|
setFloat(ADC1START + 4, 1.0);
|
||||||
setFloat(ADC2START, 0.0);
|
setFloat(ADC2START, 0.0);
|
||||||
setFloat(ADC2START + 4, 1.0);
|
setFloat(ADC2START + 4, 1.0);
|
||||||
|
setFloat(THERMO1START, 1.0);
|
||||||
|
setFloat(THERMO2START, 1.0);
|
||||||
|
|
||||||
setMagic();
|
setMagic();
|
||||||
}
|
}
|
||||||
|
2
Config.h
2
Config.h
@ -21,6 +21,8 @@ namespace Config {
|
|||||||
const int MAGIC = 0;
|
const int MAGIC = 0;
|
||||||
const int ADC1START = 4;
|
const int ADC1START = 4;
|
||||||
const int ADC2START = 12;
|
const int ADC2START = 12;
|
||||||
|
const int THERMO1ALPHA = 20;
|
||||||
|
const int THERMO2ALPHA = 24;
|
||||||
|
|
||||||
void initialize();
|
void initialize();
|
||||||
bool isInitialized();
|
bool isInitialized();
|
||||||
|
@ -5,8 +5,11 @@
|
|||||||
#include "ads1210.h"
|
#include "ads1210.h"
|
||||||
#include "led.h"
|
#include "led.h"
|
||||||
#include "SimpleModbusSlave.h"
|
#include "SimpleModbusSlave.h"
|
||||||
|
#include "Thermometer.h"
|
||||||
#include "Config.h"
|
#include "Config.h"
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
const uint8_t LED_PIN = 8;
|
const uint8_t LED_PIN = 8;
|
||||||
|
|
||||||
const uint8_t ADC_1_CS_PIN = 9;
|
const uint8_t ADC_1_CS_PIN = 9;
|
||||||
@ -23,6 +26,8 @@ const uint32_t MODBUS_BAUD = 1200;
|
|||||||
|
|
||||||
ADS1210 ads1210_1;
|
ADS1210 ads1210_1;
|
||||||
ADS1210 ads1210_2;
|
ADS1210 ads1210_2;
|
||||||
|
Thermometer thermometer1;
|
||||||
|
Thermometer thermometer2;
|
||||||
LED led;
|
LED led;
|
||||||
Metro secondTick = Metro(1000);
|
Metro secondTick = Metro(1000);
|
||||||
uint32_t uptimeSeconds;
|
uint32_t uptimeSeconds;
|
||||||
@ -42,21 +47,37 @@ struct {
|
|||||||
uint16_t modbusRegisters[2]; // 4, 5
|
uint16_t modbusRegisters[2]; // 4, 5
|
||||||
} adc1R;
|
} adc1R;
|
||||||
union {
|
union {
|
||||||
uint32_t in;
|
float in;
|
||||||
uint16_t modbusRegisters[2]; // 6, 7
|
uint16_t modbusRegisters[2]; // 6, 7
|
||||||
} adc2Value;
|
} temperature1Raw;
|
||||||
union {
|
union {
|
||||||
float in;
|
float in;
|
||||||
uint16_t modbusRegisters[2]; // 8, 9
|
uint16_t modbusRegisters[2]; // 8, 9
|
||||||
|
} temperature1;
|
||||||
|
union {
|
||||||
|
uint32_t in;
|
||||||
|
uint16_t modbusRegisters[2]; // 10, 11
|
||||||
|
} adc2Value;
|
||||||
|
union {
|
||||||
|
float in;
|
||||||
|
uint16_t modbusRegisters[2]; // 12, 13
|
||||||
} adc2U;
|
} adc2U;
|
||||||
union {
|
union {
|
||||||
float in;
|
float in;
|
||||||
uint16_t modbusRegisters[2]; // 10, 11
|
uint16_t modbusRegisters[2]; // 14, 15
|
||||||
} adc2R;
|
} adc2R;
|
||||||
union {
|
union {
|
||||||
uint32_t in;
|
uint32_t in;
|
||||||
uint16_t modbusRegisters[2]; // 12, 13
|
uint16_t modbusRegisters[2]; // 16, 17
|
||||||
} uptimeSeconds;
|
} uptimeSeconds;
|
||||||
|
union {
|
||||||
|
float in;
|
||||||
|
uint16_t modbusRegisters[2]; // 18, 19
|
||||||
|
} temperature2Raw;
|
||||||
|
union {
|
||||||
|
float in;
|
||||||
|
uint16_t modbusRegisters[2]; // 20, 21
|
||||||
|
} temperature2;
|
||||||
} modbusHoldingRegisters;
|
} modbusHoldingRegisters;
|
||||||
|
|
||||||
|
|
||||||
@ -66,6 +87,8 @@ void setup() {
|
|||||||
led.begin(LED_PIN);
|
led.begin(LED_PIN);
|
||||||
ads1210_1.begin(ADC_1_CS_PIN, ADC_1_RDY_PIN, Config::ADC1START);
|
ads1210_1.begin(ADC_1_CS_PIN, ADC_1_RDY_PIN, Config::ADC1START);
|
||||||
ads1210_2.begin(ADC_2_CS_PIN, ADC_2_RDY_PIN, Config::ADC2START);
|
ads1210_2.begin(ADC_2_CS_PIN, ADC_2_RDY_PIN, Config::ADC2START);
|
||||||
|
thermometer1.begin(Config::THERMO1START);
|
||||||
|
thermometer2.begin(Config::THERMO2START);
|
||||||
modbus_configure(&Serial, MODBUS_BAUD, SERIAL_8N2, MODBUS_ID, MODBUS_TX_ENABLE_PIN,
|
modbus_configure(&Serial, MODBUS_BAUD, SERIAL_8N2, MODBUS_ID, MODBUS_TX_ENABLE_PIN,
|
||||||
sizeof(modbusHoldingRegisters), (uint16_t*)(&modbusHoldingRegisters));
|
sizeof(modbusHoldingRegisters), (uint16_t*)(&modbusHoldingRegisters));
|
||||||
uptimeSeconds = 0;
|
uptimeSeconds = 0;
|
||||||
@ -78,12 +101,18 @@ void loop() {
|
|||||||
modbusHoldingRegisters.adc1Value.in = ads1210_1.value;
|
modbusHoldingRegisters.adc1Value.in = ads1210_1.value;
|
||||||
modbusHoldingRegisters.adc1U.in = ads1210_1.u;
|
modbusHoldingRegisters.adc1U.in = ads1210_1.u;
|
||||||
modbusHoldingRegisters.adc1R.in = ads1210_1.r;
|
modbusHoldingRegisters.adc1R.in = ads1210_1.r;
|
||||||
|
thermometer1.exec(ads1210_1.r);
|
||||||
|
modbusHoldingRegisters.temperature1Raw = thermometer1.temperatureRaw;
|
||||||
|
modbusHoldingRegisters.temperature1 = thermometer1.temperatur;
|
||||||
|
|
||||||
|
|
||||||
ads1210_2.exec();
|
ads1210_2.exec();
|
||||||
modbusHoldingRegisters.adc2Value.in = ads1210_2.value;
|
modbusHoldingRegisters.adc2Value.in = ads1210_2.value;
|
||||||
modbusHoldingRegisters.adc2U.in = ads1210_2.u;
|
modbusHoldingRegisters.adc2U.in = ads1210_2.u;
|
||||||
modbusHoldingRegisters.adc2R.in = ads1210_2.r;
|
modbusHoldingRegisters.adc2R.in = ads1210_2.r;
|
||||||
|
thermometer2.exec(ads1210_2.r);
|
||||||
|
modbusHoldingRegisters.temperature2Raw = thermometer2.temperatureRaw;
|
||||||
|
modbusHoldingRegisters.temperature2 = thermometer2.temperatur;
|
||||||
|
|
||||||
if (secondTick.check() == 1) {
|
if (secondTick.check() == 1) {
|
||||||
led.toggle();
|
led.toggle();
|
||||||
|
@ -2,18 +2,27 @@
|
|||||||
|
|
||||||
using namespace nsThermometer;
|
using namespace nsThermometer;
|
||||||
|
|
||||||
|
const float INVALID_TEMPERATURE = -300.0;
|
||||||
|
|
||||||
Thermometer::Thermometer() {
|
Thermometer::Thermometer() {
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
void Thermometer::begin() {
|
void Thermometer::begin(int eepromAddr) {
|
||||||
m_lastSmoothedTemperature = 0.0;
|
m_eepromAddr = eepromAddr;
|
||||||
|
m_lastSmoothedTemperature = INVALID_TEMPERATURE;
|
||||||
|
m_alpha = 0.1;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
void Thermometer::exec(float r) {
|
void Thermometer::exec(float r) {
|
||||||
temperatureRaw = (r / PT1000_R0 - 1) / PT1000_Coeff;
|
temperatureRaw = (r / PT1000_R0 - 1) / PT1000_Coeff;
|
||||||
|
|
||||||
|
if (m_lastSmoothedTemperature == INVALID_TEMPERATURE) {
|
||||||
|
temperature = temperatureRaw;
|
||||||
|
} else {
|
||||||
|
temperature = m_alpha * temperatureRaw + (1 - m_alpha) * m_lastSmoothedTemperature;
|
||||||
|
}
|
||||||
|
m_lastSmoothedTemperature = temperature;
|
||||||
}
|
}
|
||||||
|
@ -8,21 +8,20 @@ namespace nsThermometer {
|
|||||||
const float R_REF = 3000.0;
|
const float R_REF = 3000.0;
|
||||||
const float PT1000_R0 = 1000.0;
|
const float PT1000_R0 = 1000.0;
|
||||||
const float PT1000_Coeff = 3.85e-3;
|
const float PT1000_Coeff = 3.85e-3;
|
||||||
|
|
||||||
const float ALPHA = 0.1;
|
|
||||||
const float CYCLE_TIME = 1000; // ms
|
|
||||||
};
|
};
|
||||||
|
|
||||||
class Thermometer {
|
class Thermometer {
|
||||||
public:
|
public:
|
||||||
Thermometer();
|
Thermometer();
|
||||||
void begin();
|
void begin(int eepromAddr);
|
||||||
void exec(float r);
|
void exec(float r);
|
||||||
float temperature;
|
float temperature;
|
||||||
float temperatureRaw;
|
float temperatureRaw;
|
||||||
private:
|
private:
|
||||||
|
int m_eepromAddr;
|
||||||
float m_lastSmoothedTemperature;
|
float m_lastSmoothedTemperature;
|
||||||
float m_smoothedTemperature;
|
float m_smoothedTemperature;
|
||||||
|
float m_alpha;
|
||||||
};
|
};
|
||||||
|
|
||||||
#endif // _THERMOMETER_H_
|
#endif // _THERMOMETER_H_
|
||||||
|
Reference in New Issue
Block a user