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 
30 {
31  offeringTopic(getProperty<std::string>("ViconDataTopicName").getValue());
32  offeringTopic(getProperty<std::string>("DebugDrawerTopicName").getValue());
33 }
34 
35 
37 {
38  listener = getTopic<ViconMarkerProviderListenerInterfacePrx>(getProperty<std::string>("ViconDataTopicName").getValue());
39  debugDrawer = getTopic<DebugDrawerInterfacePrx>(getProperty<std::string>("DebugDrawerTopicName").getValue());
40 
41  std::string address = getProperty<std::string>("Hostname").getValue();
42  address += ":" + to_string(getProperty<int>("Port").getValue());
43 
44  ARMARX_INFO << "Connecting to VICON server at " << address << "...";
45 
46  try
47  {
48  dsClient.Connect(address);
49  if (dsClient.IsConnected().Connected)
50  {
51  ARMARX_INFO << "Connected to VICON server. Setting up configuration...";
52 
53  if (dsClient.EnableMarkerData().Result && dsClient.EnableUnlabeledMarkerData().Result && dsClient.SetStreamMode(ViconDataStreamSDK::CPP::StreamMode::ClientPull).Result)
54  {
55  ARMARX_INFO << "Setup complete. Starting polling thread.";
56 
58  publisherThread->start();
59  }
60  else
61  {
62  ARMARX_ERROR << "Failed to set up configuration!";
63  }
64  }
65  else
66  {
67  ARMARX_ERROR << "Connection failed!";
68  }
69  }
70  catch (...)
71  {
72  ARMARX_ERROR << "Exception while connecting to vicon server!";
74  }
75 }
76 
77 
79 {
80  publisherThread->stop();
81  dsClient.Disconnect();
82 }
83 
84 
86 {
87 
88 }
89 
91 {
92  while (publisherThread->isRunning())
93  {
94  if (!dsClient.IsConnected().Connected)
95  {
96  ARMARX_ERROR << "Connection to VICON server broken!";
97  return;
98  }
99 
100  // Wait for frame (blocking call)
101  if (dsClient.GetFrame().Result != ViconDataStreamSDK::CPP::Result::Success)
102  {
103  ARMARX_WARNING << "Failed to receive frame, will retry in one second! (Is VICON Nexus currently running in online or playback mode?)";
104  sleep(1);
105  continue;
106  }
107 
108  DebugDrawerColoredPointCloud pointCloud;
109  pointCloud.pointSize = 10;
110 
111  // Receive labeled markers
112  std::map<std::string, Vector3f> labeledMarkers;
113  int labeledMarkerCount = 0;
114  int subjectCount = dsClient.GetSubjectCount().SubjectCount;
115 
116  for (int curSubj = 0; curSubj < subjectCount; ++curSubj)
117  {
118  std::string subjectName = dsClient.GetSubjectName(curSubj).SubjectName;
119 
120  int subjectMarkerCount = dsClient.GetMarkerCount(subjectName).MarkerCount;
121  for (int i = 0; i < subjectMarkerCount; ++i)
122  {
123  std::string markerName = dsClient.GetMarkerName(subjectName, i).MarkerName;
124  double* translation = dsClient.GetMarkerGlobalTranslation(subjectName, markerName).Translation;
125 
126  Vector3f v;
127  v.e0 = translation[0];
128  v.e1 = translation[1];
129  v.e2 = translation[2];
130  labeledMarkers[subjectName + ":" + markerName] = v;
131 
132  DebugDrawerColoredPointCloudElement p;
133  p.x = v.e0;
134  p.y = v.e1;
135  p.z = v.e2;
136  p.color.r = 0.0f;
137  p.color.g = 0.0f;
138  p.color.b = 1.0f;
139  pointCloud.points.push_back(p);
140 
141  ++labeledMarkerCount;
142  }
143  }
144 
145  // for (auto& it : labeledMarkers)
146  // {
147  // Vector3f v = it.second;
148  // ARMARX_INFO << it.first << " " << v.e0 << " " << v.e1 << " " << v.e2;
149  // }
150  listener->reportLabeledViconMarkerFrame(labeledMarkers);
151 
152  // Receive unlabeled markers
153  std::vector<Vector3f> unlabeledMarkers;
154  int unlabeledMarkerCount = dsClient.GetUnlabeledMarkerCount().MarkerCount;
155 
156  for (int i = 0; i < unlabeledMarkerCount; ++i)
157  {
158  double* translation = dsClient.GetUnlabeledMarkerGlobalTranslation(i).Translation;
159 
160  Vector3f v;
161  v.e0 = translation[0];
162  v.e1 = translation[1];
163  v.e2 = translation[2];
164  unlabeledMarkers.push_back(v);
165 
166  DebugDrawerColoredPointCloudElement p;
167  p.x = v.e0;
168  p.y = v.e1;
169  p.z = v.e2;
170  p.color.r = 1.0f;
171  p.color.g = 0.0f;
172  p.color.b = 0.0f;
173  pointCloud.points.push_back(p);
174  }
175 
176  listener->reportUnlabeledViconMarkerFrame(unlabeledMarkers);
177 
178  ARMARX_DEBUG << "Frame processed: " << subjectCount << " subjects, " << labeledMarkerCount << " labeled markers, " << unlabeledMarkers.size() << " unlabeled markers";
179 
180  debugDrawer->setColoredPointCloudVisu("ViconMarkerProvider", "MarkerCloud", pointCloud);
181  }
182 }
183 
185 {
188 }
189 
armarx::ViconMarkerProvider::publish
void publish()
Definition: ViconMarkerProvider.cpp:90
armarx::ViconMarkerProvider::onDisconnectComponent
void onDisconnectComponent() override
Definition: ViconMarkerProvider.cpp:78
armarx::ViconMarkerProvider::listener
ViconMarkerProviderListenerInterfacePrx listener
Definition: ViconMarkerProvider.h:103
armarx::ViconMarkerProvider::createPropertyDefinitions
armarx::PropertyDefinitionsPtr createPropertyDefinitions() override
Definition: ViconMarkerProvider.cpp:184
armarx::RunningTask
Definition: ArmarXMultipleObjectsScheduler.h:35
armarx::ViconMarkerProvider::dsClient
ViconDataStreamSDK::CPP::Client dsClient
Definition: ViconMarkerProvider.h:105
ARMARX_DEBUG
#define ARMARX_DEBUG
Definition: Logging.h:177
armarx::ViconMarkerProvider::debugDrawer
DebugDrawerInterfacePrx debugDrawer
Definition: ViconMarkerProvider.h:106
armarx::ViconMarkerProviderPropertyDefinitions
Definition: ViconMarkerProvider.h:38
ARMARX_ERROR
#define ARMARX_ERROR
Definition: Logging.h:189
ViconMarkerProvider.h
armarx::to_string
const std::string & to_string(const std::string &s)
Definition: StringHelpers.h:40
armarx::ViconMarkerProvider::publisherThread
RunningTask< ViconMarkerProvider >::pointer_type publisherThread
Definition: ViconMarkerProvider.h:104
armarx::Component::getConfigIdentifier
std::string getConfigIdentifier()
Retrieve config identifier for this component as set in constructor.
Definition: Component.cpp:74
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:174
armarx::ManagedIceObject::offeringTopic
void offeringTopic(const std::string &name)
Registers a topic for retrival after initialization.
Definition: ManagedIceObject.cpp:290
IceUtil::Handle< class PropertyDefinitionContainer >
armarx::ViconMarkerProvider::onInitComponent
void onInitComponent() override
Definition: ViconMarkerProvider.cpp:29
armarx::handleExceptions
void handleExceptions()
Definition: Exception.cpp:141
ARMARX_WARNING
#define ARMARX_WARNING
Definition: Logging.h:186
armarx::PropertyDefinitionsPtr
IceUtil::Handle< class PropertyDefinitionContainer > PropertyDefinitionsPtr
PropertyDefinitions smart pointer type.
Definition: forward_declarations.h:34
armarx::ViconMarkerProvider::onConnectComponent
void onConnectComponent() override
Definition: ViconMarkerProvider.cpp:36
armarx::ViconMarkerProvider::onExitComponent
void onExitComponent() override
Definition: ViconMarkerProvider.cpp:85
armarx
This file offers overloads of toIce() and fromIce() functions for STL container types.
Definition: ArmarXTimeserver.cpp:28