2022-12-11 22:25:31 -04:00
|
|
|
#pragma once
|
|
|
|
|
|
|
|
#include <AP_Common/AP_Common.h>
|
|
|
|
#include <AP_HAL/SPIDevice.h>
|
|
|
|
#include "AP_BattMonitor_Backend.h"
|
|
|
|
#include <utility>
|
|
|
|
|
2023-03-06 22:02:49 -04:00
|
|
|
#if AP_BATTERY_INA239_ENABLED
|
2022-12-11 22:25:31 -04:00
|
|
|
|
|
|
|
class AP_BattMonitor_INA239 : public AP_BattMonitor_Backend
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
/// Constructor
|
|
|
|
AP_BattMonitor_INA239(AP_BattMonitor &mon,
|
|
|
|
AP_BattMonitor::BattMonitor_State &mon_state,
|
|
|
|
AP_BattMonitor_Params ¶ms);
|
|
|
|
|
|
|
|
bool has_cell_voltages() const override { return false; }
|
|
|
|
bool has_temperature() const override { return false; }
|
|
|
|
bool has_current() const override { return true; }
|
|
|
|
bool get_cycle_count(uint16_t &cycles) const override { return false; }
|
|
|
|
|
|
|
|
void init(void) override;
|
|
|
|
void read() override;
|
|
|
|
|
|
|
|
protected:
|
|
|
|
AP_HAL::OwnPtr<AP_HAL::Device> dev;
|
|
|
|
|
|
|
|
void configure(void);
|
|
|
|
bool read_word(const uint8_t reg, int16_t& data) const;
|
|
|
|
bool write_word(const uint8_t reg, const uint16_t data) const;
|
|
|
|
void timer(void);
|
|
|
|
|
|
|
|
bool configured;
|
|
|
|
bool callback_registered;
|
|
|
|
uint32_t failed_reads;
|
|
|
|
uint32_t last_configure_ms;
|
|
|
|
|
|
|
|
struct {
|
|
|
|
uint16_t count;
|
|
|
|
float volt_sum;
|
|
|
|
float current_sum;
|
|
|
|
HAL_Semaphore sem;
|
|
|
|
} accumulate;
|
|
|
|
float current_LSB;
|
|
|
|
float voltage_LSB;
|
|
|
|
};
|
|
|
|
|
2023-03-06 22:02:49 -04:00
|
|
|
#endif // AP_BATTERY_INA239_ENABLED
|