1 #include "area_division.h" 9 map<string, vector<int>> swarm_grid;
12 pos[0] = int(round((cps.second.pose.position.x -
gridmap.info.origin.position.x) /
gridmap.info.resolution));
13 pos[1] = int(round((cps.second.pose.position.y -
gridmap.info.origin.position.y) /
gridmap.info.resolution));
14 swarm_grid.emplace(cps.first, pos);
15 ROS_DEBUG(
"Other CPS %s at (%d,%d)", cps.first.c_str(), pos[0], pos[1]);
20 pos[0] = int(round((
pose.position.x -
gridmap.info.origin.position.x) /
gridmap.info.resolution));
21 pos[1] = int(round((
pose.position.y -
gridmap.info.origin.position.y) /
gridmap.info.resolution));
22 swarm_grid.emplace(
uuid, pos);
23 ROS_DEBUG(
"Me %s at (%d,%d)",
uuid.c_str(), pos[0], pos[1]);
26 ROS_INFO(
"Dividing area...");
27 vector<signed char, allocator<signed char>> map =
gridmap.data;
47 geometry_msgs::PoseStamped goal_pose;
48 goal_pose.header.stamp = Time::now();
49 goal_pose.pose =
pose;
59 cpswarm_msgs::AreaDivisionEvent event;
60 event.header.stamp = Time::now();
61 event.swarmio.name =
"area_division";
62 geometry_msgs::PoseStamped ps;
63 ps.header.frame_id =
"local_origin_ned";
76 bool get_area (nav_msgs::GetMap::Request &req, nav_msgs::GetMap::Response &res)
118 if (msg->header.stamp.isValid())
135 auto idx =
swarm_pose.find(msg->swarmio.node);
139 ROS_DEBUG(
"Add CPS %s", msg->swarmio.node.c_str());
142 swarm_pose.emplace(msg->swarmio.node, msg->pose);
147 ROS_DEBUG(
"Update CPS %s", msg->swarmio.node.c_str());
150 idx->second.header.stamp = Time::now();
151 idx->second = msg->pose;
172 int main (
int argc,
char **argv)
175 init(argc, argv,
"area_division");
184 nh.param(this_node::getName() +
"/loop_rate", loop_rate, 1.5);
186 nh.param(this_node::getName() +
"/queue_size", queue_size, 10);
187 nh.param(this_node::getName() +
"/swarm_timeout",
swarm_timeout, 5.0);
188 nh.param(this_node::getName() +
"/visualize",
visualize,
false);
197 Subscriber uuid_sub = nh.subscribe(
"bridge/uuid", queue_size,
uuid_callback);
198 Subscriber pose_sub = nh.subscribe(
"pos_provider/pose", queue_size,
pose_callback);
199 Subscriber swarm_sub = nh.subscribe(
"bridge/events/area_division", queue_size,
swarm_callback);
200 Subscriber map_sub = nh.subscribe(
"area_provider/map", queue_size,
map_callback);
201 pos_pub = nh.advertise<geometry_msgs::PoseStamped>(
"pos_controller/goal_position", queue_size,
true);
202 swarm_pub = nh.advertise<cpswarm_msgs::AreaDivisionEvent>(
"area_division", queue_size,
true);
204 area_pub = nh.advertise<nav_msgs::OccupancyGrid>(
"assigned_map", queue_size,
true);
207 rate =
new Rate(loop_rate);
210 while (ok() &&
uuid ==
"") {
211 ROS_DEBUG_ONCE(
"Waiting for UUID...");
218 ROS_DEBUG_ONCE(
"Waiting for valid position information...");
225 ROS_DEBUG_ONCE(
"Waiting for grid map...");
234 ServiceServer area_service = nh.advertiseService(
"area/assigned",
get_area);
Publisher area_pub
Publisher to visualize the assigned area grid map.
bool visualize
Whether to publish the area division on a topic for visualization.
bool swarm_valid
Whether valid position information has been received from the other swarm members.
A class to divide the environment optimally among multiple cyber physical systems (CPSs)...
void divide_area()
Divide the area of the grid map equally among multiple CPSs.
void uuid_callback(const swarmros::String::ConstPtr &msg)
Callback function to receive the UUID from the communication library.
double swarm_timeout
The time in seconds to wait after an area division event before starting the area division...
bool pose_valid
Whether a valid position has been received.
nav_msgs::OccupancyGrid gridmap
The complete grid map.
geometry_msgs::Pose pose
Current position of the CPS.
Publisher swarm_pub
Publisher to syncronize with other CPSs.
void pose_callback(const geometry_msgs::PoseStamped::ConstPtr &msg)
Callback function for position updates.
Time sync_start
The time at which the synchronization for the area division started.
void initialize_cps(map< string, vector< int >> cpss)
Define the CPS positions.
void divide()
Perform the area division.
area_division * division
The object encapsulating the area division optimization algorithm.
string uuid
UUID of this CPS.
void initialize_map(int r, int c, vector< signed char > src)
Define the grid map.
void sync()
Synchronize The CPSs by exchanging an event.
map< string, geometry_msgs::PoseStamped > swarm_pose
The positions of the other swarm members.
int main(int argc, char **argv)
A ROS node that divides the available area among a swarm of CPSs.
Publisher pos_pub
Publisher to stop the CPS.
void swarm_callback(const cpswarm_msgs::AreaDivisionEvent::ConstPtr &msg)
Callback function to receive area division requests from other CPSs.
nav_msgs::OccupancyGrid get_grid(nav_msgs::OccupancyGrid map, string cps)
Get the region assigned to a CPS.
bool map_valid
Whether a valid grid map has been received.
bool reconfigure
Whether the swarm configuration has changed and the area needs to be divided again.
Rate * rate
ROS rate object for controlling loop rates.
bool get_area(nav_msgs::GetMap::Request &req, nav_msgs::GetMap::Response &res)
Callback function to get the area assignment of this CPS.
void map_callback(const nav_msgs::OccupancyGrid::ConstPtr &msg)
Callback function to receive the grid map.