YARP
Yet Another Robot Platform
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"
20 #include "OVRHeadsetLogComponent.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"
33 #include "img-battery-charging.h"
39 
40 TextureBattery::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 
90 bool 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 {
164  currentTexture = textures[BatteryStatus100];
165  }
166  } else if (charge > 80.0) {
169  } else {
170  currentTexture = textures[BatteryStatus080];
171  }
172  } else if (charge > 60.0) {
175  } else {
176  currentTexture = textures[BatteryStatus060];
177  }
178  } else if (charge > 40.0) {
181  } else {
182  currentTexture = textures[BatteryStatus040];
183  }
184  } else if (charge > 20.0) {
187  } else {
189  }
190  } else {
193  } else {
194  currentTexture = textures[BatteryStatusLow];
195  }
196  }
197 }
LogStream.h
yarp::dev::IBattery::Battery_status
Battery_status
Definition: IBattery.h:36
battery_missing
const TextureStatic::Image battery_missing
Definition: img-battery-missing.h:39
battery_060
const TextureStatic::Image battery_060
Definition: img-battery-060.h:39
yarp::os::Property::put
void put(const std::string &key, const std::string &value)
Associate the given key with the given string.
Definition: Property.cpp:998
img-battery-060.h
yarp::dev::IBattery::getBatteryStatus
virtual bool getBatteryStatus(Battery_status &status)=0
get the battery status
OVRHeadsetLogComponent.h
yCWarning
#define yCWarning(component,...)
Definition: LogComponent.h:146
TextureBattery::BatteryStatusCharging
@ BatteryStatusCharging
Definition: TextureBattery.h:44
img-battery-charging-080.h
battery_caution
const TextureStatic::Image battery_caution
Definition: img-battery-caution.h:39
yarp::dev::PolyDriver::isValid
bool isValid() const
Check if device is valid.
Definition: PolyDriver.cpp:199
TextureBattery::BatteryStatusCharging060
@ BatteryStatusCharging060
Definition: TextureBattery.h:46
TextureBattery::BatteryStatusMissing
@ BatteryStatusMissing
Definition: TextureBattery.h:37
img-battery-charging-040.h
img-battery-080.h
TextureBattery::BatteryStatusCharging040
@ BatteryStatusCharging040
Definition: TextureBattery.h:47
OVRHEADSET
const yarp::os::LogComponent & OVRHEADSET()
Definition: OVRHeadsetLogComponent.cpp:11
yarp::os::PeriodicThread::getPeriod
double getPeriod() const
Return the current period of the thread.
Definition: PeriodicThread.cpp:286
yarp::dev::DeviceDriver::view
bool view(T *&x)
Get an interface to the device driver.
Definition: DeviceDriver.h:77
ret
bool ret
Definition: ImplementAxisInfo.cpp:72
img-battery-100.h
img-battery-040.h
img-battery-charging-060.h
TextureBattery::session
ovrSession session
Definition: TextureBattery.h:55
TextureBattery::BatteryStatus100
@ BatteryStatus100
Definition: TextureBattery.h:38
battery_charging_low
const TextureStatic::Image battery_charging_low
Definition: img-battery-charging-low.h:39
img-battery-charging.h
battery_080
const TextureStatic::Image battery_080
Definition: img-battery-080.h:39
battery_charging_060
const TextureStatic::Image battery_charging_060
Definition: img-battery-charging-060.h:39
TextureBattery::currentTexture
TextureStatic * currentTexture
Definition: TextureBattery.h:57
battery_040
const TextureStatic::Image battery_040
Definition: img-battery-040.h:39
TextureBattery::run
virtual void run()
Loop function.
Definition: TextureBattery.cpp:133
TextureBattery::BatteryStatusChargingLow
@ BatteryStatusChargingLow
Definition: TextureBattery.h:49
PolyDriver.h
battery_charging_caution
const TextureStatic::Image battery_charging_caution
Definition: img-battery-charging-caution.h:39
TextureBattery::BatteryStatus080
@ BatteryStatus080
Definition: TextureBattery.h:39
yarp::os::PeriodicThread::start
bool start()
Call this to start the thread.
Definition: PeriodicThread.cpp:311
yarp::dev::PolyDriver
A container for a device driver.
Definition: PolyDriver.h:27
img-battery-low.h
yarp::dev::PolyDriver::close
bool close() override
Close the DeviceDriver.
Definition: PolyDriver.cpp:176
img-battery-charging-caution.h
TextureBattery::TextureBattery
TextureBattery(ovrSession session, bool enabled)
Definition: TextureBattery.cpp:40
img-battery-missing.h
TextureBattery.h
TextureBattery::BatteryStatusCaution
@ BatteryStatusCaution
Definition: TextureBattery.h:42
yarp::os::PeriodicThread::suspend
void suspend()
Suspend the thread, the thread keeps running by doLoop is never executed.
Definition: PeriodicThread.cpp:321
battery_charging_040
const TextureStatic::Image battery_charging_040
Definition: img-battery-charging-040.h:39
yCAssert
#define yCAssert(component, x)
Definition: LogComponent.h:172
TextureBattery::BatteryStatusLow
@ BatteryStatusLow
Definition: TextureBattery.h:43
battery_charging
const TextureStatic::Image battery_charging
Definition: img-battery-charging.h:39
yCError
#define yCError(component,...)
Definition: LogComponent.h:157
TextureBattery::BatteryStatus040
@ BatteryStatus040
Definition: TextureBattery.h:41
battery_charging_080
const TextureStatic::Image battery_charging_080
Definition: img-battery-charging-080.h:39
TextureBattery::BatteryStatus060
@ BatteryStatus060
Definition: TextureBattery.h:40
battery_100
const TextureStatic::Image battery_100
Definition: img-battery-100.h:39
TextureStatic
Definition: TextureStatic.h:27
IBattery.h
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
yarp::os::ShouldUseSystemClock
ShouldUseSystemClock
Definition: Time.h:23
yarp::os::PeriodicThread::stop
void stop()
Call this to stop the thread, this call blocks until the thread is terminated (and releaseThread() ca...
Definition: PeriodicThread.cpp:296
battery_low
const TextureStatic::Image battery_low
Definition: img-battery-low.h:39
TextureBattery::BatteryStatusChargingCaution
@ BatteryStatusChargingCaution
Definition: TextureBattery.h:48
TextureBattery::BatteryStatusCharging080
@ BatteryStatusCharging080
Definition: TextureBattery.h:45
TextureBattery::~TextureBattery
~TextureBattery()
Definition: TextureBattery.cpp:71
img-battery-caution.h
img-battery-charging-low.h
yarp::os::Property
A class for storing options and configuration information.
Definition: Property.h:37
yarp::dev::IBattery::getBatteryCharge
virtual bool getBatteryCharge(double &charge)=0
get the battery status of charge