CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutSign UpSign In
Ardupilot

Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place. Commercial Alternative to JupyterHub.

GitHub Repository: Ardupilot/ardupilot
Path: blob/master/libraries/AP_BattMonitor/AP_BattMonitor_AD7091R5.h
Views: 1798
1
#pragma once
2
3
#include "AP_BattMonitor_Backend.h"
4
5
#ifndef AP_BATTERY_AD7091R5_ENABLED
6
#define AP_BATTERY_AD7091R5_ENABLED (BOARD_FLASH_SIZE > 1024)
7
#endif
8
9
#if AP_BATTERY_AD7091R5_ENABLED
10
11
#include <AP_HAL/utility/OwnPtr.h>
12
#include <AP_HAL/I2CDevice.h>
13
14
#define AD7091R5_NO_OF_CHANNELS 4
15
#define AD7091R5_CONF_CMD 0x04
16
#define AD7091R5_CHAN_ALL 0x0F
17
#define AD7091R5_CONF_PDOWN0 0x00
18
#define AD7091R5_CONF_PDOWN2 0x02
19
#define AD7091R5_CONF_PDOWN3 0x03
20
#define AD7091R5_CONF_PDOWN_MASK 0x03
21
22
class AP_BattMonitor_AD7091R5 : public AP_BattMonitor_Backend
23
{
24
public:
25
// Constructor
26
AP_BattMonitor_AD7091R5(AP_BattMonitor &mon,
27
AP_BattMonitor::BattMonitor_State &mon_state,
28
AP_BattMonitor_Params &params);
29
30
// Read the battery voltage and current. Should be called at 10hz
31
void read() override;
32
void init(void) override;
33
34
// returns true if battery monitor provides consumed energy info
35
bool has_consumed_energy() const override
36
{
37
return has_current();
38
}
39
40
// returns true if battery monitor provides current info
41
bool has_current() const override
42
{
43
return true;
44
}
45
46
static const struct AP_Param::GroupInfo var_info[];
47
48
private:
49
void _read_adc();
50
bool _initialize();
51
float _data_to_volt(uint32_t data);
52
53
static struct AnalogData {
54
uint32_t data;
55
} _analog_data[AD7091R5_NO_OF_CHANNELS];
56
static bool _first;
57
static bool _health;
58
59
HAL_Semaphore sem; // semaphore for access to shared frontend data
60
AP_HAL::OwnPtr<AP_HAL::I2CDevice> _dev;
61
uint8_t volt_buff_pt;
62
uint8_t curr_buff_pt;
63
64
protected:
65
66
// Parameters
67
AP_Float _volt_multiplier; // voltage on volt pin multiplied by this to calculate battery voltage
68
AP_Float _curr_amp_per_volt; // voltage on current pin multiplied by this to calculate current in amps
69
AP_Float _curr_amp_offset; // offset voltage that is subtracted from current pin before conversion to amps
70
AP_Float _volt_offset; // offset voltage that is subtracted from voltage pin before conversion
71
AP_Int8 _volt_pin; // board pin used to measure battery voltage
72
AP_Int8 _curr_pin; // board pin used to measure battery current
73
};
74
75
#endif // AP_BATTERY_AD7091R5_ENABLED
76
77