34#include <visp3/core/vpConfig.h>
41#include <visp3/core/vpTime.h>
42#include <visp3/sensor/vpMocapVicon.h>
44#include <DataStreamClient.h>
45#include <IDataStreamClientBase.h>
47using namespace ViconDataStreamSDK::CPP;
50#ifndef DOXYGEN_SHOULD_SKIP_THIS
51class vpMocapVicon::vpMocapViconImpl
54 vpMocapViconImpl() : m_DirectClient(), m_verbose(false), m_serverAddr() { }
55 virtual ~vpMocapViconImpl() { close(); }
59 m_DirectClient.DisableSegmentData();
60 m_DirectClient.DisableMarkerData();
61 m_DirectClient.DisableUnlabeledMarkerData();
62 m_DirectClient.DisableDeviceData();
64 std::cout <<
"Disconnecting..." << std::endl;
66 m_DirectClient.Disconnect();
72 for (
auto i = 0;
i < n_attempt;
i++) {
73 if (!m_DirectClient.IsConnected().Connected) {
76 const Output_Connect ConnectResult = m_DirectClient.Connect(m_serverAddr);
77 const bool ok = (ConnectResult.Result == Result::Success);
81 std::cout <<
"Warning - connection failed... ";
82 switch (ConnectResult.Result) {
83 case Result::ClientAlreadyConnected:
84 std::cout <<
"Client Already Connected" << std::endl;
86 case Result::InvalidHostName:
87 std::cout <<
"Invalid Host Name" << std::endl;
89 case Result::ClientConnectionFailed:
90 std::cout <<
"Client Connection Failed" << std::endl;
93 std::cout <<
"Unrecognized Error: " << ConnectResult.Result << std::endl;
101 std::cout <<
"Successful connection to : " << m_serverAddr << std::endl;
103 return setupDataStreamed();
109 std::cout <<
"Vicon connection timeout" << std::endl;
114 bool getBodiesPose(std::map<std::string, vpHomogeneousMatrix> &bodies_pose,
bool all_bodies =
false)
116 if (m_DirectClient.GetFrame().Result == Result::Success) {
117 for (
unsigned int iBody = 0; iBody < m_DirectClient.GetSubjectCount().SubjectCount; iBody++) {
118 std::string bodyName = m_DirectClient.GetSubjectName(iBody).SubjectName;
119 std::string rootSegment = m_DirectClient.GetSubjectRootSegmentName(bodyName).SegmentName;
120 bool data_extraction_success = (m_DirectClient.GetSegmentGlobalRotationMatrix(bodyName, rootSegment).Result &&
121 m_DirectClient.GetSegmentGlobalTranslation(bodyName, rootSegment).Result);
122 vpHomogeneousMatrix bodyPose;
124 if (!data_extraction_success) {
125 std::cout <<
"Error : Could not get pose from body n°" << iBody << std::endl;
130 bodyPose[0][3] = m_DirectClient.GetSegmentGlobalTranslation(bodyName, rootSegment).Translation[0] / 1000.0;
131 bodyPose[1][3] = m_DirectClient.GetSegmentGlobalTranslation(bodyName, rootSegment).Translation[1] / 1000.0;
132 bodyPose[2][3] = m_DirectClient.GetSegmentGlobalTranslation(bodyName, rootSegment).Translation[2] / 1000.0;
133 bodyPose[3][3] = 1.0;
137 for (
unsigned int i = 0;
i < 3;
i++) {
138 for (
unsigned int j = 0;
j < 3;
j++) {
139 bodyPose[
i][
j] = m_DirectClient.GetSegmentGlobalRotationMatrix(bodyName, rootSegment).Rotation[k++];
144 bodies_pose[bodyName] = bodyPose;
147 bodies_pose[bodyName] = bodyPose;
155 bool getSpecificBodyPose(
const std::string &body_name, vpHomogeneousMatrix &body_pose)
157 std::map<std::string, vpHomogeneousMatrix> bodies_pose;
158 if (getBodiesPose(bodies_pose,
true)) {
159 if (bodies_pose.find(body_name) != bodies_pose.end()) {
160 body_pose = bodies_pose[body_name];
164 std::cout <<
"The body " << body_name <<
" was not found in Vicon. Please check the name you typed."
171 std::cout <<
"Error : could not process data from Vicon" << std::endl;
177 void setServerAddress(
const std::string &serverAddr) { m_serverAddr = serverAddr; }
179 void setVerbose(
bool verbose) { m_verbose = verbose; }
181 bool setupDataStreamed()
184 m_DirectClient.EnableSegmentData();
185 m_DirectClient.EnableMarkerData();
186 m_DirectClient.EnableUnlabeledMarkerData();
187 m_DirectClient.EnableMarkerRayData();
188 m_DirectClient.EnableDeviceData();
189 m_DirectClient.EnableDebugData();
191 m_DirectClient.SetStreamMode(ViconDataStreamSDK::CPP::StreamMode::ServerPush);
194 m_DirectClient.SetAxisMapping(Direction::Forward, Direction::Left,
201 ViconDataStreamSDK::CPP::Client m_DirectClient;
203 std::string m_serverAddr;
242 return m_impl->getBodiesPose(bodies_pose, all_bodies);
253 return m_impl->getSpecificBodyPose(body_name, body_pose);
273void dummy_vpMocapVicon() { }
Implementation of an homogeneous matrix and operations on such kind of matrices.
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)
VISP_EXPORT void sleepMs(double t)