more cali stuff
This commit is contained in:
@ -1,6 +1,7 @@
|
|||||||
#include "Arduino.h"
|
#include "Arduino.h"
|
||||||
|
|
||||||
#include "Metro.h"
|
#include "Metro.h"
|
||||||
|
#include "Streaming.h"
|
||||||
|
|
||||||
#include "ads1210.h"
|
#include "ads1210.h"
|
||||||
#include "led.h"
|
#include "led.h"
|
||||||
@ -10,6 +11,7 @@
|
|||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
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;
|
||||||
@ -39,6 +41,8 @@ uint32_t uptimeSeconds = 0;
|
|||||||
|
|
||||||
typedef enum { e_CAL_IDLE, e_CAL_RUNNING, e_CAL_SET, e_CAL_COMPLETE } tCalibrationState;
|
typedef enum { e_CAL_IDLE, e_CAL_RUNNING, e_CAL_SET, e_CAL_COMPLETE } tCalibrationState;
|
||||||
|
|
||||||
|
bool calibrationOffsetEnabled = false;
|
||||||
|
bool calibrationFactorEnabled = false;
|
||||||
tCalibrationState calibrationState = e_CAL_IDLE;
|
tCalibrationState calibrationState = e_CAL_IDLE;
|
||||||
uint16_t calibrationCycleCnt = 0;
|
uint16_t calibrationCycleCnt = 0;
|
||||||
float calibrationValueSum[NUM_OF_CHANNELS];
|
float calibrationValueSum[NUM_OF_CHANNELS];
|
||||||
@ -88,15 +92,28 @@ struct {
|
|||||||
|
|
||||||
|
|
||||||
void setup() {
|
void setup() {
|
||||||
|
pinMode(CAL_ENABLE, INPUT_PULLUP);
|
||||||
|
pinMode(CAL_FACTOR_ENABLE, INPUT_PULLUP);
|
||||||
|
pinMode(CAL_OFFSET_ENABLE, INPUT_PULLUP);
|
||||||
|
|
||||||
delay(100);
|
delay(100);
|
||||||
|
|
||||||
|
calibrationOffsetEnable = (digitalRead(CAL_ENABLE) == 0) && (digitalRead(CAL_OFFSET_ENABLE) == 0);
|
||||||
|
calibrationFactorEnable = (digitalRead(CAL_ENABLE) == 0) && (digitalRead(CAL_FACTOR_ENABLE) == 0);
|
||||||
|
|
||||||
bool initializeConfig = Config::initialize();
|
bool initializeConfig = Config::initialize();
|
||||||
led.begin(LED_PIN);
|
led.begin(LED_PIN);
|
||||||
ads1210[0].begin(ADC_1_CS_PIN, ADC_1_RDY_PIN, initializeConfig, Config::ADC1START);
|
ads1210[0].begin(ADC_1_CS_PIN, ADC_1_RDY_PIN, initializeConfig, Config::ADC1START);
|
||||||
ads1210[1].begin(ADC_2_CS_PIN, ADC_2_RDY_PIN, initializeConfig, Config::ADC2START);
|
ads1210[1].begin(ADC_2_CS_PIN, ADC_2_RDY_PIN, initializeConfig, Config::ADC2START);
|
||||||
thermometer[0].begin(initializeConfig, Config::THERMO1START);
|
thermometer[0].begin(initializeConfig, Config::THERMO1START);
|
||||||
thermometer[1].begin(initializeConfig, Config::THERMO2START);
|
thermometer[1].begin(initializeConfig, Config::THERMO2START);
|
||||||
modbus_configure(&Serial, MODBUS_BAUD, SERIAL_8N2, MODBUS_ID, MODBUS_TX_ENABLE_PIN,
|
|
||||||
sizeof(modbusHoldingRegisters), (uint16_t*)(&modbusHoldingRegisters));
|
if (calibrationOffsetEnable || calibrationFactor Enable) {
|
||||||
|
Serial.begin(9600);
|
||||||
|
} else {
|
||||||
|
modbus_configure(&Serial, MODBUS_BAUD, SERIAL_8N2, MODBUS_ID, MODBUS_TX_ENABLE_PIN,
|
||||||
|
sizeof(modbusHoldingRegisters), (uint16_t*)(&modbusHoldingRegisters));
|
||||||
|
}
|
||||||
|
|
||||||
for (uint8_t i = 0; i < NUM_OF_CHANNELS; i++) {
|
for (uint8_t i = 0; i < NUM_OF_CHANNELS; i++) {
|
||||||
modbusHoldingRegisters.channelVariables[i].calOffset.in = ads1210[i].getCalOffset();
|
modbusHoldingRegisters.channelVariables[i].calOffset.in = ads1210[i].getCalOffset();
|
||||||
@ -105,9 +122,6 @@ void setup() {
|
|||||||
calibrationValueSum[i] = 0.0;
|
calibrationValueSum[i] = 0.0;
|
||||||
}
|
}
|
||||||
|
|
||||||
pinMode(CAL_ENABLE, INPUT_PULLUP);
|
|
||||||
pinMode(CAL_FACTOR_ENABLE, INPUT_PULLUP);
|
|
||||||
pinMode(CAL_OFFSET_ENABLE, INPUT_PULLUP);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void loop() {
|
void loop() {
|
||||||
@ -128,13 +142,15 @@ void loop() {
|
|||||||
|
|
||||||
|
|
||||||
if (secondTick.check() == 1) {
|
if (secondTick.check() == 1) {
|
||||||
if ((digitalRead(CAL_ENABLE) == 0) &&
|
if ((calibrationOffsetEnable || calibrationFactorEnable) &&
|
||||||
(calibrationState != e_CAL_COMPLETE)) {
|
(calibrationState != e_CAL_COMPLETE)) {
|
||||||
|
Serial << "Calibration enabled" << endl;
|
||||||
led.on();
|
led.on();
|
||||||
|
|
||||||
|
|
||||||
switch (calibrationState) {
|
switch (calibrationState) {
|
||||||
case e_CAL_IDLE:
|
case e_CAL_IDLE:
|
||||||
|
Serial << "Calibration state idle" << endl;
|
||||||
for (uint8_t i = 0; i < NUM_OF_CHANNELS; i++) {
|
for (uint8_t i = 0; i < NUM_OF_CHANNELS; i++) {
|
||||||
ads1210[i].setCalOffset(0.0);
|
ads1210[i].setCalOffset(0.0);
|
||||||
ads1210[i].setCalFactor(1.0);
|
ads1210[i].setCalFactor(1.0);
|
||||||
@ -142,35 +158,43 @@ void loop() {
|
|||||||
calibrationState = e_CAL_RUNNING;
|
calibrationState = e_CAL_RUNNING;
|
||||||
break;
|
break;
|
||||||
case e_CAL_RUNNING:
|
case e_CAL_RUNNING:
|
||||||
|
Serial << "Calibration state running" << endl;
|
||||||
calibrationCycleCnt++;
|
calibrationCycleCnt++;
|
||||||
|
Serial << " Cnt: " << calibrationCycleCnt++ << endl;
|
||||||
for (uint8_t i = 0; i < NUM_OF_CHANNELS; i++) {
|
for (uint8_t i = 0; i < NUM_OF_CHANNELS; i++) {
|
||||||
calibrationValueSum[i] += ads1210[i].getR();
|
float r = ads1210[i].getR();
|
||||||
|
calibrationValueSum[i] += r;
|
||||||
|
Serial << " Channel: " << i << ", r: " << r << endl;
|
||||||
}
|
}
|
||||||
if (calibrationCycleCnt >= CALIBRATION_CYCLES) {
|
if (calibrationCycleCnt >= CALIBRATION_CYCLES) {
|
||||||
calibrationState = e_CAL_SET;
|
calibrationState = e_CAL_SET;
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
case e_CAL_SET:
|
case e_CAL_SET:
|
||||||
|
Serial << "Calibration state set" << endl;
|
||||||
// calculate and set according to selected calibration mode
|
// calculate and set according to selected calibration mode
|
||||||
if (digitalRead(CAL_OFFSET_ENABLE) == 0) {
|
if (calibrationOffsetEnable) {
|
||||||
// for offset calibration, the terminals needs to be shorten
|
// for offset calibration, the terminals needs to be shorten
|
||||||
// offset calibration needs to be performed first
|
// offset calibration needs to be performed first
|
||||||
for (uint8_t i = 0; i < NUM_OF_CHANNELS; i++) {
|
for (uint8_t i = 0; i < NUM_OF_CHANNELS; i++) {
|
||||||
float offset = calibrationValueSum[i] / ((float)calibrationCycleCnt);
|
float offset = calibrationValueSum[i] / ((float)calibrationCycleCnt);
|
||||||
ads1210[i].setCalOffset(offset);
|
ads1210[i].setCalOffset(offset);
|
||||||
|
Serial << "Setting offset, Channel: " << i << ", Offset: " << offset << endl;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if (digitalRead(CAL_FACTOR_ENABLE) == 0) {
|
if (calibrationFactorEnable) {
|
||||||
// for factor calibration, a 1000R resistor needs to be connected
|
// for factor calibration, a 1000R resistor needs to be connected
|
||||||
// to the terminals
|
// to the terminals
|
||||||
for (uint8_t i = 0; i < NUM_OF_CHANNELS; i++) {
|
for (uint8_t i = 0; i < NUM_OF_CHANNELS; i++) {
|
||||||
float factor = 1000.0 / (calibrationValueSum[i] / ((float)calibrationCycleCnt));
|
float factor = 1000.0 / (calibrationValueSum[i] / ((float)calibrationCycleCnt));
|
||||||
ads1210[i].setCalFactor(factor);
|
ads1210[i].setCalFactor(factor);
|
||||||
|
Serial << "Setting factor, Channel: " << i << ", Factor: " << factor << endl;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
calibrationState = e_CAL_COMPLETE;
|
calibrationState = e_CAL_COMPLETE;
|
||||||
break;
|
break;
|
||||||
default:
|
default:
|
||||||
|
Serial << "Calibration state default" << endl;
|
||||||
calibrationState = e_CAL_COMPLETE;
|
calibrationState = e_CAL_COMPLETE;
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
@ -178,12 +202,11 @@ void loop() {
|
|||||||
led.toggle();
|
led.toggle();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
uptimeSeconds++;
|
uptimeSeconds++;
|
||||||
modbusHoldingRegisters.uptimeSeconds.in = uptimeSeconds;
|
modbusHoldingRegisters.uptimeSeconds.in = uptimeSeconds;
|
||||||
}
|
}
|
||||||
|
|
||||||
modbus_update();
|
if (! (calibrationOffsetEnable || calibrationFactorEnable)) {
|
||||||
|
modbus_update();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user