00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00026 #include <urdf/model.h>
00027 #include "ros/ros.h"
00028 #include <ros/package.h>
00029 #include "udev_control.h"
00030 #include <libxml/encoding.h>
00031 #include <libxml/xmlwriter.h>
00032 #include <libxml/tree.h>
00033 #include <libxml/parser.h>
00034 #include <libxml/xpath.h>
00035 #include <libxml/xpathInternals.h>
00036 #include <libxml/xmlreader.h>
00037
00038
00039 using namespace ros;
00040 using namespace std;
00041 using namespace boost;
00042
00044 vector<shared_ptr<class_udev_control> > devices;
00045
00047 NodeHandle*nh;
00048
00057 void AddCallback(string action,string node,void*data)
00058 {
00059 string*id=(string*)data;
00060 nh->setParam("/device_mapper/"+*id,node);
00061 cout<<"Added device id: "<<*id<<" in node "<<node<<" action: "<<action<<endl;
00062 }
00063
00072 void RemoveCallback(string action,string node,void*data)
00073 {
00074 string*id=(string*)data;
00075 nh->setParam("/device_mapper/"+*id,"Not found");
00076 cout<<"Removed device id: "<<*id<<" in node "<<node<<" action: "<<action<<endl;
00077 }
00078
00086 void GetName(xmlTextReaderPtr reader,string& str)
00087 {
00088 xmlChar*name=xmlTextReaderName(reader);
00089 if(!name)return;
00090 str=(char*)name;
00091 xmlFree(name);
00092 }
00093
00101 void GetValue(xmlTextReaderPtr reader,string& str)
00102 {
00103 xmlChar*value=xmlTextReaderValue(reader);
00104 if(!value)return;
00105 str=(char*)value;
00106 xmlFree(value);
00107 }
00108
00117 void GetAttribute(xmlTextReaderPtr reader,const xmlChar*name,string& str)
00118 {
00119 xmlChar*attribute=xmlTextReaderGetAttribute(reader,name);
00120 if(!attribute)return;
00121 str=(char*)attribute;
00122 xmlFree(attribute);
00123 }
00124
00135 void NodeHandler(xmlTextReaderPtr reader)
00136 {
00137 string name,value;
00138 string dev_name;
00139 GetName(reader,name);
00140
00141 to_upper(name);
00142
00143 if(name=="SENSOR" && xmlTextReaderNodeType(reader)==1)
00144 {
00145 GetAttribute(reader,BAD_CAST "name",dev_name);
00146
00147 shared_ptr<class_udev_control> dev(new class_udev_control(dev_name));
00148
00149 dev->RegistryAction("add",AddCallback,&dev->id);
00150 dev->RegistryAction("remove",RemoveCallback,&dev->id);
00151
00152 devices.push_back(dev);
00153 }
00154
00155
00156 if(name=="DEVICE_INFORMATION" && xmlTextReaderNodeType(reader)==1)
00157 {
00158
00159
00160 int ret = xmlTextReaderRead(reader);
00161 do
00162 {
00163 ret=xmlTextReaderRead(reader);
00164 GetName(reader,name);
00165 to_upper(name);
00166
00167 if(xmlTextReaderNodeType(reader)==1)
00168 {
00169 ret=xmlTextReaderRead(reader);
00170
00171 GetValue(reader,value);
00172
00173 shared_ptr<class_udev_control> dev = devices.back();
00174
00175 if(name=="SUBSYSTEM")
00176 dev->AddSubsystem(value);
00177 else
00178 dev->AddProperty(name,value);
00179 }
00180
00181 if(name=="DEVICE_INFORMATION" && xmlTextReaderNodeType(reader)==15)
00182 break;
00183
00184 }while(ret==1);
00185 }
00186 }
00187
00188 int main(int argc, char** argv)
00189 {
00190 std::string urdf_file;
00191
00192
00193 std::string urdf_file_default = ros::package::getPath("atlascar_base") + "/urdf/atlascar_urdf_3dsmodels.xml";
00194
00195
00196 init(argc,argv,"device_mapper");
00197
00198 nh=new NodeHandle("~");
00199 nh->param("robot_urdf_path",urdf_file,urdf_file_default);
00200
00201 int ret;
00202 xmlTextReaderPtr reader = xmlNewTextReaderFilename(urdf_file.c_str());
00203 if(reader!=NULL)
00204 {
00205 do
00206 {
00207 ret=xmlTextReaderRead(reader);
00208 NodeHandler(reader);
00209 }while(ret==1);
00210
00211 xmlFreeTextReader(reader);
00212 if (ret != 0)
00213 {
00214 ROS_ERROR("Failed to parse file");
00215 return -1;
00216 }
00217 }else
00218 {
00219 ROS_ERROR("Failed to parse file");
00220 return -1;
00221 }
00222
00223 for(uint i=0;i<devices.size();i++)
00224 {
00225 devices[i]->SetUpMonitoring();
00226 devices[i]->EnumerateDevices();
00227
00228
00229
00230 nh->setParam("/device_mapper/"+devices[i]->GetId(),devices[i]->GetPath());
00231
00232 if (devices[i]->GetPath()=="Not found")
00233 {
00234 ROS_ERROR("Device Mapper Error: device %s was not found. Is the equipment connected?", (devices[i]->GetId()).c_str());
00235 }
00236 }
00237
00238 Rate r(100);
00239 while(ok())
00240 {
00241 spinOnce();
00242 r.sleep();
00243
00244 for(uint i=0;i<devices.size();i++)
00245 devices[i]->Monitoring();
00246 }
00247
00248 return 0;
00249 }