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