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_Generator.cpp
Views: 1798
1
/*
2
This program is free software: you can redistribute it and/or modify
3
it under the terms of the GNU General Public License as published by
4
the Free Software Foundation, either version 3 of the License, or
5
(at your option) any later version.
6
7
This program is distributed in the hope that it will be useful,
8
but WITHOUT ANY WARRANTY; without even the implied warranty of
9
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10
GNU General Public License for more details.
11
12
You should have received a copy of the GNU General Public License
13
along with this program. If not, see <http://www.gnu.org/licenses/>.
14
*/
15
16
#include "AP_BattMonitor_config.h"
17
18
#if AP_BATTERY_GENERATOR_ENABLED
19
20
#include <AP_Common/AP_Common.h>
21
#include <AP_Math/AP_Math.h>
22
23
#include "AP_BattMonitor_Generator.h"
24
25
/*
26
Fuel class
27
*/
28
// This is where we tell the battery monitor 'we have current' if we want to report a fuel level remaining
29
bool AP_BattMonitor_Generator_FuelLevel::has_current(void) const
30
{
31
// If the generator has fuel remaining we must also state that we have current
32
return has_consumed_energy();
33
}
34
35
// This is where we tell the battery monitor 'we have consumed energy' if we want to report a fuel level remaining
36
bool AP_BattMonitor_Generator_FuelLevel::has_consumed_energy(void) const
37
{
38
// Get pointer to generator singleton
39
AP_Generator *generator = AP::generator();
40
41
if (generator == nullptr) {
42
return false;
43
}
44
45
// Use consumed_mAh in BattMonitor to display fuel remaining
46
return generator->has_fuel_remaining();
47
}
48
49
void AP_BattMonitor_Generator_FuelLevel::init()
50
{
51
// Set params for users:
52
// Fuel level is only reported as a percentage
53
_params._pack_capacity.set(100);
54
// Fuel only reports a fixed 1v, don't want batt monitor failsafes on this instance
55
_params._low_voltage.set(0);
56
_params._critical_voltage.set(0);
57
}
58
59
// Read the fuel level. Should be called at 10hz
60
void AP_BattMonitor_Generator_FuelLevel::read()
61
{
62
_state.healthy = false;
63
64
// Get pointer to generator singleton
65
AP_Generator *generator = AP::generator();
66
67
// Not healthy if we can't find a generator
68
if (generator == nullptr) {
69
return;
70
}
71
72
if (!generator->healthy()) {
73
return;
74
}
75
76
// As this is a battery monitor instance report voltage
77
// Report fixed voltage of 1V
78
_state.voltage = 1.0f;
79
80
// This is a bodge to display tank level as a percentage on GCS. Users should set _params.pack_capacity == 100 to get a clear percentage in GCS
81
_state.consumed_mah = (1 - generator->get_fuel_remaining()) * _params._pack_capacity.get();
82
83
// If we got this far then must be healthy
84
_state.healthy = true;
85
_state.last_time_micros = AP_HAL::micros();
86
}
87
88
/*
89
Electrical class
90
*/
91
bool AP_BattMonitor_Generator_Elec::has_current(void) const
92
{
93
// Get pointer to generator singleton
94
AP_Generator *generator = AP::generator();
95
96
if (generator == nullptr) {
97
return false;
98
}
99
100
return generator->has_current();
101
}
102
103
bool AP_BattMonitor_Generator_Elec::has_consumed_energy(void) const
104
{
105
// Get pointer to generator singleton
106
AP_Generator *generator = AP::generator();
107
108
if (generator == nullptr) {
109
return false;
110
}
111
112
return generator->has_consumed_energy();
113
}
114
115
// Read the electrical measurements from the generator
116
void AP_BattMonitor_Generator_Elec::read()
117
{
118
_state.healthy = false;
119
120
// Get pointer to generator singleton
121
AP_Generator *generator = AP::generator();
122
123
// Not healthy if we can't find a generator
124
if (generator == nullptr) {
125
return;
126
}
127
128
if (!generator->healthy()) {
129
return;
130
}
131
132
// Update readings
133
_state.voltage = generator->get_voltage();
134
135
_state.current_amps = generator->get_current();
136
137
// Always reset consumed value, integration is done in AP_Generator library
138
_state.consumed_mah = generator->get_batt_consumed();
139
_state.consumed_wh = 0.001f * _state.consumed_mah * _state.voltage;
140
141
// If we got this far then must be healthy
142
_state.healthy = true;
143
_state.last_time_micros = AP_HAL::micros();
144
}
145
146
AP_BattMonitor::Failsafe AP_BattMonitor_Generator_Elec::update_failsafes()
147
{
148
AP_BattMonitor::Failsafe failsafe = AP_BattMonitor::Failsafe::None;
149
150
AP_Generator *generator = AP::generator();
151
152
// Only check for failsafes on the electrical monitor
153
// no point in having the same failsafe on two battery monitors
154
if (generator != nullptr) {
155
failsafe = generator->update_failsafes();
156
}
157
return MAX(AP_BattMonitor_Backend::update_failsafes(), failsafe);
158
}
159
#endif // AP_BATTERY_GENERATOR_ENABLED
160
161