Visual Servoing Platform version 3.6.0
Loading...
Searching...
No Matches
testForceTorqueAti.cpp
1/****************************************************************************
2 *
3 * ViSP, open source Visual Servoing Platform software.
4 * Copyright (C) 2005 - 2023 by Inria. All rights reserved.
5 *
6 * This software is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
10 * See the file LICENSE.txt at the root directory of this source
11 * distribution for additional information about the GNU GPL.
12 *
13 * For using ViSP with software that can not be combined with the GNU
14 * GPL, please contact Inria about acquiring a ViSP Professional
15 * Edition License.
16 *
17 * See https://visp.inria.fr for more information.
18 *
19 * This software was developed at:
20 * Inria Rennes - Bretagne Atlantique
21 * Campus Universitaire de Beaulieu
22 * 35042 Rennes Cedex
23 * France
24 *
25 * If you have questions regarding the use of this file, please contact
26 * Inria at visp@inria.fr
27 *
28 * This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
29 * WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
30 *
31 * Description:
32 * Test force/torque ATI sensor.
33 *
34*****************************************************************************/
35
45#include <iostream>
46
47#include <visp3/core/vpIoTools.h>
48#include <visp3/core/vpMutex.h>
49#include <visp3/core/vpThread.h>
50#include <visp3/core/vpTime.h>
51#include <visp3/gui/vpPlot.h>
52#include <visp3/sensor/vpForceTorqueAtiSensor.h>
53
54#if defined(VISP_HAVE_PTHREAD)
55
56typedef enum { BIAS_DONE, UNBIAS_DONE, TO_BIAS, TO_UNBIAS } BiasState;
57
58vpMutex s_mutex_data;
59
60#ifndef DOXYGEN_SHOULD_SKIP_THIS
61typedef struct {
62 vpColVector ft;
63 double timestamp;
64 BiasState bias_state;
65} t_shared_data;
66#endif
67
68t_shared_data s_shared_data;
69
70vpMutex s_mutex_state;
71bool s_state_stop = false;
72
73vpThread::Return scopeFunction(vpThread::Args args)
74{
75 (void)args; // Avoid warning: unused parameter args
76
77#ifdef VISP_HAVE_DISPLAY
78 vpPlot scope(2, 700, 700, 100, 200, "ATI F/T sensor data");
79 scope.initGraph(0, 3);
80 scope.initGraph(1, 3);
81 scope.setTitle(0, "Forces (N)");
82 scope.setTitle(1, "Torques (Nm)");
83 scope.setLegend(0, 0, "x");
84 scope.setLegend(0, 1, "y");
85 scope.setLegend(0, 2, "z");
86 scope.setLegend(1, 0, "x");
87 scope.setLegend(1, 1, "y");
88 scope.setLegend(1, 2, "z");
89#endif
90
91 t_shared_data shared_data;
92#ifdef VISP_HAVE_DISPLAY
93 bool click = false;
95#else
96 double start_time = vpTime::measureTimeMs();
97#endif
98 do {
99#ifdef VISP_HAVE_DISPLAY
100 { // Get new measures to plot
101 vpMutex::vpScopedLock lock(s_mutex_data);
102 shared_data.ft = s_shared_data.ft;
103 shared_data.timestamp = s_shared_data.timestamp;
104 shared_data.bias_state = s_shared_data.bias_state;
105 }
106
107 vpColVector force = shared_data.ft.extract(0, 3);
108 vpColVector torque = shared_data.ft.extract(3, 3);
109 scope.plot(0, shared_data.timestamp, force);
110 scope.plot(1, shared_data.timestamp, torque);
111
112 vpDisplay::displayText(scope.I, 15, 500, "Left click to quit", vpColor::red);
113 vpDisplay::displayText(scope.I, 30, 500, "Right click to bias/unbias", vpColor::red);
114 if (shared_data.bias_state == BIAS_DONE)
115 vpDisplay::displayText(scope.I, 45, 500, "Sensor is biased...", vpColor::blue);
116 else if (shared_data.bias_state == UNBIAS_DONE)
117 vpDisplay::displayText(scope.I, 45, 500, "Sensor is unbiased...", vpColor::blue);
118 else if (shared_data.bias_state == TO_BIAS)
119 vpDisplay::displayText(scope.I, 45, 500, "Sensor bias in progress...", vpColor::blue);
120 else if (shared_data.bias_state == TO_UNBIAS)
121 vpDisplay::displayText(scope.I, 45, 500, "Sensor unbias in progress...", vpColor::blue);
122 vpDisplay::flush(scope.I);
123 click = vpDisplay::getClick(scope.I, button, false);
124 if (click && button == vpMouseButton::button3) {
125 if (shared_data.bias_state == BIAS_DONE)
126 shared_data.bias_state = TO_UNBIAS;
127 else if (shared_data.bias_state == UNBIAS_DONE)
128 shared_data.bias_state = TO_BIAS;
129 { // Set new bias state
130 vpMutex::vpScopedLock lock(s_mutex_data);
131 s_shared_data.bias_state = shared_data.bias_state;
132 }
133 }
134
135#endif
136 }
137#ifdef VISP_HAVE_DISPLAY
138 while (!(click && button == vpMouseButton::button1)); // Stop recording by a user left click
139#else
140 while (vpTime::measureTimeMs() - start_time < 20000); // Stop recording after 20 seconds
141#endif
142
143 { // Update state to stop
144 vpMutex::vpScopedLock lock(s_mutex_state);
145 s_state_stop = true;
146 }
147
148 std::cout << "End of scope thread" << std::endl;
149 return 0;
150}
151
152int main(int argc, char **argv)
153{
154#if defined(VISP_HAVE_ATIDAQ) && defined(VISP_HAVE_COMEDI)
155
156#ifdef VISP_HAVE_VIPER850_DATA
157 (void)argc;
158 (void)argv;
159 std::string calibfile = std::string(VISP_VIPER850_DATA_PATH) + std::string("/ati/FT17824.cal");
160 if (!vpIoTools::checkFilename(calibfile)) {
161 std::cout << "ATI F/T calib file \"" << calibfile << "\" doesn't exist";
162 return EXIT_FAILURE;
163 }
164#else
165 if (argc != 2) {
166 std::cout << "Usage: " << argv[0] << " <ATI calibration file FT*.cal]>" << std::endl;
167 return EXIT_FAILURE;
168 }
169 std::string calibfile(argv[1]);
170#endif
171
173 ati.setCalibrationFile(calibfile);
174 ati.open();
175 std::cout << "ATI F/T sensor characteristics: \n" << ati << std::endl;
176
177 ati.bias();
178 std::cout << "Data recording in progress..." << std::endl;
179
180 // Start scope thread
181 vpThread thread_scope(scopeFunction);
182
183 std::string file("recorded-ft-sync.txt");
184 std::ofstream f(file.c_str());
185 bool state_stop;
186 t_shared_data shared_data;
187
188 double start_time = vpTime::measureTimeMs();
189
190 do {
191 double loop_time = vpTime::measureTimeMs();
192 vpColVector ft = ati.getForceTorque();
193 double timestamp = loop_time - start_time;
194
195 { // Update shared F/T measure used by the scope to plot curves
196 vpMutex::vpScopedLock lock(s_mutex_data);
197 shared_data.bias_state = s_shared_data.bias_state;
198 }
199 if (shared_data.bias_state == TO_BIAS) {
200 std::cout << "Bias sensor" << std::endl;
201 ati.bias();
202 std::cout << "Unbias sensor" << std::endl;
203 shared_data.bias_state = BIAS_DONE;
204 } else if (shared_data.bias_state == TO_UNBIAS) {
205 ati.unbias();
206 shared_data.bias_state = UNBIAS_DONE;
207 }
208
209 { // Update shared F/T measure used by the scope to plot curves
210 vpMutex::vpScopedLock lock(s_mutex_data);
211 s_shared_data.ft = ft;
212 s_shared_data.timestamp = timestamp;
213 s_shared_data.bias_state = shared_data.bias_state;
214 }
215 { // Get state to stop
216 vpMutex::vpScopedLock lock(s_mutex_state);
217 state_stop = s_state_stop;
218 }
219
220 f << timestamp << " " << ft.t() << std::endl;
221 vpTime::wait(loop_time, 1); // Get a new data each 1 millisecond
222 } while (!state_stop);
223
224 // Wait until thread ends up
225 thread_scope.join();
226
227 ati.close();
228 f.close();
229 std::cout << "Data recorded in " << file << std::endl;
230#else
231 (void)argc;
232 (void)argv;
233 std::cout << "You should install comedi and build atidaq to enable this test..." << std::endl;
234#endif
235 return EXIT_SUCCESS;
236}
237
238#else
239int main()
240{
241 std::cout << "You should build this test with threading capabilities..." << std::endl;
242 return EXIT_SUCCESS;
243}
244#endif
Implementation of column vector and the associated operations.
vpColVector extract(unsigned int r, unsigned int colsize) const
vpRowVector t() const
static const vpColor red
Definition vpColor.h:211
static const vpColor blue
Definition vpColor.h:217
static bool getClick(const vpImage< unsigned char > &I, bool blocking=true)
static void flush(const vpImage< unsigned char > &I)
static void displayText(const vpImage< unsigned char > &I, const vpImagePoint &ip, const std::string &s, const vpColor &color)
void setCalibrationFile(const std::string &calibfile, unsigned short index=1)
static bool checkFilename(const std::string &filename)
Class that allows protection by mutex.
Definition vpMutex.h:166
This class enables real time drawing of 2D or 3D graphics. An instance of the class open a window whi...
Definition vpPlot.h:113
void * Args
Definition vpThread.h:72
void * Return
Definition vpThread.h:73
VISP_EXPORT double measureTimeMs()
VISP_EXPORT int wait(double t0, double t)