Tekkotsu Homepage
Demos
Overview
Downloads
Dev. Resources
Reference
Credits

SensorStateAccessor.cc

Go to the documentation of this file.
00001 #include "SensorStateAccessor.h"
00002 #include "Shared/plist.h"
00003 #include "Shared/RobotInfo.h"
00004 #include <sstream>
00005 #include <string>
00006 
00007 using namespace std; 
00008 using namespace plist;
00009 
00010 static const string BUTTON_PREFIX = "WorldState.Buttons.";
00011 static const string SENSOR_PREFIX = "WorldState.Sensors.";
00012 
00013 bool SensorStateAccessor::set(const std::string& key, const std::string& value) const {
00014   float val;
00015   if(!(stringstream(value) >> val)) {
00016     cerr << "Bad value '" << value << "', need numeric" << endl;
00017     return false;
00018   }
00019   
00020   if(key.substr(0,BUTTON_PREFIX.size())==BUTTON_PREFIX) {
00021     string entry = key.substr(BUTTON_PREFIX.size());
00022     for(unsigned int i=0; i<NumButtons; ++i) {
00023       if(entry==buttonNames[i]) {
00024         setButtonValue(i, val);
00025         return true;
00026       }
00027     }
00028     unsigned int i;
00029     if(stringstream(entry) >> i) {
00030       setButtonValue(i, val);
00031       return true;
00032     }
00033     cerr << "Unknown button '" << entry << "', could not set" << endl;
00034     return false;
00035     
00036   } else if(key.substr(0,SENSOR_PREFIX.size())==SENSOR_PREFIX) {
00037     string entry = key.substr(SENSOR_PREFIX.size());
00038     for(unsigned int i=0; i<NumSensors; ++i) {
00039       if(entry==sensorNames[i]) {
00040         setSensorValue(i, val);
00041         return true;
00042       }
00043     }
00044     unsigned int i;
00045     if(stringstream(entry) >> i) {
00046       setSensorValue(i, val);
00047       return true;
00048     }
00049     cerr << "Unknown sensor '" << entry << "', could not set" << endl;
00050     return false;
00051     
00052   } else {
00053     cerr << "Unknown WorldState entry '" << key << "'" << endl;
00054     return false;
00055     
00056   }
00057 }
00058 
00059 bool SensorStateAccessor::print(const std::string& key) const {
00060   bool printButtons=true, printSensors=true;
00061   
00062   if(key.substr(0,BUTTON_PREFIX.size()-1)==BUTTON_PREFIX.substr(0,BUTTON_PREFIX.size()-1)) {
00063     if(key.size()<=BUTTON_PREFIX.size()) {
00064       printSensors=false;
00065     } else {
00066       string entry = key.substr(BUTTON_PREFIX.size());
00067       for(unsigned int i=0; i<NumButtons; ++i) {
00068         if(entry==buttonNames[i]) {
00069           cout << getButtonValue(i) << endl;
00070           return true;
00071         }
00072       }
00073       unsigned int i;
00074       if(stringstream(entry) >> i) {
00075         cout << getButtonValue(i) << endl;
00076         return true;
00077       }
00078       cerr << "Unknown button '" << entry << "'" << endl;
00079       return false;
00080     }
00081     
00082   } else if(key.substr(0,SENSOR_PREFIX.size()-1)==SENSOR_PREFIX.substr(0,SENSOR_PREFIX.size()-1)) {
00083     if(key.size()<=SENSOR_PREFIX.size()) {
00084       printButtons=false;
00085     } else {
00086       string entry = key.substr(SENSOR_PREFIX.size());
00087       for(unsigned int i=0; i<NumSensors; ++i) {
00088         if(entry==sensorNames[i]) {
00089           cout << getSensorValue(i) << endl;
00090           return true;
00091         }
00092       }
00093       unsigned int i;
00094       if(stringstream(entry) >> i) {
00095         cout << getSensorValue(i) << endl;
00096         return true;
00097       }
00098       cerr << "Unknown sensor '" << entry << "', could not set" << endl;
00099       return false;
00100     }
00101   }
00102   
00103   if(printButtons) {
00104     Dictionary b;
00105     for(unsigned int i=0; i<NumButtons; ++i) {
00106       stringstream ss;
00107       ss << setw(2) << i << '/' << buttonNames[i];
00108       b.addValue(ss.str(), getButtonValue(i));
00109     }
00110     Dictionary r;
00111     r.addEntry("Buttons", b);
00112     if(printSensors)
00113       plist::filteredDisplay(cout,r,"^[^.].*",REG_EXTENDED,3);
00114     else
00115       plist::filteredDisplay(cout,b,"^[^.].*",REG_EXTENDED,3);
00116   }
00117   if(printButtons && printSensors)
00118     cout << '\n';
00119   if(printSensors) {
00120     Dictionary s;
00121     for(unsigned int i=0; i<NumSensors; ++i) {
00122       stringstream ss;
00123       ss << setw(2) << i << '/' << sensorNames[i];
00124       s.addValue(ss.str(), getSensorValue(i));
00125     }
00126     Dictionary r;
00127     r.addEntry("Sensors", s);
00128     if(printButtons)
00129       plist::filteredDisplay(cout,r,"^[^.].*",REG_EXTENDED,3);
00130     else
00131       plist::filteredDisplay(cout,s,"^[^.].*",REG_EXTENDED,3);
00132   }
00133   cout << flush;
00134   return true;
00135 }
00136 
00137 
00138 /*! @file
00139  * @brief Implements SensorStateAccessor, not a real DataSource, but an interface for the HAL command line to support set/print of values
00140  * @author Ethan Tira-Thompson (ejt) (Creator)
00141  */

Tekkotsu Hardware Abstraction Layer 5.1CVS
Generated Mon May 9 05:01:39 2016 by Doxygen 1.6.3