master_plugin.cpp
Go to the documentation of this file.
4 
5 #include <set>
6 
7 namespace canopen {
8 
9 class ManagingSyncLayer: public SyncLayer {
10 protected:
12  boost::chrono::milliseconds step_, half_step_;
13 
14  std::set<void *> nodes_;
15  boost::mutex nodes_mutex_;
16  boost::atomic<size_t> nodes_size_;
17 
18  virtual void handleShutdown(LayerStatus &status) {
19  }
20 
21  virtual void handleHalt(LayerStatus &status) { /* nothing to do */ }
22  virtual void handleDiag(LayerReport &report) { /* TODO */ }
23  virtual void handleRecover(LayerStatus &status) { /* TODO */ }
24 
25 public:
27  : SyncLayer(p), interface_(interface), step_(p.period_ms_), half_step_(p.period_ms_/2), nodes_size_(0)
28  {
29  }
30 
31  virtual void addNode(void * const ptr) {
32  boost::mutex::scoped_lock lock(nodes_mutex_);
33  nodes_.insert(ptr);
34  nodes_size_ = nodes_.size();
35  }
36  virtual void removeNode(void * const ptr) {
37  boost::mutex::scoped_lock lock(nodes_mutex_);
38  nodes_.erase(ptr);
39  nodes_size_ = nodes_.size();
40  }
41 };
42 
43 
45  time_point read_time_, write_time_;
46 protected:
47  virtual void handleRead(LayerStatus &status, const LayerState &current_state) {
48  if(current_state > Init){
49  boost::this_thread::sleep_until(read_time_);
50  write_time_ += step_;
51  }
52  }
53  virtual void handleWrite(LayerStatus &status, const LayerState &current_state) {
54  if(current_state > Init){
55  can::Frame frame(properties.header_, 0);
56  boost::this_thread::sleep_until(write_time_);
57  if(nodes_size_){ //)
58  interface_->send(frame);
59  }
60  read_time_ = get_abs_time(half_step_);
61  }
62  }
63 
64  virtual void handleInit(LayerStatus &status){
65  write_time_ = get_abs_time(step_);
66  read_time_ = get_abs_time(half_step_);
67  }
68 public:
70  : ManagingSyncLayer(p, interface) {}
71 };
72 
75 protected:
76  virtual void handleRead(LayerStatus &status, const LayerState &current_state) {
77  can::Frame msg;
78  if(current_state > Init){
79  if(reader_.readUntil(&msg, get_abs_time(step_))){ // wait for sync
80  boost::this_thread::sleep_until(get_abs_time(half_step_)); // shift readout to middle of period
81  }
82  }
83  }
84  virtual void handleWrite(LayerStatus &status, const LayerState &current_state) {
85  // nothing to do here
86  }
87  virtual void handleInit(LayerStatus &status){
89  }
90 public:
92  : ManagingSyncLayer(p, interface), reader_(true,1) {}
93 };
94 
95 
96 template<typename SyncType> class WrapMaster: public Master{
98 public:
100  return boost::make_shared<SyncType>(properties, interface_);
101  }
102  WrapMaster(can::CommInterfaceSharedPtr interface) : interface_(interface) {}
103 
104  class Allocator : public Master::Allocator{
105  public:
106  virtual MasterSharedPtr allocate(const std::string &name, can::CommInterfaceSharedPtr interface){
107  return boost::make_shared<WrapMaster>(interface);
108  }
109  };
110 };
111 
114 }
117 
std::set< void * > nodes_
const SyncProperties properties
Definition: canopen.h:176
can::BufferedReader reader_
void listen(CommInterfaceSharedPtr interface)
virtual void handleWrite(LayerStatus &status, const LayerState &current_state)
virtual MasterSharedPtr allocate(const std::string &name, can::CommInterfaceSharedPtr interface)
virtual void handleShutdown(LayerStatus &status)
can::CommInterfaceSharedPtr interface_
bool readUntil(can::Frame *msg, boost::chrono::high_resolution_clock::time_point abs_time)
const can::Header header_
Definition: canopen.h:166
can::CommInterfaceSharedPtr interface_
CLASS_LOADER_REGISTER_CLASS(canopen::SimpleMaster::Allocator, canopen::Master::Allocator)
boost::shared_ptr< CommInterface > CommInterfaceSharedPtr
boost::chrono::milliseconds step_
boost::chrono::high_resolution_clock::time_point time_point
Definition: canopen.h:18
virtual SyncLayerSharedPtr getSync(const SyncProperties &properties)
virtual void addNode(void *const ptr)
virtual void handleRead(LayerStatus &status, const LayerState &current_state)
WrapMaster< SimpleSyncLayer > SimpleMaster
WrapMaster(can::CommInterfaceSharedPtr interface)
const std::string name
Definition: layer.h:74
virtual void handleRecover(LayerStatus &status)
boost::atomic< size_t > nodes_size_
boost::shared_ptr< Master > MasterSharedPtr
Definition: canopen.h:309
SimpleSyncLayer(const SyncProperties &p, can::CommInterfaceSharedPtr interface)
virtual void handleRead(LayerStatus &status, const LayerState &current_state)
WrapMaster< ExternalSyncLayer > ExternalMaster
virtual void handleInit(LayerStatus &status)
virtual void removeNode(void *const ptr)
boost::chrono::milliseconds half_step_
ManagingSyncLayer(const SyncProperties &p, can::CommInterfaceSharedPtr interface)
virtual void handleDiag(LayerReport &report)
ExternalSyncLayer(const SyncProperties &p, can::CommInterfaceSharedPtr interface)
time_point get_abs_time(const time_duration &timeout)
Definition: canopen.h:20
boost::shared_ptr< SyncLayer > SyncLayerSharedPtr
Definition: canopen.h:302
virtual void handleInit(LayerStatus &status)
virtual void handleWrite(LayerStatus &status, const LayerState &current_state)
virtual void handleHalt(LayerStatus &status)


canopen_master
Author(s): Mathias L├╝dtke
autogenerated on Sat May 4 2019 02:40:43