mirror of
https://github.com/meshtastic/firmware.git
synced 2025-09-06 11:39:32 +00:00
Reroute to Meshimi pinout
Add MAX17261 sensor
This commit is contained in:
parent
685c8c1df7
commit
b326fb76f3
176
src/modules/Telemetry/Sensor/MAX17261Sensor.cpp
Normal file
176
src/modules/Telemetry/Sensor/MAX17261Sensor.cpp
Normal file
@ -0,0 +1,176 @@
|
||||
#include "MAX17048Sensor.h"
|
||||
|
||||
#if !MESHTASTIC_EXCLUDE_I2C && !defined(ARCH_STM32WL) && __has_include(<Adafruit_MAX1704X.h>)
|
||||
|
||||
MAX17048Singleton *MAX17048Singleton::GetInstance()
|
||||
{
|
||||
if (pinstance == nullptr) {
|
||||
pinstance = new MAX17048Singleton();
|
||||
}
|
||||
return pinstance;
|
||||
}
|
||||
|
||||
MAX17048Singleton::MAX17048Singleton() {}
|
||||
|
||||
MAX17048Singleton::~MAX17048Singleton() {}
|
||||
|
||||
MAX17048Singleton *MAX17048Singleton::pinstance{nullptr};
|
||||
|
||||
bool MAX17048Singleton::runOnce(TwoWire *theWire)
|
||||
{
|
||||
initialized = begin(theWire);
|
||||
LOG_DEBUG("%s::runOnce %s", sensorStr, initialized ? "began ok" : "begin failed");
|
||||
return initialized;
|
||||
}
|
||||
|
||||
bool MAX17048Singleton::isBatteryCharging()
|
||||
{
|
||||
float volts = cellVoltage();
|
||||
if (isnan(volts)) {
|
||||
LOG_DEBUG("%s::isBatteryCharging not connected", sensorStr);
|
||||
return 0;
|
||||
}
|
||||
|
||||
MAX17048ChargeSample sample;
|
||||
sample.chargeRate = chargeRate(); // charge/discharge rate in percent/hr
|
||||
sample.cellPercent = cellPercent(); // state of charge in percent 0 to 100
|
||||
chargeSamples.push(sample); // save a sample into a fifo buffer
|
||||
|
||||
// Keep the fifo buffer trimmed
|
||||
while (chargeSamples.size() > MAX17048_CHARGING_SAMPLES)
|
||||
chargeSamples.pop();
|
||||
|
||||
// Based on the past n samples, is the lipo charging, discharging or idle
|
||||
if (chargeSamples.front().chargeRate > MAX17048_CHARGING_MINIMUM_RATE &&
|
||||
chargeSamples.back().chargeRate > MAX17048_CHARGING_MINIMUM_RATE) {
|
||||
if (chargeSamples.front().cellPercent > chargeSamples.back().cellPercent)
|
||||
chargeState = MAX17048ChargeState::EXPORT;
|
||||
else if (chargeSamples.front().cellPercent < chargeSamples.back().cellPercent)
|
||||
chargeState = MAX17048ChargeState::IMPORT;
|
||||
else
|
||||
chargeState = MAX17048ChargeState::IDLE;
|
||||
} else {
|
||||
chargeState = MAX17048ChargeState::IDLE;
|
||||
}
|
||||
|
||||
LOG_DEBUG("%s::isBatteryCharging %s volts: %.3f soc: %.3f rate: %.3f", sensorStr, chargeLabels[chargeState], volts,
|
||||
sample.cellPercent, sample.chargeRate);
|
||||
return chargeState == MAX17048ChargeState::IMPORT;
|
||||
}
|
||||
|
||||
uint16_t MAX17048Singleton::getBusVoltageMv()
|
||||
{
|
||||
float volts = cellVoltage();
|
||||
if (isnan(volts)) {
|
||||
LOG_DEBUG("%s::getBusVoltageMv is not connected", sensorStr);
|
||||
return 0;
|
||||
}
|
||||
LOG_DEBUG("%s::getBusVoltageMv %.3fmV", sensorStr, volts);
|
||||
return (uint16_t)(volts * 1000.0f);
|
||||
}
|
||||
|
||||
uint8_t MAX17048Singleton::getBusBatteryPercent()
|
||||
{
|
||||
float soc = cellPercent();
|
||||
LOG_DEBUG("%s::getBusBatteryPercent %.1f%%", sensorStr, soc);
|
||||
return clamp(static_cast<uint8_t>(round(soc)), static_cast<uint8_t>(0), static_cast<uint8_t>(100));
|
||||
}
|
||||
|
||||
uint16_t MAX17048Singleton::getTimeToGoSecs()
|
||||
{
|
||||
float rate = chargeRate(); // charge/discharge rate in percent/hr
|
||||
float soc = cellPercent(); // state of charge in percent 0 to 100
|
||||
soc = clamp(soc, 0.0f, 100.0f); // clamp soc between 0 and 100%
|
||||
float ttg = ((100.0f - soc) / rate) * 3600.0f; // calculate seconds to charge/discharge
|
||||
LOG_DEBUG("%s::getTimeToGoSecs %.0f seconds", sensorStr, ttg);
|
||||
return (uint16_t)ttg;
|
||||
}
|
||||
|
||||
bool MAX17048Singleton::isBatteryConnected()
|
||||
{
|
||||
float volts = cellVoltage();
|
||||
if (isnan(volts)) {
|
||||
LOG_DEBUG("%s::isBatteryConnected is not connected", sensorStr);
|
||||
return false;
|
||||
}
|
||||
|
||||
// if a valid voltage is returned, then battery must be connected
|
||||
return true;
|
||||
}
|
||||
|
||||
bool MAX17048Singleton::isExternallyPowered()
|
||||
{
|
||||
float volts = cellVoltage();
|
||||
if (isnan(volts)) {
|
||||
// if the battery is not connected then there must be external power
|
||||
LOG_DEBUG("%s::isExternallyPowered battery is", sensorStr);
|
||||
return true;
|
||||
}
|
||||
// if the bus voltage is over MAX17048_BUS_POWER_VOLTS, then the external power
|
||||
// is assumed to be connected
|
||||
LOG_DEBUG("%s::isExternallyPowered %s connected", sensorStr, volts >= MAX17048_BUS_POWER_VOLTS ? "is" : "is not");
|
||||
return volts >= MAX17048_BUS_POWER_VOLTS;
|
||||
}
|
||||
|
||||
#if (HAS_TELEMETRY && (!MESHTASTIC_EXCLUDE_ENVIRONMENTAL_SENSOR || !MESHTASTIC_EXCLUDE_POWER_TELEMETRY))
|
||||
|
||||
MAX17048Sensor::MAX17048Sensor() : TelemetrySensor(meshtastic_TelemetrySensorType_MAX17048, "MAX17048") {}
|
||||
|
||||
int32_t MAX17048Sensor::runOnce()
|
||||
{
|
||||
if (isInitialized()) {
|
||||
LOG_INFO("Init sensor: %s is already initialised", sensorName);
|
||||
return true;
|
||||
}
|
||||
|
||||
LOG_INFO("Init sensor: %s", sensorName);
|
||||
if (!hasSensor()) {
|
||||
return DEFAULT_SENSOR_MINIMUM_WAIT_TIME_BETWEEN_READS;
|
||||
}
|
||||
|
||||
// Get a singleton instance and initialise the max17048
|
||||
if (max17048 == nullptr) {
|
||||
max17048 = MAX17048Singleton::GetInstance();
|
||||
}
|
||||
status = max17048->runOnce(nodeTelemetrySensorsMap[sensorType].second);
|
||||
return initI2CSensor();
|
||||
}
|
||||
|
||||
void MAX17048Sensor::setup() {}
|
||||
|
||||
bool MAX17048Sensor::getMetrics(meshtastic_Telemetry *measurement)
|
||||
{
|
||||
LOG_DEBUG("MAX17048 getMetrics id: %i", measurement->which_variant);
|
||||
|
||||
float volts = max17048->cellVoltage();
|
||||
if (isnan(volts)) {
|
||||
LOG_DEBUG("MAX17048 getMetrics battery is not connected");
|
||||
return false;
|
||||
}
|
||||
|
||||
float rate = max17048->chargeRate(); // charge/discharge rate in percent/hr
|
||||
float soc = max17048->cellPercent(); // state of charge in percent 0 to 100
|
||||
soc = clamp(soc, 0.0f, 100.0f); // clamp soc between 0 and 100%
|
||||
float ttg = (100.0f - soc) / rate; // calculate hours to charge/discharge
|
||||
|
||||
LOG_DEBUG("MAX17048 getMetrics volts: %.3fV soc: %.1f%% ttg: %.1f hours", volts, soc, ttg);
|
||||
if ((int)measurement->which_variant == meshtastic_Telemetry_power_metrics_tag) {
|
||||
measurement->variant.power_metrics.has_ch1_voltage = true;
|
||||
measurement->variant.power_metrics.ch1_voltage = volts;
|
||||
} else if ((int)measurement->which_variant == meshtastic_Telemetry_device_metrics_tag) {
|
||||
measurement->variant.device_metrics.has_battery_level = true;
|
||||
measurement->variant.device_metrics.has_voltage = true;
|
||||
measurement->variant.device_metrics.battery_level = static_cast<uint32_t>(round(soc));
|
||||
measurement->variant.device_metrics.voltage = volts;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
uint16_t MAX17048Sensor::getBusVoltageMv()
|
||||
{
|
||||
return max17048->getBusVoltageMv();
|
||||
};
|
||||
|
||||
#endif
|
||||
|
||||
#endif
|
111
src/modules/Telemetry/Sensor/MAX17261Sensor.h
Normal file
111
src/modules/Telemetry/Sensor/MAX17261Sensor.h
Normal file
@ -0,0 +1,111 @@
|
||||
#pragma once
|
||||
|
||||
#ifndef MAX17048_SENSOR_H
|
||||
#define MAX17048_SENSOR_H
|
||||
|
||||
#include "configuration.h"
|
||||
|
||||
#if !MESHTASTIC_EXCLUDE_I2C && !defined(ARCH_STM32WL) && __has_include(<Adafruit_MAX1704X.h>)
|
||||
|
||||
// Samples to store in a buffer to determine if the battery is charging or discharging
|
||||
#define MAX17048_CHARGING_SAMPLES 3
|
||||
|
||||
// Threshold to determine if the battery is on charge, in percent/hour
|
||||
#define MAX17048_CHARGING_MINIMUM_RATE 1.0f
|
||||
|
||||
// Threshold to determine if the board has bus power
|
||||
#define MAX17048_BUS_POWER_VOLTS 4.195f
|
||||
|
||||
#include "../mesh/generated/meshtastic/telemetry.pb.h"
|
||||
#include "TelemetrySensor.h"
|
||||
#include "VoltageSensor.h"
|
||||
|
||||
#include "meshUtils.h"
|
||||
#include <Adafruit_MAX1704X.h>
|
||||
#include <queue>
|
||||
|
||||
struct MAX17048ChargeSample {
|
||||
float cellPercent;
|
||||
float chargeRate;
|
||||
};
|
||||
|
||||
enum MAX17048ChargeState { IDLE, EXPORT, IMPORT };
|
||||
|
||||
// Singleton wrapper for the Adafruit_MAX17048 class
|
||||
class MAX17048Singleton : public Adafruit_MAX17048
|
||||
{
|
||||
private:
|
||||
static MAX17048Singleton *pinstance;
|
||||
bool initialized = false;
|
||||
std::queue<MAX17048ChargeSample> chargeSamples;
|
||||
MAX17048ChargeState chargeState = IDLE;
|
||||
const String chargeLabels[3] = {F("idle"), F("export"), F("import")};
|
||||
const char *sensorStr = "MAX17048Sensor";
|
||||
|
||||
protected:
|
||||
MAX17048Singleton();
|
||||
~MAX17048Singleton();
|
||||
|
||||
public:
|
||||
// Create a singleton instance (not thread safe)
|
||||
static MAX17048Singleton *GetInstance();
|
||||
|
||||
// Singletons should not be cloneable.
|
||||
MAX17048Singleton(MAX17048Singleton &other) = delete;
|
||||
|
||||
// Singletons should not be assignable.
|
||||
void operator=(const MAX17048Singleton &) = delete;
|
||||
|
||||
// Initialise the sensor (not thread safe)
|
||||
virtual bool runOnce(TwoWire *theWire = &Wire);
|
||||
|
||||
// Get the current bus voltage
|
||||
uint16_t getBusVoltageMv();
|
||||
|
||||
// Get the state of charge in percent 0 to 100
|
||||
uint8_t getBusBatteryPercent();
|
||||
|
||||
// Calculate the seconds to charge/discharge
|
||||
uint16_t getTimeToGoSecs();
|
||||
|
||||
// Returns true if the battery sensor has started
|
||||
inline virtual bool isInitialised() { return initialized; };
|
||||
|
||||
// Returns true if the battery is currently on charge (not thread safe)
|
||||
bool isBatteryCharging();
|
||||
|
||||
// Returns true if a battery is actually connected
|
||||
bool isBatteryConnected();
|
||||
|
||||
// Returns true if there is bus or external power connected
|
||||
bool isExternallyPowered();
|
||||
};
|
||||
|
||||
#if (HAS_TELEMETRY && (!MESHTASTIC_EXCLUDE_ENVIRONMENTAL_SENSOR || !MESHTASTIC_EXCLUDE_POWER_TELEMETRY))
|
||||
|
||||
class MAX17048Sensor : public TelemetrySensor, VoltageSensor
|
||||
{
|
||||
private:
|
||||
MAX17048Singleton *max17048 = nullptr;
|
||||
|
||||
protected:
|
||||
virtual void setup() override;
|
||||
|
||||
public:
|
||||
MAX17048Sensor();
|
||||
|
||||
// Initialise the sensor
|
||||
virtual int32_t runOnce() override;
|
||||
|
||||
// Get the current bus voltage and state of charge
|
||||
virtual bool getMetrics(meshtastic_Telemetry *measurement) override;
|
||||
|
||||
// Get the current bus voltage
|
||||
virtual uint16_t getBusVoltageMv() override;
|
||||
};
|
||||
|
||||
#endif
|
||||
|
||||
#endif
|
||||
|
||||
#endif
|
@ -23,9 +23,9 @@
|
||||
|
||||
#ifdef USE_SX1262
|
||||
#define LORA_CS 21
|
||||
#define LORA_DIO1 2
|
||||
#define LORA_BUSY 0
|
||||
#define LORA_RESET 5
|
||||
#define LORA_DIO1 7
|
||||
#define LORA_BUSY 6
|
||||
#define LORA_RESET 2
|
||||
#define SX126X_CS LORA_CS
|
||||
#define SX126X_DIO1 LORA_DIO1
|
||||
#define SX126X_BUSY LORA_BUSY
|
||||
@ -41,4 +41,4 @@
|
||||
#undef GPS_TX_PIN
|
||||
|
||||
// For BLE/WiFi connectivity
|
||||
#define USE_XIAO_ESP32C6_EXTERNAL_ANTENNA
|
||||
// #define USE_XIAO_ESP32C6_EXTERNAL_ANTENNA
|
Loading…
Reference in New Issue
Block a user