2021-10-07 08:19:01 -03:00
|
|
|
#pragma once
|
|
|
|
|
|
|
|
#include <AP_Common/AP_Common.h>
|
|
|
|
#include <AP_HAL/I2CDevice.h>
|
|
|
|
#include "AP_BattMonitor_Backend.h"
|
2021-10-24 21:58:19 -03:00
|
|
|
#include <AP_Param/AP_Param.h>
|
2021-10-07 08:19:01 -03:00
|
|
|
#include <utility>
|
|
|
|
|
2023-03-06 22:02:49 -04:00
|
|
|
#if AP_BATTERY_INA2XX_ENABLED
|
2021-10-07 08:19:01 -03:00
|
|
|
|
2021-10-24 21:58:19 -03:00
|
|
|
class AP_BattMonitor_INA2XX : public AP_BattMonitor_Backend
|
2021-10-07 08:19:01 -03:00
|
|
|
{
|
|
|
|
public:
|
2021-10-24 21:58:19 -03:00
|
|
|
/// Constructor
|
|
|
|
AP_BattMonitor_INA2XX(AP_BattMonitor &mon,
|
|
|
|
AP_BattMonitor::BattMonitor_State &mon_state,
|
|
|
|
AP_BattMonitor_Params ¶ms);
|
2021-10-07 08:19:01 -03:00
|
|
|
|
|
|
|
bool has_cell_voltages() const override { return false; }
|
2024-02-02 01:36:11 -04:00
|
|
|
bool has_temperature() const override { return has_temp; }
|
2021-10-07 08:19:01 -03:00
|
|
|
bool has_current() const override { return true; }
|
|
|
|
bool get_cycle_count(uint16_t &cycles) const override { return false; }
|
2024-02-02 01:36:11 -04:00
|
|
|
bool get_temperature(float &temperature) const override;
|
2021-10-07 08:19:01 -03:00
|
|
|
|
2022-06-20 22:58:58 -03:00
|
|
|
void init(void) override;
|
|
|
|
void read() override;
|
2021-10-24 21:58:19 -03:00
|
|
|
|
|
|
|
static const struct AP_Param::GroupInfo var_info[];
|
|
|
|
|
2021-10-07 08:19:01 -03:00
|
|
|
private:
|
|
|
|
AP_HAL::OwnPtr<AP_HAL::I2CDevice> dev;
|
|
|
|
|
2023-04-28 20:58:55 -03:00
|
|
|
enum class DevType : uint8_t {
|
|
|
|
UNKNOWN = 0,
|
|
|
|
INA226,
|
|
|
|
INA228,
|
|
|
|
INA238,
|
2024-07-06 19:27:11 -03:00
|
|
|
INA231,
|
2023-04-28 20:58:55 -03:00
|
|
|
};
|
|
|
|
|
2023-05-01 03:06:31 -03:00
|
|
|
static const uint8_t i2c_probe_addresses[];
|
|
|
|
uint8_t i2c_probe_next;
|
|
|
|
|
2023-04-28 20:58:55 -03:00
|
|
|
bool configure(DevType dtype);
|
|
|
|
bool read_word16(const uint8_t reg, int16_t& data) const;
|
|
|
|
bool read_word24(const uint8_t reg, int32_t& data) const;
|
2021-10-07 08:19:01 -03:00
|
|
|
bool write_word(const uint8_t reg, const uint16_t data) const;
|
|
|
|
void timer(void);
|
2023-04-28 20:58:55 -03:00
|
|
|
bool detect_device(void);
|
|
|
|
|
|
|
|
DevType dev_type;
|
|
|
|
uint32_t last_detect_ms;
|
2021-10-07 08:19:01 -03:00
|
|
|
|
2021-10-24 21:58:19 -03:00
|
|
|
AP_Int8 i2c_bus;
|
|
|
|
AP_Int8 i2c_address;
|
2023-05-09 01:56:22 -03:00
|
|
|
AP_Float max_amps;
|
2023-10-05 05:11:29 -03:00
|
|
|
AP_Float rShunt;
|
2022-06-20 22:58:58 -03:00
|
|
|
uint32_t failed_reads;
|
2021-10-24 21:58:19 -03:00
|
|
|
|
2021-10-07 08:19:01 -03:00
|
|
|
struct {
|
|
|
|
uint16_t count;
|
|
|
|
float volt_sum;
|
|
|
|
float current_sum;
|
|
|
|
HAL_Semaphore sem;
|
|
|
|
} accumulate;
|
|
|
|
float current_LSB;
|
|
|
|
float voltage_LSB;
|
2024-02-02 01:36:11 -04:00
|
|
|
|
|
|
|
float temperature;
|
|
|
|
|
|
|
|
bool has_temp;
|
2021-10-07 08:19:01 -03:00
|
|
|
};
|
|
|
|
|
2023-03-06 22:02:49 -04:00
|
|
|
#endif // AP_BATTERY_INA2XX_ENABLED
|