YARP
Yet Another Robot Platform
 
Loading...
Searching...
No Matches
TextureBattery.cpp
Go to the documentation of this file.
1/*
2 * Copyright (C) 2006-2020 Istituto Italiano di Tecnologia (IIT)
3 *
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Lesser General Public
6 * License as published by the Free Software Foundation; either
7 * version 2.1 of the License, or (at your option) any later version.
8 *
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Lesser General Public License for more details.
13 *
14 * You should have received a copy of the GNU Lesser General Public
15 * License along with this library; if not, write to the Free Software
16 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
17 */
18
19#include "TextureBattery.h"
21
22#include <yarp/os/LogStream.h>
23#include <yarp/dev/PolyDriver.h>
24#include <yarp/dev/IBattery.h>
25
26#include "img-battery-missing.h"
27#include "img-battery-100.h"
28#include "img-battery-080.h"
29#include "img-battery-060.h"
30#include "img-battery-040.h"
31#include "img-battery-caution.h"
32#include "img-battery-low.h"
39
40TextureBattery::TextureBattery(ovrSession session, bool enabled) :
41 PeriodicThread(5.0, yarp::os::ShouldUseSystemClock::Yes),
42 session(session),
43 currentTexture(nullptr),
44 currentStatus(BatteryStatusMissing),
45 drv(nullptr),
46 ibat(nullptr)
47{
61
62 currentTexture = textures[currentStatus];
63
64 if (!enabled) {
65 // Calling suspend() before start should ensure that run() is never called
66 suspend();
67 }
68 start();
69}
70
72{
73 stop();
74
75 if (drv) {
76 drv->close();
77 delete drv;
78 drv = nullptr;
79 ibat = nullptr;
80 }
81
82 for (TextureStatic* texture : textures) {
83 delete texture;
84 texture = nullptr;
85 }
86
87 currentTexture = nullptr;
88}
89
90bool TextureBattery::initBatteryClient()
91{
92 if (ibat) {
93 return true;
94 }
95
96 // FIXME
97 std::string robot_name = "foo";
98 std::string localPort = "/oculus/battery:i";
99 std::string remotePort = "/" + robot_name + "/battery:o";
100
101 yarp::os::Property options;
102 options.put("robot", robot_name.c_str());
103 options.put("device", "BatteryClient");
104 options.put("local", localPort.c_str());
105 options.put("remote", remotePort.c_str());
106 options.put("period", getPeriod()*1000); //s to ms
107 options.put("quiet", true);
108
109 drv = new yarp::dev::PolyDriver(options);
110
111 if (!drv || !(drv->isValid())) {
112 yCError(OVRHEADSET, "Problems instantiating the device driver");
113 delete drv;
114 drv = nullptr;
115 ibat = nullptr;
116 return false;
117 }
118
119 drv->view(ibat);
120 if (!ibat) {
121 yCError(OVRHEADSET, "Problems viewing the battery interface");
122 drv->close();
123 delete drv;
124 drv = nullptr;
125 ibat = nullptr;
126 return false;
127 }
128
129 return true;
130}
131
132
134{
135 // currentStatus = (BatteryStatus)(((int)currentStatus + 1) % textures.size());
136 // currentTexture = textures[currentStatus];
137 // return;
138
139 if (!ibat) {
140 if (!initBatteryClient()) {
141 yCWarning(OVRHEADSET) << "Cannot connect to battery. Suspending thread.";
143 suspend();
144 return;
145 }
146 }
147 yCAssert(OVRHEADSET, ibat);
148
150 double charge;
151 bool ret = true;
152 ret &= ibat->getBatteryCharge(charge);
153 ret &= ibat->getBatteryStatus(status);
154
155 if (!ret) {
157 return;
158 }
159
160 if (charge > 95.0) {
163 } else {
165 }
166 } else if (charge > 80.0) {
169 } else {
171 }
172 } else if (charge > 60.0) {
175 } else {
177 }
178 } else if (charge > 40.0) {
181 } else {
183 }
184 } else if (charge > 20.0) {
187 } else {
189 }
190 } else {
193 } else {
195 }
196 }
197}
bool ret
const yarp::os::LogComponent & OVRHEADSET()
ovrSession session
TextureStatic * currentTexture
virtual void run()
Loop function.
TextureBattery(ovrSession session, bool enabled)
bool view(T *&x)
Get an interface to the device driver.
virtual yarp::dev::ReturnValue getBatteryCharge(double &charge)=0
get the battery status of charge
virtual yarp::dev::ReturnValue getBatteryStatus(Battery_status &status)=0
get the battery status
A container for a device driver.
Definition PolyDriver.h:23
bool close() override
Close the DeviceDriver.
bool isValid() const
Check if device is valid.
void suspend()
Suspend the thread, the thread keeps running by doLoop is never executed.
bool start()
Call this to start the thread.
double getPeriod() const
Return the current period of the thread.
void stop()
Call this to stop the thread, this call blocks until the thread is terminated (and releaseThread() ca...
A class for storing options and configuration information.
Definition Property.h:33
void put(const std::string &key, const std::string &value)
Associate the given key with the given string.
Definition Property.cpp:987
const TextureStatic::Image battery_040
const TextureStatic::Image battery_060
const TextureStatic::Image battery_080
const TextureStatic::Image battery_100
const TextureStatic::Image battery_caution
const TextureStatic::Image battery_charging_040
const TextureStatic::Image battery_charging_060
const TextureStatic::Image battery_charging_080
const TextureStatic::Image battery_charging_caution
const TextureStatic::Image battery_charging_low
const TextureStatic::Image battery_charging
const TextureStatic::Image battery_low
const TextureStatic::Image battery_missing
#define yCError(component,...)
#define yCAssert(component, x)
#define yCWarning(component,...)
The main, catch-all namespace for YARP.
Definition dirs.h:16