ViconMarkerProvider.cpp
Go to the documentation of this file.
1 /*
2  * This file is part of ArmarX.
3  *
4  * ArmarX is free software; you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License version 2 as
6  * published by the Free Software Foundation.
7  *
8  * ArmarX is distributed in the hope that it will be useful, but
9  * WITHOUT ANY WARRANTY; without even the implied warranty of
10  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11  * GNU General Public License for more details.
12  *
13  * You should have received a copy of the GNU General Public License
14  * along with this program. If not, see <http://www.gnu.org/licenses/>.
15  *
16  * @package RobotComponents::ArmarXObjects::ViconMarkerProvider
17  * @author Mirko Waechter ( mirko dot waechter at kit dot edu )
18  * @date 2017
19  * @copyright http://www.gnu.org/licenses/gpl-2.0.txt
20  * GNU General Public License
21  */
22 
23 #include "ViconMarkerProvider.h"
24 
25 
26 using namespace armarx;
27 
28 void
30 {
31  offeringTopic(getProperty<std::string>("ViconDataTopicName").getValue());
32  offeringTopic(getProperty<std::string>("DebugDrawerTopicName").getValue());
33 }
34 
35 void
37 {
38  listener = getTopic<ViconMarkerProviderListenerInterfacePrx>(
39  getProperty<std::string>("ViconDataTopicName").getValue());
40  debugDrawer = getTopic<DebugDrawerInterfacePrx>(
41  getProperty<std::string>("DebugDrawerTopicName").getValue());
42 
43  std::string address = getProperty<std::string>("Hostname").getValue();
44  address += ":" + to_string(getProperty<int>("Port").getValue());
45 
46  ARMARX_INFO << "Connecting to VICON server at " << address << "...";
47 
48  try
49  {
50  dsClient.Connect(address);
51  if (dsClient.IsConnected().Connected)
52  {
53  ARMARX_INFO << "Connected to VICON server. Setting up configuration...";
54 
55  if (dsClient.EnableMarkerData().Result && dsClient.EnableUnlabeledMarkerData().Result &&
56  dsClient.SetStreamMode(ViconDataStreamSDK::CPP::StreamMode::ClientPull).Result)
57  {
58  ARMARX_INFO << "Setup complete. Starting polling thread.";
59 
61  this, &ViconMarkerProvider::publish, "ViconPublishThread");
62  publisherThread->start();
63  }
64  else
65  {
66  ARMARX_ERROR << "Failed to set up configuration!";
67  }
68  }
69  else
70  {
71  ARMARX_ERROR << "Connection failed!";
72  }
73  }
74  catch (...)
75  {
76  ARMARX_ERROR << "Exception while connecting to vicon server!";
78  }
79 }
80 
81 void
83 {
84  publisherThread->stop();
85  dsClient.Disconnect();
86 }
87 
88 void
90 {
91 }
92 
93 void
95 {
96  while (publisherThread->isRunning())
97  {
98  if (!dsClient.IsConnected().Connected)
99  {
100  ARMARX_ERROR << "Connection to VICON server broken!";
101  return;
102  }
103 
104  // Wait for frame (blocking call)
105  if (dsClient.GetFrame().Result != ViconDataStreamSDK::CPP::Result::Success)
106  {
107  ARMARX_WARNING << "Failed to receive frame, will retry in one second! (Is VICON Nexus "
108  "currently running in online or playback mode?)";
109  sleep(1);
110  continue;
111  }
112 
113  DebugDrawerColoredPointCloud pointCloud;
114  pointCloud.pointSize = 10;
115 
116  // Receive labeled markers
117  std::map<std::string, Vector3f> labeledMarkers;
118  int labeledMarkerCount = 0;
119  int subjectCount = dsClient.GetSubjectCount().SubjectCount;
120 
121  for (int curSubj = 0; curSubj < subjectCount; ++curSubj)
122  {
123  std::string subjectName = dsClient.GetSubjectName(curSubj).SubjectName;
124 
125  int subjectMarkerCount = dsClient.GetMarkerCount(subjectName).MarkerCount;
126  for (int i = 0; i < subjectMarkerCount; ++i)
127  {
128  std::string markerName = dsClient.GetMarkerName(subjectName, i).MarkerName;
129  double* translation =
130  dsClient.GetMarkerGlobalTranslation(subjectName, markerName).Translation;
131 
132  Vector3f v;
133  v.e0 = translation[0];
134  v.e1 = translation[1];
135  v.e2 = translation[2];
136  labeledMarkers[subjectName + ":" + markerName] = v;
137 
138  DebugDrawerColoredPointCloudElement p;
139  p.x = v.e0;
140  p.y = v.e1;
141  p.z = v.e2;
142  p.color.r = 0.0f;
143  p.color.g = 0.0f;
144  p.color.b = 1.0f;
145  pointCloud.points.push_back(p);
146 
147  ++labeledMarkerCount;
148  }
149  }
150 
151  // for (auto& it : labeledMarkers)
152  // {
153  // Vector3f v = it.second;
154  // ARMARX_INFO << it.first << " " << v.e0 << " " << v.e1 << " " << v.e2;
155  // }
156  listener->reportLabeledViconMarkerFrame(labeledMarkers);
157 
158  // Receive unlabeled markers
159  std::vector<Vector3f> unlabeledMarkers;
160  int unlabeledMarkerCount = dsClient.GetUnlabeledMarkerCount().MarkerCount;
161 
162  for (int i = 0; i < unlabeledMarkerCount; ++i)
163  {
164  double* translation = dsClient.GetUnlabeledMarkerGlobalTranslation(i).Translation;
165 
166  Vector3f v;
167  v.e0 = translation[0];
168  v.e1 = translation[1];
169  v.e2 = translation[2];
170  unlabeledMarkers.push_back(v);
171 
172  DebugDrawerColoredPointCloudElement p;
173  p.x = v.e0;
174  p.y = v.e1;
175  p.z = v.e2;
176  p.color.r = 1.0f;
177  p.color.g = 0.0f;
178  p.color.b = 0.0f;
179  pointCloud.points.push_back(p);
180  }
181 
182  listener->reportUnlabeledViconMarkerFrame(unlabeledMarkers);
183 
184  ARMARX_DEBUG << "Frame processed: " << subjectCount << " subjects, " << labeledMarkerCount
185  << " labeled markers, " << unlabeledMarkers.size() << " unlabeled markers";
186 
187  debugDrawer->setColoredPointCloudVisu("ViconMarkerProvider", "MarkerCloud", pointCloud);
188  }
189 }
190 
193 {
196 }
armarx::ViconMarkerProvider::publish
void publish()
Definition: ViconMarkerProvider.cpp:94
armarx::ViconMarkerProvider::onDisconnectComponent
void onDisconnectComponent() override
Definition: ViconMarkerProvider.cpp:82
armarx::ViconMarkerProvider::listener
ViconMarkerProviderListenerInterfacePrx listener
Definition: ViconMarkerProvider.h:107
armarx::ViconMarkerProvider::createPropertyDefinitions
armarx::PropertyDefinitionsPtr createPropertyDefinitions() override
Definition: ViconMarkerProvider.cpp:192
armarx::RunningTask
Definition: ArmarXMultipleObjectsScheduler.h:36
armarx::ViconMarkerProvider::dsClient
ViconDataStreamSDK::CPP::Client dsClient
Definition: ViconMarkerProvider.h:109
ARMARX_DEBUG
#define ARMARX_DEBUG
Definition: Logging.h:184
armarx::ViconMarkerProvider::debugDrawer
DebugDrawerInterfacePrx debugDrawer
Definition: ViconMarkerProvider.h:110
armarx::ViconMarkerProviderPropertyDefinitions
Definition: ViconMarkerProvider.h:40
ARMARX_ERROR
#define ARMARX_ERROR
Definition: Logging.h:196
ViconMarkerProvider.h
armarx::to_string
const std::string & to_string(const std::string &s)
Definition: StringHelpers.h:41
armarx::ViconMarkerProvider::publisherThread
RunningTask< ViconMarkerProvider >::pointer_type publisherThread
Definition: ViconMarkerProvider.h:108
armarx::Component::getConfigIdentifier
std::string getConfigIdentifier()
Retrieve config identifier for this component as set in constructor.
Definition: Component.cpp:79
armarx::ctrlutil::v
double v(double t, double v0, double a0, double j)
Definition: CtrlUtil.h:39
ARMARX_INFO
#define ARMARX_INFO
Definition: Logging.h:181
armarx::ManagedIceObject::offeringTopic
void offeringTopic(const std::string &name)
Registers a topic for retrival after initialization.
Definition: ManagedIceObject.cpp:300
IceUtil::Handle< class PropertyDefinitionContainer >
armarx::ViconMarkerProvider::onInitComponent
void onInitComponent() override
Definition: ViconMarkerProvider.cpp:29
armarx::handleExceptions
void handleExceptions()
Definition: Exception.cpp:157
ARMARX_WARNING
#define ARMARX_WARNING
Definition: Logging.h:193
armarx::PropertyDefinitionsPtr
IceUtil::Handle< class PropertyDefinitionContainer > PropertyDefinitionsPtr
PropertyDefinitions smart pointer type.
Definition: forward_declarations.h:35
armarx::ViconMarkerProvider::onConnectComponent
void onConnectComponent() override
Definition: ViconMarkerProvider.cpp:36
armarx::ViconMarkerProvider::onExitComponent
void onExitComponent() override
Definition: ViconMarkerProvider.cpp:89
armarx
This file offers overloads of toIce() and fromIce() functions for STL container types.
Definition: ArmarXTimeserver.cpp:27