Visual Servoing Platform version 3.6.0
Loading...
Searching...
No Matches
testMocapVicon.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 Vicon Motion Capture System.
33 *
34*****************************************************************************/
35
40#include <visp3/sensor/vpMocapVicon.h>
41
42#include <iostream>
43
44#if defined(VISP_HAVE_VICON) && (VISP_CXX_STANDARD >= VISP_CXX_STANDARD_11)
45
46#include <mutex>
47#include <signal.h>
48#include <thread>
49
50#include <visp3/sensor/vpMocapVicon.h>
51
52#include <visp3/core/vpTime.h>
53
54bool g_quit = false;
55
60void 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
68void usage(const char *argv[], int error)
69{
70 std::cout << "SYNOPSIS" << std::endl
71 << " " << argv[0] << " [--server-address <address>]"
72 << " [--only-body]"
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.1." << 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 Vicon 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
107void 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 vpMocapVicon vicon;
111 vicon.setVerbose(opt_verbose);
112 vicon.setServerAddress(opt_serverAddress);
113 vicon.connect();
114 while (!g_quit) {
115 std::map<std::string, vpHomogeneousMatrix> bodies_pose;
116
117 if (opt_onlyBody == "") {
118 vicon.getBodiesPose(bodies_pose, opt_all_bodies);
119 } else {
121 vicon.getSpecificBodyPose(opt_onlyBody, pose);
122 bodies_pose[opt_onlyBody] = pose;
123 }
124
125 lock.lock();
126 current_bodies_pose = bodies_pose;
127 lock.unlock();
128
130 }
131}
132
133void display_loop(std::mutex &lock, const std::map<std::string, vpHomogeneousMatrix> &current_bodies_pose, bool verbose)
134{
135 std::map<std::string, vpHomogeneousMatrix> bodies_pose;
136
137 while (!g_quit) {
138
139 lock.lock();
140 bodies_pose = current_bodies_pose;
141 lock.unlock();
142 for (std::map<std::string, vpHomogeneousMatrix>::iterator it = bodies_pose.begin(); it != bodies_pose.end(); ++it) {
143 vpRxyzVector rxyz(it->second.getRotationMatrix());
144 std::cout << "Found body: " << it->first << std::endl;
145 if (verbose) {
146 std::cout << " Translation [m]: " << it->second.getTranslationVector().t() << std::endl
147 << " Quaternion: " << vpQuaternionVector(it->second.getRotationMatrix()).t() << std::endl;
148 std::cout << " Roll/pitch/yaw [deg]: ";
149 for (unsigned int i = 0; i < 3; i++) {
150 std::cout << vpMath::deg(rxyz[i]) << " ";
151 }
152 std::cout << std::endl;
153 }
154 }
155
156 vpTime::sleepMs(200);
157 }
158}
159
160int main(int argc, const char *argv[])
161{
162 bool opt_verbose = false;
163 std::string opt_serverAddress = "192.168.30.1";
164 std::string opt_onlyBody = "";
165 bool opt_all_bodies = false;
166
167 // Map containig all the current poses of the drones
168 std::map<std::string, vpHomogeneousMatrix> current_bodies_pose;
169
170 signal(SIGINT, quitHandler);
171
172 for (int i = 1; i < argc; i++) {
173 if (std::string(argv[i]) == "--verbose" || std::string(argv[i]) == "-v") {
174 opt_verbose = true;
175 } else if (std::string(argv[i]) == "--server-address") {
176 opt_serverAddress = std::string(argv[i + 1]);
177 i++;
178 } else if (std::string(argv[i]) == "--only-body") {
179 opt_onlyBody = std::string(argv[i + 1]);
180 i++;
181 } else if (std::string(argv[i]) == "--all-bodies") {
182 opt_all_bodies = true;
183 } else if (std::string(argv[i]) == "--help" || std::string(argv[i]) == "-h") {
184 usage(argv, 0);
185 return EXIT_SUCCESS;
186 } else {
187 usage(argv, i);
188 return EXIT_FAILURE;
189 }
190 }
191
192 std::mutex lock;
193 std::thread mocap_thread(
194 [&lock, &opt_verbose, &opt_all_bodies, &opt_serverAddress, &opt_onlyBody, &current_bodies_pose]() {
195 mocap_loop(lock, opt_verbose, opt_all_bodies, opt_serverAddress, opt_onlyBody, current_bodies_pose);
196 });
197 std::thread display_thread(
198 [&lock, &current_bodies_pose, &opt_verbose]() { display_loop(lock, current_bodies_pose, opt_verbose); });
199
200 mocap_thread.join();
201 display_thread.join();
202
203 return EXIT_SUCCESS;
204}
205#else
206int main()
207{
208#ifndef VISP_HAVE_VICON
209 std::cout << "Install Vicon Datastream SDK to be able to test Vicon Mocap System using ViSP" << std::endl;
210#endif
211#if !(VISP_CXX_STANDARD >= VISP_CXX_STANDARD_11)
212 std::cout << "This test required c++11 or more recent c++ standard." << std::endl;
213#endif
214 return EXIT_SUCCESS;
215}
216#endif
Implementation of an homogeneous matrix and operations on such kind of matrices.
static double deg(double rad)
Definition vpMath.h:106
bool getBodiesPose(std::map< std::string, vpHomogeneousMatrix > &bodies_pose, bool all_bodies=false)
void setVerbose(bool verbose)
void setServerAddress(const std::string &serverAddr)
bool getSpecificBodyPose(const std::string &body_name, vpHomogeneousMatrix &body_pose)
Implementation of a rotation vector as quaternion angle minimal representation.
vpRowVector t() const
Implementation of a rotation vector as Euler angle minimal representation.
VISP_EXPORT void sleepMs(double t)