Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008 #include <telekyb_vicon/ViconTracker.hpp>
00009
00010 namespace TELEKYB_NAMESPACE
00011 {
00012
00013 ViconTracker::ViconTracker()
00014 : options(ViconTrackerOptions::Instance()), totalFrameCount(0), totalDroppedFrameCount(0)
00015 {
00016 init();
00017 }
00018
00019 ViconTracker::~ViconTracker()
00020 {
00021
00022 }
00023
00024 void ViconTracker::init() {
00025 ROS_INFO_STREAM("Connecting to Vicon Tracker at " << options.tTrackerHostname->getValue());
00026
00027
00028 while (!ViconBridge::Instance().getClient().IsConnected().Connected)
00029 {
00030 ViconBridge::Instance().getClient().Connect(options.tTrackerHostname->getValue());
00031 ROS_INFO(".");
00032 if (!ros::ok()) {
00033 return;
00034 }
00035
00036
00037 usleep(1000*1000);
00038 }
00039 ROS_ASSERT_MSG(ViconBridge::Instance().getClient().IsConnected().Connected,
00040 "Unable to connect to Vicon Tracker at %s", options.tTrackerHostname->getValue().c_str());
00041 ROS_INFO_STREAM("... connected!");
00042
00043
00044 Vicon::Output_GetVersion viconVersion = ViconBridge::Instance().getClient().GetVersion();
00045 ROS_INFO_STREAM("Tracker Version: " << viconVersion.Major << "." << viconVersion.Minor << "."
00046 << viconVersion.Point);
00047
00048 if (options.tEnableSegmentData->getValue()) {
00049
00050 ROS_INFO_STREAM("Enabling SegmentData");
00051 ViconBridge::Instance().getClient().EnableSegmentData();
00052 ROS_ASSERT(ViconBridge::Instance().getClient().IsSegmentDataEnabled().Enabled);
00053 } else {
00054
00055 ROS_INFO_STREAM("Disabling SegmentData");
00056 ViconBridge::Instance().getClient().DisableSegmentData();
00057 ROS_ASSERT(!ViconBridge::Instance().getClient().IsSegmentDataEnabled().Enabled);
00058 }
00059
00060 if (options.tEnableMarkerData->getValue()) {
00061
00062 ROS_INFO_STREAM("Enabling MarkerData");
00063 ViconBridge::Instance().getClient().EnableMarkerData();
00064 ROS_ASSERT(ViconBridge::Instance().getClient().IsMarkerDataEnabled().Enabled);
00065 } else {
00066
00067 ROS_INFO_STREAM("Disabling MarkerData");
00068 ViconBridge::Instance().getClient().DisableMarkerData();
00069 ROS_ASSERT(!ViconBridge::Instance().getClient().IsMarkerDataEnabled().Enabled);
00070 }
00071
00072
00073
00074 ViconDirection xAxisIn = options.tAxisMapping->getValue()[0];;
00075 ViconDirection yAxisIn = options.tAxisMapping->getValue()[1];;
00076 ViconDirection zAxisIn = options.tAxisMapping->getValue()[2];;
00077 if (!options.tAxisMapping->isOnInitialValue()) {
00078 Vicon::Output_SetAxisMapping setAxisOutput =
00079 ViconBridge::Instance().getClient().SetAxisMapping(xAxisIn.value(), yAxisIn.value(), zAxisIn.value());
00080
00081 ROS_ASSERT_MSG(setAxisOutput.Result == Vicon::Result::Success,
00082 "Unable to set Axis(setAxisOutput.Result): %s",
00083 (*ViconResult::get_by_value(setAxisOutput.Result)).str());
00084
00085
00086 }
00087
00088
00089 Vicon::Output_GetAxisMapping axisMapping = ViconBridge::Instance().getClient().GetAxisMapping();
00090 ViconDirection xAxisOut(axisMapping.XAxis);
00091 ViconDirection yAxisOut(axisMapping.YAxis);
00092 ViconDirection zAxisOut(axisMapping.ZAxis);
00093
00094
00095
00096
00097
00098
00099
00100
00101
00102 ROS_INFO_STREAM("AxisMapping X: " << xAxisOut.str() << " Y: " << yAxisOut.str() << " Z: " << zAxisOut.str());
00103
00104
00105
00106 ROS_INFO_STREAM("Setting up SteamMode: " << options.tStreamMode->getValue().str() << "("
00107 << options.tStreamMode->getValue().value() << ")");
00108 ViconBridge::Instance().getClient().SetStreamMode(options.tStreamMode->getValue().value());
00109 }
00110
00111 void ViconTracker::spin()
00112 {
00113
00114 unsigned int lastFrameNumber = 0;
00115 unsigned int currentFrameNumber = 0;
00116 int frameDiff = 1;
00117
00118 while(ros::ok()) {
00119
00120 if (ViconBridge::Instance().getClient().GetFrame().Result != Vicon::Result::Success) {
00121 ROS_WARN("GetFrame().Result != Vicon::Result::Success");
00122 ros::Duration(1.0/240.0).sleep();
00123 }
00124 currentFrameNumber = ViconBridge::Instance().getClient().GetFrameNumber().FrameNumber;
00125
00126 if (lastFrameNumber != 0) {
00127 frameDiff = currentFrameNumber - lastFrameNumber;
00128
00129 totalFrameCount += frameDiff;
00130 } else {
00131
00132 frameDiff = 1;
00133
00134
00135 int nSubjects = ViconBridge::Instance().getClient().GetSubjectCount().SubjectCount;
00136
00137 if (nSubjects == 0) {
00138 ROS_ERROR("No subjects in Scene. Quitting...");
00139 ros::shutdown();
00140 return;
00141 }
00142 subjects.resize(nSubjects);
00143 for (int i = 0; i < nSubjects; ++i) {
00144 subjects[i] = ViconSubject(i);
00145 subjects[i].init();
00146 }
00147 }
00148
00149
00150 if (frameDiff != 0) {
00151
00152
00153 if (frameDiff > 1) {
00154 totalDroppedFrameCount += (frameDiff - 1);
00155
00156
00157 ROS_WARN_STREAM(frameDiff << " more (total "
00158 << totalDroppedFrameCount << "/" << totalFrameCount << ", "
00159 << ((double)totalDroppedFrameCount / totalFrameCount) * 100 <<
00160 "%) frame(s) dropped. Consider adjusting rates.");
00161 }
00162
00163
00164 for (unsigned int i = 0; i < subjects.size(); ++i) {
00165 subjects.at(i).process();
00166 }
00167
00168
00169 lastFrameNumber = currentFrameNumber;
00170 } else {
00171
00172 ROS_WARN_STREAM("No new Frame. FrameDiff == 0!");
00173 }
00174
00175
00176 usleep(100);
00177 }
00178 }
00179
00180
00181 }
00182