This commit is contained in:
norbert-walter 2022-03-23 18:26:31 +01:00
parent c785c169a1
commit 9150552b34
4 changed files with 158 additions and 18 deletions

View File

@ -21,6 +21,12 @@
#define SHT21_I2C_ADDR 0x40 // Addr. 0x40 (fix) #define SHT21_I2C_ADDR 0x40 // Addr. 0x40 (fix)
// AS5600 // AS5600
#define AS5600_I2C_ADDR 0x36 // Addr. 0x36 (fix) #define AS5600_I2C_ADDR 0x36 // Addr. 0x36 (fix)
// INA226
#define SHUNT_VOLTAGE 0.75 // 75mV by max. current
#define INA226_I2C_ADDR1 0x40 // Addr. 0x40 (fix)
#define INA226_I2C_ADDR2 0x41 // Addr. 0x41 (fix)
#define INA226_I2C_ADDR3 0x44 // Addr. 0x44 (fix)
#define INA226_I2C_ADDR4 0x45 // Addr. 0x45 (fix)
// SPI (E-Ink display, Extern Bus) // SPI (E-Ink display, Extern Bus)
#define OBP_SPI_CS 5 #define OBP_SPI_CS 5
#define OBP_SPI_DC 17 #define OBP_SPI_DC 17

View File

@ -4,7 +4,8 @@
#include <Adafruit_BMP280.h> // Adafruit Lib for BMP280 #include <Adafruit_BMP280.h> // Adafruit Lib for BMP280
#include <Adafruit_BMP085.h> // Adafruit Lib for BMP085 and BMP180 #include <Adafruit_BMP085.h> // Adafruit Lib for BMP085 and BMP180
#include <HTU21D.h> // Lib for SHT21/HTU21 #include <HTU21D.h> // Lib for SHT21/HTU21
#include <AS5600.h> #include <AS5600.h> // Lib for magnetic rotation sensor AS5600
#include "INA226.h" // Lib for power management IC INA226
#include <Ticker.h> // Timer Lib for timer interrupts #include <Ticker.h> // Timer Lib for timer interrupts
#include "OBPSensorTask.h" #include "OBPSensorTask.h"
#include "OBP60Hardware.h" #include "OBP60Hardware.h"
@ -67,6 +68,7 @@ void sensorTask(void *param){
Adafruit_BMP085 bmp085; // Evironment sensor BMP085 and BMP180 Adafruit_BMP085 bmp085; // Evironment sensor BMP085 and BMP180
HTU21D sht21(HTU21D_RES_RH12_TEMP14); // Environment sensor SHT21 and HTU21 HTU21D sht21(HTU21D_RES_RH12_TEMP14); // Environment sensor SHT21 and HTU21
AMS_5600 as5600; // Rotation sensor AS5600 AMS_5600 as5600; // Rotation sensor AS5600
INA226 ina226_1(INA226_I2C_ADDR1);// Power management IC INA226
// Init sensor stuff // Init sensor stuff
bool gps_ready = false; // GPS initialized and ready to use bool gps_ready = false; // GPS initialized and ready to use
@ -75,6 +77,7 @@ void sensorTask(void *param){
bool BMP180_ready = false; // BMP180 initialized and ready to use bool BMP180_ready = false; // BMP180 initialized and ready to use
bool SHT21_ready = false; // SHT21 initialized and ready to use bool SHT21_ready = false; // SHT21 initialized and ready to use
bool AS5600_ready = false; // AS5600 initialized and ready to use bool AS5600_ready = false; // AS5600 initialized and ready to use
bool INA226_1_ready = false; // INA226_1 initialized and ready to use
// Start timer interrupts // Start timer interrupts
bool uvoltage = api->getConfig()->getConfigItem(api->getConfig()->underVoltage,true)->asBoolean(); bool uvoltage = api->getConfig()->getConfigItem(api->getConfig()->underVoltage,true)->asBoolean();
@ -85,7 +88,7 @@ void sensorTask(void *param){
// Settings for NMEA0183 // Settings for NMEA0183
String nmea0183Mode = api->getConfig()->getConfigItem(api->getConfig()->serialDirection, true)->asString(); String nmea0183Mode = api->getConfig()->getConfigItem(api->getConfig()->serialDirection, true)->asString();
api->getLogger()->logDebug(GwLog::DEBUG, "NMEA0183 Mode is: %s", nmea0183Mode); api->getLogger()->logDebug(GwLog::LOG, "NMEA0183 Mode is: %s", nmea0183Mode);
pinMode(OBP_DIRECTION_PIN, OUTPUT); pinMode(OBP_DIRECTION_PIN, OUTPUT);
if (String(nmea0183Mode) == "receive" || String(nmea0183Mode) == "off") if (String(nmea0183Mode) == "receive" || String(nmea0183Mode) == "off")
{ {
@ -105,7 +108,7 @@ void sensorTask(void *param){
gps_ready = false; gps_ready = false;
} }
else{ else{
api->getLogger()->logDebug(GwLog::DEBUG,"GPS modul NEO-M6 found"); api->getLogger()->logDebug(GwLog::LOG,"GPS modul NEO-M6 found");
NMEA0183.SetMessageStream(&Serial2); NMEA0183.SetMessageStream(&Serial2);
NMEA0183.Open(); NMEA0183.Open();
gps_ready = true; gps_ready = true;
@ -118,7 +121,7 @@ void sensorTask(void *param){
gps_ready = false; gps_ready = false;
} }
else{ else{
api->getLogger()->logDebug(GwLog::DEBUG,"GPS modul NEO-M8N found"); api->getLogger()->logDebug(GwLog::LOG,"GPS modul NEO-M8N found");
NMEA0183.SetMessageStream(&Serial2); NMEA0183.SetMessageStream(&Serial2);
NMEA0183.Open(); NMEA0183.Open();
gps_ready = true; gps_ready = true;
@ -128,10 +131,10 @@ void sensorTask(void *param){
// Settings for temperature sensors // Settings for temperature sensors
String tempSensor = api->getConfig()->getConfigItem(api->getConfig()->useTempSensor,true)->asString(); String tempSensor = api->getConfig()->getConfigItem(api->getConfig()->useTempSensor,true)->asString();
if(String(tempSensor) == "DS18B20"){ if(String(tempSensor) == "DS18B20"){
api->getLogger()->logDebug(GwLog::DEBUG,"1Wire Mode is On"); api->getLogger()->logDebug(GwLog::LOG,"1Wire Mode is On");
} }
else{ else{
api->getLogger()->logDebug(GwLog::DEBUG,"1Wire Mode is Off"); api->getLogger()->logDebug(GwLog::LOG,"1Wire Mode is Off");
} }
// Settings for environment sensors on I2C bus // Settings for environment sensors on I2C bus
@ -142,7 +145,7 @@ void sensorTask(void *param){
api->getLogger()->logDebug(GwLog::ERROR,"Modul BME280 not found, check wiring"); api->getLogger()->logDebug(GwLog::ERROR,"Modul BME280 not found, check wiring");
} }
else{ else{
api->getLogger()->logDebug(GwLog::DEBUG,"Modul BME280 found"); api->getLogger()->logDebug(GwLog::LOG,"Modul BME280 found");
sensors.airTemperature = bme280.readTemperature(); sensors.airTemperature = bme280.readTemperature();
sensors.airPressure = bme280.readPressure()/100; sensors.airPressure = bme280.readPressure()/100;
sensors.airHumidity = bme280.readHumidity(); sensors.airHumidity = bme280.readHumidity();
@ -154,7 +157,7 @@ void sensorTask(void *param){
api->getLogger()->logDebug(GwLog::ERROR,"Modul BMP280 not found, check wiring"); api->getLogger()->logDebug(GwLog::ERROR,"Modul BMP280 not found, check wiring");
} }
else{ else{
api->getLogger()->logDebug(GwLog::DEBUG,"Modul BMP280 found"); api->getLogger()->logDebug(GwLog::LOG,"Modul BMP280 found");
sensors.airTemperature = bmp280.readTemperature(); sensors.airTemperature = bmp280.readTemperature();
sensors.airPressure =bmp280.readPressure()/100; sensors.airPressure =bmp280.readPressure()/100;
BMP280_ready = true; BMP280_ready = true;
@ -165,7 +168,7 @@ void sensorTask(void *param){
api->getLogger()->logDebug(GwLog::ERROR,"Modul BMP085/BMP180 not found, check wiring"); api->getLogger()->logDebug(GwLog::ERROR,"Modul BMP085/BMP180 not found, check wiring");
} }
else{ else{
api->getLogger()->logDebug(GwLog::DEBUG,"Modul BMP085/BMP180 found"); api->getLogger()->logDebug(GwLog::LOG,"Modul BMP085/BMP180 found");
sensors.airTemperature = bmp085.readTemperature(); sensors.airTemperature = bmp085.readTemperature();
sensors.airPressure =bmp085.readPressure()/100; sensors.airPressure =bmp085.readPressure()/100;
BMP180_ready = true; BMP180_ready = true;
@ -176,14 +179,14 @@ void sensorTask(void *param){
api->getLogger()->logDebug(GwLog::ERROR,"Modul HTU21/SHT21 not found, check wiring"); api->getLogger()->logDebug(GwLog::ERROR,"Modul HTU21/SHT21 not found, check wiring");
} }
else{ else{
api->getLogger()->logDebug(GwLog::DEBUG,"Modul HTU21/SHT21 found"); api->getLogger()->logDebug(GwLog::LOG,"Modul HTU21/SHT21 found");
sensors.airHumidity = sht21.readCompensatedHumidity(); sensors.airHumidity = sht21.readCompensatedHumidity();
sensors.airTemperature = sht21.readTemperature(); sensors.airTemperature = sht21.readTemperature();
SHT21_ready = true; SHT21_ready = true;
} }
} }
// Settings for rotation sensors on I2C bus // Settings for rotation sensors AS5600 on I2C bus
String envsensor = api->getConfig()->getConfigItem(api->getConfig()->useEnvSensor, true)->asString(); String envsensor = api->getConfig()->getConfigItem(api->getConfig()->useEnvSensor, true)->asString();
String rotsensor = api->getConfig()->getConfigItem(api->getConfig()->useRotSensor, true)->asString(); String rotsensor = api->getConfig()->getConfigItem(api->getConfig()->useRotSensor, true)->asString();
String rotfunction = api->getConfig()->getConfigItem(api->getConfig()->rotFunction, true)->asString(); String rotfunction = api->getConfig()->getConfigItem(api->getConfig()->rotFunction, true)->asString();
@ -195,19 +198,44 @@ void sensorTask(void *param){
api->getLogger()->logDebug(GwLog::ERROR,"Modul AS5600 not found, check wiring"); api->getLogger()->logDebug(GwLog::ERROR,"Modul AS5600 not found, check wiring");
} }
else{ else{
api->getLogger()->logDebug(GwLog::DEBUG,"Modul AS5600 found"); api->getLogger()->logDebug(GwLog::LOG,"Modul AS5600 found");
sensors.rotationAngle = DegToRad(as5600.getRawAngle() * 0.087); // 0...4095 segments = 0.087 degree sensors.rotationAngle = DegToRad(as5600.getRawAngle() * 0.087); // 0...4095 segments = 0.087 degree
//sensors.magnitude = as5600.getMagnitude(); // Magnetic magnitude in [mT] //sensors.magnitude = as5600.getMagnitude(); // Magnetic magnitude in [mT]
AS5600_ready = true; AS5600_ready = true;
} }
} }
// Settings for power amangement sensors INA226 #1 on I2C bus
String powsensor1 = api->getConfig()->getConfigItem(api->getConfig()->usePowSensor1, true)->asString();
String shunt1 = api->getConfig()->getConfigItem(api->getConfig()->shunt1, true)->asString();
float shuntResistor = 1.0; // Default value for shunt resistor
float current = 10.0; // Default value for max. current
if(String(powsensor1) == "INA226"){
if (!ina226_1.begin()){
api->getLogger()->logDebug(GwLog::ERROR,"Modul 1 INA226 not found, check wiring");
}
else{
api->getLogger()->logDebug(GwLog::LOG,"Modul 1 INA226 found");
shuntResistor = 0.075 / float(shunt1.toInt()); // Calculate shunt resisitor for max. shunt voltage 75mV
current = float(shunt1.toInt());
api->getLogger()->logDebug(GwLog::LOG,"Calibation INA226, Imax:%3.0fA Rs:%7.5fOhm Us:0.075V", current, shuntResistor);
ina226_1.setMaxCurrentShunt(current, shuntResistor);
sensors.batteryVoltage = ina226_1.getBusVoltage();
sensors.batteryCurrent = ina226_1.getCurrent();
sensors.batteryPower = ina226_1.getPower();
INA226_1_ready = true;
}
}
int rotoffset = api->getConfig()->getConfigItem(api->getConfig()->rotOffset,true)->asInt(); int rotoffset = api->getConfig()->getConfigItem(api->getConfig()->rotOffset,true)->asInt();
long starttime0 = millis(); long starttime0 = millis(); // GPS update all 1s
long starttime5 = millis(); // Voltage update all 1s long starttime5 = millis(); // Voltage update all 1s
long starttime6 = millis(); // Environment sensor update all 1s long starttime6 = millis(); // Environment sensor update all 1s
long starttime7 = millis(); // Rotation sensor update all 100ms long starttime7 = millis(); // Rotation sensor update all 500ms
long starttime8 = millis(); // Power management sensor update all 1s
tN2kMsg N2kMsg; tN2kMsg N2kMsg;
shared->setSensorData(sensors); //set initially read values shared->setSensorData(sensors); //set initially read values
@ -218,7 +246,7 @@ void sensorTask(void *param){
delay(100); // Loop time 100ms delay(100); // Loop time 100ms
Timer1.update(); // Update for Timer1 Timer1.update(); // Update for Timer1
Timer2.update(); // Update for Timer2 Timer2.update(); // Update for Timer2
if (millis() > starttime0 + 100) if (millis() > starttime0 + 1000)
{ {
starttime0 = millis(); starttime0 = millis();
// Send NMEA0183 GPS data on several bus systems all 1000ms // Send NMEA0183 GPS data on several bus systems all 1000ms
@ -309,7 +337,7 @@ void sensorTask(void *param){
} }
} }
// Send rotation angle all 1000ms // Send rotation angle all 500ms
if(millis() > starttime7 + 500){ if(millis() > starttime7 + 500){
starttime7 = millis(); starttime7 = millis();
double rotationAngle=0; double rotationAngle=0;

View File

@ -261,6 +261,111 @@
"obp60":"true" "obp60":"true"
} }
}, },
{
"name": "usePowSensor1",
"label": "Pow. Sensor 1",
"type": "list",
"default": "off",
"description": "Use external power management sensor via I2C bus [off|INA219|INA226|]",
"list": [
"off",
"INA219",
"INA226"
],
"category": "OBP60 Hardware",
"capabilities": {
"obp60":"true"
}
},
{
"name": "shunt1",
"label": "Shunt Sensor 1",
"type": "list",
"default": "10",
"description": "Shunt current value [10A|50A|100A|200A|300A|500A]",
"list": [
"10",
"50",
"100",
"200",
"300",
"500"
],
"category": "OBP60 Hardware",
"capabilities": {
"obp60":"true"
}
},
{
"name": "usePowSensor2",
"label": "Pow. Sensor 2",
"type": "list",
"default": "off",
"description": "Use external power management sensor via I2C bus [off|INA219|INA226|]",
"list": [
"off",
"INA219",
"INA226"
],
"category": "OBP60 Hardware",
"capabilities": {
"obp60":"true"
}
},
{
"name": "shunt2",
"label": "Shunt Sensor 2",
"type": "list",
"default": "10",
"description": "Shunt current value [10A|50A|100A|200A|300A|500A]",
"list": [
"10",
"50",
"100",
"200",
"300",
"500"
],
"category": "OBP60 Hardware",
"capabilities": {
"obp60":"true"
}
},
{
"name": "usePowSensor3",
"label": "Pow. Sensor 3",
"type": "list",
"default": "off",
"description": "Use external power management sensor via I2C bus [off|INA219|INA226|]",
"list": [
"off",
"INA219",
"INA226"
],
"category": "OBP60 Hardware",
"capabilities": {
"obp60":"true"
}
},
{
"name": "shunt3",
"label": "Shunt Sensor 3",
"type": "list",
"default": "10",
"description": "Shunt current value [10A|50A|100A|200A|300A|500A]",
"list": [
"10",
"50",
"100",
"200",
"300",
"500"
],
"category": "OBP60 Hardware",
"capabilities": {
"obp60":"true"
}
},
{ {
"name": "useRotSensor", "name": "useRotSensor",
"label": "Rot. Sensor", "label": "Rot. Sensor",

View File

@ -17,6 +17,7 @@ lib_deps =
adafruit/Adafruit BME280 Library@2.2.2 adafruit/Adafruit BME280 Library@2.2.2
adafruit/Adafruit BMP085 Library@1.2.1 adafruit/Adafruit BMP085 Library@1.2.1
enjoyneering/HTU21D@1.2.1 enjoyneering/HTU21D@1.2.1
robtillaart/INA226@0.2.0
build_flags= build_flags=
-D BOARD_NODEMCU32S_OBP60 -D BOARD_NODEMCU32S_OBP60
${env.build_flags} ${env.build_flags}