Visual Servoing Platform  version 3.6.1 under development (2024-11-15)
testForceTorqueAtiNetFTSensor.cpp
1 /*
2  * ViSP, open source Visual Servoing Platform software.
3  * Copyright (C) 2005 - 2024 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  * Test force/torque ATI sensor.
32  */
33 
40 #include <iostream>
41 
42 #include <visp3/gui/vpPlot.h>
43 #include <visp3/sensor/vpForceTorqueAtiNetFTSensor.h>
44 
45 int main(int argc, char **argv)
46 {
47 #ifdef ENABLE_VISP_NAMESPACE
48  using namespace VISP_NAMESPACE_NAME;
49 #endif
50  // Check if vpForceTorqueAtiNetFTSensor can be used since inet_ntop() used to
51  // communicate by UDP with the sensor is not supported on win XP
52 #ifdef VISP_HAVE_FUNC_INET_NTOP
53 
54  std::string opt_ip = "192.168.1.1";
55  int opt_port = 49152;
56  bool opt_no_display = false;
57 
58  for (int i = 0; i < argc; i++) {
59  if (std::string(argv[i]) == "--ip")
60  opt_ip = std::string(argv[i + 1]);
61  else if (std::string(argv[i]) == "--port")
62  opt_port = atoi(argv[i + 1]);
63  else if (std::string(argv[i]) == "--no-display" || std::string(argv[i]) == "-d")
64  opt_no_display = true;
65  else if (std::string(argv[i]) == "--help" || std::string(argv[i]) == "-h") {
66  std::cout << "\nUsage: " << argv[0]
67  << " [--ip <Net F/T IP address (default: 192.168.1.1)>] [--port <Ethernet port (default: 49152)>]"
68  << " [--no-display] [-d] [--help] [-h]\n"
69  << std::endl;
70  return EXIT_SUCCESS;
71  }
72  }
73 
74  std::cout << "Use IP : " << opt_ip << std::endl;
75  std::cout << "Use port: " << opt_port << std::endl;
76  std::cout << "Disable display: " << opt_no_display << std::endl;
77 
78  vpForceTorqueAtiNetFTSensor ati_net_ft;
79  ati_net_ft.init(opt_ip, opt_port);
80  if (!ati_net_ft.startStreaming()) {
81  std::cout << "Unable to start streaming" << std::endl;
82  return EXIT_FAILURE;
83  }
84 
85 #if defined(VISP_HAVE_DISPLAY)
86  vpPlot *plotter = nullptr;
87  if (!opt_no_display) {
88  plotter = new vpPlot(2, 700, 700, 100, 200, "Curves...");
89  plotter->initGraph(0, 3);
90  plotter->setTitle(0, "Force measurements");
91  plotter->setLegend(0, 0, "Fx");
92  plotter->setLegend(0, 1, "Fy");
93  plotter->setLegend(0, 2, "Fz");
94  plotter->initGraph(1, 3);
95  plotter->setTitle(1, "Torque measurements");
96  plotter->setLegend(1, 0, "Tx");
97  plotter->setLegend(1, 1, "Ty");
98  plotter->setLegend(1, 2, "Tz");
99  }
100  bool bias = false;
101 #endif
102 
103  vpColVector ft;
104  bool end = false;
105  unsigned long nbacq = 0;
106  double t_start = vpTime::measureTimeMs();
107  while (!end) {
108  double t = vpTime::measureTimeMs();
109 
110  if (ati_net_ft.waitForNewData()) {
111  ft = ati_net_ft.getForceTorque();
112 #if defined(VISP_HAVE_DISPLAY)
113  if (!opt_no_display) {
114  vpColVector force = ft.extract(0, 3);
115  vpColVector torque = ft.extract(3, 3);
116  plotter->plot(0, nbacq, force);
117  plotter->plot(1, nbacq, torque);
118  vpDisplay::displayText(plotter->I, 20, 80, "Left click to quit", vpColor::red);
119  if (bias) {
120  vpDisplay::displayText(plotter->I, 40, 80, "Right click to unbias", vpColor::red);
121  }
122  else {
123  vpDisplay::displayText(plotter->I, 40, 80, "Right click to bias", vpColor::red);
124  }
126 
127  if (vpDisplay::getClick(plotter->I, button, false)) {
128  if (button == vpMouseButton::button1) {
129  end = true;
130  }
131  else if (button == vpMouseButton::button3) {
132  bias = !bias;
133  if (bias) {
134  std::cout << "Bias F/T sensor" << std::endl;
135  ati_net_ft.bias();
136  }
137  else {
138  std::cout << "Unbias F/T sensor" << std::endl;
139  ati_net_ft.unbias();
140  }
141  }
142  }
143  vpDisplay::flush(plotter->I);
144  }
145  else {
146  std::cout << "F/T: " << ft.t() << std::endl;
147  if (nbacq > 30) {
148  end = true;
149  }
150  }
151 #else
152  std::cout << "F/T: " << ft.t() << std::endl;
153  if (nbacq > 30) {
154  end = true;
155  }
156 #endif
157  nbacq++;
158  }
159  vpTime::wait(t, 2);
160  std::cout << "Loop time: " << vpTime::measureTimeMs() - t << " ms" << std::endl;
161  }
162  ati_net_ft.stopStreaming();
163  double fps = 1000. * nbacq / (vpTime::measureTimeMs() - t_start);
164 
165 #if defined(VISP_HAVE_DISPLAY)
166  if (plotter) {
167  delete plotter;
168  }
169 #endif
170 
171  std::cout << "Mean acquisition frequency: " << fps << " Hz" << std::endl;
172  std::cout << "Test succeed" << std::endl;
173 #else
174  (void)argc;
175  (void)argv;
176  std::cout << "vpForceTorqueAtiNetFTSensor is not supported on this platform" << std::endl;
177 #endif
178  return EXIT_SUCCESS;
179 }
Implementation of column vector and the associated operations.
Definition: vpColVector.h:191
vpColVector extract(unsigned int r, unsigned int colsize) const
Definition: vpColVector.h:405
vpRowVector t() const
static const vpColor red
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 bias(unsigned int n_counts=50)
bool waitForNewData(unsigned int timeout=50)
This class enables real time drawing of 2D or 3D graphics. An instance of the class open a window whi...
Definition: vpPlot.h:112
void initGraph(unsigned int graphNum, unsigned int curveNbr)
Definition: vpPlot.cpp:203
vpImage< unsigned char > I
Definition: vpPlot.h:114
void setLegend(unsigned int graphNum, unsigned int curveNum, const std::string &legend)
Definition: vpPlot.cpp:552
void plot(unsigned int graphNum, unsigned int curveNum, double x, double y)
Definition: vpPlot.cpp:270
void setTitle(unsigned int graphNum, const std::string &title)
Definition: vpPlot.cpp:510
void init(const std::string &hostname, int port)
VISP_EXPORT int wait(double t0, double t)
VISP_EXPORT double measureTimeMs()