Tekkotsu Homepage
Demos
Overview
Downloads
Dev. Resources
Reference
Credits

MoCapLogger.cc

Go to the documentation of this file.
00001 #include "MoCapLogger.h"
00002 
00003 REGISTER_CONTROL(MoCapLogger,"Status Reports");
00004 
00005 void MoCapLogger::rename(ControlBase* c, std::ostream& sstream) {
00006   std::stringstream& ss = dynamic_cast<std::stringstream&>(sstream);
00007   c->setName(ss.str());
00008   ss.str("");
00009 }
00010 
00011 /*! This section demonstrates the getPosition/getOrientation interface
00012  *  (with exception handling in case frame is not available in current update)
00013  *  See dump() below for iteration through all provided frames. */
00014 void MoCapLogger::gotMoCapGUI(const MoCapEvent& mce) {
00015   try {
00016     fmat::Column<3> p = mce.getPosition(BaseFrameOffset);
00017     std::stringstream ss;
00018     ss.precision(1);
00019     ss.setf(std::ios::fixed);
00020     rename(x, ss << "X: " << p[0]);
00021     rename(y, ss << "Y: " << p[1]);
00022     rename(z, ss << "Z: " << p[2]);
00023     try {
00024       fmat::Quaternion q = mce.getOrientation(BaseFrameOffset);
00025       if(rotAxis->getStatus()) {
00026         ss.precision(3);
00027         rename(r0, ss << "X: " << q.getX());
00028         rename(r1, ss << "Y: " << q.getY());
00029         rename(r2, ss << "Z: " << q.getZ());
00030       } else {
00031         fmat::Column<3> ypr = q.ypr() * 180/(fmat::fmatReal)M_PI;
00032         rename(r0, ss << "Heading: " << ypr[0] << "°");
00033         rename(r1, ss << "Pitch: " << ypr[1] << "°");
00034         rename(r2, ss << "Roll: " << ypr[2] << "°");
00035       }
00036     } catch(const std::exception&) {
00037       r0->setName("");
00038       r1->setName("");
00039       r2->setName("");
00040     }
00041   } catch(const std::exception&) {
00042     x->setName("X: No BaseFrame");
00043     y->setName("Y: No BaseFrame");
00044     z->setName("Z: No BaseFrame");
00045   }
00046   if(lastRefresh.Age().Value()>=0.5)
00047      refresh();
00048 }
00049 
00050 void MoCapLogger::gotMoCapConsole(const MoCapEvent& mce) {
00051   dump(std::cout,mce);
00052 }
00053 
00054 void MoCapLogger::gotMoCapFile(const MoCapEvent& mce) {
00055   dump(file,mce);
00056 }
00057 
00058 void MoCapLogger::gotTxtMsgSingle(const TextMsgEvent& txt) {
00059   if(txt.getText()=="mocap") {
00060     erouter->addListener(&mocapSingle, EventBase::mocapEGID);
00061     erouter->addTimer(&mocapSingle, 0, 2000, false);
00062   }
00063 }
00064 
00065 void MoCapLogger::gotMoCapSingle(const EventBase& event) {
00066   std::cout << '\n'; // probably has the prompt displayed, start a new line
00067   if(const MoCapEvent* mce = dynamic_cast<const MoCapEvent*>(&event)) {
00068     dump(std::cout,*mce);
00069     erouter->remove(&mocapSingle);
00070   } else {
00071     std::cerr << "MoCapLogger has not received a MoCapEvent yet, will continue to wait... (check driver configuration?)" << std::endl;
00072   }
00073 }
00074 
00075 /*! Only logs frame which have a position component, checks to see if they happen to also have an orientation */
00076 void MoCapLogger::dump(std::ostream& os, const MoCapEvent& mce) {
00077   for(MoCapEvent::position_iterator it=mce.positions.begin(); it!=mce.positions.end(); ++it) {
00078     os << mce.getTimeStamp() << '\t' << outputNames[it->first] << '\t' << it->second[0] << '\t' << it->second[1] << '\t' << it->second[2];
00079     MoCapEvent::orientation_iterator oit = mce.orientations.find(it->first);
00080     if(oit!=mce.orientations.end()) {
00081       if(rotAxis->getStatus()) {
00082         os << '\t' << oit->second.getX() << '\t' << oit->second.getY() << '\t' << oit->second.getZ();
00083       } else {
00084         fmat::Column<3> ypr = oit->second.ypr() * 180/(fmat::fmatReal)M_PI;
00085         os << '\t' << ypr[0] << '\t' << ypr[1] << '\t' << ypr[2];
00086       }
00087     }
00088     os << '\n';
00089   }
00090 }
00091 
00092 void MoCapLogger::refresh() {
00093   if(mocapGUI.get()==NULL) {
00094     mocapGUI.reset(new EventCallbackAs<MoCapEvent>(&MoCapLogger::gotMoCapGUI, *this));
00095     erouter->addListener(mocapGUI.get(),EventBase::mocapEGID);
00096   } else {
00097     lastRefresh.Set();
00098   }
00099   
00100   // has console log status changed?
00101   if(consoleLog->getStatus()) {
00102     if(mocapConsole.get()==NULL) {
00103       mocapConsole.reset(new EventCallbackAs<MoCapEvent>(&MoCapLogger::gotMoCapConsole, *this));
00104       erouter->addListener(mocapConsole.get(),EventBase::mocapEGID);
00105     }
00106   } else {
00107     mocapConsole.reset();
00108   }
00109   
00110   // has file log status changed? (ignore is triggered on entry to fileLog, this is on return)
00111   if(fileLog->getLastInput().size()>0) {
00112     file.open(fileLog->getLastInput().c_str());
00113     if(file) {
00114       fileLog->setName("Logging to " + fileLog->getLastInput());
00115       mocapFile.reset(new EventCallbackAs<MoCapEvent>(&MoCapLogger::gotMoCapFile, *this));
00116       erouter->addListener(mocapFile.get(),EventBase::mocapEGID);
00117     } else {
00118       std::cerr << "Could not open " << fileLog->getLastInput() << std::endl;
00119     }
00120     fileLog->clearLastInput();
00121   }
00122   ControlBase::refresh();
00123 }
00124 
00125 void MoCapLogger::deactivate() {
00126   mocapGUI.reset();
00127   ControlBase::deactivate();
00128 }
00129 
00130 ControlBase * MoCapLogger::doSelect() {
00131   ControlBase * c = ControlBase::doSelect();
00132   if(c==fileLog) {
00133     file.close();
00134     mocapFile.reset();
00135     fileLog->setName("Log To File");
00136   }
00137   if(c!=this)
00138     mocapGUI.reset(); // don't refresh display from background when child active
00139   return c;
00140 }
00141 
00142 
00143 /*! @file
00144  * @brief Implements MoCapLogger, which provides display and logging of mocap data
00145  * @author ejt (Creator)
00146  */

Tekkotsu v5.1CVS
Generated Mon May 9 04:58:45 2016 by Doxygen 1.6.3