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