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
55void StateExtendedInputPort::init(int numberOfJoints)
56{
57 last.jointPosition.resize(numberOfJoints);
58 last.jointVelocity.resize(numberOfJoints);
59 last.jointAcceleration.resize(numberOfJoints);
60 last.motorPosition.resize(numberOfJoints);
61 last.motorVelocity.resize(numberOfJoints);
62 last.motorAcceleration.resize(numberOfJoints);
63 last.torque.resize(numberOfJoints);
64 last.pwmDutycycle.resize(numberOfJoints);
65 last.temperature.resize(numberOfJoints);
66 last.current.resize(numberOfJoints);
67 last.controlMode.resize(numberOfJoints);
68 last.interactionMode.resize(numberOfJoints);
69}
70
72{
73 now=Time::now();
74 mutex.lock();
75
76 if (count>0)
77 {
78 double tmpDT=now-prev;
79 deltaT+=tmpDT;
80 if (tmpDT > deltaTMax) {
81 deltaTMax = tmpDT;
82 }
83 if (tmpDT < deltaTMin) {
84 deltaTMin = tmpDT;
85 }
86 }
87
88 prev=now;
89 count++;
90
91 valid=true;
92 last=v;
93 getEnvelope(lastStamp);
94 //check that timestamp are available
95 if (!lastStamp.isValid()) {
96 lastStamp.update(now);
97 }
98 mutex.unlock();
99}
100
101void StateExtendedInputPort::setTimeout(const double& timeout) {
102 this->timeout = timeout;
103}
104
105bool StateExtendedInputPort::getLastSingle(int j, int field, double *data, Stamp &stamp, double &localArrivalTime)
106{
107 mutex.lock();
108 bool ret = valid;
109 if (ret)
110 {
111 switch(field)
112 {
113 case VOCAB_ENCODER:
114 *data = last.jointPosition[j];
116 break;
117
120 *data = last.jointVelocity[j];
121 break;
122
125 *data = last.jointAcceleration[j];
126 break;
127
130 *data = last.motorPosition[j];
131 break;
132
135 *data = last.motorVelocity[j];
136 break;
137
140 *data = last.motorAcceleration[j];
141 break;
142
143 case VOCAB_TRQ:
144 ret = last.torque_isValid;
145 *data = last.torque[j];
146 break;
147
150 *data = double(last.pwmDutycycle[j]);
151 break;
152
154 ret = last.current_isValid;
155 *data = last.current[j];
156 break;
157
160 *data = double(last.temperature[j]);
161 break;
162
163
164 default:
165 yCError(REMOTECONTROLBOARD) << "RemoteControlBoard internal error while reading data. Cannot get 'single' data of type " << yarp::os::Vocab32::decode(field);
166 break;
167 }
168
170 stamp = lastStamp;
171 double current_time = Time::now();
172 if (ret && ((current_time - localArrivalTime) > timeout)) {
173 ret = false;
174 }
175 }
176 mutex.unlock();
177
178 return ret;
179}
180
181bool StateExtendedInputPort::getLastSingle(int j, int field, int *data, Stamp &stamp, double &localArrivalTime)
182{
183 mutex.lock();
184 bool ret = valid;
185 if (ret)
186 {
187 switch(field)
188 {
191 *data = last.controlMode[j];
192 break;
193
196 *data = last.interactionMode[j];
197 break;
198
199 default:
200 yCError(REMOTECONTROLBOARD) << "RemoteControlBoard internal error while reading data. Cannot get 'single' data of type " << yarp::os::Vocab32::decode(field);
201 break;
202 }
204 stamp = lastStamp;
205 double curr_time = Time::now();
206 if (ret && (( curr_time - localArrivalTime) > timeout)) {
207 ret = false;
208 }
209 }
210 mutex.unlock();
211 return ret;
212}
213
215{
216 mutex.lock();
217 bool ret = valid;
218 if (ret)
219 {
220 switch(field)
221 {
222 case VOCAB_ENCODERS:
225 break;
226
230 break;
231
235 break;
236
240 break;
241
245 break;
246
250 break;
251
252 case VOCAB_TRQS:
253 ret = last.torque_isValid;
254 memcpy(data, last.torque.data(), last.torque.size() * last.torque.getElementSize() );
255 break;
256
259
260 // In the interface the pwmDutycycle is a double, but in the jointData it is a float so we need to copy it manually
261 for (size_t i = 0; i < last.pwmDutycycle.size(); i++) {
262 data[i] = last.pwmDutycycle[i];
263 }
264 break;
265
267 ret = last.current_isValid;
268 memcpy(data, last.current.data(), last.current.size() * last.current.getElementSize());
269 break;
270
273
274 // In the interface the temperature is a double, but in the jointData it is a float so we need to copy it manually
275 for (size_t i = 0; i < last.temperature.size(); i++) {
276 data[i] = last.temperature[i];
277 }
278 break;
279
280 default:
281 yCError(REMOTECONTROLBOARD) << "RemoteControlBoard internal error while reading data. Cannot get 'vector' data of type " << yarp::os::Vocab32::decode(field);
282 break;
283 }
284
286 stamp = lastStamp;
287 if (ret && ((Time::now() - localArrivalTime) > timeout)) {
288 ret = false;
289 }
290 }
291 mutex.unlock();
292
293 return ret;
294}
295
297{
298 mutex.lock();
299 bool ret = valid;
300 if (ret)
301 {
302 switch(field)
303 {
306 memcpy(data, last.controlMode.data(), last.controlMode.size() * last.controlMode.getElementSize());
307 break;
308
312 break;
313
314 default:
315 yCError(REMOTECONTROLBOARD) << "RemoteControlBoard internal error while reading data. Cannot get 'vector' data of type " << yarp::os::Vocab32::decode(field);
316 break;
317 }
319 stamp = lastStamp;
320 if (ret && ((Time::now() - localArrivalTime) > timeout)) {
321 ret = false;
322 }
323 }
324 mutex.unlock();
325 return ret;
326}
327
329{
330 mutex.lock();
331 int ret=count;
332 mutex.unlock();
333 return ret;
334}
335
336// time is in ms
337void StateExtendedInputPort::getEstFrequency(int &ite, double &av, double &min, double &max)
338{
339 mutex.lock();
340 ite=count;
341 min=deltaTMin*1000;
342 max=deltaTMax*1000;
343 if (count<1)
344 {
345 av=0;
346 }
347 else
348 {
349 av=deltaT/count;
350 }
351 av=av*1000;
352 mutex.unlock();
353}
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_TEMPERATURES
Definition IMotor.h:162
constexpr yarp::conf::vocab32_t VOCAB_TEMPERATURE
Definition IMotor.h:160
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< float > pwmDutycycle
Definition jointData.h:41
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 > torque
Definition jointData.h:39
yarp::sig::VectorOf< float > temperature
Definition jointData.h:49
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:211
size_t size() const
Definition Vector.h:331
T * data()
Return a pointer to the first element of the vector.
Definition Vector.h:196
size_t getElementSize() const override
Definition Vector.h:169
std::string current_time()
Definition utils.h:24
#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 Vocab32.cpp:33
An interface to the operating system, including Port based communication.