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_Analog.h
Views: 1798
1
#pragma once
2
3
#include "AP_BattMonitor_Backend.h"
4
5
#if AP_BATTERY_ANALOG_ENABLED
6
7
#include "AP_BattMonitor.h"
8
9
// default pins and dividers
10
#if defined(HAL_BATT_VOLT_PIN)
11
// pins defined in board config (hwdef.dat on ChibiOS)
12
# define AP_BATT_VOLT_PIN HAL_BATT_VOLT_PIN
13
# define AP_BATT_CURR_PIN HAL_BATT_CURR_PIN
14
# define AP_BATT_VOLTDIVIDER_DEFAULT HAL_BATT_VOLT_SCALE
15
# define AP_BATT_CURR_AMP_PERVOLT_DEFAULT HAL_BATT_CURR_SCALE
16
#elif CONFIG_HAL_BOARD == HAL_BOARD_CHIBIOS
17
# define AP_BATT_VOLT_PIN 4
18
# define AP_BATT_CURR_PIN 3
19
# define AP_BATT_VOLTDIVIDER_DEFAULT 10.1f
20
# define AP_BATT_CURR_AMP_PERVOLT_DEFAULT 17.0f
21
#elif CONFIG_HAL_BOARD == HAL_BOARD_SITL
22
# define AP_BATT_VOLT_PIN 13
23
# define AP_BATT_CURR_PIN 12
24
# define AP_BATT_VOLTDIVIDER_DEFAULT 10.1f
25
# define AP_BATT_CURR_AMP_PERVOLT_DEFAULT 17.0f
26
27
#elif CONFIG_HAL_BOARD == HAL_BOARD_LINUX && (CONFIG_HAL_BOARD_SUBTYPE == HAL_BOARD_SUBTYPE_LINUX_ERLEBOARD || CONFIG_HAL_BOARD_SUBTYPE == HAL_BOARD_SUBTYPE_LINUX_PXF)
28
# define AP_BATT_VOLT_PIN 5
29
# define AP_BATT_CURR_PIN 6
30
# define AP_BATT_VOLTDIVIDER_DEFAULT 10.1f
31
# define AP_BATT_CURR_AMP_PERVOLT_DEFAULT 17.0f
32
33
#elif CONFIG_HAL_BOARD == HAL_BOARD_LINUX && CONFIG_HAL_BOARD_SUBTYPE == HAL_BOARD_SUBTYPE_LINUX_BBBMINI
34
# define AP_BATT_VOLT_PIN 0
35
# define AP_BATT_CURR_PIN 1
36
# define AP_BATT_VOLTDIVIDER_DEFAULT 10.1f
37
# define AP_BATT_CURR_AMP_PERVOLT_DEFAULT 17.0f
38
39
#elif CONFIG_HAL_BOARD == HAL_BOARD_LINUX && CONFIG_HAL_BOARD_SUBTYPE == HAL_BOARD_SUBTYPE_LINUX_BLUE
40
# define AP_BATT_VOLT_PIN 0
41
# define AP_BATT_CURR_PIN 1
42
# define AP_BATT_VOLTDIVIDER_DEFAULT 10.1f
43
# define AP_BATT_CURR_AMP_PERVOLT_DEFAULT 17.0f
44
45
#elif CONFIG_HAL_BOARD == HAL_BOARD_LINUX && CONFIG_HAL_BOARD_SUBTYPE == HAL_BOARD_SUBTYPE_LINUX_POCKET
46
# define AP_BATT_VOLT_PIN 1
47
# define AP_BATT_CURR_PIN 0
48
# define AP_BATT_VOLTDIVIDER_DEFAULT 10.1f
49
# define AP_BATT_CURR_AMP_PERVOLT_DEFAULT 17.0f
50
51
#elif CONFIG_HAL_BOARD == HAL_BOARD_LINUX && (CONFIG_HAL_BOARD_SUBTYPE == HAL_BOARD_SUBTYPE_LINUX_NAVIO || CONFIG_HAL_BOARD_SUBTYPE == HAL_BOARD_SUBTYPE_LINUX_NAVIO2)
52
# define AP_BATT_VOLT_PIN 2
53
# define AP_BATT_CURR_PIN 3
54
# define AP_BATT_VOLTDIVIDER_DEFAULT 11.3f
55
# define AP_BATT_CURR_AMP_PERVOLT_DEFAULT 17.0f
56
57
#elif CONFIG_HAL_BOARD == HAL_BOARD_LINUX && (CONFIG_HAL_BOARD_SUBTYPE == HAL_BOARD_SUBTYPE_LINUX_NAVIGATOR)
58
# define AP_BATT_VOLT_PIN 5
59
# define AP_BATT_CURR_PIN 4
60
# define AP_BATT_VOLTDIVIDER_DEFAULT 11.0f
61
# define AP_BATT_CURR_AMP_PERVOLT_DEFAULT 37.8788f
62
# define AP_BATT_CURR_AMP_OFFSET_DEFAULT 0.330f
63
64
#elif CONFIG_HAL_BOARD == HAL_BOARD_LINUX && (CONFIG_HAL_BOARD_SUBTYPE == HAL_BOARD_SUBTYPE_LINUX_EDGE)
65
# define AP_BATT_VOLT_PIN 3
66
# define AP_BATT_CURR_PIN 2
67
# define AP_BATT_VOLTDIVIDER_DEFAULT 18.62
68
# define AP_BATT_CURR_AMP_PERVOLT_DEFAULT 62.98f
69
70
# define AP_BATT2_VOLT_PIN 5
71
# define AP_BATT2_CURR_PIN 4
72
# define AP_BATT2_VOLTDIVIDER_DEFAULT 18.62
73
# define AP_BATT2_CURR_AMP_PERVOLT_DEFAULT 62.98f
74
75
#elif CONFIG_HAL_BOARD == HAL_BOARD_LINUX && (CONFIG_HAL_BOARD_SUBTYPE == HAL_BOARD_SUBTYPE_LINUX_OBAL_V1)
76
# define AP_BATT_VOLT_PIN 0
77
# define AP_BATT_CURR_PIN 1
78
# define AP_BATT_VOLTDIVIDER_DEFAULT 10.1f
79
# define AP_BATT_CURR_AMP_PERVOLT_DEFAULT 17.0f
80
81
#else
82
# define AP_BATT_VOLT_PIN -1
83
# define AP_BATT_CURR_PIN -1
84
# define AP_BATT_VOLTDIVIDER_DEFAULT 10.1f
85
# define AP_BATT_CURR_AMP_PERVOLT_DEFAULT 17.0f
86
#endif
87
88
// This is 0 for the majority of the power modules.
89
#ifndef AP_BATT_CURR_AMP_OFFSET_DEFAULT
90
#define AP_BATT_CURR_AMP_OFFSET_DEFAULT 0.0f
91
#endif
92
93
// Other values normally set directly by mission planner
94
// # define AP_BATT_VOLTDIVIDER_DEFAULT 15.70 // Volt divider for AttoPilot 50V/90A sensor
95
// # define AP_BATT_VOLTDIVIDER_DEFAULT 4.127 // Volt divider for AttoPilot 13.6V/45A sensor
96
// # define AP_BATT_CURR_AMP_PERVOLT_DEFAULT 27.32 // Amp/Volt for AttoPilot 50V/90A sensor
97
// # define AP_BATT_CURR_AMP_PERVOLT_DEFAULT 13.66 // Amp/Volt for AttoPilot 13.6V/45A sensor
98
99
class AP_BattMonitor_Analog : public AP_BattMonitor_Backend
100
{
101
public:
102
103
/// Constructor
104
AP_BattMonitor_Analog(AP_BattMonitor &mon, AP_BattMonitor::BattMonitor_State &mon_state, AP_BattMonitor_Params &params);
105
106
/// Read the battery voltage and current. Should be called at 10hz
107
virtual void read() override;
108
109
/// returns true if battery monitor provides consumed energy info
110
virtual bool has_consumed_energy() const override { return has_current(); }
111
112
/// returns true if battery monitor provides current info
113
virtual bool has_current() const override;
114
115
virtual void init(void) override {}
116
117
static const struct AP_Param::GroupInfo var_info[];
118
119
protected:
120
121
AP_HAL::AnalogSource *_volt_pin_analog_source;
122
AP_HAL::AnalogSource *_curr_pin_analog_source;
123
124
// Parameters
125
AP_Float _volt_multiplier; /// voltage on volt pin multiplied by this to calculate battery voltage
126
AP_Float _curr_amp_per_volt; /// voltage on current pin multiplied by this to calculate current in amps
127
AP_Float _curr_amp_offset; /// offset voltage that is subtracted from current pin before conversion to amps
128
AP_Float _volt_offset; /// offset voltage that is subtracted from voltage pin before conversion
129
AP_Int8 _volt_pin; /// board pin used to measure battery voltage
130
AP_Int8 _curr_pin; /// board pin used to measure battery current
131
};
132
133
#endif // AP_BATTERY_ANALOG_ENABLED
134
135