bid_action.cpp
Go to the documentation of this file.
1 #include <ros/ros.h>
2 #include <actionlib/server/simple_action_server.h>
3 #include <geometry_msgs/Pose.h>
4 #include <geometry_msgs/PoseStamped.h>
5 #include <swarmros/String.h>
6 #include <cpswarm_msgs/TaskAllocationEvent.h>
7 #include <cpswarm_msgs/TaskAllocatedEvent.h>
8 #include <cpswarm_msgs/TaskAllocationAction.h>
9 
10 using namespace std;
11 using namespace ros;
12 
16 typedef actionlib::SimpleActionServer<cpswarm_msgs::TaskAllocationAction> Server;
17 
21 Publisher publisher;
22 
26 geometry_msgs::Pose pose;
27 
31 cpswarm_msgs::TaskAllocatedEvent allocation;
32 
36 string uuid;
37 
42 
46 int task_id;
47 
52 void pose_callback(const geometry_msgs::PoseStamped::ConstPtr& msg)
53 {
54  pose = msg->pose;
55  pose_received = true;
56 }
57 
62 void allocation_callback(const cpswarm_msgs::TaskAllocatedEvent::ConstPtr& msg)
63 {
64  if (msg->task_id == task_id) {
65  allocation = *msg;
66  }
67 }
68 
73 void uuid_callback(const swarmros::String::ConstPtr& msg)
74 {
75  uuid = msg->value;
76 }
77 
83 void bid_callback(const cpswarm_msgs::TaskAllocationGoal::ConstPtr& goal, Server* as)
84 {
85  // compute bid
86  double distance = hypot(goal->task_pose.pose.position.x - pose.position.x, goal->task_pose.pose.position.y - pose.position.y);
87  task_id = goal->task_id;
88 
89  ROS_INFO("TASK_BID - Compute bid for task %d, value: %.2f", task_id, 1.0/distance);
90 
91  // create bid message
92  cpswarm_msgs::TaskAllocationEvent task_allocation;
93  task_allocation.header.stamp = Time::now();
94  task_allocation.header.frame_id = "local_origin_ned";
95  task_allocation.swarmio.name = "cps_selection";
96  task_allocation.swarmio.node = goal->auctioneer;
97  task_allocation.id = task_id;
98  task_allocation.bid = 1.0 / distance;
99 
100  // publish bid until auction ends
101  NodeHandle nh;
102  double loop_rate;
103  nh.param(this_node::getName() + "/loop_rate", loop_rate, 5.0);
104  Rate rate(loop_rate);
105  while (ok() && !as->isPreemptRequested() && allocation.task_id < 0) {
106  ROS_DEBUG_ONCE("TASK_BID - Waiting for auction to end");
107  publisher.publish(task_allocation);
108  spinOnce();
109  rate.sleep();
110  }
111 
112  ROS_INFO("TASK_BID - Task allocated to: %s", allocation.cps_id.c_str());
113 
114  // action server has been preempted
115  if (as->isPreemptRequested()) {
116  as->setPreempted();
117  }
118 
119  // this cps has won the auction, return result
120  else if (allocation.cps_id.compare(uuid) == 0) {
121  cpswarm_msgs::TaskAllocationResult result;
122  result.task_id = allocation.task_id;
123  result.task_pose = goal->task_pose;
124  as->setSucceeded(result);
125  }
126 
127  // auction lost, return negative result
128  else {
129  as->setAborted();
130  }
131 
132  // reset global variables
133  task_id = -1;
134  allocation.task_id = -1;
135 }
136 
143 int main(int argc, char **argv)
144 {
145  // init ros node
146  init(argc, argv, "task_allocation_bid");
147  NodeHandle nh;
148 
149  // init global variables
150  uuid = "";
151  pose_received = false;
152  task_id = -1;
153  allocation.task_id = -1;
154 
155  // read parameters
156  double loop_rate;
157  nh.param(this_node::getName() + "/loop_rate", loop_rate, 5.0);
158  Rate rate(loop_rate);
159  int queue_size;
160  nh.param(this_node::getName() + "/queue_size", queue_size, 10);
161 
162  // publishers and subscribers
163  Subscriber pose_subscriber = nh.subscribe<geometry_msgs::PoseStamped>("pos_provider/pose", queue_size, pose_callback);
164  Subscriber allocation_subscriber = nh.subscribe<cpswarm_msgs::TaskAllocatedEvent>("bridge/events/cps_selected", queue_size, allocation_callback);
165  Subscriber uuid_subscriber = nh.subscribe<swarmros::String>("bridge/uuid", 1, uuid_callback);
166  publisher = nh.advertise<cpswarm_msgs::TaskAllocationEvent>("cps_selection", queue_size);
167 
168  // wait for pose and uuid
169  while (ok() && (!pose_received || uuid.compare("") == 0)) {
170  ROS_DEBUG_ONCE("TASK_BID - Waiting for position or UUID...");
171  spinOnce();
172  rate.sleep();
173  }
174 
175  // start action server and wait
176  Server server(nh, "cmd/task_allocation_bid", boost::bind(&bid_callback, _1, &server), false);
177  server.start();
178  ROS_INFO("TASK_BID - Task allocation bid action available");
179  spin();
180  return 0;
181 }
actionlib::SimpleActionServer< cpswarm_msgs::TaskAllocationAction > Server
The server object that offers the task allocation auction as action server.
string uuid
The UUID if this CPS.
Definition: bid_action.cpp:36
actionlib::SimpleActionServer< cpswarm_msgs::TaskAllocationAction > Server
The server object that offers the task allocation auction as action server.
Definition: bid_action.cpp:16
geometry_msgs::Pose pose
Position of this CPS.
Definition: bid_action.cpp:26
void pose_callback(const geometry_msgs::PoseStamped::ConstPtr &msg)
Callback function to receive the position of this CPS.
Definition: bid_action.cpp:52
Publisher publisher
Publisher to submit a bid to the auction.
Definition: bid_action.cpp:21
int task_id
The ID of the task that is auctioned.
Definition: bid_action.cpp:46
void uuid_callback(const swarmros::String::ConstPtr &msg)
Callback function to receive the UUID of this CPS.
Definition: bid_action.cpp:73
bool pose_received
Whether a position of this CPS has been received.
Definition: bid_action.cpp:41
int main(int argc, char **argv)
A ROS node that exchanges relative kinematics between CPSs in a swarm.
Definition: bid_action.cpp:143
cpswarm_msgs::TaskAllocatedEvent allocation
The result of the task allocation auction.
Definition: bid_action.cpp:31
void bid_callback(const cpswarm_msgs::TaskAllocationGoal::ConstPtr &goal, Server *as)
Callback function to participate in a task allocation auction.
Definition: bid_action.cpp:83
void allocation_callback(const cpswarm_msgs::TaskAllocatedEvent::ConstPtr &msg)
Callback function to receive the task allocation result.
Definition: bid_action.cpp:62


task_allocation
Author(s):
autogenerated on Thu Oct 31 2019 16:22:51