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
26using namespace armarx;
27
28void
30{
31 offeringTopic(getProperty<std::string>("ViconDataTopicName").getValue());
32 offeringTopic(getProperty<std::string>("DebugDrawerTopicName").getValue());
33}
34
35void
37{
39 getProperty<std::string>("ViconDataTopicName").getValue());
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
81void
87
88void
92
93void
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
std::string getConfigIdentifier()
Retrieve config identifier for this component as set in constructor.
Definition Component.cpp:90
Property< PropertyType > getProperty(const std::string &name)
void offeringTopic(const std::string &name)
Registers a topic for retrival after initialization.
TopicProxyType getTopic(const std::string &name)
Returns a proxy of the specified topic.
ViconMarkerProviderListenerInterfacePrx listener
RunningTask< ViconMarkerProvider >::pointer_type publisherThread
armarx::PropertyDefinitionsPtr createPropertyDefinitions() override
ViconDataStreamSDK::CPP::Client dsClient
DebugDrawerInterfacePrx debugDrawer
#define ARMARX_INFO
The normal logging level.
Definition Logging.h:181
#define ARMARX_ERROR
The logging level for unexpected behaviour, that must be fixed.
Definition Logging.h:196
#define ARMARX_DEBUG
The logging level for output that is only interesting while debugging.
Definition Logging.h:184
#define ARMARX_WARNING
The logging level for unexpected behaviour, but not a serious problem.
Definition Logging.h:193
This file offers overloads of toIce() and fromIce() functions for STL container types.
void handleExceptions()
IceUtil::Handle< class PropertyDefinitionContainer > PropertyDefinitionsPtr
PropertyDefinitions smart pointer type.
const std::string & to_string(const std::string &s)