YARP
Yet Another Robot Platform
 
Loading...
Searching...
No Matches
Odometry2D_nwc_yarp.cpp
Go to the documentation of this file.
1/*
2 * SPDX-FileCopyrightText: 2025-2025 Istituto Italiano di Tecnologia (IIT)
3 * SPDX-License-Identifier: LGPL-2.1-or-later
4 */
5
6#define _USE_MATH_DEFINES
7
9
10#include <yarp/os/Log.h>
11#include <yarp/os/LogStream.h>
12#include <yarp/math/Math.h>
13
14#include <limits>
15#include <cmath>
16
17using namespace yarp::dev;
18using namespace yarp::os;
19using namespace yarp::sig;
20
21namespace {
22YARP_LOG_COMPONENT(ODOMETRY2DCLIENT, "yarp.device.Odometry2D_nwc_yarp")
23}
24
26{
27 mutex.lock();
28 count=0;
29 deltaT=0;
30 deltaTMax=0;
31 deltaTMin=1e22;
33 prev=now;
34 mutex.unlock();
35}
36
41
43{
45 mutex.lock();
46
47 if (count>0)
48 {
49 double tmpDT=now-prev;
50 deltaT+=tmpDT;
51 if (tmpDT > deltaTMax) {
52 deltaTMax = tmpDT;
53 }
54 if (tmpDT < deltaTMin) {
55 deltaTMin = tmpDT;
56 }
57
58 //compare network time
59 if (tmpDT*1000<DATA_TIMEOUT_ms)
60 {
61 timeout_occurred = false;
62 }
63 else
64 {
65 timeout_occurred = true;
66 }
67 }
68
69 prev=now;
70 count++;
71
72 lastScan=b;
75
76 //initializations (first received data)
77 if (lastStamp.isValid()==false)
78 {
79 lastStamp = newStamp;
80 }
81
82 //now compare timestamps
83 if ((1000 * (newStamp.getTime() - lastStamp.getTime())) < DATA_TIMEOUT_ms)
84 {
85 timeout_occurred = false;
86 }
87 else
88 {
89 timeout_occurred = true;
90 }
91 lastStamp = newStamp;
92
93 mutex.unlock();
94}
95
97{
98 mutex.lock();
99 data=lastScan;
100 stmp = lastStamp;
101 mutex.unlock();
102
103 return true;
104}
105
107{
108 mutex.lock();
109 int ret=count;
110 mutex.unlock();
111 return ret;
112}
113
114// time is in ms
115void OdometryData2DInputPortProcessor::getEstFrequency(int &ite, double &av, double &min, double &max)
116{
117 mutex.lock();
118 ite=count;
119 min=deltaTMin*1000;
120 max=deltaTMax*1000;
121 if (count<1)
122 {
123 av=0;
124 }
125 else
126 {
127 av=deltaT/count;
128 }
129 av=av*1000;
130 mutex.unlock();
131}
132
134{
135 if (!parseParams(config)) { return false; }
136
137 std::string local_rpc_portname = m_local+ "/rpc";
138 std::string remote_rpc_portname = m_remote + "/rpc";
139 std::string local_odometry_input_portname = m_local + "/odometry:i";
140 std::string remote_odometry_input_portname = m_remote + "/odometry:o";
141
142 //open receiving streaming port
144 {
145 yCError(ODOMETRY2DCLIENT, "open() error could not open port %s, check network\n", local_odometry_input_portname.c_str());
146 return false;
147 }
149
150 //open rpc port
152 {
153 yCError(ODOMETRY2DCLIENT, "open() error could not open rpc port %s, check network\n", local_rpc_portname.c_str());
154 return false;
155 }
156
157 //connection for the streaming port
159 if (!ok)
160 {
161 yCError(ODOMETRY2DCLIENT, "open() error could not connect to %s\n", m_remote.c_str());
162 return false;
163 }
164
165 //connection for the rpc port
166 ok=Network::connect(local_rpc_portname, remote_rpc_portname);
167 if (!ok)
168 {
169 yCError(ODOMETRY2DCLIENT, "open() error could not connect to %s\n", remote_rpc_portname.c_str());
170 return false;
171 }
172
174 {
175 yCError(ODOMETRY2DCLIENT, "Error! Cannot attach the port as a client");
176 return false;
177 }
178
179 //protocol check
180 //to be added here
181
182 return true;
183}
184
186{
190 return true;
191}
192
194{
195 std::lock_guard<std::mutex> lg(m_mutex);
197 bool recv = m_inputPort.getLast(measure, m_lastTs);
198
199 odom = measure;
200
201 if (timestamp != nullptr)
202 {
203 *timestamp = m_lastTs.getTime();
204 }
205 return ReturnValue_ok;
206}
207
209{
210 std::lock_guard<std::mutex> lg(m_mutex);
212 if (!ret) {
213 yCError(ODOMETRY2DCLIENT, "Unable to resetOdometry");
214 return ret;
215 }
216 return ReturnValue_ok;
217}
bool ret
#define ReturnValue_ok
Definition ReturnValue.h:77
virtual yarp::dev::ReturnValue reset_odometry_RPC()
bool parseParams(const yarp::os::Searchable &config) override
Parse the DeviceDriver parameters.
OdometryData2DInputPortProcessor m_inputPort
bool open(yarp::os::Searchable &config) override
Open the DeviceDriver.
virtual yarp::dev::ReturnValue getOdometry(yarp::dev::OdometryData &odom, double *timestamp=nullptr) override
Gets the odometry of the robot, including its velocity expressed in the world and in the local refere...
bool close() override
Close the DeviceDriver.
virtual yarp::dev::ReturnValue resetOdometry() override
Resets the odometry of the robot to zero.
void onRead(yarp::dev::OdometryData &v) override
void getEstFrequency(int &ite, double &av, double &min, double &max)
bool getLast(yarp::dev::OdometryData &data, yarp::os::Stamp &stmp)
A mini-server for performing network communication in the background.
bool getEnvelope(PortReader &envelope) override
void close() override
Stop port activity.
void disableCallback() override
Remove a callback set up with useCallback()
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
static double nowSystem()
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.
An interface to the operating system, including Port based communication.