ViconTracker.cpp
Go to the documentation of this file.
00001 /*
00002  * ViconBridge.cpp
00003  *
00004  *  Created on: Oct 17, 2011
00005  *      Author: mriedel
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 // disconnect is done by singleton
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                 // 1sec
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     // Tracker Version
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                 // enable SegmentData
00050                 ROS_INFO_STREAM("Enabling SegmentData");
00051                 ViconBridge::Instance().getClient().EnableSegmentData();
00052                 ROS_ASSERT(ViconBridge::Instance().getClient().IsSegmentDataEnabled().Enabled);
00053         } else {
00054                 // disable SegmentData
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                 // enable MarkerData
00062                 ROS_INFO_STREAM("Enabling MarkerData");
00063                 ViconBridge::Instance().getClient().EnableMarkerData();
00064                 ROS_ASSERT(ViconBridge::Instance().getClient().IsMarkerDataEnabled().Enabled);
00065         } else {
00066                 // disable MarkerData
00067                 ROS_INFO_STREAM("Disabling MarkerData");
00068                 ViconBridge::Instance().getClient().DisableMarkerData();
00069                 ROS_ASSERT(!ViconBridge::Instance().getClient().IsMarkerDataEnabled().Enabled);
00070         }
00071 
00072 
00073         // Set Axis Mapping
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         // Get Axis Mapping
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 //      // ASSERT
00095 //      if (!options.tAxisMapping->isOnInitialValue()) {
00096 //              ROS_ASSERT(xAxisIn == xAxisOut);
00097 //              ROS_ASSERT(yAxisIn == yAxisOut);
00098 //              ROS_ASSERT(zAxisIn == zAxisOut);
00099 //      }
00100 
00101         // Print
00102     ROS_INFO_STREAM("AxisMapping X: " << xAxisOut.str() << " Y: " << yAxisOut.str() << " Z: " << zAxisOut.str());
00103 
00104 
00105     // Stream Mode
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         // loop variables
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                         //std::cout << frameDiff << " ";
00129                         totalFrameCount += frameDiff;
00130                 } else {
00131                         // frameDiff = 1; already set
00132                         frameDiff = 1;
00133                         // sleep a little bit
00134                         // Initial condition - Fill subject array.
00135                     int nSubjects = ViconBridge::Instance().getClient().GetSubjectCount().SubjectCount;
00136                     //std::cout << "Found " << nSubjects << " in Scene." << std::endl;
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                 //std::cout << "Framediff: " << frameDiff << std::endl;
00150                 if (frameDiff != 0) {
00151 
00152                         // dropped frames?
00153                         if (frameDiff > 1) {
00154                                 totalDroppedFrameCount += (frameDiff - 1);
00155 
00156                                 // div by 0 cannot happen here.
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                         // process Segments
00164                         for (unsigned int i = 0; i < subjects.size(); ++i) {
00165                                 subjects.at(i).process();
00166                         }
00167 
00168                         // set new lastFrameNumber
00169                         lastFrameNumber = currentFrameNumber;
00170                 } else {
00171                         // no new frame
00172                         ROS_WARN_STREAM("No new Frame. FrameDiff == 0!");
00173                 }
00174 
00175                 // sleep a little
00176                 usleep(100);
00177         }
00178 }
00179 
00180 
00181 } // namepsace
00182 
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Friends


telekyb_vicon
Author(s): Dr. Antonio Franchi
autogenerated on Wed Apr 24 2013 11:29:26