blackboard.h
Go to the documentation of this file.
1 
23 #ifndef BLACKBOARD_H_
24 #define BLACKBOARD_H_
25 
26 #include <iostream>
27 #include <ros/ros.h>
28 
29 #include "micros_swarm/singleton.h"
31 #include "micros_swarm/serialize.h"
34 #include "gsdf_msgs/CommPacket.h"
35 #include "gsdf_msgs/BlackBoardPut.h"
36 #include "gsdf_msgs/BlackBoardQuery.h"
37 #include "gsdf_msgs/BlackBoardAck.h"
38 
39 namespace micros_swarm{
40 
41  template<class Type>
42  class BlackBoard{
43  public:
45  {
46  bb_id_ = -1;
47  on_robot_id_ = -1;
48  robot_id_ = -1;
49  is_local_ = false;
50  }
51 
52  BlackBoard(int bb_id, int on_robot_id)
53  {
54  bb_id_ = bb_id;
55  on_robot_id_ = on_robot_id;
57  robot_id_ = rth_->getRobotID();
59  is_local_ = false;
60  if(on_robot_id_ == robot_id_) {
61  rth_->createBlackBoard(bb_id_);
62  is_local_ = true;
63  }
64  }
65 
67  {
70  bb_id_ = bb.bb_id_;
72  robot_id_ = bb.robot_id_;
73  is_local_ = bb.is_local_;
74  }
75 
77  {
78  if(this == &bb) {
79  return *this;
80  }
83  bb_id_ = bb.bb_id_;
85  robot_id_ = bb.robot_id_;
86  is_local_ = bb.is_local_;
87  return *this;
88  }
89 
91  {
92  rth_.reset();
93  mqm_.reset();
94  }
95 
96  void put(const std::string& key, const Type& data)
97  {
98  std::vector<uint8_t> s = serialize_ros(data);
99  ros::Time timestamp = ros::Time::now();
100  if(is_local_) {
101  rth_->insertOrUpdateBlackBoard(bb_id_, key, s, timestamp, rth_->getRobotID());
102  }
103  else {
104  gsdf_msgs::BlackBoardPut bbp;
105  bbp.bb_id = bb_id_;
106  bbp.on_robot_id = on_robot_id_;
107  bbp.key = key;
108  bbp.value = s;
109  bbp.timestamp = timestamp;
110  bbp.robot_id = rth_->getRobotID();
111  std::vector<uint8_t> bbp_vec = serialize_ros(bbp);
112 
113  gsdf_msgs::CommPacket p;
114  p.header.source = rth_->getRobotID();
115  p.header.type = BLACKBOARD_PUT;
116  p.header.data_len = bbp_vec.size();
117  p.header.version = 1;
118  p.header.checksum = 0;
119  p.content.buf = bbp_vec;
120  std::vector<uint8_t> msg_data = serialize_ros(p);
121  mqm_->getOutMsgQueue("bb")->push(msg_data);
122  }
123  }
124 
125  Type get(const std::string& key)
126  {
127  if(is_local_) {
128  BlackBoardTuple bb;
129  rth_->getBlackBoardTuple(bb_id_, key, bb);
130 
131  if ((bb.timestamp.sec == 0) && (bb.timestamp.nsec == 0)) {
132  std::cout << "ID " << bb_id_ << " blackboard, " << key << " is not exist." << std::endl;
133  exit(-1);
134  }
135 
136  Type data = deserialize_ros<Type>(bb.bb_value);
137  return data;
138  }
139  else {
140  gsdf_msgs::BlackBoardQuery bbq;
141  bbq.bb_id = bb_id_;
142  bbq.on_robot_id = on_robot_id_;
143  bbq.key = key;
144  bbq.timestamp = ros::Time::now();
145  bbq.robot_id = robot_id_;
146  std::vector<uint8_t> bbq_vec = serialize_ros(bbq);
147 
148  gsdf_msgs::CommPacket p;
149  p.header.source = rth_->getRobotID();
150  p.header.type = BLACKBOARD_QUERY;
151  p.header.data_len = bbq_vec.size();
152  p.header.version = 1;
153  p.header.checksum = 0;
154  p.content.buf = bbq_vec;
155  rth_->createBlackBoard(bb_id_);
156  ros::Time timestamp;
157  timestamp.sec = 0;
158  timestamp.nsec = 0;
159  std::vector<uint8_t> empty_vec;
160  empty_vec.clear();
161  rth_->insertOrUpdateBlackBoard(bb_id_, key, empty_vec, timestamp, -1);
162  std::vector<uint8_t> msg_data = serialize_ros(p);
163  mqm_->getOutMsgQueue("bb")->push(msg_data);
164 
165  Type data;
166  BlackBoardTuple bbt;
167  ros::Rate loop_rate(100);
168  int count = 0;
169  while(count < 500) {
170  rth_->getBlackBoardTuple(bb_id_, key, bbt);
171  if(bbt.bb_value.size() != 0) {
172  data = deserialize_ros<Type>(bbt.bb_value);
173  rth_->deleteBlackBoardValue(bb_id_, key);
174  break;
175  }
176  loop_rate.sleep();
177  ros::spinOnce();
178  }
179 
180  return data;
181  }
182  }
183 
184  int size()
185  {
186  if(is_local_) {
187  return rth_->getBlackBoardSize(bb_id_);
188  }
189  else {
190 
191  }
192  }
193  private:
195  int bb_id_;
197  bool is_local_;
200  };
201 }
202 #endif
boost::shared_ptr< RuntimeHandle > rth_
Definition: blackboard.h:198
BlackBoard(int bb_id, int on_robot_id)
Definition: blackboard.h:52
void put(const std::string &key, const Type &data)
Definition: blackboard.h:96
XmlRpcServer s
BlackBoard & operator=(const BlackBoard &bb)
Definition: blackboard.h:76
std::vector< uint8_t > serialize_ros(T t)
Definition: serialize.h:39
static boost::shared_ptr< T > getSingleton()
Definition: singleton.h:70
bool sleep()
boost::shared_ptr< micros_swarm::MsgQueueManager > mqm_
Definition: blackboard.h:199
BlackBoard(const BlackBoard &bb)
Definition: blackboard.h:66
std::vector< uint8_t > bb_value
Definition: data_type.h:150
static Time now()
ROSCPP_DECL void spinOnce()


micros_swarm
Author(s):
autogenerated on Mon Jun 10 2019 14:02:06