Visual Servoing Platform  version 3.6.1 under development (2024-11-15)
testMocapQualisys.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 Qualisys Motion Capture System.
32  */
33 
38 #include <visp3/sensor/vpMocapQualisys.h>
39 
40 #include <iostream>
41 
42 #if defined(VISP_HAVE_QUALISYS) && defined(VISP_HAVE_THREADS)
43 
44 #include <mutex>
45 #include <signal.h>
46 #include <thread>
47 
48 #include <visp3/core/vpTime.h>
49 
50 #ifdef ENABLE_VISP_NAMESPACE
51 using namespace VISP_NAMESPACE_NAME;
52 #endif
53 
54 bool g_quit = false;
55 
60 void quitHandler(int sig)
61 {
62  std::cout << std::endl << "TERMINATING AT USER REQUEST" << std::endl << std::endl;
63 
64  g_quit = true;
65  (void)sig;
66 }
67 
68 void usage(const char *argv[], int error)
69 {
70  std::cout << "SYNOPSIS" << std::endl
71  << " " << argv[0] << " [--server-address <address>] [-sa]"
72  << " [--only-body] [-ob]"
73  << " [--all-bodies]"
74  << " [--verbose] [-v]"
75  << " [--help] [-h]" << std::endl
76  << std::endl;
77  std::cout << "DESCRIPTION" << std::endl
78  << " --server-address <address>" << std::endl
79  << " Server address." << std::endl
80  << " Default: 192.168.30.42." << std::endl
81  << std::endl
82  << " --only-body <name>" << std::endl
83  << " Name of the specific body you want to be displayed." << std::endl
84  << " Default: ''" << std::endl
85  << std::endl
86  << " --all-bodies" << std::endl
87  << " When used, get all bodies pose including non visible bodies." << std::endl
88  << std::endl
89  << " --verbose, -v" << std::endl
90  << " Enable verbose mode." << std::endl
91  << std::endl
92  << " --help, -h" << std::endl
93  << " Print this helper message." << std::endl
94  << std::endl;
95  std::cout << "USAGE" << std::endl
96  << " Example to test Qualisys connection:" << std::endl
97  << " " << argv[0] << " --server-address 127.0.0.1 --verbose" << std::endl
98  << std::endl;
99 
100  if (error) {
101  std::cout << "Error" << std::endl
102  << " "
103  << "Unsupported parameter " << argv[error] << std::endl;
104  }
105 }
106 
107 void mocap_loop(std::mutex &lock, bool opt_verbose, bool opt_all_bodies, std::string &opt_serverAddress,
108  std::string &opt_onlyBody, std::map<std::string, vpHomogeneousMatrix> &current_bodies_pose)
109 {
110  vpMocapQualisys qualisys;
111  qualisys.setVerbose(opt_verbose);
112  qualisys.setServerAddress(opt_serverAddress);
113  if (!qualisys.connect()) {
114  std::cout << "Qualisys connection error. Check the Qualisys Task Manager or your IP address." << std::endl;
115  return;
116  }
117  while (!g_quit) {
118  std::map<std::string, vpHomogeneousMatrix> bodies_pose;
119 
120  if (opt_onlyBody == "") {
121  if (!qualisys.getBodiesPose(bodies_pose, opt_all_bodies)) {
122  std::cout << "Qualisys error. Check the Qualisys Task Manager" << std::endl;
123  }
124  }
125  else {
126  vpHomogeneousMatrix pose;
127  if (!qualisys.getSpecificBodyPose(opt_onlyBody, pose)) {
128  std::cout << "Qualisys error. Check the Qualisys Task Manager" << std::endl;
129  }
130  bodies_pose[opt_onlyBody] = pose;
131  }
132 
133  lock.lock();
134  current_bodies_pose = bodies_pose;
135  lock.unlock();
136 
137  vpTime::sleepMs(5);
138  }
139 }
140 
141 void display_loop(std::mutex &lock, const std::map<std::string, vpHomogeneousMatrix> &current_bodies_pose, bool verbose)
142 {
143  std::map<std::string, vpHomogeneousMatrix> bodies_pose;
144 
145  while (!g_quit) {
146 
147  lock.lock();
148  bodies_pose = current_bodies_pose;
149  lock.unlock();
150  for (std::map<std::string, vpHomogeneousMatrix>::iterator it = bodies_pose.begin(); it != bodies_pose.end(); ++it) {
151  vpRxyzVector rxyz(it->second.getRotationMatrix());
152  std::cout << "Found body: " << it->first << std::endl;
153  if (verbose) {
154  std::cout << " Translation [m]: " << it->second.getTranslationVector().t() << std::endl
155  << " Quaternion: " << vpQuaternionVector(it->second.getRotationMatrix()).t() << std::endl;
156  std::cout << " Roll/pitch/yaw [deg]: ";
157  for (unsigned int i = 0; i < 3; i++) {
158  std::cout << vpMath::deg(rxyz[i]) << " ";
159  }
160  std::cout << std::endl;
161  std::cout << " Transformation Matrix wMb:\n" << it->second << std::endl;
162  }
163  }
164 
165  vpTime::sleepMs(200);
166  }
167 }
168 
169 int main(int argc, const char *argv[])
170 {
171  bool opt_verbose = false;
172  std::string opt_serverAddress = "192.168.30.42";
173  std::string opt_onlyBody = "";
174  bool opt_all_bodies = false;
175 
176  // Map containig all the current poses of the drones
177  std::map<std::string, vpHomogeneousMatrix> current_bodies_pose;
178 
179  signal(SIGINT, quitHandler);
180 
181  for (int i = 1; i < argc; i++) {
182  if (std::string(argv[i]) == "--verbose" || std::string(argv[i]) == "-v") {
183  opt_verbose = true;
184  }
185  else if (std::string(argv[i]) == "--server-address" || std::string(argv[i]) == "-sa") {
186  opt_serverAddress = std::string(argv[i + 1]);
187  i++;
188  }
189  else if (std::string(argv[i]) == "--only-body" || std::string(argv[i]) == "-ob") {
190  opt_onlyBody = std::string(argv[i + 1]);
191  i++;
192  }
193  else if (std::string(argv[i]) == "--all-bodies") {
194  opt_all_bodies = true;
195  }
196  else if (std::string(argv[i]) == "--help" || std::string(argv[i]) == "-h") {
197  usage(argv, 0);
198  return EXIT_SUCCESS;
199  }
200  else {
201  usage(argv, i);
202  return EXIT_FAILURE;
203  }
204  }
205 
206  std::mutex lock;
207  std::thread mocap_thread(
208  [&lock, &opt_verbose, &opt_all_bodies, &opt_serverAddress, &opt_onlyBody, &current_bodies_pose]() {
209  mocap_loop(lock, opt_verbose, opt_all_bodies, opt_serverAddress, opt_onlyBody, current_bodies_pose);
210  });
211  std::thread display_thread(
212  [&lock, &current_bodies_pose, &opt_verbose]() { display_loop(lock, current_bodies_pose, opt_verbose); });
213 
214  mocap_thread.join();
215  display_thread.join();
216 
217  return EXIT_SUCCESS;
218 }
219 #else
220 int main()
221 {
222  std::cout << "Install qualisys_cpp_sdk to be able to test Qualisys Mocap System using ViSP" << std::endl;
223 
224  return EXIT_SUCCESS;
225 }
226 #endif
Implementation of an homogeneous matrix and operations on such kind of matrices.
static double deg(double rad)
Definition: vpMath.h:119
void setServerAddress(const std::string &serverAddr)
bool getSpecificBodyPose(const std::string &body_name, vpHomogeneousMatrix &body_pose)
void setVerbose(bool verbose)
bool getBodiesPose(std::map< std::string, vpHomogeneousMatrix > &bodies_pose, bool all_bodies=false)
Implementation of a rotation vector as quaternion angle minimal representation.
vpRowVector t() const
Implementation of a rotation vector as Euler angle minimal representation.
Definition: vpRxyzVector.h:183
VISP_EXPORT void sleepMs(double t)