YARP
Yet Another Robot Platform
stateExtendedReader.cpp
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2006-2020 Istituto Italiano di Tecnologia (IIT)
3  * All rights reserved.
4  *
5  * This software may be modified and distributed under the terms of the
6  * BSD-3-Clause license. See the accompanying LICENSE file for details.
7  */
8 
9 #include "stateExtendedReader.h"
11 #include <cstring>
12 
13 #include <yarp/os/PortablePair.h>
14 #include <yarp/os/BufferedPort.h>
15 #include <yarp/os/Time.h>
16 #include <yarp/os/Network.h>
17 #include <yarp/os/Thread.h>
18 #include <yarp/os/Vocab.h>
19 #include <yarp/os/Stamp.h>
20 #include <yarp/os/LogStream.h>
21 
22 #include <yarp/sig/Vector.h>
23 
25 #include <yarp/dev/PolyDriver.h>
29 
30 
31 using namespace yarp::os;
32 using namespace yarp::dev;
33 using namespace yarp::sig;
34 
36 {
37  mutex.lock();
38  count=0;
39  deltaT=0;
40  deltaTMax=0;
41  deltaTMin=1e22;
42  now=Time::now();
43  prev=now;
44  mutex.unlock();
45 }
46 
48  deltaTMax{0},
49  deltaTMin{1e22},
50  now{Time::now()},
51  prev{now},
52  timeout{0.5},
53  valid{false},
54  count{0}
55 {
56 }
57 
58 void StateExtendedInputPort::init(int numberOfJoints)
59 {
60  last.jointPosition.resize(numberOfJoints);
61  last.jointVelocity.resize(numberOfJoints);
62  last.jointAcceleration.resize(numberOfJoints);
63  last.motorPosition.resize(numberOfJoints);
64  last.motorVelocity.resize(numberOfJoints);
65  last.motorAcceleration.resize(numberOfJoints);
66  last.torque.resize(numberOfJoints);
67  last.pwmDutycycle.resize(numberOfJoints);
68  last.current.resize(numberOfJoints);
69  last.controlMode.resize(numberOfJoints);
70  last.interactionMode.resize(numberOfJoints);
71 }
72 
74 {
75  now=Time::now();
76  mutex.lock();
77 
78  if (count>0)
79  {
80  double tmpDT=now-prev;
81  deltaT+=tmpDT;
82  if (tmpDT>deltaTMax)
83  deltaTMax=tmpDT;
84  if (tmpDT<deltaTMin)
85  deltaTMin=tmpDT;
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  mutex.unlock();
98 }
99 
100 void StateExtendedInputPort::setTimeout(const double& timeout) {
101  this->timeout = timeout;
102 }
103 
104 bool 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];
114  ret = last.jointPosition_isValid;
115  break;
116 
117  case VOCAB_ENCODER_SPEED:
118  ret = last.jointVelocity_isValid;
119  *data = last.jointVelocity[j];
120  break;
121 
124  *data = last.jointAcceleration[j];
125  break;
126 
127  case VOCAB_MOTOR_ENCODER:
128  ret = last.motorPosition_isValid;
129  *data = last.motorPosition[j];
130  break;
131 
133  ret = last.motorVelocity_isValid;
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 
148  ret = last.pwmDutycycle_isValid;
149  *data = last.pwmDutycycle[j];
150  break;
151 
152  case VOCAB_AMP_CURRENT:
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::Vocab::decode(field);
159  break;
160  }
161 
162  localArrivalTime=now;
163  stamp = lastStamp;
164  if (ret && ( (Time::now()-localArrivalTime) > timeout) )
165  ret = false;
166  }
167  mutex.unlock();
168 
169  return ret;
170 }
171 
172 bool StateExtendedInputPort::getLastSingle(int j, int field, int *data, Stamp &stamp, double &localArrivalTime)
173 {
174  mutex.lock();
175  bool ret = valid;
176  if (ret)
177  {
178  switch(field)
179  {
181  ret = last.controlMode_isValid;
182  *data = last.controlMode[j];
183  break;
184 
187  *data = last.interactionMode[j];
188  break;
189 
190  default:
191  yCError(REMOTECONTROLBOARD) << "RemoteControlBoard internal error while reading data. Cannot get 'single' data of type " << yarp::os::Vocab::decode(field);
192  break;
193  }
194  localArrivalTime=now;
195  stamp = lastStamp;
196  if (ret && ( (Time::now()-localArrivalTime) > timeout) )
197  ret = false;
198 
199  }
200  mutex.unlock();
201  return ret;
202 }
203 
204 bool StateExtendedInputPort::getLastVector(int field, double* data, Stamp& stamp, double& localArrivalTime)
205 {
206  mutex.lock();
207  bool ret = valid;
208  if (ret)
209  {
210  switch(field)
211  {
212  case VOCAB_ENCODERS:
213  ret = last.jointPosition_isValid;
214  memcpy(data, last.jointPosition.data(), last.jointPosition.size() * last.jointPosition.getElementSize() );
215  break;
216 
218  ret = last.jointVelocity_isValid;
219  memcpy(data, last.jointVelocity.data(), last.jointVelocity.size() * last.jointVelocity.getElementSize() );
220  break;
221 
224  memcpy(data, last.jointAcceleration.data(), last.jointAcceleration.size() * last.jointAcceleration.getElementSize() );
225  break;
226 
228  ret = last.motorPosition_isValid;
229  memcpy(data, last.motorPosition.data(), last.motorPosition.size() * last.motorPosition.getElementSize() );
230  break;
231 
233  ret = last.motorVelocity_isValid;
234  memcpy(data, last.motorVelocity.data(), last.motorVelocity.size() * last.motorVelocity.getElementSize() );
235  break;
236 
239  memcpy(data, last.motorAcceleration.data(), last.motorAcceleration.size() * last.motorAcceleration.getElementSize() );
240  break;
241 
242  case VOCAB_TRQS:
243  ret = last.torque_isValid;
244  memcpy(data, last.torque.data(), last.torque.size() * last.torque.getElementSize() );
245  break;
246 
248  ret = last.pwmDutycycle_isValid;
249  memcpy(data, last.pwmDutycycle.data(), last.pwmDutycycle.size() * last.pwmDutycycle.getElementSize());
250  break;
251 
252  case VOCAB_AMP_CURRENTS:
253  ret = last.current_isValid;
254  memcpy(data, last.current.data(), last.current.size() * last.current.getElementSize());
255  break;
256 
257  default:
258  yCError(REMOTECONTROLBOARD) << "RemoteControlBoard internal error while reading data. Cannot get 'vector' data of type " << yarp::os::Vocab::decode(field);
259  break;
260  }
261 
262  localArrivalTime=now;
263  stamp = lastStamp;
264  if (ret && ( (Time::now()-localArrivalTime) > timeout) )
265  ret = false;
266  }
267  mutex.unlock();
268 
269  return ret;
270 }
271 
272 bool StateExtendedInputPort::getLastVector(int field, int* data, Stamp& stamp, double& localArrivalTime)
273 {
274  mutex.lock();
275  bool ret = valid;
276  if (ret)
277  {
278  switch(field)
279  {
281  ret = last.controlMode_isValid;
282  memcpy(data, last.controlMode.data(), last.controlMode.size() * last.controlMode.getElementSize());
283  break;
284 
287  memcpy(data, last.interactionMode.data(), last.interactionMode.size() * last.interactionMode.getElementSize());
288  break;
289 
290  default:
291  yCError(REMOTECONTROLBOARD) << "RemoteControlBoard internal error while reading data. Cannot get 'vector' data of type " << yarp::os::Vocab::decode(field);
292  break;
293  }
294  localArrivalTime=now;
295  stamp = lastStamp;
296  if (ret && ( (Time::now()-localArrivalTime) > timeout) )
297  ret = false;
298  }
299  mutex.unlock();
300  return ret;
301 }
302 
304 {
305  mutex.lock();
306  int ret=count;
307  mutex.unlock();
308  return ret;
309 }
310 
311 // time is in ms
312 void StateExtendedInputPort::getEstFrequency(int &ite, double &av, double &min, double &max)
313 {
314  mutex.lock();
315  ite=count;
316  min=deltaTMin*1000;
317  max=deltaTMax*1000;
318  if (count<1)
319  {
320  av=0;
321  }
322  else
323  {
324  av=deltaT/count;
325  }
326  av=av*1000;
327  mutex.unlock();
328 }
LogStream.h
yarp::dev::impl::jointData::current_isValid
bool current_isValid
Definition: jointData.h:49
yarp::dev::impl::jointData::controlMode
yarp::sig::VectorOf< int > controlMode
Definition: jointData.h:50
yarp::dev::impl::jointData::torque
yarp::sig::VectorOf< double > torque
Definition: jointData.h:44
VOCAB_MOTOR_ENCODERS
constexpr yarp::conf::vocab32_t VOCAB_MOTOR_ENCODERS
Definition: IMotorEncoders.h:280
Network.h
VOCAB_AMP_CURRENT
constexpr yarp::conf::vocab32_t VOCAB_AMP_CURRENT
Definition: IAmplifierControl.h:325
yarp::sig::VectorOf::resize
void resize(size_t size) override
Resize the vector.
Definition: Vector.h:254
StateExtendedInputPort::resetStat
void resetStat()
Definition: stateExtendedReader.cpp:35
stateExtendedReader.h
yarp::dev::impl::jointData::pwmDutycycle
yarp::sig::VectorOf< double > pwmDutycycle
Definition: jointData.h:46
VOCAB_INTERACTION_MODES
constexpr yarp::conf::vocab32_t VOCAB_INTERACTION_MODES
Definition: IInteractionMode.h:32
VOCAB_ENCODER_SPEEDS
constexpr yarp::conf::vocab32_t VOCAB_ENCODER_SPEEDS
Definition: IEncoders.h:216
Vector.h
contains the definition of a Vector type
yarp::sig
Signal processing.
Definition: Image.h:25
VOCAB_ENCODER_ACCELERATIONS
constexpr yarp::conf::vocab32_t VOCAB_ENCODER_ACCELERATIONS
Definition: IEncoders.h:218
VOCAB_MOTOR_ENCODER_SPEED
constexpr yarp::conf::vocab32_t VOCAB_MOTOR_ENCODER_SPEED
Definition: IMotorEncoders.h:285
VOCAB_INTERACTION_MODE
constexpr yarp::conf::vocab32_t VOCAB_INTERACTION_MODE
Definition: IInteractionMode.h:30
IPreciselyTimed.h
yarp::dev::impl::jointData::motorAcceleration_isValid
bool motorAcceleration_isValid
Definition: jointData.h:43
VOCAB_ENCODER
constexpr yarp::conf::vocab32_t VOCAB_ENCODER
Definition: IEncoders.h:211
yarp::dev::impl::jointData::torque_isValid
bool torque_isValid
Definition: jointData.h:45
VOCAB_MOTOR_ENCODER_ACCELERATIONS
constexpr yarp::conf::vocab32_t VOCAB_MOTOR_ENCODER_ACCELERATIONS
Definition: IMotorEncoders.h:288
VOCAB_AMP_CURRENTS
constexpr yarp::conf::vocab32_t VOCAB_AMP_CURRENTS
Definition: IAmplifierControl.h:326
ControlBoardHelpers.h
REMOTECONTROLBOARD
const yarp::os::LogComponent & REMOTECONTROLBOARD()
Definition: RemoteControlBoardLogComponent.cpp:11
yarp::dev::impl::jointData::motorAcceleration
yarp::sig::VectorOf< double > motorAcceleration
Definition: jointData.h:42
ret
bool ret
Definition: ImplementAxisInfo.cpp:72
yarp::dev::impl::jointData::motorVelocity_isValid
bool motorVelocity_isValid
Definition: jointData.h:41
ControlBoardInterfaces.h
define control board standard interfaces
VOCAB_TRQ
constexpr yarp::conf::vocab32_t VOCAB_TRQ
Definition: ITorqueControl.h:237
PortablePair.h
yarp::os::Time::now
double now()
Return the current time in seconds, relative to an arbitrary starting point.
Definition: Time.cpp:124
yarp::dev
An interface for the device drivers.
Definition: audioBufferSizeData.cpp:17
RemoteControlBoardLogComponent.h
yarp::dev::impl::jointData::jointVelocity
yarp::sig::VectorOf< double > jointVelocity
Definition: jointData.h:34
yarp::dev::impl::jointData
Definition: jointData.h:29
StateExtendedInputPort::getLastVector
bool getLastVector(int field, double *data, Stamp &stamp, double &localArrivalTime)
Definition: stateExtendedReader.cpp:204
VOCAB_TRQS
constexpr yarp::conf::vocab32_t VOCAB_TRQS
Definition: ITorqueControl.h:236
yarp::os::Vocab::decode
std::string decode(NetInt32 code)
Convert a vocabulary identifier into a string.
Definition: Vocab.cpp:36
VOCAB_ENCODER_SPEED
constexpr yarp::conf::vocab32_t VOCAB_ENCODER_SPEED
Definition: IEncoders.h:215
VOCAB_CM_CONTROL_MODE
constexpr yarp::conf::vocab32_t VOCAB_CM_CONTROL_MODE
Definition: IControlMode.h:120
yarp::dev::impl::jointData::motorVelocity
yarp::sig::VectorOf< double > motorVelocity
Definition: jointData.h:40
VOCAB_MOTOR_ENCODER_ACCELERATION
constexpr yarp::conf::vocab32_t VOCAB_MOTOR_ENCODER_ACCELERATION
Definition: IMotorEncoders.h:287
VOCAB_MOTOR_ENCODER
constexpr yarp::conf::vocab32_t VOCAB_MOTOR_ENCODER
Definition: IMotorEncoders.h:279
PolyDriver.h
yarp::dev::impl::jointData::jointPosition_isValid
bool jointPosition_isValid
Definition: jointData.h:33
Stamp.h
StateExtendedInputPort::StateExtendedInputPort
StateExtendedInputPort()
Definition: stateExtendedReader.cpp:47
yarp::dev::impl::jointData::pwmDutycycle_isValid
bool pwmDutycycle_isValid
Definition: jointData.h:47
Thread.h
VOCAB_MOTOR_ENCODER_SPEEDS
constexpr yarp::conf::vocab32_t VOCAB_MOTOR_ENCODER_SPEEDS
Definition: IMotorEncoders.h:286
StateExtendedInputPort::setTimeout
void setTimeout(const double &timeout)
setTimeout, set the timeout for retrieving data
Definition: stateExtendedReader.cpp:100
yarp::dev::impl::jointData::motorPosition_isValid
bool motorPosition_isValid
Definition: jointData.h:39
VOCAB_PWMCONTROL_PWM_OUTPUT
constexpr yarp::conf::vocab32_t VOCAB_PWMCONTROL_PWM_OUTPUT
Definition: IPWMControl.h:148
BufferedPort.h
yarp::dev::impl::jointData::motorPosition
yarp::sig::VectorOf< double > motorPosition
Definition: jointData.h:38
yarp::dev::impl::jointData::jointAcceleration
yarp::sig::VectorOf< double > jointAcceleration
Definition: jointData.h:36
VOCAB_ENCODER_ACCELERATION
constexpr yarp::conf::vocab32_t VOCAB_ENCODER_ACCELERATION
Definition: IEncoders.h:217
VOCAB_CM_CONTROL_MODES
constexpr yarp::conf::vocab32_t VOCAB_CM_CONTROL_MODES
Definition: IControlMode.h:122
yarp::dev::impl::jointData::jointPosition
yarp::sig::VectorOf< double > jointPosition
Definition: jointData.h:32
yarp::dev::impl::jointData::interactionMode
yarp::sig::VectorOf< int > interactionMode
Definition: jointData.h:52
StateExtendedInputPort::getEstFrequency
void getEstFrequency(int &ite, double &av, double &min, double &max)
Definition: stateExtendedReader.cpp:312
VOCAB_ENCODERS
constexpr yarp::conf::vocab32_t VOCAB_ENCODERS
Definition: IEncoders.h:212
yarp::dev::impl::jointData::jointAcceleration_isValid
bool jointAcceleration_isValid
Definition: jointData.h:37
yarp::sig::VectorOf::data
T * data()
Return a pointer to the first element of the vector.
Definition: Vector.h:239
StateExtendedInputPort::init
void init(int numberOfJoints)
Definition: stateExtendedReader.cpp:58
yarp::os::Stamp
An abstraction for a time stamp and/or sequence number.
Definition: Stamp.h:25
yCError
#define yCError(component,...)
Definition: LogComponent.h:157
yarp::dev::impl::jointData::current
yarp::sig::VectorOf< double > current
Definition: jointData.h:48
yarp::os::Stamp::update
void update()
Set the timestamp to the current time, and increment the sequence number (wrapping to 0 if the sequen...
Definition: Stamp.cpp:113
StateExtendedInputPort::getLastSingle
bool getLastSingle(int j, int field, double *data, Stamp &stamp, double &localArrivalTime)
Definition: stateExtendedReader.cpp:104
yarp::os
An interface to the operating system, including Port based communication.
Definition: AbstractCarrier.h:17
StateExtendedInputPort::getIterations
int getIterations()
Definition: stateExtendedReader.cpp:303
Vocab.h
ControlBoardInterfacesImpl.h
StateExtendedInputPort::onRead
void onRead(yarp::dev::impl::jointData &v) override
Definition: stateExtendedReader.cpp:73
yarp::dev::impl::jointData::controlMode_isValid
bool controlMode_isValid
Definition: jointData.h:51
yarp::os::Stamp::isValid
bool isValid() const
Check if this Stamp is valid.
Definition: Stamp.cpp:42
yarp::os::BufferedPort< yarp::dev::impl::jointData >::getEnvelope
bool getEnvelope(PortReader &envelope) override
Definition: BufferedPort-inl.h:239
Time.h
yarp::sig::VectorOf::size
size_t size() const
Definition: Vector.h:355
VOCAB_PWMCONTROL_PWM_OUTPUTS
constexpr yarp::conf::vocab32_t VOCAB_PWMCONTROL_PWM_OUTPUTS
Definition: IPWMControl.h:149
yarp::sig::VectorOf::getElementSize
size_t getElementSize() const override
Definition: Vector.h:197
yarp::dev::impl::jointData::jointVelocity_isValid
bool jointVelocity_isValid
Definition: jointData.h:35
yarp::dev::impl::jointData::interactionMode_isValid
bool interactionMode_isValid
Definition: jointData.h:53