YARP
Yet Another Robot Platform
BatteryClient.cpp
Go to the documentation of this file.
1/*
2 * SPDX-FileCopyrightText: 2006-2021 Istituto Italiano di Tecnologia (IIT)
3 * SPDX-License-Identifier: LGPL-2.1-or-later
4 */
5
6#include "BatteryClient.h"
7
8#include <yarp/os/Log.h>
10#include <yarp/os/LogStream.h>
11
14using namespace yarp::dev;
15using namespace yarp::os;
16using namespace yarp::sig;
17
18namespace {
19YARP_LOG_COMPONENT(BATTERYCLIENT, "yarp.device.batteryClient")
20} // namespace
21
23{
24 mutex.lock();
25 count=0;
26 deltaT=0;
27 deltaTMax=0;
28 deltaTMin=1e22;
29 now=Time::now();
30 prev=now;
31 mutex.unlock();
32}
33
35{
36 state=IBattery::BATTERY_GENERAL_ERROR;
37 resetStat();
38}
39
41{
42 now=Time::now();
43 mutex.lock();
44
45 if (count>0)
46 {
47 double tmpDT=now-prev;
48 deltaT+=tmpDT;
49 if (tmpDT > deltaTMax) {
50 deltaTMax = tmpDT;
51 }
52 if (tmpDT < deltaTMin) {
53 deltaTMin = tmpDT;
54 }
55
56 //compare network time
57 if (tmpDT*1000<BATTERY_TIMEOUT)
58 {
59 state = b.get(5).asInt32();
60 }
61 else
62 {
64 }
65 }
66
67 prev=now;
68 count++;
69
70 lastBottle=b;
71 Stamp newStamp;
72 getEnvelope(newStamp);
73
74 //initialization (first received data)
75 if (lastStamp.isValid()==false)
76 {
77 lastStamp = newStamp;
78 }
79
80 //now compare timestamps
81 if ((1000*(newStamp.getTime()-lastStamp.getTime()))<BATTERY_TIMEOUT)
82 {
83 state = b.get(5).asInt32();
84 }
85 else
86 {
88 }
89 lastStamp = newStamp;
90
91 mutex.unlock();
92}
93
95{
96 mutex.lock();
97 int ret=state;
98 if (ret != IBattery::BATTERY_GENERAL_ERROR)
99 {
100 data=lastBottle;
101 stmp = lastStamp;
102 }
103 mutex.unlock();
104
105 return ret;
106}
107
109{
110 mutex.lock();
111 double voltage = lastBottle.get(0).asFloat64();
112 mutex.unlock();
113 return voltage;
114}
115
117{
118 mutex.lock();
119 double current = lastBottle.get(1).asFloat64();
120 mutex.unlock();
121 return current;
122}
123
125{
126 mutex.lock();
127 double charge = lastBottle.get(2).asFloat64();
128 mutex.unlock();
129 return charge;
130}
131
133{
134 mutex.lock();
135 int status = lastBottle.get(4).asInt32();
136 mutex.unlock();
137 return status;
138}
139
141{
142 mutex.lock();
143 double temperature = lastBottle.get(3).asFloat64();
144 mutex.unlock();
145 return temperature;
146}
147
149{
150 mutex.lock();
151 int ret=count;
152 mutex.unlock();
153 return ret;
154}
155
156// time is in ms
157void BatteryInputPortProcessor::getEstFrequency(int &ite, double &av, double &min, double &max)
158{
159 mutex.lock();
160 ite=count;
161 min=deltaTMin*1000;
162 max=deltaTMax*1000;
163 if (count<1)
164 {
165 av=0;
166 }
167 else
168 {
169 av=deltaT/count;
170 }
171 av=av*1000;
172 mutex.unlock();
173}
174
176{
177 local.clear();
178 remote.clear();
179 yCDebug(BATTERYCLIENT) << config.toString();
180 local = config.find("local").asString();
181 remote = config.find("remote").asString();
182 m_carrier = config.check("carrier", yarp::os::Value("tcp"), "the carrier used for the connection with the server").asString();
183
184 if (local=="")
185 {
186 yCError(BATTERYCLIENT, "open(): Invalid local name. --local parameter missing.");
187 return false;
188 }
189 if (remote=="")
190 {
191 yCError(BATTERYCLIENT, "open(): Invalid remote name. --remote parameter missing.");
192 return false;
193 }
194
195 std::string local_stream = local;
196 local_stream += "/data:i";
197 std::string local_rpc = local;
198 local_rpc += "/rpc:o";
199 std::string remote_stream = remote;
200 remote_stream += "/data:o";
201 std::string remote_rpc = remote;
202 remote_rpc += "/rpc:i";
203
204 if (!inputPort.open(local_stream))
205 {
206 yCError(BATTERYCLIENT, "open(): Could not open port %s, check network", local_stream.c_str());
207 return false;
208 }
210
211 if (!rpcPort.open(local_rpc))
212 {
213 yCError(BATTERYCLIENT, "open(): Could not open rpc port %s, check network", local_rpc.c_str());
214 return false;
215 }
216
217 bool ok=Network::connect(remote_stream.c_str(), local_stream.c_str(), m_carrier);
218 if (!ok)
219 {
220 yCError(BATTERYCLIENT, "open(): Could not connect %s -> %s", remote_stream.c_str(), local_stream.c_str());
221 return false;
222 }
223
224 ok=Network::connect(local_rpc, remote_rpc);
225 if (!ok)
226 {
227 yCError(BATTERYCLIENT, "open() Could not connect %s -> %s", remote_rpc.c_str(), local_rpc.c_str());
228 return false;
229 }
230
231 return true;
232}
233
235{
236 rpcPort.close();
238 return true;
239}
240
242{
243 voltage = inputPort.getVoltage();
244 return true;
245}
246
248{
249 current = inputPort.getCurrent();
250 return true;
251}
252
254{
255 charge = inputPort.getCharge();
256 return true;
257}
258
260{
262 return true;
263}
264
266{
267 temperature = inputPort.getTemperature();
268 return true;
269}
270
271bool BatteryClient::getBatteryInfo(std::string &battery_info)
272{
273 Bottle cmd, response;
276 bool ok = rpcPort.write(cmd, response);
277 if (CHECK_FAIL(ok, response)!=false)
278 {
279 battery_info = response.get(2).asString();
280 return true;
281 }
282 return false;
283}
284
286{
287 return lastTs;
288}
const int BATTERY_TIMEOUT
Definition: BatteryClient.h:24
constexpr yarp::conf::vocab32_t VOCAB_IBATTERY
Definition: IBattery.h:14
constexpr yarp::conf::vocab32_t VOCAB_BATTERY_INFO
Definition: IBattery.h:15
bool ret
bool open(yarp::os::Searchable &config) override
Open the DeviceDriver.
bool getBatteryCurrent(double &current) override
Get the instantaneous current measurement.
std::string local
Definition: BatteryClient.h:87
bool close() override
Close the DeviceDriver.
bool getBatteryTemperature(double &temperature) override
get the battery temperature
yarp::os::Stamp lastTs
Definition: BatteryClient.h:90
yarp::os::Stamp getLastInputStamp() override
Get the time stamp for the last read data.
bool getBatteryVoltage(double &voltage) override
Get the instantaneous voltage measurement.
bool getBatteryInfo(std::string &battery_info) override
get the battery hardware charactestics (e.g.
std::string remote
Definition: BatteryClient.h:88
bool getBatteryStatus(Battery_status &status) override
get the battery status
bool getBatteryCharge(double &charge) override
get the battery status of charge
yarp::os::Port rpcPort
Definition: BatteryClient.h:86
std::string m_carrier
Definition: BatteryClient.h:89
BatteryInputPortProcessor inputPort
Definition: BatteryClient.h:85
int getLast(yarp::os::Bottle &data, yarp::os::Stamp &stmp)
void onRead(yarp::os::Bottle &v) override
Callback method.
void getEstFrequency(int &ite, double &av, double &min, double &max)
A simple collection of objects that can be described and transmitted in a portable way.
Definition: Bottle.h:64
void addVocab32(yarp::conf::vocab32_t x)
Places a vocabulary item in the bottle, at the end of the list.
Definition: Bottle.cpp:164
Value & get(size_type index) const
Reads a Value v from a certain part of the list.
Definition: Bottle.cpp:246
bool getEnvelope(PortReader &envelope) override
void close() override
Stop port activity.
bool open(const std::string &name) override
Start port operation, with a specific name, with automatically-chosen network parameters.
void useCallback(TypedReaderCallback< T > &callback) override
Set an object whose onRead method will be called when data is available.
bool write(const PortWriter &writer, const PortWriter *callback=nullptr) const override
Write an object to the port.
Definition: Port.cpp:436
void close() override
Stop port activity.
Definition: Port.cpp:363
bool open(const std::string &name) override
Start port operation, with a specific name, with automatically-chosen network parameters.
Definition: Port.cpp:79
A base class for nested structures that can be searched.
Definition: Searchable.h:63
virtual bool check(const std::string &key) const =0
Check if there exists a property of the given name.
virtual std::string toString() const =0
Return a standard text representation of the content of the object.
virtual Value & find(const std::string &key) const =0
Gets a value corresponding to a given keyword.
An abstraction for a time stamp and/or sequence number.
Definition: Stamp.h:21
double getTime() const
Get the time stamp.
Definition: Stamp.cpp:34
bool isValid() const
Check if this Stamp is valid.
Definition: Stamp.cpp:39
A single value (typically within a Bottle).
Definition: Value.h:43
virtual yarp::conf::float64_t asFloat64() const
Get 64-bit floating point value.
Definition: Value.cpp:222
virtual std::int32_t asInt32() const
Get 32-bit integer value.
Definition: Value.cpp:204
virtual std::string asString() const
Get string value.
Definition: Value.cpp:234
#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.
double now()
Return the current time in seconds, relative to an arbitrary starting point.
Definition: Time.cpp:121
An interface to the operating system, including Port based communication.