YARP
Yet Another Robot Platform
 
Loading...
Searching...
No Matches
Battery_nwc_yarp.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 "Battery_nwc_yarp.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.Battery_nwc_yarp")
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
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.status;
60 }
61 else
62 {
64 }
65 }
66
67 prev=now;
68 count++;
69
70 lastData=b;
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.status;
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=lastData;
101 stmp = lastStamp;
102 }
103 mutex.unlock();
104
105 return ret;
106}
107
109{
110 mutex.lock();
111 double voltage = lastData.voltage;
112 mutex.unlock();
113 return voltage;
114}
115
117{
118 mutex.lock();
119 double current = lastData.current;
120 mutex.unlock();
121 return current;
122}
123
125{
126 mutex.lock();
127 double charge = lastData.charge;
128 mutex.unlock();
129 return charge;
130}
131
139
141{
142 mutex.lock();
143 double temperature = lastData.temperature;
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 Battery_InputPortProcessor::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 if (!parseParams(config)) { return false; }
178
179 std::string local_stream = m_local;
180 local_stream += "/data:i";
181 std::string local_rpc = m_local;
182 local_rpc += "/rpc:o";
183 std::string remote_stream = m_remote;
184 remote_stream += "/data:o";
185 std::string remote_rpc = m_remote;
186 remote_rpc += "/rpc:i";
187
189 {
190 yCError(BATTERYCLIENT, "open(): Could not open port %s, check network", local_stream.c_str());
191 return false;
192 }
194
195 if (!rpcPort.open(local_rpc))
196 {
197 yCError(BATTERYCLIENT, "open(): Could not open rpc port %s, check network", local_rpc.c_str());
198 return false;
199 }
200
201 bool ok=Network::connect(remote_stream.c_str(), local_stream.c_str(), m_carrier);
202 if (!ok)
203 {
204 yCError(BATTERYCLIENT, "open(): Could not connect %s -> %s", remote_stream.c_str(), local_stream.c_str());
205 return false;
206 }
207
208 ok=Network::connect(local_rpc, remote_rpc);
209 if (!ok)
210 {
211 yCError(BATTERYCLIENT, "open() Could not connect %s -> %s", remote_rpc.c_str(), local_rpc.c_str());
212 return false;
213 }
215 {
216 yCError(BATTERYCLIENT, "Error! Cannot attach the rpcPort as a client");
217 return false;
218 }
219
220 return true;
221}
222
224{
225 rpcPort.close();
227 return true;
228}
229
231{
232 voltage = inputPort.getVoltage();
233 return ReturnValue_ok;
234}
235
237{
238 current = inputPort.getCurrent();
239 return ReturnValue_ok;
240}
241
243{
244 charge = inputPort.getCharge();
245 return ReturnValue_ok;
246}
247
253
255{
256 temperature = inputPort.getTemperature();
257 return ReturnValue_ok;
258}
259
261{
263 if (!ret.result)
264 {
265 yCError(BATTERYCLIENT, "Unable to: getBatteryInfo()");
266 return ret.result;
267 }
268 battery_info = ret.info;
269 return ret.result;
270}
271
bool ret
#define ReturnValue_ok
Definition ReturnValue.h:77
yarp::dev::IBattery::Battery_status getStatus()
void onRead(yarp::dev::BatteryData &v) override
int getLast(yarp::dev::BatteryData &data, yarp::os::Stamp &stmp)
void getEstFrequency(int &ite, double &av, double &min, double &max)
bool parseParams(const yarp::os::Searchable &config) override
Parse the DeviceDriver parameters.
bool close() override
Close the DeviceDriver.
yarp::dev::ReturnValue getBatteryVoltage(double &voltage) override
Get the instantaneous voltage measurement.
yarp::os::Stamp getLastInputStamp() override
Get the time stamp for the last read data.
IBatteryMsgs m_battery_RPC
Battery_InputPortProcessor inputPort
yarp::dev::ReturnValue getBatteryCharge(double &charge) override
get the battery status of charge
yarp::dev::ReturnValue getBatteryStatus(yarp::dev::IBattery::Battery_status &status) override
get the battery status
yarp::dev::ReturnValue getBatteryTemperature(double &temperature) override
get the battery temperature
yarp::dev::ReturnValue getBatteryInfo(std::string &battery_info) override
get the battery hardware characteristics (e.g.
yarp::os::Stamp lastTs
bool open(yarp::os::Searchable &config) override
Open the DeviceDriver.
yarp::os::Port rpcPort
yarp::dev::ReturnValue getBatteryCurrent(double &current) override
Get the instantaneous current measurement.
virtual return_get_BatteryInfo getBatteryInfoRPC()
double charge
Battery charge percentage [%].
Definition BatteryData.h:29
double voltage
Battery Voltage [V].
Definition BatteryData.h:33
double current
Battery Current [A].
Definition BatteryData.h:37
std::int32_t status
the status of the device.
Definition BatteryData.h:45
double temperature
Battery Temperature [degC].
Definition BatteryData.h:41
A mini-server for performing network communication in the background.
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.
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:31
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
yarp::os::WireLink & yarp()
Get YARP state associated with this object.
Definition Wire.h:28
#define yCError(component,...)
#define YARP_LOG_COMPONENT(name,...)
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.