PListSensorDriver.cc
Go to the documentation of this file.00001 #include "PListSensorDriver.h"
00002 #include "Shared/DynamicRobotState.h"
00003 #include "Shared/RobotInfo.h"
00004 #include "Events/MoCapEvent.h"
00005 #include "Events/EventRouter.h"
00006
00007 using namespace std;
00008
00009 const std::string PListSensorDriver::autoRegisterDriver = DeviceDriver::getRegistry().registerType<PListSensorDriver>("PListSensor");
00010
00011 void PListSensorDriver::deregisterSource() {
00012 DataStreamDriver::deregisterSource();
00013 for(unsigned int i=0; i<NumOutputs; ++i) {
00014 if(providing[i])
00015 ignoringOutput(i);
00016 providing[i] = false;
00017 }
00018 }
00019
00020 bool PListSensorDriver::readData(std::istream& is) {
00021 DynamicRobotState drs;
00022 if(drs.loadStream(is,true)==0) {
00023 testCancel();
00024
00025 return false;
00026 }
00027 if(!paceInput) {
00028 char firstByte;
00029 while(is.readsome(&firstByte,1)>0 && is) {
00030 is.putback(firstByte);
00031 if(drs.loadStream(is,true)==0) {
00032 testCancel();
00033
00034 return false;
00035 }
00036 }
00037 }
00038 timestamp = get_time();
00039
00040 {
00041 MarkScope l(getSensorWriteLock());
00042 typedef plist::DictionaryOf<plist::Primitive<float> >::const_iterator drs_iter;
00043
00044
00045 for(drs_iter it=drs.outputs.begin(); it!=drs.outputs.end(); ++it) {
00046 unsigned int offset = capabilities.findOutputOffset(it->first);
00047 if(offset>=NumOutputs)
00048 continue;
00049 if(!providing[offset]) {
00050 providing[offset] = true;
00051 providingOutput(offset);
00052 }
00053 setOutputValue(offset,*it->second);
00054 }
00055
00056
00057 for(drs_iter it=drs.buttons.begin(); it!=drs.buttons.end(); ++it) {
00058 unsigned int offset = capabilities.findButtonOffset(it->first);
00059 if(offset>=NumButtons)
00060 continue;
00061 setButtonValue(offset, *it->second);
00062 }
00063
00064
00065 for(drs_iter it=drs.sensors.begin(); it!=drs.sensors.end(); ++it) {
00066 unsigned int offset = capabilities.findSensorOffset(it->first);
00067 if(offset>=NumSensors)
00068 continue;
00069 setSensorValue(offset, *it->second);
00070 }
00071
00072
00073 for(drs_iter it=drs.torques.begin(); it!=drs.torques.end(); ++it) {
00074 unsigned int offset = capabilities.findOutputOffset(it->first);
00075 if(offset>=NumOutputs)
00076 continue;
00077 setPIDDutyValue(offset, *it->second);
00078 }
00079 }
00080
00081 if(drs.framePositions.size()>0 || drs.frameOrientations.size()>0) {
00082 MoCapEvent * mce = new MoCapEvent(instanceName,reinterpret_cast<size_t>(this));
00083 for(plist::DictionaryOf<plist::Point>::const_iterator it=drs.framePositions.begin(); it!=drs.framePositions.end(); ++it) {
00084 unsigned int idx = capabilities.findFrameOffset(it->first);
00085 if(idx!=-1U)
00086 mce->positions.insert(std::pair<unsigned int, fmat::Column<3> >(idx,it->second->exportTo<fmat::Column<3> >()));
00087 }
00088 for(plist::DictionaryOf<plist::Point>::const_iterator it=drs.frameOrientations.begin(); it!=drs.frameOrientations.end(); ++it) {
00089 unsigned int idx = capabilities.findFrameOffset(it->first);
00090 if(idx!=-1U)
00091 mce->orientations.insert(std::pair<unsigned int, fmat::Quaternion>(idx,fmat::Quaternion::fromAxis(*it->second)));
00092 }
00093 erouter->requeueEvent(mce);
00094 }
00095
00096 return true;
00097 }
00098
00099
00100
00101
00102
00103