-
Notifications
You must be signed in to change notification settings - Fork 194
/
stateExtendedReader.cpp
331 lines (285 loc) · 9.72 KB
/
stateExtendedReader.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
/*
* SPDX-FileCopyrightText: 2006-2021 Istituto Italiano di Tecnologia (IIT)
* SPDX-License-Identifier: BSD-3-Clause
*/
#include "stateExtendedReader.h"
#include "RemoteControlBoardLogComponent.h"
#include <cstring>
#include <yarp/os/PortablePair.h>
#include <yarp/os/BufferedPort.h>
#include <yarp/os/Time.h>
#include <yarp/os/Network.h>
#include <yarp/os/Thread.h>
#include <yarp/os/Vocab.h>
#include <yarp/os/Stamp.h>
#include <yarp/os/LogStream.h>
#include <yarp/sig/Vector.h>
#include <yarp/dev/ControlBoardInterfaces.h>
#include <yarp/dev/PolyDriver.h>
#include <yarp/dev/ControlBoardInterfacesImpl.h>
#include <yarp/dev/ControlBoardHelpers.h>
#include <yarp/dev/IPreciselyTimed.h>
using namespace yarp::os;
using namespace yarp::dev;
using namespace yarp::sig;
void StateExtendedInputPort::resetStat()
{
mutex.lock();
count=0;
deltaT=0;
deltaTMax=0;
deltaTMin=1e22;
now=Time::now();
prev=now;
mutex.unlock();
}
StateExtendedInputPort::StateExtendedInputPort() : deltaT{0},
deltaTMax{0},
deltaTMin{1e22},
now{Time::now()},
prev{now},
timeout{0.5},
valid{false},
count{0}
{
}
void StateExtendedInputPort::init(int numberOfJoints)
{
last.jointPosition.resize(numberOfJoints);
last.jointVelocity.resize(numberOfJoints);
last.jointAcceleration.resize(numberOfJoints);
last.motorPosition.resize(numberOfJoints);
last.motorVelocity.resize(numberOfJoints);
last.motorAcceleration.resize(numberOfJoints);
last.torque.resize(numberOfJoints);
last.pwmDutycycle.resize(numberOfJoints);
last.current.resize(numberOfJoints);
last.controlMode.resize(numberOfJoints);
last.interactionMode.resize(numberOfJoints);
}
void StateExtendedInputPort::onRead(yarp::dev::impl::jointData &v)
{
now=Time::now();
mutex.lock();
if (count>0)
{
double tmpDT=now-prev;
deltaT+=tmpDT;
if (tmpDT > deltaTMax) {
deltaTMax = tmpDT;
}
if (tmpDT < deltaTMin) {
deltaTMin = tmpDT;
}
}
prev=now;
count++;
valid=true;
last=v;
getEnvelope(lastStamp);
//check that timestamp are available
if (!lastStamp.isValid()) {
lastStamp.update(now);
}
mutex.unlock();
}
void StateExtendedInputPort::setTimeout(const double& timeout) {
this->timeout = timeout;
}
bool StateExtendedInputPort::getLastSingle(int j, int field, double *data, Stamp &stamp, double &localArrivalTime)
{
mutex.lock();
bool ret = valid;
if (ret)
{
switch(field)
{
case VOCAB_ENCODER:
*data = last.jointPosition[j];
ret = last.jointPosition_isValid;
break;
case VOCAB_ENCODER_SPEED:
ret = last.jointVelocity_isValid;
*data = last.jointVelocity[j];
break;
case VOCAB_ENCODER_ACCELERATION:
ret = last.jointAcceleration_isValid;
*data = last.jointAcceleration[j];
break;
case VOCAB_MOTOR_ENCODER:
ret = last.motorPosition_isValid;
*data = last.motorPosition[j];
break;
case VOCAB_MOTOR_ENCODER_SPEED:
ret = last.motorVelocity_isValid;
*data = last.motorVelocity[j];
break;
case VOCAB_MOTOR_ENCODER_ACCELERATION:
ret = last.motorAcceleration_isValid;
*data = last.motorAcceleration[j];
break;
case VOCAB_TRQ:
ret = last.torque_isValid;
*data = last.torque[j];
break;
case VOCAB_PWMCONTROL_PWM_OUTPUT:
ret = last.pwmDutycycle_isValid;
*data = last.pwmDutycycle[j];
break;
case VOCAB_AMP_CURRENT:
ret = last.current_isValid;
*data = last.current[j];
break;
default:
yCError(REMOTECONTROLBOARD) << "RemoteControlBoard internal error while reading data. Cannot get 'single' data of type " << yarp::os::Vocab32::decode(field);
break;
}
localArrivalTime=now;
stamp = lastStamp;
if (ret && ((Time::now() - localArrivalTime) > timeout)) {
ret = false;
}
}
mutex.unlock();
return ret;
}
bool StateExtendedInputPort::getLastSingle(int j, int field, int *data, Stamp &stamp, double &localArrivalTime)
{
mutex.lock();
bool ret = valid;
if (ret)
{
switch(field)
{
case VOCAB_CM_CONTROL_MODE:
ret = last.controlMode_isValid;
*data = last.controlMode[j];
break;
case VOCAB_INTERACTION_MODE:
ret = last.interactionMode_isValid;
*data = last.interactionMode[j];
break;
default:
yCError(REMOTECONTROLBOARD) << "RemoteControlBoard internal error while reading data. Cannot get 'single' data of type " << yarp::os::Vocab32::decode(field);
break;
}
localArrivalTime=now;
stamp = lastStamp;
if (ret && ((Time::now() - localArrivalTime) > timeout)) {
ret = false;
}
}
mutex.unlock();
return ret;
}
bool StateExtendedInputPort::getLastVector(int field, double* data, Stamp& stamp, double& localArrivalTime)
{
mutex.lock();
bool ret = valid;
if (ret)
{
switch(field)
{
case VOCAB_ENCODERS:
ret = last.jointPosition_isValid;
memcpy(data, last.jointPosition.data(), last.jointPosition.size() * last.jointPosition.getElementSize() );
break;
case VOCAB_ENCODER_SPEEDS:
ret = last.jointVelocity_isValid;
memcpy(data, last.jointVelocity.data(), last.jointVelocity.size() * last.jointVelocity.getElementSize() );
break;
case VOCAB_ENCODER_ACCELERATIONS:
ret = last.jointAcceleration_isValid;
memcpy(data, last.jointAcceleration.data(), last.jointAcceleration.size() * last.jointAcceleration.getElementSize() );
break;
case VOCAB_MOTOR_ENCODERS:
ret = last.motorPosition_isValid;
memcpy(data, last.motorPosition.data(), last.motorPosition.size() * last.motorPosition.getElementSize() );
break;
case VOCAB_MOTOR_ENCODER_SPEEDS:
ret = last.motorVelocity_isValid;
memcpy(data, last.motorVelocity.data(), last.motorVelocity.size() * last.motorVelocity.getElementSize() );
break;
case VOCAB_MOTOR_ENCODER_ACCELERATIONS:
ret = last.motorAcceleration_isValid;
memcpy(data, last.motorAcceleration.data(), last.motorAcceleration.size() * last.motorAcceleration.getElementSize() );
break;
case VOCAB_TRQS:
ret = last.torque_isValid;
memcpy(data, last.torque.data(), last.torque.size() * last.torque.getElementSize() );
break;
case VOCAB_PWMCONTROL_PWM_OUTPUTS:
ret = last.pwmDutycycle_isValid;
memcpy(data, last.pwmDutycycle.data(), last.pwmDutycycle.size() * last.pwmDutycycle.getElementSize());
break;
case VOCAB_AMP_CURRENTS:
ret = last.current_isValid;
memcpy(data, last.current.data(), last.current.size() * last.current.getElementSize());
break;
default:
yCError(REMOTECONTROLBOARD) << "RemoteControlBoard internal error while reading data. Cannot get 'vector' data of type " << yarp::os::Vocab32::decode(field);
break;
}
localArrivalTime=now;
stamp = lastStamp;
if (ret && ((Time::now() - localArrivalTime) > timeout)) {
ret = false;
}
}
mutex.unlock();
return ret;
}
bool StateExtendedInputPort::getLastVector(int field, int* data, Stamp& stamp, double& localArrivalTime)
{
mutex.lock();
bool ret = valid;
if (ret)
{
switch(field)
{
case VOCAB_CM_CONTROL_MODES:
ret = last.controlMode_isValid;
memcpy(data, last.controlMode.data(), last.controlMode.size() * last.controlMode.getElementSize());
break;
case VOCAB_INTERACTION_MODES:
ret = last.interactionMode_isValid;
memcpy(data, last.interactionMode.data(), last.interactionMode.size() * last.interactionMode.getElementSize());
break;
default:
yCError(REMOTECONTROLBOARD) << "RemoteControlBoard internal error while reading data. Cannot get 'vector' data of type " << yarp::os::Vocab32::decode(field);
break;
}
localArrivalTime=now;
stamp = lastStamp;
if (ret && ((Time::now() - localArrivalTime) > timeout)) {
ret = false;
}
}
mutex.unlock();
return ret;
}
int StateExtendedInputPort::getIterations()
{
mutex.lock();
int ret=count;
mutex.unlock();
return ret;
}
// time is in ms
void StateExtendedInputPort::getEstFrequency(int &ite, double &av, double &min, double &max)
{
mutex.lock();
ite=count;
min=deltaTMin*1000;
max=deltaTMax*1000;
if (count<1)
{
av=0;
}
else
{
av=deltaT/count;
}
av=av*1000;
mutex.unlock();
}