2018-02-16 18:32:25 -04:00
|
|
|
#pragma once
|
|
|
|
|
|
|
|
#include "AP_BattMonitor.h"
|
|
|
|
#include "AP_BattMonitor_Backend.h"
|
|
|
|
|
2018-07-20 10:01:13 -03:00
|
|
|
#include <AP_UAVCAN/AP_UAVCAN.h>
|
|
|
|
|
2018-02-16 18:32:25 -04:00
|
|
|
#define AP_BATTMONITOR_UAVCAN_TIMEOUT_MICROS 5000000 // sensor becomes unhealthy if no successful readings for 5 seconds
|
|
|
|
|
2018-07-20 10:01:13 -03:00
|
|
|
class BattInfoCb;
|
|
|
|
|
2018-02-16 18:32:25 -04:00
|
|
|
class AP_BattMonitor_UAVCAN : public AP_BattMonitor_Backend
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
enum BattMonitor_UAVCAN_Type {
|
|
|
|
UAVCAN_BATTERY_INFO = 0
|
|
|
|
};
|
|
|
|
|
|
|
|
/// Constructor
|
|
|
|
AP_BattMonitor_UAVCAN(AP_BattMonitor &mon, AP_BattMonitor::BattMonitor_State &mon_state, BattMonitor_UAVCAN_Type type, AP_BattMonitor_Params ¶ms);
|
|
|
|
|
2018-07-20 10:01:13 -03:00
|
|
|
void init() override {}
|
|
|
|
|
2018-02-16 18:32:25 -04:00
|
|
|
/// Read the battery voltage and current. Should be called at 10hz
|
|
|
|
void read() override;
|
|
|
|
|
2020-12-07 01:30:23 -04:00
|
|
|
/// capacity_remaining_pct - returns the % battery capacity remaining (0 ~ 100)
|
|
|
|
uint8_t capacity_remaining_pct() const override;
|
|
|
|
|
2021-05-24 23:07:48 -03:00
|
|
|
bool has_temperature() const override { return _has_temperature; }
|
|
|
|
|
2018-02-16 18:32:25 -04:00
|
|
|
bool has_current() const override {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2018-07-20 10:01:13 -03:00
|
|
|
static void subscribe_msgs(AP_UAVCAN* ap_uavcan);
|
2020-11-21 01:28:12 -04:00
|
|
|
static AP_BattMonitor_UAVCAN* get_uavcan_backend(AP_UAVCAN* ap_uavcan, uint8_t node_id, uint8_t battery_id);
|
2018-07-20 10:01:13 -03:00
|
|
|
static void handle_battery_info_trampoline(AP_UAVCAN* ap_uavcan, uint8_t node_id, const BattInfoCb &cb);
|
|
|
|
|
|
|
|
private:
|
|
|
|
void handle_battery_info(const BattInfoCb &cb);
|
2018-02-16 18:32:25 -04:00
|
|
|
|
2020-11-21 01:28:12 -04:00
|
|
|
static bool match_battery_id(uint8_t instance, uint8_t battery_id) {
|
|
|
|
// when serial number is negative, all batteries are accepted. Else, it must match
|
|
|
|
return (AP::battery().get_serial_number(instance) < 0) || (AP::battery().get_serial_number(instance) == (int32_t)battery_id);
|
|
|
|
}
|
|
|
|
|
2018-07-20 10:01:13 -03:00
|
|
|
AP_BattMonitor::BattMonitor_State _interim_state;
|
2018-02-16 18:32:25 -04:00
|
|
|
BattMonitor_UAVCAN_Type _type;
|
2018-07-20 10:01:13 -03:00
|
|
|
|
|
|
|
HAL_Semaphore _sem_battmon;
|
|
|
|
|
|
|
|
AP_UAVCAN* _ap_uavcan;
|
|
|
|
uint8_t _node_id;
|
2020-12-07 01:30:23 -04:00
|
|
|
uint8_t _soc;
|
2021-05-24 23:07:48 -03:00
|
|
|
bool _has_temperature;
|
2018-02-16 18:32:25 -04:00
|
|
|
};
|