Visual Servoing Platform
version 3.7.0
Toggle main menu visibility
Loading...
Searching...
No Matches
vpForceTorqueAtiNetFTSensor.cpp
1
/*
2
* ViSP, open source Visual Servoing Platform software.
3
* Copyright (C) 2005 - 2025 by Inria. All rights reserved.
4
*
5
* This software is free software; you can redistribute it and/or modify
6
* it under the terms of the GNU General Public License as published by
7
* the Free Software Foundation; either version 2 of the License, or
8
* (at your option) any later version.
9
* See the file LICENSE.txt at the root directory of this source
10
* distribution for additional information about the GNU GPL.
11
*
12
* For using ViSP with software that can not be combined with the GNU
13
* GPL, please contact Inria about acquiring a ViSP Professional
14
* Edition License.
15
*
16
* See https://visp.inria.fr for more information.
17
*
18
* This software was developed at:
19
* Inria Rennes - Bretagne Atlantique
20
* Campus Universitaire de Beaulieu
21
* 35042 Rennes Cedex
22
* France
23
*
24
* If you have questions regarding the use of this file, please contact
25
* Inria at visp@inria.fr
26
*
27
* This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
28
* WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
29
*
30
* Description:
31
* ATI Force torque interface.
32
*/
33
34
#include <stdint.h>
35
36
#include <visp3/core/vpConfig.h>
37
#include <visp3/core/vpException.h>
38
#include <visp3/core/vpTime.h>
39
#include <visp3/sensor/vpForceTorqueAtiNetFTSensor.h>
40
41
// Make vpForceTorqueAtiNetFTSensor available only if inet_ntop() used to
42
// communicate by UDP with the sensor through vpUDPClient is available; inet_ntop()
43
// is not supported on win XP
44
#ifdef VISP_HAVE_FUNC_INET_NTOP
45
46
#ifndef DOXYGEN_SHOULD_SKIP_THIS
47
typedef
struct
response_struct
48
{
49
uint32_t rdt_sequence;
50
uint32_t ft_sequence;
51
uint32_t status;
52
int32_t FTData[6];
53
} RESPONSE;
54
#endif
// #ifndef DOXYGEN_SHOULD_SKIP_THIS
55
56
BEGIN_VISP_NAMESPACE
63
vpForceTorqueAtiNetFTSensor::vpForceTorqueAtiNetFTSensor
()
64
:
vpUDPClient
(),
m_counts_per_force
(1000000),
m_counts_per_torque
(1000000000),
m_scaling_factor
(1),
m_ft_bias
(6, 0),
65
m_data_count
(0),
m_data_count_prev
(0),
m_ft
(6, 0),
m_is_streaming_started
(false)
66
{ }
67
73
vpForceTorqueAtiNetFTSensor::vpForceTorqueAtiNetFTSensor
(
const
std::string &hostname,
int
port)
74
:
vpUDPClient
(hostname, port),
m_counts_per_force
(1000000),
m_counts_per_torque
(1000000000),
m_scaling_factor
(1),
75
m_ft_bias
(6, 0),
m_data_count
(0),
m_data_count_prev
(0),
m_ft
(6, 0),
m_is_streaming_started
(false)
76
{ }
77
82
bool
vpForceTorqueAtiNetFTSensor::startStreaming
()
83
{
84
if
(!
m_is_init
) {
85
throw
(
vpException
(
vpException::notInitialized
,
"Cannot start streaming: UDP client is not initialized"
));
86
}
87
88
if
(
m_is_streaming_started
) {
89
throw
(
vpException
(
vpException::notInitialized
,
"Streaming is already started"
));
90
}
91
92
// Since UDP packet could be lost, retry startup 10 times before giving up
93
for
(
unsigned
int
i = 0; i < 10; ++i) {
94
int
len = 8;
95
unsigned
char
request[8];
// The request data sent to the Net F/T
96
uint16_t val1 = htons(0x1234);
// Standard header
97
uint16_t val2 = htons(0x0002);
// Start high-speed streaming (see table 10.1 in Net F/T user manual)
98
uint32_t val3 = htons(0);
// Infinite sample (see section 10.1 in Net F/T user manual
99
memcpy(&request[0], &val1,
sizeof
(val1));
100
memcpy(&request[2], &val2,
sizeof
(val2));
101
memcpy(&request[4], &val3,
sizeof
(val3));
102
103
// Send start stream
104
if
(
send
(request, len) != len) {
105
throw
(
vpException
(
vpException::notInitialized
,
"UDP client is not initialized"
));
106
}
107
std::cout <<
"wait: "
<< i << std::endl;
108
109
m_is_streaming_started
=
true
;
110
if
(
waitForNewData
()) {
111
return
true
;
112
}
113
}
114
m_is_streaming_started
=
false
;
115
return
false
;
116
}
117
121
void
vpForceTorqueAtiNetFTSensor::stopStreaming
()
122
{
123
if
(!
m_is_init
) {
124
throw
(
vpException
(
vpException::notInitialized
,
"Cannot stop streaming: UDP client is not initialized"
));
125
}
126
127
if
(!
m_is_streaming_started
) {
128
throw
(
vpException
(
vpException::notInitialized
,
"Cannot stop streaming: streaming was not started"
));
129
}
130
131
int
len = 8;
132
unsigned
char
request[8];
// The request data sent to the Net F/T
133
uint16_t val1 = htons(0x1234);
// Standard header
134
uint16_t val2 = htons(0x0002);
// Start high-speed streaming (see table 10.1 in Net F/T user manual)
135
uint32_t val3 = htons(0);
// Infinite sample (see section 10.1 in Net F/T user manual
136
memcpy(&request[0], &val1,
sizeof
(val1));
137
memcpy(&request[2], &val2,
sizeof
(val2));
138
memcpy(&request[4], &val3,
sizeof
(val3));
139
140
// Send start stream
141
if
(
send
(request, len) != len) {
142
throw
(
vpException
(
vpException::notInitialized
,
"Cannot stop streaming"
));
143
}
144
145
m_is_streaming_started
=
false
;
146
}
147
153
vpForceTorqueAtiNetFTSensor::~vpForceTorqueAtiNetFTSensor
()
154
{
155
if
(
m_is_streaming_started
) {
156
stopStreaming
();
157
}
158
}
159
167
void
vpForceTorqueAtiNetFTSensor::bias
(
unsigned
int
n_counts)
168
{
169
if
(!
m_is_init
) {
170
throw
(
vpException
(
vpException::notInitialized
,
"Cannot bias: UDP client is not initialized"
));
171
}
172
173
if
(!
m_is_streaming_started
) {
174
throw
(
vpException
(
vpException::notInitialized
,
"Cannot bias: streaming was not started"
));
175
}
176
177
vpColVector
ft_bias_tmp(6, 0);
178
m_ft_bias
= 0;
179
180
if
(n_counts == 0) {
181
m_ft_bias
=
getForceTorque
();
182
}
183
else
{
184
for
(
unsigned
int
i = 0; i < n_counts; i++) {
185
ft_bias_tmp +=
getForceTorque
();
186
waitForNewData
();
187
}
188
m_ft_bias
= ft_bias_tmp / n_counts;
189
}
190
}
191
197
void
vpForceTorqueAtiNetFTSensor::unbias
() {
m_ft_bias
= 0; }
198
209
vpColVector
vpForceTorqueAtiNetFTSensor::getForceTorque
()
const
210
{
211
if
(!
m_is_init
) {
212
throw
(
vpException
(
vpException::notInitialized
,
"Cannot get F/T: UDP client is not initialized"
));
213
}
214
215
if
(!
m_is_streaming_started
) {
216
throw
(
vpException
(
vpException::notInitialized
,
"Cannot get F/T: streaming was not started"
));
217
}
218
219
if
(
m_data_count_prev
==
m_data_count
) {
220
throw
(
vpException
(
vpException::notInitialized
,
"Cannot get F/T: no new data available"
));
221
}
222
223
return
m_ft
;
224
}
225
231
bool
vpForceTorqueAtiNetFTSensor::waitForNewData
(
unsigned
int
timeout)
232
{
233
if
(!
m_is_init
) {
234
throw
(
vpException
(
vpException::notInitialized
,
"Cannot wait for new data: UDP client is not initialized"
));
235
}
236
237
if
(!
m_is_streaming_started
) {
238
throw
(
vpException
(
vpException::notInitialized
,
"Cannot wait for new data: streaming was not started"
));
239
}
240
241
double
t =
vpTime::measureTimeMs
();
242
m_data_count_prev
=
m_data_count
;
243
while
(
vpTime::measureTimeMs
() - t <
static_cast<
double
>
(timeout)) {
244
unsigned
char
response[36];
245
RESPONSE resp;
246
if
(
receive
((
void
*)response, 36)) {
247
uint32_t resp1, resp2, resp3;
248
memcpy(&resp1, &response[0],
sizeof
(uint32_t));
249
memcpy(&resp2, &response[4],
sizeof
(uint32_t));
250
memcpy(&resp3, &response[8],
sizeof
(uint32_t));
251
resp.rdt_sequence = ntohl(resp1);
252
resp.ft_sequence = ntohl(resp2);
253
resp.status = ntohl(resp3);
254
255
for
(
int
i = 0; i < 6; i++) {
256
int32_t resp4;
257
memcpy(&resp4, &response[12 + i * 4],
sizeof
(int32_t));
258
resp.FTData[i] = ntohl(resp4);
259
}
260
// Output the response data.
261
if
(resp.status) {
262
throw
(
vpException
(
vpException::notInitialized
,
"Cannot wait for new data: status 0x%08x is not 0x00000000"
,
263
resp.status));
264
}
265
double
force_factor =
static_cast<
double
>
(
m_scaling_factor
) /
static_cast<
double
>
(
m_counts_per_force
);
266
double
torque_factor =
static_cast<
double
>
(
m_scaling_factor
) /
static_cast<
double
>
(
m_counts_per_torque
);
267
for
(
int
i = 0; i < 3; i++) {
268
m_ft
[i] = resp.FTData[i] * force_factor;
269
}
270
for
(
int
i = 3; i < 6; i++) {
271
m_ft
[i] = resp.FTData[i] * torque_factor;
272
}
273
// Consider bias
274
m_ft
-=
m_ft_bias
;
275
276
m_data_count
++;
277
}
278
279
if
(
m_data_count
!=
m_data_count_prev
) {
280
return
true
;
281
}
282
vpTime::sleepMs
(1);
283
}
284
285
return
false
;
286
}
287
END_VISP_NAMESPACE
288
#endif
vpColVector
Implementation of column vector and the associated operations.
Definition
vpColVector.h:191
vpException
error that can be emitted by ViSP classes.
Definition
vpException.h:60
vpException::notInitialized
@ notInitialized
Used to indicate that a parameter is not initialized.
Definition
vpException.h:74
vpForceTorqueAtiNetFTSensor::m_is_streaming_started
bool m_is_streaming_started
Definition
vpForceTorqueAtiNetFTSensor.h:161
vpForceTorqueAtiNetFTSensor::getForceTorque
vpColVector getForceTorque() const
Definition
vpForceTorqueAtiNetFTSensor.cpp:209
vpForceTorqueAtiNetFTSensor::m_ft_bias
vpColVector m_ft_bias
Definition
vpForceTorqueAtiNetFTSensor.h:157
vpForceTorqueAtiNetFTSensor::m_counts_per_force
unsigned long m_counts_per_force
Definition
vpForceTorqueAtiNetFTSensor.h:154
vpForceTorqueAtiNetFTSensor::stopStreaming
void stopStreaming()
Definition
vpForceTorqueAtiNetFTSensor.cpp:121
vpForceTorqueAtiNetFTSensor::startStreaming
bool startStreaming()
Definition
vpForceTorqueAtiNetFTSensor.cpp:82
vpForceTorqueAtiNetFTSensor::m_scaling_factor
unsigned long m_scaling_factor
Definition
vpForceTorqueAtiNetFTSensor.h:156
vpForceTorqueAtiNetFTSensor::unbias
void unbias()
Definition
vpForceTorqueAtiNetFTSensor.cpp:197
vpForceTorqueAtiNetFTSensor::bias
void bias(unsigned int n_counts=50)
Definition
vpForceTorqueAtiNetFTSensor.cpp:167
vpForceTorqueAtiNetFTSensor::m_data_count_prev
unsigned long m_data_count_prev
Definition
vpForceTorqueAtiNetFTSensor.h:159
vpForceTorqueAtiNetFTSensor::m_data_count
unsigned long m_data_count
Definition
vpForceTorqueAtiNetFTSensor.h:158
vpForceTorqueAtiNetFTSensor::~vpForceTorqueAtiNetFTSensor
virtual ~vpForceTorqueAtiNetFTSensor() VP_OVERRIDE
Definition
vpForceTorqueAtiNetFTSensor.cpp:153
vpForceTorqueAtiNetFTSensor::m_counts_per_torque
unsigned long m_counts_per_torque
Definition
vpForceTorqueAtiNetFTSensor.h:155
vpForceTorqueAtiNetFTSensor::vpForceTorqueAtiNetFTSensor
vpForceTorqueAtiNetFTSensor()
Definition
vpForceTorqueAtiNetFTSensor.cpp:63
vpForceTorqueAtiNetFTSensor::waitForNewData
bool waitForNewData(unsigned int timeout=50)
Definition
vpForceTorqueAtiNetFTSensor.cpp:231
vpForceTorqueAtiNetFTSensor::m_ft
vpColVector m_ft
Definition
vpForceTorqueAtiNetFTSensor.h:160
vpUDPClient::receive
int receive(std::string &msg, int timeoutMs=0)
Definition
vpUDPClient.cpp:222
vpUDPClient::m_is_init
bool m_is_init
Definition
vpUDPClient.h:206
vpUDPClient::vpUDPClient
vpUDPClient()
Definition
vpUDPClient.cpp:77
vpUDPClient::send
int send(const std::string &msg)
Definition
vpUDPClient.cpp:328
BEGIN_VISP_NAMESPACE
Definition
vpMbtDistanceCircle.cpp:55
vpTime::measureTimeMs
VISP_EXPORT double measureTimeMs()
vpTime::sleepMs
VISP_EXPORT void sleepMs(double t)
modules
sensor
src
force-torque
vpForceTorqueAtiNetFTSensor.cpp
Generated by
1.17.0