YARP
Yet Another Robot Platform
upowerBattery.cpp
Go to the documentation of this file.
1 /*
2  * SPDX-FileCopyrightText: 2006-2021 Istituto Italiano di Tecnologia (IIT)
3  * SPDX-License-Identifier: BSD-3-Clause
4  */
5 
6 #include "upowerBattery.h"
7 
8 #include <yarp/os/Log.h>
9 #include <yarp/os/LogComponent.h>
10 #include <yarp/os/LogStream.h>
11 #include <yarp/os/Time.h>
12 
13 #include <iostream>
14 #include <cstring>
15 
16 #include <QString>
17 #include <QDBusInterface>
18 #include <QDBusReply>
19 
20 using namespace yarp::os;
21 using namespace yarp::dev;
22 
23 namespace {
24 YARP_LOG_COMPONENT(UPOWERBATTERY, "yarp.device.upowerBattery")
25 const QString UPOWER_SERVICE = QStringLiteral("org.freedesktop.UPower");
26 const QString UPOWER_OBJECT = QStringLiteral("org.freedesktop.UPower.Device");
27 }
28 
29 bool UpowerBattery::open(yarp::os::Searchable& config)
30 {
31  std::string device_path = config.check("device_path",
32  Value("/org/freedesktop/UPower/devices/battery_BAT0"),
33  "Battery device path (as returned by 'upower -e', for example '/org/freedesktop/UPower/devices/battery_BAT0')").asString();
34 
35  m_interface = new QDBusInterface(UPOWER_SERVICE, QString(device_path.c_str()), UPOWER_OBJECT, QDBusConnection::systemBus());
36  if (!m_interface->isValid()) {
37  yCError(UPOWERBATTERY) << "Interface not found";
38  delete m_interface;
39  return false;
40  }
41 
42  return true;
43 }
44 
46 {
47  delete m_interface;
48  return true;
49 }
50 
51 bool UpowerBattery::getBatteryVoltage(double& voltage)
52 {
53  voltage = m_interface->property("Voltage").toDouble();
54  return true;
55 }
56 
57 bool UpowerBattery::getBatteryCurrent(double& current)
58 {
59  auto energyrate = m_interface->property("EnergyRate").toDouble();
60  auto voltage = m_interface->property("Voltage").toDouble();
61  auto state = m_interface->property("State").toUInt();
62  current = energyrate / voltage;
63  if (current > 0 && state == 1 /* Charging */) {
64  current = -current;
65  }
66  return true;
67 }
68 
69 bool UpowerBattery::getBatteryCharge(double& charge)
70 {
71  charge = m_interface->property("Percentage").toInt();
72  return true;
73 }
74 
76 {
78  auto st = m_interface->property("State").toUInt();
79  auto wl = m_interface->property("WarningLevel").toUInt();
80 
81  switch (st) {
82  case 1 /* Charging */: [[fallthrough]];
83  case 5 /* Pending charge */:
85  break;
86  case 2 /* Discharging */:
87  case 6 /* Pending discharge */:
88  switch (wl) {
89  case 1 /* None */: [[fallthrough]];
90  case 2 /* Discharging (only for UPSes) */:
92  break;
93  case 3 /* Low */:
95  break;
96  case 4 /* Critical */: [[fallthrough]];
97  case 5 /* Action */:
99  break;
100  case 0 /* Unknown */: [[fallthrough]];
101  default:
103  }
104  break;
105  case 3 /* Empty */:
107  break;
108  case 4 /* Fully charged */:
110  break;
111  case 0 /* Unknown */: [[fallthrough]];
112  default:
114  break;
115  }
116 
117  return true;
118 }
119 
120 bool UpowerBattery::getBatteryTemperature(double& temperature)
121 {
122  temperature = m_interface->property("Temperature").toDouble();
123  return true;
124 }
125 
126 bool UpowerBattery::getBatteryInfo(std::string& info)
127 {
128  info = QStringLiteral("Vendor: %1, Model: %2, Serial: %3")
129  .arg(m_interface->property("Vendor").toString())
130  .arg(m_interface->property("Model").toString())
131  .arg(m_interface->property("Serial").toString())
132  .toStdString();
133  yCDebug(UPOWERBATTERY) << info;
134  return true;
135 }
upowerBattery: A device to view the battery of a linux laptop in YARP using the yarp::dev::IBattery i...
Definition: upowerBattery.h:25
bool getBatteryCurrent(double &current) override
Get the instantaneous current measurement.
bool getBatteryStatus(Battery_status &status) override
get the battery status
bool close() override
Close the DeviceDriver.
bool getBatteryTemperature(double &temperature) override
get the battery temperature
bool getBatteryCharge(double &charge) override
get the battery status of charge
bool getBatteryVoltage(double &voltage) override
Get the instantaneous voltage measurement.
bool getBatteryInfo(std::string &info) override
get the battery hardware characteristics (e.g.
A base class for nested structures that can be searched.
Definition: Searchable.h:63
A single value (typically within a Bottle).
Definition: Value.h:43
#define yCError(component,...)
Definition: LogComponent.h:213
#define yCDebug(component,...)
Definition: LogComponent.h:128
#define YARP_LOG_COMPONENT(name,...)
Definition: LogComponent.h:76
For streams capable of holding different kinds of content, check what they actually have.
An interface to the operating system, including Port based communication.
yarp::robotinterface::Device Device
Definition: Device.h:31
The main, catch-all namespace for YARP.
Definition: dirs.h:16