Monitor the XIAO's battery and make the XIAO the production board
The production OBU is now a Seeed XIAO ESP32-C5 (8 MB flash, one native USB-C port) on a LiPo with no protection board. The firmware reads the cell on GPIO6 (divider switched by GPIO26) every 5 s, sends it in STATUS (battery_mv and battery_flags, octets 103-105; decoders accept 103 or >= 106 octets), warns below 3.50 V and deep-sleeps below 3.30 V when no phone is on USB, waking every 5 min to check the cell again. Thresholds are in Kconfig; host test `make battery`. The app shows voltage, a rough percentage and the low/critical state on the ESP32-C5 card and in the top bar. With several bonded micrOBUs (a replaced board stays bonded), the BLE link tries the one that last connected first and moves on to the next after each failure. tools/station_status_watch.py reads STATUS from the native port.
This commit is contained in:
@@ -12,11 +12,11 @@
|
||||
# Still compiled by the host tests in test/host.
|
||||
# tx_custom.c/.h - previous copy of the OpenTrafficMap raw TX; otm_tx_custom.c replaces it.
|
||||
# denm.c/.h, cam.c/.h, wifi_patches.c - reference only, as before.
|
||||
idf_component_register(SRCS "app_main.cpp" "board_controls.cpp" "simple_ble.cpp" "c5_radio.cpp" "otm_tx_custom.c"
|
||||
idf_component_register(SRCS "app_main.cpp" "board_controls.cpp" "battery.cpp" "simple_ble.cpp" "c5_radio.cpp" "otm_tx_custom.c"
|
||||
"link_protocol.cpp" "serial_link.cpp" "station.cpp" "station_test.cpp"
|
||||
"link_service.cpp" "test_channel.cpp"
|
||||
"gn_unwrap.c" "geonet.c"
|
||||
INCLUDE_DIRS "."
|
||||
LDFRAGMENTS "linker.lf"
|
||||
REQUIRES vanetza-idf esp_wifi esp_phy esp_event bt esp_driver_gpio esp_driver_usb_serial_jtag nvs_flash esp_timer)
|
||||
REQUIRES vanetza-idf esp_wifi esp_phy esp_event bt esp_driver_gpio esp_adc esp_driver_usb_serial_jtag nvs_flash esp_timer)
|
||||
target_compile_features(${COMPONENT_LIB} PRIVATE cxx_std_17)
|
||||
|
||||
@@ -36,4 +36,44 @@ config MICROBU_TX_LED_BLINK_MS
|
||||
config MICROBU_LED_INVERTED_RX
|
||||
bool "Inverted LED mode for receiver/bridge (normally ON, blink OFF on packet)"
|
||||
default n
|
||||
config MICROBU_BATTERY_ADC_GPIO
|
||||
int "Battery voltage ADC GPIO (-1: no battery monitoring)"
|
||||
range -1 6
|
||||
default 6
|
||||
help
|
||||
The XIAO ESP32-C5 reads its battery on GPIO6 (BAT_VOLT_PIN) through two 100 k resistors.
|
||||
config MICROBU_BATTERY_EN_GPIO
|
||||
int "Battery divider enable GPIO"
|
||||
range 0 28
|
||||
default 26
|
||||
help
|
||||
XIAO ESP32-C5: GPIO26 (BAT_VOLT_PIN_EN) switches the divider on for the reading.
|
||||
config MICROBU_BATTERY_SCALE_PERMILLE
|
||||
int "Battery reading trim (1000 = none)"
|
||||
range 800 1200
|
||||
default 1000
|
||||
help
|
||||
Multiplies the reading. Compare the STATUS battery voltage with a multimeter on the cell
|
||||
and set this to 1000 * multimeter / reading if the divider or ADC is off.
|
||||
config MICROBU_BATTERY_WARN_MV
|
||||
int "Low-battery warning below this cell voltage (mV)"
|
||||
range 3000 4200
|
||||
default 3500
|
||||
config MICROBU_BATTERY_SHUTDOWN_MV
|
||||
int "Shutdown below this cell voltage (mV), see battery_monitor.hpp"
|
||||
range 3000 4200
|
||||
default 3300
|
||||
help
|
||||
There is no protection board on the prototype cell, so this is the only guard against
|
||||
over-discharge. The board shuts down (deep sleep) once the smoothed voltage has stayed below
|
||||
this for 30 s and no phone is talking over USB; it checks the cell again every
|
||||
MICROBU_BATTERY_SLEEP_S and runs again from 3600 mV.
|
||||
config MICROBU_BATTERY_RESUME_MV
|
||||
int "After a shutdown, run again at or above this cell voltage (mV)"
|
||||
range 3300 4200
|
||||
default 3600
|
||||
config MICROBU_BATTERY_SLEEP_S
|
||||
int "Seconds between cell checks while shut down"
|
||||
range 30 3600
|
||||
default 300
|
||||
endmenu
|
||||
|
||||
@@ -3,6 +3,8 @@
|
||||
// enqueues frames. Link messages: implementation/station-link/README.md.
|
||||
#include "link_protocol.hpp"
|
||||
#include "link_service.hpp"
|
||||
#include "battery.hpp"
|
||||
#include "battery_monitor.hpp"
|
||||
#include "board_controls.hpp"
|
||||
#include "simple_ble.hpp"
|
||||
#include "serial_link.hpp"
|
||||
@@ -30,6 +32,9 @@ struct Incoming {
|
||||
microbu::serial::Frame frame;
|
||||
};
|
||||
|
||||
constexpr std::int64_t battery_interval_us = 5000000; // one cell sample every 5 s
|
||||
constexpr std::uint32_t battery_usb_window_ms = 10000; // a USB frame this recently means a phone powers the board
|
||||
|
||||
QueueHandle_t frames = nullptr; // FIFO of Incoming* (8 bytes each); frame bytes never enter the queue itself
|
||||
|
||||
/// @brief Attempts to heap-allocate an Incoming so it can be pushed onto the frames queue.
|
||||
@@ -74,6 +79,36 @@ void pause_ble_while_usb_in_use() {
|
||||
microbu::ble::set_advertising_allowed(!usb_in_use);
|
||||
}
|
||||
|
||||
/// @brief MicrOBU: the battery policy with this build's Kconfig thresholds.
|
||||
microbu::BatteryConfig battery_config() {
|
||||
microbu::BatteryConfig config;
|
||||
config.warn_mv = CONFIG_MICROBU_BATTERY_WARN_MV;
|
||||
config.warn_clear_mv = CONFIG_MICROBU_BATTERY_WARN_MV + 100; // 100 mV of hysteresis
|
||||
config.shutdown_mv = CONFIG_MICROBU_BATTERY_SHUTDOWN_MV;
|
||||
config.resume_mv = CONFIG_MICROBU_BATTERY_RESUME_MV;
|
||||
return config;
|
||||
}
|
||||
|
||||
/// @brief MicrOBU: samples the cell, publishes it in STATUS and powers the board down before the
|
||||
/// unprotected cell is over-discharged. A phone on USB powers the board itself, so the cell does not
|
||||
/// force a shutdown while one is talking (see BatteryMonitor::update).
|
||||
void check_battery(microbu::Battery& battery, microbu::BatteryMonitor& monitor, microbu::LinkService& link) {
|
||||
const auto now_ms = static_cast<std::uint32_t>(esp_timer_get_time() / 1000);
|
||||
const auto last_usb = microbu::serial::last_frame_ms();
|
||||
const bool usb_active = last_usb != 0 && now_ms - last_usb < battery_usb_window_ms;
|
||||
monitor.update(battery.measure(), usb_active, now_ms);
|
||||
std::uint8_t flags = 0;
|
||||
if (monitor.low()) flags |= microbu::link::battery_flag_low;
|
||||
if (monitor.critical()) flags |= microbu::link::battery_flag_critical;
|
||||
link.set_battery(monitor.millivolts(), flags);
|
||||
if (monitor.shutdown_due()) {
|
||||
ESP_LOGW(TAG, "cell at %u mV and no phone on USB: shutting down", unsigned(monitor.millivolts()));
|
||||
link.send_status_now(); // the phone sees the critical flag before the link goes away
|
||||
vTaskDelay(pdMS_TO_TICKS(500));
|
||||
microbu::Battery::deep_sleep();
|
||||
}
|
||||
}
|
||||
|
||||
/// @brief FreeRTOS task body: owns the station, link service and board controls. Drains the incoming queue.
|
||||
void station_task(void*) {
|
||||
microbu::Station station;
|
||||
@@ -81,6 +116,9 @@ void station_task(void*) {
|
||||
microbu::BoardControls controls;
|
||||
station.on_disseminated([&controls] { controls.blink(); });
|
||||
station.on_received([&controls] { controls.blink(); });
|
||||
microbu::Battery battery;
|
||||
microbu::BatteryMonitor monitor(battery_config());
|
||||
std::int64_t next_battery_us = 0;
|
||||
ESP_LOGI(TAG, "station task ready; the phone/PC configures the station over BLE or USB Serial-JTAG");
|
||||
for (;;) { /// main loop: drain the incoming queue, tick the station and link service, tick the controls
|
||||
Incoming* incoming = nullptr;
|
||||
@@ -95,12 +133,27 @@ void station_task(void*) {
|
||||
link.tick();
|
||||
controls.tick();
|
||||
pause_ble_while_usb_in_use();
|
||||
if (esp_timer_get_time() >= next_battery_us) {
|
||||
next_battery_us = esp_timer_get_time() + battery_interval_us;
|
||||
check_battery(battery, monitor, link);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// @brief Firmware entry point: initializes NVS, watchdog, serial/BLE transports, and starts the station task.
|
||||
extern "C" void app_main() {
|
||||
// MicrOBU: woken from a low-battery shutdown. Judge the cell before anything else starts, and
|
||||
// go back to sleep unless it has recovered (charged over USB).
|
||||
if (microbu::Battery::woke_from_battery_sleep()) {
|
||||
microbu::Battery battery;
|
||||
const auto millivolts = battery.measure();
|
||||
if (!microbu::BatteryMonitor(battery_config()).resume_ok(millivolts)) {
|
||||
ESP_LOGW(TAG, "cell still at %u mV, back to sleep", unsigned(millivolts));
|
||||
microbu::Battery::deep_sleep();
|
||||
}
|
||||
ESP_LOGI(TAG, "cell at %u mV, starting", unsigned(millivolts));
|
||||
}
|
||||
// NVS: the credential store (and the Wi-Fi driver's calibration data)
|
||||
esp_err_t nvs = nvs_flash_init();
|
||||
if (nvs == ESP_ERR_NVS_NO_FREE_PAGES || nvs == ESP_ERR_NVS_NEW_VERSION_FOUND) {
|
||||
|
||||
@@ -0,0 +1,100 @@
|
||||
#include "battery.hpp"
|
||||
#include "sdkconfig.h"
|
||||
|
||||
#include <algorithm>
|
||||
#include <driver/gpio.h>
|
||||
#include <esp_adc/adc_cali.h>
|
||||
#include <esp_adc/adc_cali_scheme.h>
|
||||
#include <esp_adc/adc_oneshot.h>
|
||||
#include <esp_log.h>
|
||||
#include <esp_rom_sys.h>
|
||||
#include <esp_sleep.h>
|
||||
|
||||
namespace microbu {
|
||||
namespace {
|
||||
const char* TAG = "battery";
|
||||
constexpr int samples = 16;
|
||||
constexpr std::uint32_t settle_us = 2000; // the load switch and the divider's RC settle in well under this
|
||||
constexpr int divider = 2; // two equal 100 k resistors on the XIAO ESP32-C5
|
||||
}
|
||||
|
||||
Battery::Battery() {
|
||||
#if CONFIG_MICROBU_BATTERY_ADC_GPIO < 0
|
||||
ESP_LOGI(TAG, "battery monitoring disabled");
|
||||
return;
|
||||
#endif
|
||||
gpio_config_t enable = {};
|
||||
enable.pin_bit_mask = 1ULL << CONFIG_MICROBU_BATTERY_EN_GPIO;
|
||||
enable.mode = GPIO_MODE_OUTPUT;
|
||||
if (gpio_config(&enable) != ESP_OK) { ESP_LOGE(TAG, "enable GPIO %d unusable", CONFIG_MICROBU_BATTERY_EN_GPIO); return; }
|
||||
gpio_set_level(static_cast<gpio_num_t>(CONFIG_MICROBU_BATTERY_EN_GPIO), 0);
|
||||
|
||||
adc_unit_t unit;
|
||||
adc_channel_t channel;
|
||||
if (adc_oneshot_io_to_channel(CONFIG_MICROBU_BATTERY_ADC_GPIO, &unit, &channel) != ESP_OK) {
|
||||
ESP_LOGE(TAG, "GPIO %d is not an ADC pin", CONFIG_MICROBU_BATTERY_ADC_GPIO);
|
||||
return;
|
||||
}
|
||||
adc_oneshot_unit_init_cfg_t unit_config = {};
|
||||
unit_config.unit_id = unit;
|
||||
adc_oneshot_unit_handle_t adc = nullptr;
|
||||
if (adc_oneshot_new_unit(&unit_config, &adc) != ESP_OK) { ESP_LOGE(TAG, "ADC unit init failed"); return; }
|
||||
adc_oneshot_chan_cfg_t channel_config = {};
|
||||
channel_config.atten = ADC_ATTEN_DB_12; // the divided cell is at most about 2.1 V
|
||||
channel_config.bitwidth = ADC_BITWIDTH_DEFAULT;
|
||||
if (adc_oneshot_config_channel(adc, channel, &channel_config) != ESP_OK) {
|
||||
ESP_LOGE(TAG, "ADC channel config failed");
|
||||
adc_oneshot_del_unit(adc);
|
||||
return;
|
||||
}
|
||||
#if ADC_CALI_SCHEME_CURVE_FITTING_SUPPORTED
|
||||
adc_cali_curve_fitting_config_t cali_config = {};
|
||||
cali_config.unit_id = unit;
|
||||
cali_config.chan = channel;
|
||||
cali_config.atten = ADC_ATTEN_DB_12;
|
||||
cali_config.bitwidth = ADC_BITWIDTH_DEFAULT;
|
||||
adc_cali_handle_t cali = nullptr;
|
||||
if (adc_cali_create_scheme_curve_fitting(&cali_config, &cali) == ESP_OK) cali_ = cali;
|
||||
#endif
|
||||
if (!cali_) ESP_LOGW(TAG, "no ADC calibration: readings are approximate");
|
||||
adc_ = adc;
|
||||
channel_ = static_cast<int>(channel);
|
||||
}
|
||||
|
||||
std::uint16_t Battery::measure() {
|
||||
if (!adc_) return 0;
|
||||
auto* adc = static_cast<adc_oneshot_unit_handle_t>(adc_);
|
||||
gpio_set_level(static_cast<gpio_num_t>(CONFIG_MICROBU_BATTERY_EN_GPIO), 1);
|
||||
esp_rom_delay_us(settle_us);
|
||||
int values[samples];
|
||||
int count = 0;
|
||||
for (int i = 0; i < samples; ++i) {
|
||||
int raw = 0;
|
||||
if (adc_oneshot_read(adc, static_cast<adc_channel_t>(channel_), &raw) != ESP_OK) continue;
|
||||
int millivolts = 0;
|
||||
if (cali_) {
|
||||
if (adc_cali_raw_to_voltage(static_cast<adc_cali_handle_t>(cali_), raw, &millivolts) != ESP_OK) continue;
|
||||
} else {
|
||||
millivolts = raw * 3100 / 4095; // uncalibrated 12 dB full scale, roughly
|
||||
}
|
||||
values[count++] = millivolts;
|
||||
}
|
||||
gpio_set_level(static_cast<gpio_num_t>(CONFIG_MICROBU_BATTERY_EN_GPIO), 0);
|
||||
if (count < samples / 2) return 0;
|
||||
std::sort(values, values + count);
|
||||
const int median = values[count / 2];
|
||||
const long cell_mv = static_cast<long>(median) * divider * CONFIG_MICROBU_BATTERY_SCALE_PERMILLE / 1000;
|
||||
return static_cast<std::uint16_t>(std::clamp(cell_mv, 0L, 65535L));
|
||||
}
|
||||
|
||||
void Battery::deep_sleep() {
|
||||
ESP_LOGW(TAG, "deep sleep for %d s", CONFIG_MICROBU_BATTERY_SLEEP_S);
|
||||
esp_sleep_enable_timer_wakeup(static_cast<std::uint64_t>(CONFIG_MICROBU_BATTERY_SLEEP_S) * 1000000ULL);
|
||||
esp_deep_sleep_start();
|
||||
}
|
||||
|
||||
bool Battery::woke_from_battery_sleep() {
|
||||
return esp_sleep_get_wakeup_cause() == ESP_SLEEP_WAKEUP_TIMER;
|
||||
}
|
||||
|
||||
} // namespace microbu
|
||||
@@ -0,0 +1,28 @@
|
||||
#pragma once
|
||||
// Cell voltage of the XIAO ESP32-C5's battery, read through the board's own divider: GPIO6 (ADC1)
|
||||
// behind two 100 k resistors, switched on by GPIO26 (a load switch that keeps the divider from
|
||||
// draining the cell between readings). The policy (warning, shutdown) is in battery_monitor.hpp.
|
||||
#include <cstdint>
|
||||
|
||||
namespace microbu {
|
||||
|
||||
class Battery {
|
||||
public:
|
||||
/// @brief Configures the enable GPIO and the ADC channel. On failure measure() returns 0.
|
||||
Battery();
|
||||
/// @brief Takes one measurement: enables the divider, samples, disables it again. Blocks for a few ms.
|
||||
/// @return Cell voltage in millivolts, or 0 if the ADC could not be set up or read.
|
||||
std::uint16_t measure();
|
||||
/// @brief Puts the board into deep sleep and wakes it after CONFIG_MICROBU_BATTERY_SLEEP_S seconds.
|
||||
/// Does not return.
|
||||
[[noreturn]] static void deep_sleep();
|
||||
/// @return true if this boot is the end of such a sleep (the only timer wake-up the firmware arms).
|
||||
static bool woke_from_battery_sleep();
|
||||
|
||||
private:
|
||||
void* adc_ = nullptr; // adc_oneshot_unit_handle_t
|
||||
void* cali_ = nullptr; // adc_cali_handle_t, null when the chip has no calibration
|
||||
int channel_ = -1;
|
||||
};
|
||||
|
||||
} // namespace microbu
|
||||
@@ -0,0 +1,70 @@
|
||||
#pragma once
|
||||
// Cell-voltage policy for the XIAO ESP32-C5's LiPo: smoothing, the low-battery warning with
|
||||
// hysteresis, and the decision to shut down. Pure logic with no ESP-IDF dependency, so the host
|
||||
// tests (test/host/test_battery.cpp) compile this header unmodified.
|
||||
#include <cstdint>
|
||||
|
||||
namespace microbu {
|
||||
|
||||
/// @brief Thresholds and timing of the battery policy. Millivolts at the cell, not at the ADC pin.
|
||||
struct BatteryConfig {
|
||||
std::uint16_t warn_mv = 3500; ///< low-battery warning below this
|
||||
std::uint16_t warn_clear_mv = 3600;///< ... and cleared again at or above this (hysteresis)
|
||||
std::uint16_t shutdown_mv = 3300; ///< shut down below this ...
|
||||
unsigned shutdown_samples = 6; ///< ... for this many consecutive samples (6 x 5 s = 30 s: rides out TX sag)
|
||||
std::uint32_t grace_ms = 60000; ///< no shutdown in the first minute after boot
|
||||
std::uint16_t resume_mv = 3600; ///< after a shutdown the board only runs again at or above this
|
||||
std::uint16_t valid_min_mv = 2500; ///< a reading outside valid_min..valid_max is "no cell / no sensor"
|
||||
std::uint16_t valid_max_mv = 4500;
|
||||
};
|
||||
|
||||
/// @brief Turns raw cell-voltage samples into a smoothed value, a warning flag and a shutdown decision.
|
||||
class BatteryMonitor {
|
||||
public:
|
||||
/// @param config Thresholds; the defaults suit a single LiPo cell.
|
||||
explicit BatteryMonitor(BatteryConfig config = {}) : config_(config) {}
|
||||
|
||||
/// @brief Feeds one sample, taken every few seconds.
|
||||
/// @param mv Cell voltage in millivolts, or 0 when the measurement failed.
|
||||
/// @param usb_active A phone is talking over USB, so the board is powered from the phone and
|
||||
/// the cell cannot be the reason it would stop.
|
||||
/// @param uptime_ms Milliseconds since boot.
|
||||
void update(std::uint16_t mv, bool usb_active, std::uint32_t uptime_ms) {
|
||||
if (mv < config_.valid_min_mv || mv > config_.valid_max_mv) {
|
||||
// No cell (or no sensor): report "not measured" and never shut down on it.
|
||||
valid_ = false; ema_ = 0; low_ = false; below_ = 0;
|
||||
return;
|
||||
}
|
||||
if (!valid_) { valid_ = true; ema_ = mv; } // first sample after a gap: no ramp from zero
|
||||
else ema_ = static_cast<std::uint16_t>(ema_ + (static_cast<int>(mv) - static_cast<int>(ema_)) / 4);
|
||||
if (ema_ < config_.warn_mv) low_ = true;
|
||||
else if (ema_ >= config_.warn_clear_mv) low_ = false;
|
||||
if (ema_ < config_.shutdown_mv && !usb_active && uptime_ms >= config_.grace_ms) ++below_;
|
||||
else below_ = 0;
|
||||
}
|
||||
|
||||
/// @return Smoothed cell voltage in millivolts, 0 while there is no valid reading.
|
||||
std::uint16_t millivolts() const { return valid_ ? ema_ : 0; }
|
||||
/// @return true once the cell fell below the warning threshold, until it is back above the clear threshold.
|
||||
bool low() const { return low_; }
|
||||
/// @return true while the smoothed voltage is below the shutdown threshold (not yet sustained).
|
||||
bool critical() const { return valid_ && ema_ < config_.shutdown_mv; }
|
||||
/// @return true once the cell has stayed below the shutdown threshold with no USB link for long enough.
|
||||
bool shutdown_due() const { return below_ >= config_.shutdown_samples; }
|
||||
|
||||
/// @param mv A single reading taken right after a wake-up.
|
||||
/// @return true if the board may run: the cell recovered, or there is no valid reading to judge by.
|
||||
bool resume_ok(std::uint16_t mv) const {
|
||||
if (mv < config_.valid_min_mv || mv > config_.valid_max_mv) return true;
|
||||
return mv >= config_.resume_mv;
|
||||
}
|
||||
|
||||
private:
|
||||
BatteryConfig config_;
|
||||
std::uint16_t ema_ = 0;
|
||||
bool valid_ = false;
|
||||
bool low_ = false;
|
||||
unsigned below_ = 0;
|
||||
};
|
||||
|
||||
} // namespace microbu
|
||||
@@ -157,9 +157,11 @@ Bytes encode(const Status& s) {
|
||||
w.u32(s.radio_submitted); w.u32(s.radio_failed); w.u32(s.radio_received); w.u32(s.radio_dropped);
|
||||
w.u32(s.link_rx_frames); w.u32(s.link_crc_errors); w.u32(s.link_malformed); w.u32(s.poti_updates);
|
||||
w.u64(s.its_time_ms);
|
||||
w.u16(s.battery_mv); w.u8(s.battery_flags);
|
||||
return w.out;
|
||||
}
|
||||
bool decode(const Bytes& body, Status& s) {
|
||||
if (body.size() != status_size_base && body.size() < status_size_battery) return false;
|
||||
Reader r(body);
|
||||
s.uptime_ms = r.u32(); s.configured = r.u8(); r.bytes(s.gn_address, 8); r.bytes(s.identifier, 8); s.change_pending = r.u8(); s.tickets = r.u8();
|
||||
s.signed_messages = r.u32(); s.refused_no_ticket = r.u32(); s.refused_change_pending = r.u32(); s.refused_permission = r.u32();
|
||||
@@ -168,7 +170,10 @@ bool decode(const Bytes& body, Status& s) {
|
||||
s.radio_submitted = r.u32(); s.radio_failed = r.u32(); s.radio_received = r.u32(); s.radio_dropped = r.u32();
|
||||
s.link_rx_frames = r.u32(); s.link_crc_errors = r.u32(); s.link_malformed = r.u32(); s.poti_updates = r.u32();
|
||||
s.its_time_ms = r.u64();
|
||||
return r.done();
|
||||
if (!r.ok()) return false;
|
||||
s.battery_mv = 0; s.battery_flags = 0;
|
||||
if (r.remaining() >= 3) { s.battery_mv = r.u16(); s.battery_flags = r.u8(); }
|
||||
return r.ok(); // octets beyond the known fields belong to a later extension
|
||||
}
|
||||
|
||||
// RESULT
|
||||
|
||||
@@ -292,9 +292,22 @@ struct Status {
|
||||
std::uint32_t radio_submitted = 0, radio_failed = 0, radio_received = 0, radio_dropped = 0;
|
||||
std::uint32_t link_rx_frames = 0, link_crc_errors = 0, link_malformed = 0, poti_updates = 0;
|
||||
std::uint64_t its_time_ms = 0;
|
||||
// MicrOBU extension, appended after the colleague's 103 octets: [103] battery_mv u16 (0: not
|
||||
// measured, no cell or no sensor), [105] battery_flags u8. A 103-octet body (the colleague's
|
||||
// layout, and this firmware before the battery) still decodes, with these two left at 0.
|
||||
std::uint16_t battery_mv = 0;
|
||||
std::uint8_t battery_flags = 0;
|
||||
};
|
||||
/// battery_flags bits of Status.
|
||||
constexpr std::uint8_t battery_flag_low = 0x01; ///< cell below the warning threshold
|
||||
constexpr std::uint8_t battery_flag_critical = 0x02; ///< cell below the shutdown threshold: the board powers down unless a phone is on USB
|
||||
/// Octets of the STATUS body without / with the MicrOBU battery extension.
|
||||
constexpr std::size_t status_size_base = 103;
|
||||
constexpr std::size_t status_size_battery = 106;
|
||||
/// @param body Raw message body bytes.
|
||||
/// @param out Receives the decoded status.
|
||||
/// @param out Receives the decoded status. Accepts the 103-octet base body and any body of at
|
||||
/// least 106 octets (trailing octets after the known fields are ignored, so a later
|
||||
/// extension does not break this decoder); 104 or 105 octets are malformed.
|
||||
/// @return false if malformed.
|
||||
bool decode(const Bytes& body, Status& out);
|
||||
/// @param value Status to encode.
|
||||
|
||||
@@ -47,6 +47,8 @@ link::Status LinkService::status() {
|
||||
value.link_rx_frames = serial_counters.frames + ble_counters.rx_messages;
|
||||
value.link_crc_errors = serial_counters.crc_errors;
|
||||
value.link_malformed += ble_counters.malformed;
|
||||
value.battery_mv = battery_mv_;
|
||||
value.battery_flags = battery_flags_;
|
||||
return value;
|
||||
}
|
||||
|
||||
@@ -149,6 +151,16 @@ void LinkService::handle(const link::Bytes& octets, LinkTransport origin) {
|
||||
}
|
||||
}
|
||||
|
||||
void LinkService::set_battery(std::uint16_t millivolts, std::uint8_t flags) {
|
||||
battery_mv_ = millivolts;
|
||||
battery_flags_ = flags;
|
||||
}
|
||||
|
||||
void LinkService::send_status_now() {
|
||||
last_status_us_ = esp_timer_get_time();
|
||||
broadcast(link::Opcode::STATUS, ++own_sequence_, link::encode(status()));
|
||||
}
|
||||
|
||||
void LinkService::tick() {
|
||||
const auto now = esp_timer_get_time();
|
||||
if (now - last_status_us_ < 1000000) return;
|
||||
|
||||
@@ -20,6 +20,12 @@ public:
|
||||
void handle(const link::Bytes& message, LinkTransport origin);
|
||||
/// @brief Periodic work: broadcasts STATUS once a second.
|
||||
void tick();
|
||||
/// @brief Sets the battery fields of every following STATUS.
|
||||
/// @param millivolts Cell voltage, 0 if not measured.
|
||||
/// @param flags link::battery_flag_* bits.
|
||||
void set_battery(std::uint16_t millivolts, std::uint8_t flags);
|
||||
/// @brief Broadcasts a STATUS now instead of waiting for the next second.
|
||||
void send_status_now();
|
||||
private:
|
||||
/// @brief Encodes and writes one message on the given transport.
|
||||
/// @param transport Transport to write to.
|
||||
@@ -46,6 +52,8 @@ private:
|
||||
link::Bytes bundle_; // credentials being provisioned
|
||||
std::uint16_t bundle_total_ = 0;
|
||||
std::int64_t last_status_us_ = 0;
|
||||
std::uint16_t battery_mv_ = 0;
|
||||
std::uint8_t battery_flags_ = 0;
|
||||
};
|
||||
|
||||
} // namespace microbu
|
||||
|
||||
Reference in New Issue
Block a user