YARP
Yet Another Robot Platform
 
Loading...
Searching...
No Matches
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>
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
20using namespace yarp::os;
21using namespace yarp::dev;
22
23namespace {
24YARP_LOG_COMPONENT(UPOWERBATTERY, "yarp.device.upowerBattery")
27}
28
29bool 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
52{
53 voltage = m_interface->property("Voltage").toDouble();
54 return ReturnValue_ok;
55}
56
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 ReturnValue_ok;
67}
68
70{
71 charge = m_interface->property("Percentage").toInt();
72 return ReturnValue_ok;
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 ReturnValue_ok;
118}
119
121{
122 temperature = m_interface->property("Temperature").toDouble();
123 return ReturnValue_ok;
124}
125
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();
134 return ReturnValue_ok;
135}
#define ReturnValue_ok
Definition ReturnValue.h:77
upowerBattery: A device to view the battery of a linux laptop in YARP using the yarp::dev::IBattery i...
yarp::dev::ReturnValue getBatteryCurrent(double &current) override
Get the instantaneous current measurement.
yarp::dev::ReturnValue getBatteryInfo(std::string &info) override
get the battery hardware characteristics (e.g.
yarp::dev::ReturnValue getBatteryTemperature(double &temperature) override
get the battery temperature
bool close() override
Close the DeviceDriver.
yarp::dev::ReturnValue getBatteryStatus(Battery_status &status) override
get the battery status
yarp::dev::ReturnValue getBatteryCharge(double &charge) override
get the battery status of charge
yarp::dev::ReturnValue getBatteryVoltage(double &voltage) override
Get the instantaneous voltage measurement.
A mini-server for performing network communication in the background.
A base class for nested structures that can be searched.
Definition Searchable.h:31
A single value (typically within a Bottle).
Definition Value.h:43
#define yCError(component,...)
#define yCDebug(component,...)
#define YARP_LOG_COMPONENT(name,...)
For streams capable of holding different kinds of content, check what they actually have.
An interface to the operating system, including Port based communication.
The main, catch-all namespace for YARP.
Definition dirs.h:16