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>
9 #include <yarp/os/LogComponent.h>
10 #include <yarp/os/LogStream.h>
11 
14 using namespace yarp::dev;
15 using namespace yarp::os;
16 using namespace yarp::sig;
17 
18 namespace {
19 YARP_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 {
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;
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
157 void 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 
183  if (local=="")
184  {
185  yCError(BATTERYCLIENT, "open(): Invalid local name. --local parameter missing.");
186  return false;
187  }
188  if (remote=="")
189  {
190  yCError(BATTERYCLIENT, "open(): Invalid remote name. --remote parameter missing.");
191  return false;
192  }
193 
194  std::string local_stream = local;
195  local_stream += "/data:i";
196  std::string local_rpc = local;
197  local_rpc += "/rpc:o";
198  std::string remote_stream = remote;
199  remote_stream += "/data:o";
200  std::string remote_rpc = remote;
201  remote_rpc += "/rpc:i";
202 
203  if (!inputPort.open(local_stream))
204  {
205  yCError(BATTERYCLIENT, "open(): Could not open port %s, check network", local_stream.c_str());
206  return false;
207  }
208  inputPort.useCallback();
209 
210  if (!rpcPort.open(local_rpc))
211  {
212  yCError(BATTERYCLIENT, "open(): Could not open rpc port %s, check network", local_rpc.c_str());
213  return false;
214  }
215 
216  bool ok=Network::connect(remote_stream.c_str(), local_stream.c_str(), "udp");
217  if (!ok)
218  {
219  yCError(BATTERYCLIENT, "open(): Could not connect %s -> %s", remote_stream.c_str(), local_stream.c_str());
220  return false;
221  }
222 
223  ok=Network::connect(local_rpc, remote_rpc);
224  if (!ok)
225  {
226  yCError(BATTERYCLIENT, "open() Could not connect %s -> %s", remote_rpc.c_str(), local_rpc.c_str());
227  return false;
228  }
229 
230  return true;
231 }
232 
234 {
235  rpcPort.close();
236  inputPort.close();
237  return true;
238 }
239 
240 bool BatteryClient::getBatteryVoltage(double &voltage)
241 {
242  voltage = inputPort.getVoltage();
243  return true;
244 }
245 
246 bool BatteryClient::getBatteryCurrent(double &current)
247 {
248  current = inputPort.getCurrent();
249  return true;
250 }
251 
253 {
254  charge = inputPort.getCharge();
255  return true;
256 }
257 
259 {
260  status = (Battery_status)inputPort.getStatus();
261  return true;
262 }
263 
264 bool BatteryClient::getBatteryTemperature(double &temperature)
265 {
266  temperature = inputPort.getTemperature();
267  return true;
268 }
269 
270 bool BatteryClient::getBatteryInfo(std::string &battery_info)
271 {
272  Bottle cmd, response;
275  bool ok = rpcPort.write(cmd, response);
276  if (CHECK_FAIL(ok, response)!=false)
277  {
278  battery_info = response.get(2).asString();
279  return true;
280  }
281  return false;
282 }
283 
285 {
286  return lastTs;
287 }
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.
bool close() override
Close the DeviceDriver.
bool getBatteryTemperature(double &temperature) override
get the battery temperature
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.
bool getBatteryStatus(Battery_status &status) override
get the battery status
bool getBatteryCharge(double &charge) override
get the battery status of charge
int getLast(yarp::os::Bottle &data, yarp::os::Stamp &stmp)
void onRead(yarp::os::Bottle &v) override
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:74
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
A base class for nested structures that can be searched.
Definition: Searchable.h:66
virtual Value & find(const std::string &key) const =0
Gets a value corresponding to a given keyword.
virtual std::string toString() const =0
Return a standard text representation of the content of the object.
An abstraction for a time stamp and/or sequence number.
Definition: Stamp.h:22
double getTime() const
Get the time stamp.
Definition: Stamp.cpp:34
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:154
#define yCDebug(component,...)
Definition: LogComponent.h:109
#define YARP_LOG_COMPONENT(name,...)
Definition: LogComponent.h:77
An interface for the device drivers.
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.
Signal processing.
Definition: Image.h:22