device_mapper.cpp
Go to the documentation of this file.
1 // This file is part of the tcp/ip client library.
2 //
3 // Copyright (C) 2011 LAR - Laboratory for Automation and Robotics, ATLAS Project
4 // Department of Mechanical Engineering
5 // University of Aveiro
6 //
7 // This program is free software; you can redistribute it and/or
8 // modify it under the terms of the GNU General Public License
9 // as published by the Free Software Foundation; either version 2.1
10 // of the License, or (at your option) any later version.
11 //
12 // This program is distributed in the hope that it will be useful,
13 // but WITHOUT ANY WARRANTY; without even the implied warranty of
14 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 // GNU General Public License for more details.
16 //
17 // You should have received a copy of the GNU General Public License
18 // along with this program; if not, write to the Free Software
19 // Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
20 // 02110-1301, USA.
21 
26 #include <urdf/model.h>
27 #include "ros/ros.h"
28 #include <ros/package.h>
29 #include "udev_control.h"
30 #include <libxml/encoding.h>
31 #include <libxml/xmlwriter.h>
32 #include <libxml/tree.h>
33 #include <libxml/parser.h>
34 #include <libxml/xpath.h>
35 #include <libxml/xpathInternals.h>
36 #include <libxml/xmlreader.h>
37 
38 
39 using namespace ros;
40 using namespace std;
41 using namespace boost;
42 
44 vector<shared_ptr<class_udev_control> > devices;
45 
47 NodeHandle*nh;
48 
57 void AddCallback(string action,string node,void*data)
58 {
59  string*id=(string*)data;//device id
60  nh->setParam("/device_mapper/"+*id,node);
61  cout<<"Added device id: "<<*id<<" in node "<<node<<" action: "<<action<<endl;
62 }
63 
72 void RemoveCallback(string action,string node,void*data)
73 {
74  string*id=(string*)data;//device id
75  nh->setParam("/device_mapper/"+*id,"Not found");
76  cout<<"Removed device id: "<<*id<<" in node "<<node<<" action: "<<action<<endl;
77 }
78 
86 void GetName(xmlTextReaderPtr reader,string& str)
87 {
88  xmlChar*name=xmlTextReaderName(reader);
89  if(!name)return;
90  str=(char*)name;
91  xmlFree(name);
92 }
93 
101 void GetValue(xmlTextReaderPtr reader,string& str)
102 {
103  xmlChar*value=xmlTextReaderValue(reader);
104  if(!value)return;
105  str=(char*)value;
106  xmlFree(value);
107 }
108 
117 void GetAttribute(xmlTextReaderPtr reader,const xmlChar*name,string& str)
118 {
119  xmlChar*attribute=xmlTextReaderGetAttribute(reader,name);
120  if(!attribute)return;
121  str=(char*)attribute;
122  xmlFree(attribute);
123 }
124 
135 void NodeHandler(xmlTextReaderPtr reader)
136 {
137  string name,value;
138  string dev_name;
139  GetName(reader,name);
140 
141  to_upper(name);
142 
143  if(name=="SENSOR" && xmlTextReaderNodeType(reader)==1)
144  {
145  GetAttribute(reader,BAD_CAST "name",dev_name);
146 
147  shared_ptr<class_udev_control> dev(new class_udev_control(dev_name));
148 
149  dev->RegistryAction("add",AddCallback,&dev->id);
150  dev->RegistryAction("remove",RemoveCallback,&dev->id);
151 
152  devices.push_back(dev);
153  }
154 
155 
156  if(name=="DEVICE_INFORMATION" && xmlTextReaderNodeType(reader)==1)
157  {
158  //from here to the end of this element we must read all names and values and add them as pairs
159 
160  int ret = xmlTextReaderRead(reader);
161  do
162  {
163  ret=xmlTextReaderRead(reader);
164  GetName(reader,name);
165  to_upper(name);//boost function
166 
167  if(xmlTextReaderNodeType(reader)==1)
168  {
169  ret=xmlTextReaderRead(reader);//jump to the node that as the value of the past name
170 
171  GetValue(reader,value);
172 
173  shared_ptr<class_udev_control> dev = devices.back();
174 
175  if(name=="SUBSYSTEM")
176  dev->AddSubsystem(value);
177  else
178  dev->AddProperty(name,value);
179  }
180 
181  if(name=="DEVICE_INFORMATION" && xmlTextReaderNodeType(reader)==15)
182  break;//this signals that we sould stop addin stuff to the last device
183 
184  }while(ret==1);//if the cicle exits by ret!=1 there was an error
185  }
186 }
187 
188 int main(int argc, char** argv)
189 {
190  std::string urdf_file;
191 
192  //for now have a hard coded link to the atlascar urdf file, we could do this by the command line arguments
193  std::string urdf_file_default = ros::package::getPath("atlascar_base") + "/urdf/atlascar_urdf_3dsmodels.xml";
194 
195  // Initialize ROS
196  init(argc,argv,"device_mapper");
197 
198  nh=new NodeHandle("~");
199  nh->param("robot_urdf_path",urdf_file,urdf_file_default);
200 
201  int ret;
202  xmlTextReaderPtr reader = xmlNewTextReaderFilename(urdf_file.c_str());
203  if(reader!=NULL)
204  {
205  do
206  {
207  ret=xmlTextReaderRead(reader);//this function returns 1 if ok, -1 on error and 0 if there is no more nodes to read
208  NodeHandler(reader);
209  }while(ret==1);
210 
211  xmlFreeTextReader(reader);
212  if (ret != 0)//zero means the end of the file
213  {
214  ROS_ERROR("Failed to parse file");
215  return -1;
216  }
217  }else
218  {
219  ROS_ERROR("Failed to parse file");
220  return -1;
221  }
222 
223  for(uint i=0;i<devices.size();i++)
224  {
225  devices[i]->SetUpMonitoring();
226  devices[i]->EnumerateDevices();
227 
228  //more than one path can exist for this ruleset, the function devices[i].size() gives you the amount, and you can access
229  //them via devices[i]->GetPath(c), to the the c path, this is zero based
230  nh->setParam("/device_mapper/"+devices[i]->GetId(),devices[i]->GetPath());
231 
232  if (devices[i]->GetPath()=="Not found")
233  {
234  ROS_ERROR("Device Mapper Error: device %s was not found. Is the equipment connected?", (devices[i]->GetId()).c_str());
235  }
236  }
237 
238  Rate r(100);
239  while(ok())
240  {
241  spinOnce();
242  r.sleep();
243 
244  for(uint i=0;i<devices.size();i++)
245  devices[i]->Monitoring();
246  }
247 
248  return 0;
249 }
NodeHandle * nh
Local node handler.
void GetName(xmlTextReaderPtr reader, string &str)
Get the name of a xml element.
void AddCallback(string action, string node, void *data)
Device add callback.
Class that simplifies the implementation of the udev library.
int main(int argc, char **argv)
void GetValue(xmlTextReaderPtr reader, string &str)
Get the value of a xml element.
void NodeHandler(xmlTextReaderPtr reader)
Handler of xml nodes, heavy duty function.
void GetAttribute(xmlTextReaderPtr reader, const xmlChar *name, string &str)
Get the value of an attribute in a xml element.
void RemoveCallback(string action, string node, void *data)
Device remove callback.
vector< shared_ptr< class_udev_control > > devices
Vector of the connected devices udev classes.
This class simplifies the implementation of the udev library.
Definition: udev_control.h:53


device_mapper
Author(s): Jorge Almeida
autogenerated on Mon Mar 2 2015 01:31:36