QGroundControl
Ground Control Station for MAVLink Drones
Loading...
Searching...
No Matches
PX4BatteryComponent.cc
Go to the documentation of this file.
2
3#include "Fact.h"
4#include "ParameterManager.h"
5#include "Vehicle.h"
6
8 : VehicleComponent(vehicle, autopilot, AutoPilotPlugin::KnownPowerVehicleComponent, parent)
9 , _name(tr("Battery"))
10{}
11
13{
14 return tr("Configure each battery's source and estimate, the shared charge levels and the power monitors.");
15}
16
18{
19 return QStringLiteral("/qmlimages/Battery.svg");
20}
21
23{
24 return {
25 tr("Voltage"), tr("Current"), tr("Power module"), tr("Cells"), tr("LiPo"),
26 tr("Capacity"), tr("Low battery"), tr("Critical battery"), tr("Emergency"),
27 };
28}
29
31{
32 ParameterManager* const parameterManager = _vehicle->parameterManager();
33 const int componentId = ParameterManager::defaultComponentId;
34 if (!parameterManager->parameterExists(componentId, "BAT1_SOURCE") ||
35 !parameterManager->parameterExists(componentId, "BAT1_V_CHARGED") ||
36 !parameterManager->parameterExists(componentId, "BAT1_V_EMPTY") ||
37 !parameterManager->parameterExists(componentId, "BAT1_N_CELLS")) {
38 return true;
39 }
40
41 // Cells and voltages are only used, and only shown on the page, when PX4 estimates the battery itself.
42 // Keep in sync with PX4BatteryRows.batteryInputs().
43 const int source = parameterManager->getParameter(componentId, "BAT1_SOURCE")->rawValue().toInt();
44 const bool estimated = source == 0 || source == 2 || _canFiltered();
45 if (!estimated) {
46 return true;
47 }
48
49 return parameterManager->getParameter(componentId, "BAT1_V_CHARGED")->rawValue().toFloat() != 0.0f &&
50 parameterManager->getParameter(componentId, "BAT1_V_EMPTY")->rawValue().toFloat() != 0.0f &&
51 parameterManager->getParameter(componentId, "BAT1_N_CELLS")->rawValue().toInt() != 0;
52}
53
55{
56 return {"BAT1_SOURCE", "BAT1_V_CHARGED", "BAT1_V_EMPTY", "BAT1_N_CELLS", "UAVCAN_ENABLE", "UAVCAN_SUB_BAT"};
57}
58
60{
61 return QUrl::fromUserInput(QStringLiteral("qrc:/qml/QGroundControl/AutoPilotPlugins/PX4/PX4BatteryComponent.qml"));
62}
63
65{
66 QStringList triggers;
67 for (int index = 1; _parameterFact(_batteryParam(index, "SOURCE")); index++) {
68 for (const char* const suffix : {"SOURCE", "V_CHARGED", "V_EMPTY", "N_CELLS"}) {
69 triggers.append(_batteryParam(index, suffix));
70 }
71 }
72
73 return triggers;
74}
75
77{
79
80 // PX4 only streams the parameters of batteries it uses, and the rest can arrive later
81 (void) connect(_vehicle->parameterManager(), &ParameterManager::factAdded, this,
82 [this](int componentId, Fact* fact) {
83 if (componentId != _vehicle->defaultComponentId() || !fact->name().startsWith("BAT")) {
84 return;
85 }
86
87 if (setupCompleteChangedTriggerList().contains(fact->name())) {
88 (void) connect(fact, &Fact::valueChanged, this, &PX4BatteryComponent::_triggerUpdated);
89 emit setupCompleteChanged();
90 } else {
91 (void) connect(fact, &Fact::valueChanged, this, &PX4BatteryComponent::summaryItemsChanged);
92 emit summaryItemsChanged();
93 }
94 });
95}
96
97QList<VehicleSummaryItem> PX4BatteryComponent::summaryItems() const
98{
99 int batteryCount = 0;
100 while (_parameterFact(_batteryParam(batteryCount + 1, "SOURCE"))) {
101 batteryCount++;
102 }
103
104 // Only battery 1 is checked by setupComplete
105 const bool battery1Incomplete = !setupComplete();
106
107 // Battery 1 is always listed, the others unless they are disabled (BATn_SOURCE -1), which still leaves the
108 // DroneCAN battery estimate running
109 QList<int> shownBatteries;
110 for (int index = 1; index <= batteryCount; index++) {
111 if (index == 1 || _parameterFact(_batteryParam(index, "SOURCE"))->rawValue().toInt() != -1 ||
112 _canFiltered()) {
113 shownBatteries.append(index);
114 }
115 }
116
117 // A single battery needs no box of its own
118 const bool grouped = shownBatteries.size() > 1;
119
120 QList<VehicleSummaryItem> items;
121 for (const int index : shownBatteries) {
122 const QString group = grouped ? tr("Battery %1").arg(index) : QString();
123 const auto caption = [grouped](const QString& single, const QString& inGroup) {
124 return grouped ? inGroup : single;
125 };
126 const auto addValue = [this, &items, &group, index, battery1Incomplete](const char* suffix,
127 const QString& text, bool isCount) {
128 const Fact* const fact = _parameterFact(_batteryParam(index, suffix));
129 if (!fact) {
130 items.append({text, tr("N/A"), VehicleSummaryItem::Muted, group});
131 return;
132 }
133 const bool unset = fact->rawValue().toFloat() == 0.0f;
134 items.append({text, isCount ? fact->cookedValueString() : _valueWithUnits(fact),
135 (index == 1 && battery1Incomplete && unset) ? VehicleSummaryItem::Attention
136 : VehicleSummaryItem::Normal,
137 group});
138 };
139
140 Fact* const source = _parameterFact(_batteryParam(index, "SOURCE"));
141 items.append({caption(tr("Battery Source"), tr("Source")), source->enumStringValue(),
142 VehicleSummaryItem::Normal, group});
143 addValue("V_CHARGED", caption(tr("Battery Full"), tr("Full")), false);
144 addValue("V_EMPTY", caption(tr("Battery Empty"), tr("Empty")), false);
145 addValue("N_CELLS", caption(tr("Number of Cells"), tr("Number of Cells")), true);
146 }
147
148 return items;
149}
150
151QString PX4BatteryComponent::_batteryParam(int batteryIndex, const char* suffix)
152{
153 return QStringLiteral("BAT%1_%2").arg(batteryIndex).arg(QLatin1String(suffix));
154}
155
156bool PX4BatteryComponent::_canFiltered() const
157{
158 const Fact* const enable = _parameterFact(QStringLiteral("UAVCAN_ENABLE"));
159 const Fact* const subscription = _parameterFact(QStringLiteral("UAVCAN_SUB_BAT"));
160 return enable && subscription && enable->rawValue().toInt() > 0 && subscription->rawValue().toInt() == 2;
161}
The AutoPilotPlugin class is an abstract base class which represents the methods and objects which ar...
A Fact is used to hold a single value within the system.
Definition Fact.h:15
QString cookedValueString() const
Definition Fact.cc:489
QString name() const
Definition Fact.h:124
QString enumStringValue()
Definition Fact.cc:248
QVariant rawValue() const
Definition Fact.h:87
QStringList searchKeywords() const final
Translated search terms for the whole component, for pages without vehicleConfigJson() sections.
QUrl setupSource() const final
bool setupComplete() const final
PX4BatteryComponent(Vehicle *vehicle, AutoPilotPlugin *autopilot, QObject *parent=nullptr)
QStringList setupCompleteChangedTriggerList() const final
QList< VehicleSummaryItem > summaryItems() const final
Values for this component's card on the Summary page. A component with none falls back to summaryQmlS...
QString description() const final
QStringList summaryChangedTriggerList() const final
Parameters whose change emits summaryItemsChanged, besides those that change setupComplete.
QString iconResource() const final
bool parameterExists(int componentId, const QString &paramName) const
Fact * getParameter(int componentId, const QString &paramName)
void factAdded(int componentId, Fact *fact)
static constexpr int defaultComponentId
VehicleComponent(Vehicle *vehicle, AutoPilotPlugin *autopilot, AutoPilotPlugin::KnownVehicleComponent KnownVehicleComponent, QObject *parent=nullptr)
Fact * _parameterFact(const QString &name) const
Fact of a parameter of the default component, nullptr when the vehicle does not have it.
virtual void setupTriggerSignals()
static QString _valueWithUnits(const Fact *fact)
The fact's value followed by its units, when it has any.