Visual Servoing Platform  version 3.6.1 under development (2024-05-05)
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/vpTime.h>
49 #include <visp3/gui/vpPlot.h>
50 #include <visp3/sensor/vpForceTorqueAtiSensor.h>
51 
52 #if defined(VISP_HAVE_THREADS)
53 
54 #include <thread>
55 #include <mutex>
56 
57 typedef enum { BIAS_DONE, UNBIAS_DONE, TO_BIAS, TO_UNBIAS } BiasState;
58 
59 #ifndef DOXYGEN_SHOULD_SKIP_THIS
60 typedef struct
61 {
62  vpColVector ft;
63  double timestamp;
64  BiasState bias_state;
65 } t_shared_data;
66 #endif
67 
68 void scopeFunction(std::mutex &mutex_data, std::mutex &mutex_state, t_shared_data &s_shared_data, bool &s_state_stop)
69 {
70 #ifdef VISP_HAVE_DISPLAY
71  vpPlot scope(2, 700, 700, 100, 200, "ATI F/T sensor data");
72  scope.initGraph(0, 3);
73  scope.initGraph(1, 3);
74  scope.setTitle(0, "Forces (N)");
75  scope.setTitle(1, "Torques (Nm)");
76  scope.setLegend(0, 0, "x");
77  scope.setLegend(0, 1, "y");
78  scope.setLegend(0, 2, "z");
79  scope.setLegend(1, 0, "x");
80  scope.setLegend(1, 1, "y");
81  scope.setLegend(1, 2, "z");
82 #else
83  (void)mutex_data;
84  (void)s_shared_data;
85 #endif
86 
87  t_shared_data shared_data;
88 #ifdef VISP_HAVE_DISPLAY
89  bool click = false;
91 #else
92  double start_time = vpTime::measureTimeMs();
93 #endif
94  do {
95 #ifdef VISP_HAVE_DISPLAY
96  { // Get new measures to plot
97  std::lock_guard<std::mutex> lock(mutex_data);
98  shared_data.ft = s_shared_data.ft;
99  shared_data.timestamp = s_shared_data.timestamp;
100  shared_data.bias_state = s_shared_data.bias_state;
101  }
102 
103  vpColVector force = shared_data.ft.extract(0, 3);
104  vpColVector torque = shared_data.ft.extract(3, 3);
105  scope.plot(0, shared_data.timestamp, force);
106  scope.plot(1, shared_data.timestamp, torque);
107 
108  vpDisplay::displayText(scope.I, 15, 500, "Left click to quit", vpColor::red);
109  vpDisplay::displayText(scope.I, 30, 500, "Right click to bias/unbias", vpColor::red);
110  if (shared_data.bias_state == BIAS_DONE)
111  vpDisplay::displayText(scope.I, 45, 500, "Sensor is biased...", vpColor::blue);
112  else if (shared_data.bias_state == UNBIAS_DONE)
113  vpDisplay::displayText(scope.I, 45, 500, "Sensor is unbiased...", vpColor::blue);
114  else if (shared_data.bias_state == TO_BIAS)
115  vpDisplay::displayText(scope.I, 45, 500, "Sensor bias in progress...", vpColor::blue);
116  else if (shared_data.bias_state == TO_UNBIAS)
117  vpDisplay::displayText(scope.I, 45, 500, "Sensor unbias in progress...", vpColor::blue);
118  vpDisplay::flush(scope.I);
119  click = vpDisplay::getClick(scope.I, button, false);
120  if (click && button == vpMouseButton::button3) {
121  if (shared_data.bias_state == BIAS_DONE)
122  shared_data.bias_state = TO_UNBIAS;
123  else if (shared_data.bias_state == UNBIAS_DONE)
124  shared_data.bias_state = TO_BIAS;
125  { // Set new bias state
126  std::lock_guard<std::mutex> lock(mutex_data);
127  s_shared_data.bias_state = shared_data.bias_state;
128  }
129  }
130 
131 #endif
132  }
133 #ifdef VISP_HAVE_DISPLAY
134  while (!(click && button == vpMouseButton::button1)); // Stop recording by a user left click
135 #else
136  while (vpTime::measureTimeMs() - start_time < 20000); // Stop recording after 20 seconds
137 #endif
138 
139  { // Update state to stop
140  std::lock_guard<std::mutex> lock(mutex_state);
141  s_state_stop = true;
142  }
143 
144  std::cout << "End of scope thread" << std::endl;
145 }
146 
147 int main(int argc, char **argv)
148 {
149 #if defined(VISP_HAVE_ATIDAQ) && defined(VISP_HAVE_COMEDI)
150 
151 #ifdef VISP_HAVE_VIPER850_DATA
152  (void)argc;
153  (void)argv;
154  std::string calibfile = std::string(VISP_VIPER850_DATA_PATH) + std::string("/ati/FT17824.cal");
155  if (!vpIoTools::checkFilename(calibfile)) {
156  std::cout << "ATI F/T calib file \"" << calibfile << "\" doesn't exist";
157  return EXIT_FAILURE;
158  }
159 #else
160  if (argc != 2) {
161  std::cout << "Usage: " << argv[0] << " <ATI calibration file FT*.cal]>" << std::endl;
162  return EXIT_FAILURE;
163  }
164  std::string calibfile(argv[1]);
165 #endif
166 
168  ati.setCalibrationFile(calibfile);
169  ati.open();
170  std::cout << "ATI F/T sensor characteristics: \n" << ati << std::endl;
171 
172  ati.bias();
173  std::cout << "Data recording in progress..." << std::endl;
174 
175  std::mutex mutex_data;
176  std::mutex mutex_state;
177  bool state_stop = false;
178  bool s_state_stop = false;
179  t_shared_data shared_data;
180  t_shared_data s_shared_data;
181 
182  // Start scope thread
183  std::thread thread_scope(&scopeFunction, std::ref(mutex_data), std::ref(mutex_state), std::ref(s_shared_data), std::ref(s_state_stop));
184 
185  std::string file("recorded-ft-sync.txt");
186  std::ofstream f(file.c_str());
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  std::lock_guard<std::mutex> lock(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  }
205  else if (shared_data.bias_state == TO_UNBIAS) {
206  ati.unbias();
207  shared_data.bias_state = UNBIAS_DONE;
208  }
209 
210  { // Update shared F/T measure used by the scope to plot curves
211  std::lock_guard<std::mutex> lock(mutex_data);
212  s_shared_data.ft = ft;
213  s_shared_data.timestamp = timestamp;
214  s_shared_data.bias_state = shared_data.bias_state;
215  }
216  { // Get state to stop
217  std::lock_guard<std::mutex> lock(mutex_state);
218  state_stop = s_state_stop;
219  }
220 
221  f << timestamp << " " << ft.t() << std::endl;
222  vpTime::wait(loop_time, 1); // Get a new data each 1 millisecond
223  } while (!state_stop);
224 
225  // Wait until thread ends up
226  thread_scope.join();
227 
228  ati.close();
229  f.close();
230  std::cout << "Data recorded in " << file << std::endl;
231 #else
232  (void)argc;
233  (void)argv;
234  std::cout << "You should install comedi and build atidaq to enable this test..." << std::endl;
235 #endif
236  return EXIT_SUCCESS;
237 }
238 
239 #else
240 int main()
241 {
242  std::cout << "You should build this test with threading capabilities..." << std::endl;
243  return EXIT_SUCCESS;
244 }
245 #endif
Implementation of column vector and the associated operations.
Definition: vpColVector.h:163
vpColVector extract(unsigned int r, unsigned int colsize) const
Definition: vpColVector.h:367
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)
vpColVector getForceTorque() const
void setCalibrationFile(const std::string &calibfile, unsigned short index=1)
static bool checkFilename(const std::string &filename)
Definition: vpIoTools.cpp:1213
This class enables real time drawing of 2D or 3D graphics. An instance of the class open a window whi...
Definition: vpPlot.h:109
VISP_EXPORT int wait(double t0, double t)
VISP_EXPORT double measureTimeMs()