ros_chain.h
Go to the documentation of this file.
1 #ifndef H_CANOPEN_ROS_CHAIN
2 #define H_CANOPEN_ROS_CHAIN
3 
6 #include <canopen_chain_node/GetObject.h>
7 #include <canopen_chain_node/SetObject.h>
9 #include <ros/ros.h>
10 #include <std_srvs/Trigger.h>
12 #include <boost/filesystem/path.hpp>
13 #include <boost/weak_ptr.hpp>
14 #include <boost/scoped_ptr.hpp>
16 
17 namespace canopen{
18 
20 public:
21  typedef boost::function<void()> func_type ROS_DEPRECATED;
22  typedef boost::function<void()> FuncType;
23 
24  static FuncType create(ros::NodeHandle &nh, const std::string &name, canopen::NodeSharedPtr node, const std::string &key, bool force);
25 private:
26  template <typename Tpub, typename Tobj, bool forced> static void publish(ros::Publisher &pub, ObjectStorage::Entry<Tobj> &entry){
27  Tpub msg;
28  msg.data = (const typename Tpub::_data_type &)(forced? entry.get() : entry.get_cached());
29  pub.publish(msg);
30  }
31  template<typename Tpub, typename Tobj> static FuncType create(ros::NodeHandle &nh, const std::string &name, ObjectStorage::Entry<Tobj> entry, bool force){
32  if(!entry.valid()) return 0;
33  ros::Publisher pub = nh.advertise<Tpub>(name, 1);
34  if(force){
35  return boost::bind(PublishFunc::publish<Tpub, Tobj, true>, pub, entry);
36  }else{
37  return boost::bind(PublishFunc::publish<Tpub, Tobj, false>, pub, entry);
38  }
39  }
40 };
41 
43  MergedXmlRpcStruct(const XmlRpc::XmlRpcValue& a) :XmlRpc::XmlRpcValue(a){ assertStruct(); }
44 public:
45  MergedXmlRpcStruct(){ assertStruct(); }
46  MergedXmlRpcStruct(const XmlRpc::XmlRpcValue& a, const MergedXmlRpcStruct &b, bool recursive= true) :XmlRpc::XmlRpcValue(a){
47  assertStruct();
48 
49  for(ValueStruct::const_iterator it = b._value.asStruct->begin(); it != b._value.asStruct->end(); ++it){
50  std::pair<XmlRpc::XmlRpcValue::iterator,bool> res = _value.asStruct->insert(*it);
51 
52  if(recursive && !res.second && res.first->second.getType() == XmlRpc::XmlRpcValue::TypeStruct && it->second.getType() == XmlRpc::XmlRpcValue::TypeStruct){
53  res.first->second = MergedXmlRpcStruct(res.first->second, it->second); // recursive struct merge with implicit cast
54  }
55  }
56 
57 
58  }
59 };
60 
61 class Logger: public DiagGroup<canopen::Layer>{
63 
64  std::vector<boost::function< void (diagnostic_updater::DiagnosticStatusWrapper &)> > entries_;
65 
66  static void log_entry(diagnostic_updater::DiagnosticStatusWrapper &stat, uint8_t level, const std::string &name, boost::function<std::string()> getter){
67  if(stat.level >= level){
68  try{
69  stat.add(name, getter());
70  }catch(...){
71  stat.add(name, "<ERROR>");
72  }
73  }
74  }
75 
76 public:
77  Logger(canopen::NodeSharedPtr node): node_(node) { add(node_); }
78 
79  bool add(uint8_t level, const std::string &key, bool forced){
80  try{
81  ObjectDict::Key k(key);
82  const ObjectDict::EntryConstSharedPtr entry = node_->getStorage()->dict_->get(k);
83  std::string name = entry->desc.empty() ? key : entry->desc;
84  entries_.push_back(boost::bind(log_entry, _1, level, name, node_->getStorage()->getStringReader(k, !forced)));
85  return true;
86  }
87  catch(std::exception& e){
88  ROS_ERROR_STREAM(boost::diagnostic_information(e));
89  return false;
90  }
91  }
92 
93  template<typename T> void add(const boost::shared_ptr<T> &n){
94  DiagGroup::add(boost::static_pointer_cast<canopen::Layer>(n));
95  }
96 
98  if(node_->getState() == canopen::Node::Unknown){
99  stat.summary(stat.WARN,"Not initailized");
100  }else{
101  LayerReport r;
102  diag(r);
103  if(r.bounded<LayerStatus::Unbounded>()){ // valid
104  stat.summary(r.get(), r.reason());
105  for(std::vector<std::pair<std::string, std::string> >::const_iterator it = r.values().begin(); it != r.values().end(); ++it){
106  stat.add(it->first, it->second);
107  }
108  for(size_t i=0; i < entries_.size(); ++i) entries_[i](stat);
109  }
110  }
111  }
112  virtual ~Logger() {}
113 };
115 
117 public:
119  static void addLoader(ClassLoaderBaseSharedPtr b){
120  guarded_loaders().push_back(b);
121  }
123  guarded_loaders().clear();
124  }
125 private:
126  static std::vector< ClassLoaderBaseSharedPtr>& guarded_loaders(){
127  static std::vector<ClassLoaderBaseSharedPtr> loaders;
128  return loaders;
129  }
130 };
131 
132 template<typename T> class GuardedClassLoader {
135 public:
137  GuardedClassLoader(const std::string& package, const std::string& allocator_base_class)
138  : loader_(new Loader(package, allocator_base_class)) {
140  }
141  ClassSharedPtr createInstance(const std::string& lookup_name){
142  return loader_->createInstance(lookup_name);
143  }
144 };
145 
146 template<typename T> class ClassAllocator : public GuardedClassLoader<typename T::Allocator> {
147 public:
149  ClassAllocator (const std::string& package, const std::string& allocator_base_class) : GuardedClassLoader<typename T::Allocator>(package, allocator_base_class) {}
150  template<typename T1> ClassSharedPtr allocateInstance(const std::string& lookup_name, const T1 & t1){
151  return this->createInstance(lookup_name)->allocate(t1);
152  }
153  template<typename T1, typename T2> ClassSharedPtr allocateInstance(const std::string& lookup_name, const T1 & t1, const T2 & t2){
154  return this->createInstance(lookup_name)->allocate(t1, t2);
155  }
156  template<typename T1, typename T2, typename T3> ClassSharedPtr allocateInstance(const std::string& lookup_name, const T1 & t1, const T2 & t2, const T3 & t3){
157  return this->createInstance(lookup_name)->allocate(t1, t2, t3);
158  }
159 };
163 protected:
168  std::map<std::string, canopen::NodeSharedPtr > nodes_lookup_;
170  std::vector<LoggerSharedPtr > loggers_;
171  std::vector<PublishFunc::FuncType> publishers_;
172 
174 
175  boost::scoped_ptr<boost::thread> thread_;
176 
179 
182 
183  boost::mutex mutex_;
190 
192 
196  bool send(){
197  return interface && interface->send(frame);
198  }
199  } hb_sender_;
201 
202  boost::atomic<bool> running_;
203  boost::mutex diag_mutex_;
204 
206 
207  void logState(const can::State &s);
208  void run();
209  virtual bool handle_init(std_srvs::Trigger::Request &req, std_srvs::Trigger::Response &res);
210  virtual bool handle_recover(std_srvs::Trigger::Request &req, std_srvs::Trigger::Response &res);
211  virtual void handleWrite(LayerStatus &status, const LayerState &current_state);
212  virtual void handleShutdown(LayerStatus &status);
213  virtual bool handle_shutdown(std_srvs::Trigger::Request &req, std_srvs::Trigger::Response &res);
214  virtual bool handle_halt(std_srvs::Trigger::Request &req, std_srvs::Trigger::Response &res);
215 
216  bool handle_get_object(canopen_chain_node::GetObject::Request &req, canopen_chain_node::GetObject::Response &res);
217  bool handle_set_object(canopen_chain_node::SetObject::Request &req, canopen_chain_node::SetObject::Response &res);
218 
219  bool setup_bus();
220  bool setup_sync();
221  bool setup_heartbeat();
222  bool setup_nodes();
223  virtual bool nodeAdded(XmlRpc::XmlRpcValue &params, const canopen::NodeSharedPtr &node, const LoggerSharedPtr &logger);
225  virtual bool setup_chain();
226 public:
227  RosChain(const ros::NodeHandle &nh, const ros::NodeHandle &nh_priv);
228  bool setup();
229  virtual ~RosChain();
230 };
231 
232 } //namespace canopen
233 
234 #endif
can::StateListenerConstSharedPtr state_listener_
Definition: ros_chain.h:173
msg
virtual void add(const VectorMemberSharedPtr &l)
ros::Timer diag_timer_
Definition: ros_chain.h:181
boost::shared_ptr< pluginlib::ClassLoaderBase > ClassLoaderBaseSharedPtr
Definition: ros_chain.h:118
ros::ServiceServer srv_shutdown_
Definition: ros_chain.h:187
static FuncType create(ros::NodeHandle &nh, const std::string &name, ObjectStorage::Entry< Tobj > entry, bool force)
Definition: ros_chain.h:31
void publish(const boost::shared_ptr< M > &message) const
ros::ServiceServer srv_set_object_
Definition: ros_chain.h:189
void summary(unsigned char lvl, const std::string s)
boost::function< void()> func_type ROS_DEPRECATED
Definition: ros_chain.h:21
ClassSharedPtr allocateInstance(const std::string &lookup_name, const T1 &t1, const T2 &t2, const T3 &t3)
Definition: ros_chain.h:156
can::DriverInterfaceSharedPtr interface
Definition: ros_chain.h:195
const canopen::NodeSharedPtr node_
Definition: ros_chain.h:62
Base * createInstance(const std::string &derived_class_name, ClassLoader *loader)
static FuncType create(ros::NodeHandle &nh, const std::string &name, canopen::NodeSharedPtr node, const std::string &key, bool force)
Definition: ros_chain.cpp:19
std::vector< LoggerSharedPtr > loggers_
Definition: ros_chain.h:170
boost::shared_ptr< T > ClassSharedPtr
Definition: ros_chain.h:148
boost::shared_ptr< Logger > LoggerSharedPtr
Definition: ros_chain.h:114
const std::string reason() const
bool reset_errors_before_recover_
Definition: ros_chain.h:205
ClassAllocator(const std::string &package, const std::string &allocator_base_class)
Definition: ros_chain.h:149
ClassSharedPtr allocateInstance(const std::string &lookup_name, const T1 &t1)
Definition: ros_chain.h:150
boost::mutex mutex_
Definition: ros_chain.h:183
static void log_entry(diagnostic_updater::DiagnosticStatusWrapper &stat, uint8_t level, const std::string &name, boost::function< std::string()> getter)
Definition: ros_chain.h:66
ros::ServiceServer srv_init_
Definition: ros_chain.h:184
ClassSharedPtr allocateInstance(const std::string &lookup_name, const T1 &t1, const T2 &t2)
Definition: ros_chain.h:153
boost::function< void()> FuncType
Definition: ros_chain.h:22
boost::shared_ptr< T > ClassSharedPtr
Definition: ros_chain.h:136
ros::ServiceServer srv_get_object_
Definition: ros_chain.h:188
boost::mutex diag_mutex_
Definition: ros_chain.h:203
ros::ServiceServer srv_halt_
Definition: ros_chain.h:186
MergedXmlRpcStruct(const XmlRpc::XmlRpcValue &a, const MergedXmlRpcStruct &b, bool recursive=true)
Definition: ros_chain.h:46
union XmlRpc::XmlRpcValue::@0 _value
boost::chrono::high_resolution_clock::duration time_duration
MergedXmlRpcStruct(const XmlRpc::XmlRpcValue &a)
Definition: ros_chain.h:43
ros::NodeHandle nh_priv_
Definition: ros_chain.h:178
Logger(canopen::NodeSharedPtr node)
Definition: ros_chain.h:77
virtual void log(diagnostic_updater::DiagnosticStatusWrapper &stat)
Definition: ros_chain.h:97
ClassAllocator< canopen::Master > master_allocator_
Definition: ros_chain.h:162
std::vector< PublishFunc::FuncType > publishers_
Definition: ros_chain.h:171
static void addLoader(ClassLoaderBaseSharedPtr b)
Definition: ros_chain.h:119
Publisher advertise(const std::string &topic, uint32_t queue_size, bool latch=false)
static std::vector< ClassLoaderBaseSharedPtr > & guarded_loaders()
Definition: ros_chain.h:126
const std::vector< std::pair< std::string, std::string > > & values() const
GuardedClassLoader< can::DriverInterface > driver_loader_
Definition: ros_chain.h:161
Timer heartbeat_timer_
Definition: ros_chain.h:200
GuardedClassLoader(const std::string &package, const std::string &allocator_base_class)
Definition: ros_chain.h:137
std::map< std::string, canopen::NodeSharedPtr > nodes_lookup_
Definition: ros_chain.h:168
void report_diagnostics(canopen::LayerStack &sync, diagnostic_updater::DiagnosticStatusWrapper &stat)
Definition: sync_node.cpp:17
std::vector< boost::function< void(diagnostic_updater::DiagnosticStatusWrapper &)> > entries_
Definition: ros_chain.h:64
MasterSharedPtr master_
Definition: ros_chain.h:165
boost::shared_ptr< canopen::LayerGroupNoDiag< canopen::Node > > nodes_
Definition: ros_chain.h:166
ClassSharedPtr createInstance(const std::string &lookup_name)
Definition: ros_chain.h:141
can::DriverInterfaceSharedPtr interface_
Definition: ros_chain.h:164
static void publish(ros::Publisher &pub, ObjectStorage::Entry< Tobj > &entry)
Definition: ros_chain.h:26
canopen::SyncLayerSharedPtr sync_
Definition: ros_chain.h:169
time_duration update_duration_
Definition: ros_chain.h:191
ValueStruct * asStruct
diagnostic_updater::Updater diag_updater_
Definition: ros_chain.h:180
boost::scoped_ptr< boost::thread > thread_
Definition: ros_chain.h:175
void add(const std::string &key, const T &val)
pluginlib::ClassLoader< T > Loader
Definition: ros_chain.h:133
#define ROS_ERROR_STREAM(args)
boost::shared_ptr< Loader > loader_
Definition: ros_chain.h:134
void add(const boost::shared_ptr< T > &n)
Definition: ros_chain.h:93
virtual ~Logger()
Definition: ros_chain.h:112
void run(ClassLoader *loader)
bool add(uint8_t level, const std::string &key, bool forced)
Definition: ros_chain.h:79
boost::atomic< bool > running_
Definition: ros_chain.h:202
bool bounded() const
ros::ServiceServer srv_recover_
Definition: ros_chain.h:185
ros::NodeHandle nh_
Definition: ros_chain.h:177
boost::shared_ptr< canopen::LayerGroupNoDiag< canopen::EMCYHandler > > emcy_handlers_
Definition: ros_chain.h:167


canopen_chain_node
Author(s): Mathias Lüdtke
autogenerated on Sat May 4 2019 02:40:46