state_exchanger.cpp
Go to the documentation of this file.
1 #include "state_exchanger.h"
2 
7 void state_callback (const smach_msgs::SmachContainerStatus::ConstPtr& msg)
8 {
9  ROS_DEBUG("Received state machine path %s", msg->path.c_str());
10 
11  if (msg->path == sm_path) {
12  // state received
13  state_valid = true;
14 
15  // store only first state in class variables
16  state = msg->active_states[0];
17  }
18 }
19 
24 void swarm_state_callback (cpswarm_msgs::StateEvent msg) {
25 
26  // uuid of the sending swarm member
27  string uuid = msg.swarmio.node;
28 
29  // add new swarm member
30  if (swarm_state.count(uuid) <= 0) {
31  state_t member_state;
32  member_state.uuid = uuid;
33  swarm_state.emplace(uuid, member_state);
34  }
35 
36  // update swarm member
37  swarm_state[uuid].state = msg.state.c_str();
38  swarm_state[uuid].stamp = Time::now();
39 }
40 
47 int main (int argc, char **argv)
48 {
49  // init ros node
50  init(argc, argv, "state_exchanger");
51  NodeHandle nh;
52 
53  // read parameters
54  double loop_rate;
55  nh.param(this_node::getName() + "/loop_rate", loop_rate, 1.5);
56  int queue_size;
57  nh.param(this_node::getName() + "/queue_size", queue_size, 10);
58  double timeout;
59  nh.param(this_node::getName() + "/timeout", timeout, 20.0);
60  sm_path = "/SM_TOP";
61  nh.param(this_node::getName() + "/sm_path", sm_path, sm_path);
62 
63  // init topic flags
64  state_valid = false;
65 
66  // publishers and subscribers
67  Subscriber state_subscriber = nh.subscribe("smach_server/smach/container_status", queue_size, state_callback);
68  Subscriber incoming_state_subscriber = nh.subscribe("bridge/events/state", queue_size, swarm_state_callback);
69  Publisher outgoing_state_publisher = nh.advertise<cpswarm_msgs::StateEvent>("state", queue_size);
70  Publisher incoming_state_publisher = nh.advertise<cpswarm_msgs::ArrayOfStates>("swarm_state", queue_size);
71 
72  // init loop rate
73  Rate rate(loop_rate);
74 
75  // init position and velocity
76  while (ok() && state_valid == false) {
77  ROS_DEBUG_ONCE("Waiting for valid state...");
78  rate.sleep();
79  spinOnce();
80  }
81 
82  // init swarm state message
83  cpswarm_msgs::ArrayOfStates swarm_state_msg;
84 
85  // continuously exchange state between swarm members
86  while (ok()) {
87  // reset swarm state message
88  swarm_state_msg.states.clear();
89 
90  // update swarm state
91  for (auto member=swarm_state.begin(); member!=swarm_state.end();) {
92  // delete members that haven't updated their state lately
93  if ((Time::now() - member->second.stamp) > Duration(timeout)) {
94  member = swarm_state.erase(member);
95  continue;
96  }
97 
98  // store state of swarm member
99  cpswarm_msgs::StateEvent state_event;
100  state_event.header.stamp = Time::now();
101  state_event.swarmio.node = member->first;
102  swarm_state_msg.states.push_back(state_event);
103 
104  // next member
105  ++member;
106  }
107 
108  // publish swarm state locally
109  incoming_state_publisher.publish(swarm_state_msg);
110 
111  // publish local state to swarm
112  cpswarm_msgs::StateEvent state_event;
113  state_event.header.stamp = Time::now();
114  state_event.swarmio.name = "state";
115  state_event.state = state;
116  outgoing_state_publisher.publish(state_event);
117 
118  rate.sleep();
119  spinOnce();
120  }
121 }
string uuid
void swarm_state_callback(cpswarm_msgs::StateEvent msg)
Callback function for state updates from other swarm members.
string sm_path
The path of the smach state machine whose state shall be exchanged.
bool state_valid
Whether a valid state has been received.
string state
Current state of the CPS.
int main(int argc, char **argv)
A ROS node that exchanges the behavioral state between CPSs in a swarm.
A STATE type containing UUID and state of the corresponding CPS together with last updated time stamp...
map< string, state_t > swarm_state
The state of all known swarm members.
void state_callback(const smach_msgs::SmachContainerStatus::ConstPtr &msg)
Callback function for state updates.


state_exchanger
Author(s): Micha Sende
autogenerated on Thu Oct 31 2019 12:13:16