obstacle_server.cpp
Go to the documentation of this file.
1 #include <ros/ros.h>
2 #include <multipoint_navigation_receiver/obstacle_srv.h>
3 #include <nav_msgs/OccupancyGrid.h>
4 #include <boost/thread/thread.hpp>
5 nav_msgs::OccupancyGrid mapdata;
6 
7 bool obstacleCallback(multipoint_navigation_receiver::obstacle_srv::Request &req,
8  multipoint_navigation_receiver::obstacle_srv::Response &res)
9 {
10  if (mapdata.data.size() - 0 < 0.001)
11  {
12  return false;
13  }
14 
15  int width = mapdata.info.width;
16  std::vector<signed char> arr = mapdata.data;
17  float x = req.positionx;
18  float y = req.positiony;
19  printf("positionx is %f and positiony is %f \n",x,y);
20  int pos = (int)((req.positiony - mapdata.info.origin.position.y) * width * (1.00/mapdata.info.resolution) + (req.positionx - mapdata.info.origin.position.x) * (1.00/mapdata.info.resolution));
21  int obstacle = (int)arr.at(pos);
22  printf("pos is %d and obstacle is %d\n",pos,obstacle);
23  res.obstacle_probability = obstacle;
24 
25  return true;
26 }
27 
28 void sub_once()
29 {
31  batEdge = ros::topic::waitForMessage<nav_msgs::OccupancyGrid>("/move_base/global_costmap/costmap",ros::Duration(3));
32  if(batEdge != NULL)
33  {
34  mapdata = *batEdge;
35  printf("update mapdate \n");
36  }
37  else
38  printf("no topic\n");
39 }
40 void subThread()
41 {
42  while(ros::ok())
43  {
44  sub_once();
45  sleep(30);
46  }
47 }
48 int main(int argc, char *argv[])
49 {
50  ros::init(argc, argv, "obstacle_server");
52  boost::thread thrd(&subThread);
53 
54  ros::ServiceServer obstacle_server = n.advertiseService("/obstacle_information",obstacleCallback);
55  ros::spin();
56  return 0;
57 }
58 
59 
60 
ROSCPP_DECL void init(int &argc, char **argv, const std::string &name, uint32_t options=0)
ServiceServer advertiseService(const std::string &service, bool(T::*srv_func)(MReq &, MRes &), T *obj)
TFSIMD_FORCE_INLINE const tfScalar & y() const
bool obstacleCallback(multipoint_navigation_receiver::obstacle_srv::Request &req, multipoint_navigation_receiver::obstacle_srv::Response &res)
ROSCPP_DECL void spin(Spinner &spinner)
int main(int argc, char *argv[])
ROSCPP_DECL bool ok()
TFSIMD_FORCE_INLINE const tfScalar & x() const
ros::NodeHandle * n
nav_msgs::OccupancyGrid mapdata
void subThread()
void sub_once()


multipoint_navigation_receiver
Author(s): qiuan ren
autogenerated on Sat Aug 22 2020 03:28:37