YARP
Yet Another Robot Platform
 
Loading...
Searching...
No Matches
stateExtendedReader.cpp
Go to the documentation of this file.
1/*
2 * SPDX-FileCopyrightText: 2006-2021 Istituto Italiano di Tecnologia (IIT)
3 * SPDX-License-Identifier: BSD-3-Clause
4 */
5
8#include <cstring>
9
12#include <yarp/os/Time.h>
13#include <yarp/os/Network.h>
14#include <yarp/os/Thread.h>
15#include <yarp/os/Vocab.h>
16#include <yarp/os/Stamp.h>
17#include <yarp/os/LogStream.h>
18
19#include <yarp/sig/Vector.h>
20
22#include <yarp/dev/PolyDriver.h>
26
27
28using namespace yarp::os;
29using namespace yarp::dev;
30using namespace yarp::sig;
31
33{
34 mutex.lock();
35 count=0;
36 deltaT=0;
37 deltaTMax=0;
38 deltaTMin=1e22;
39 now=Time::now();
40 prev=now;
41 mutex.unlock();
42}
43
45 deltaTMax{0},
46 deltaTMin{1e22},
47 now{Time::now()},
48 prev{now},
49 timeout{0.5},
50 valid{false},
51 count{0}
52{
53}
54
69
71{
72 now=Time::now();
73 mutex.lock();
74
75 if (count>0)
76 {
77 double tmpDT=now-prev;
78 deltaT+=tmpDT;
79 if (tmpDT > deltaTMax) {
80 deltaTMax = tmpDT;
81 }
82 if (tmpDT < deltaTMin) {
83 deltaTMin = tmpDT;
84 }
85 }
86
87 prev=now;
88 count++;
89
90 valid=true;
91 last=v;
92 getEnvelope(lastStamp);
93 //check that timestamp are available
94 if (!lastStamp.isValid()) {
95 lastStamp.update(now);
96 }
97 mutex.unlock();
98}
99
100void StateExtendedInputPort::setTimeout(const double& timeout) {
101 this->timeout = timeout;
102}
103
104bool StateExtendedInputPort::getLastSingle(int j, int field, double *data, Stamp &stamp, double &localArrivalTime)
105{
106 mutex.lock();
107 bool ret = valid;
108 if (ret)
109 {
110 switch(field)
111 {
112 case VOCAB_ENCODER:
113 *data = last.jointPosition[j];
115 break;
116
119 *data = last.jointVelocity[j];
120 break;
121
124 *data = last.jointAcceleration[j];
125 break;
126
129 *data = last.motorPosition[j];
130 break;
131
134 *data = last.motorVelocity[j];
135 break;
136
139 *data = last.motorAcceleration[j];
140 break;
141
142 case VOCAB_TRQ:
143 ret = last.torque_isValid;
144 *data = last.torque[j];
145 break;
146
149 *data = last.pwmDutycycle[j];
150 break;
151
153 ret = last.current_isValid;
154 *data = last.current[j];
155 break;
156
157 default:
158 yCError(REMOTECONTROLBOARD) << "RemoteControlBoard internal error while reading data. Cannot get 'single' data of type " << yarp::os::Vocab32::decode(field);
159 break;
160 }
161
163 stamp = lastStamp;
164 if (ret && ((Time::now() - localArrivalTime) > timeout)) {
165 ret = false;
166 }
167 }
168 mutex.unlock();
169
170 return ret;
171}
172
173bool StateExtendedInputPort::getLastSingle(int j, int field, int *data, Stamp &stamp, double &localArrivalTime)
174{
175 mutex.lock();
176 bool ret = valid;
177 if (ret)
178 {
179 switch(field)
180 {
183 *data = last.controlMode[j];
184 break;
185
188 *data = last.interactionMode[j];
189 break;
190
191 default:
192 yCError(REMOTECONTROLBOARD) << "RemoteControlBoard internal error while reading data. Cannot get 'single' data of type " << yarp::os::Vocab32::decode(field);
193 break;
194 }
196 stamp = lastStamp;
197 if (ret && ((Time::now() - localArrivalTime) > timeout)) {
198 ret = false;
199 }
200 }
201 mutex.unlock();
202 return ret;
203}
204
206{
207 mutex.lock();
208 bool ret = valid;
209 if (ret)
210 {
211 switch(field)
212 {
213 case VOCAB_ENCODERS:
216 break;
217
221 break;
222
226 break;
227
231 break;
232
236 break;
237
241 break;
242
243 case VOCAB_TRQS:
244 ret = last.torque_isValid;
245 memcpy(data, last.torque.data(), last.torque.size() * last.torque.getElementSize() );
246 break;
247
250 memcpy(data, last.pwmDutycycle.data(), last.pwmDutycycle.size() * last.pwmDutycycle.getElementSize());
251 break;
252
254 ret = last.current_isValid;
255 memcpy(data, last.current.data(), last.current.size() * last.current.getElementSize());
256 break;
257
258 default:
259 yCError(REMOTECONTROLBOARD) << "RemoteControlBoard internal error while reading data. Cannot get 'vector' data of type " << yarp::os::Vocab32::decode(field);
260 break;
261 }
262
264 stamp = lastStamp;
265 if (ret && ((Time::now() - localArrivalTime) > timeout)) {
266 ret = false;
267 }
268 }
269 mutex.unlock();
270
271 return ret;
272}
273
275{
276 mutex.lock();
277 bool ret = valid;
278 if (ret)
279 {
280 switch(field)
281 {
284 memcpy(data, last.controlMode.data(), last.controlMode.size() * last.controlMode.getElementSize());
285 break;
286
290 break;
291
292 default:
293 yCError(REMOTECONTROLBOARD) << "RemoteControlBoard internal error while reading data. Cannot get 'vector' data of type " << yarp::os::Vocab32::decode(field);
294 break;
295 }
297 stamp = lastStamp;
298 if (ret && ((Time::now() - localArrivalTime) > timeout)) {
299 ret = false;
300 }
301 }
302 mutex.unlock();
303 return ret;
304}
305
307{
308 mutex.lock();
309 int ret=count;
310 mutex.unlock();
311 return ret;
312}
313
314// time is in ms
315void StateExtendedInputPort::getEstFrequency(int &ite, double &av, double &min, double &max)
316{
317 mutex.lock();
318 ite=count;
319 min=deltaTMin*1000;
320 max=deltaTMax*1000;
321 if (count<1)
322 {
323 av=0;
324 }
325 else
326 {
327 av=deltaT/count;
328 }
329 av=av*1000;
330 mutex.unlock();
331}
define control board standard interfaces
constexpr yarp::conf::vocab32_t VOCAB_AMP_CURRENT
constexpr yarp::conf::vocab32_t VOCAB_AMP_CURRENTS
constexpr yarp::conf::vocab32_t VOCAB_CM_CONTROL_MODES
constexpr yarp::conf::vocab32_t VOCAB_CM_CONTROL_MODE
constexpr yarp::conf::vocab32_t VOCAB_ENCODER_ACCELERATIONS
Definition IEncoders.h:213
constexpr yarp::conf::vocab32_t VOCAB_ENCODER
Definition IEncoders.h:206
constexpr yarp::conf::vocab32_t VOCAB_ENCODER_SPEEDS
Definition IEncoders.h:211
constexpr yarp::conf::vocab32_t VOCAB_ENCODER_SPEED
Definition IEncoders.h:210
constexpr yarp::conf::vocab32_t VOCAB_ENCODER_ACCELERATION
Definition IEncoders.h:212
constexpr yarp::conf::vocab32_t VOCAB_ENCODERS
Definition IEncoders.h:207
constexpr yarp::conf::vocab32_t VOCAB_INTERACTION_MODES
constexpr yarp::conf::vocab32_t VOCAB_INTERACTION_MODE
constexpr yarp::conf::vocab32_t VOCAB_MOTOR_ENCODER_SPEED
constexpr yarp::conf::vocab32_t VOCAB_MOTOR_ENCODER_ACCELERATIONS
constexpr yarp::conf::vocab32_t VOCAB_MOTOR_ENCODER_ACCELERATION
constexpr yarp::conf::vocab32_t VOCAB_MOTOR_ENCODER
constexpr yarp::conf::vocab32_t VOCAB_MOTOR_ENCODER_SPEEDS
constexpr yarp::conf::vocab32_t VOCAB_MOTOR_ENCODERS
constexpr yarp::conf::vocab32_t VOCAB_PWMCONTROL_PWM_OUTPUTS
constexpr yarp::conf::vocab32_t VOCAB_PWMCONTROL_PWM_OUTPUT
constexpr yarp::conf::vocab32_t VOCAB_TRQ
constexpr yarp::conf::vocab32_t VOCAB_TRQS
bool ret
const yarp::os::LogComponent & REMOTECONTROLBOARD()
contains the definition of a Vector type
void onRead(yarp::dev::impl::jointData &v) override
void setTimeout(const double &timeout)
setTimeout, set the timeout for retrieving data
bool getLastVector(int field, double *data, Stamp &stamp, double &localArrivalTime)
bool getLastSingle(int j, int field, double *data, Stamp &stamp, double &localArrivalTime)
void init(int numberOfJoints)
void getEstFrequency(int &ite, double &av, double &min, double &max)
yarp::sig::VectorOf< double > motorVelocity
Definition jointData.h:35
yarp::sig::VectorOf< double > jointAcceleration
Definition jointData.h:31
yarp::sig::VectorOf< int > controlMode
Definition jointData.h:45
yarp::sig::VectorOf< int > interactionMode
Definition jointData.h:47
yarp::sig::VectorOf< double > current
Definition jointData.h:43
yarp::sig::VectorOf< double > motorAcceleration
Definition jointData.h:37
yarp::sig::VectorOf< double > motorPosition
Definition jointData.h:33
yarp::sig::VectorOf< double > pwmDutycycle
Definition jointData.h:41
yarp::sig::VectorOf< double > torque
Definition jointData.h:39
yarp::sig::VectorOf< double > jointPosition
Definition jointData.h:27
yarp::sig::VectorOf< double > jointVelocity
Definition jointData.h:29
A mini-server for performing network communication in the background.
bool getEnvelope(PortReader &envelope) override
An abstraction for a time stamp and/or sequence number.
Definition Stamp.h:21
void update()
Set the timestamp to the current time, and increment the sequence number (wrapping to 0 if the sequen...
Definition Stamp.cpp:124
bool isValid() const
Check if this Stamp is valid.
Definition Stamp.cpp:39
void resize(size_t size) override
Resize the vector.
Definition Vector.h:221
size_t size() const
Definition Vector.h:341
T * data()
Return a pointer to the first element of the vector.
Definition Vector.h:206
size_t getElementSize() const override
Definition Vector.h:164
#define yCError(component,...)
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
std::string decode(NetInt32 code)
Convert a vocabulary identifier into a string.
Definition Vocab.cpp:33
An interface to the operating system, including Port based communication.