ROS Resources: Documentation | Support | Discussion Forum | Index | Service Status | ros @ Robotics Stack Exchange
Ask Your Question
0

Is it possible to read data from /map?

asked 2018-04-06 05:12:19 -0500

JaimeGJ gravatar image

Hello weveryone, I'm trying to read the data from the topic /map (in ROS Kinetic) but I don't know if I'm subscribing correctly.

I'm using the following code to count the number of gray pixels (unexplored pixels):

#include <ros/ros.h>
#include <nav_msgs/MapMetaData.h>
#include <nav_msgs/OccupancyGrid.h>
#include "std_msgs/Header.h"
#include "nav_msgs/MapMetaData.h"

double Ngray = 0;

void mapCallback(const nav_msgs::OccupancyGrid::ConstPtr& msg){
  std_msgs::Header header = msg->header;
  nav_msgs::MapMetaData info = msg->info;
  nav_msgs::OccupancyGrid data = msg->data;
  ROS_INFO("Got map %d %d", info.width, info.height);
   for (unsigned int x = 0; x < info.width; x++)
    for (unsigned int y = 0; y < info.height; y++)
      if (data(x,y) == -1)
        Ngray++;
        ROS_INFO("Number of gray cells is: %e", Ngray);
      //map.Insert(Cell(x,y,info.width,msg->data[x+ info.width * y]));
  //nav_msgs::OccupancyGrid* newGrid = map.Grid();
  //newGrid->header = header;
  //newGrid->info = info;
  //map_pub.publish(*newGrid);
}
int main(int argc, char **argv){
  ros::init(argc, argv, "map_reader");
  ros::NodeHandle n;

  ros::Subscriber map_sub = n.subscribe("map",2000,mapCallback);

  ros::spin();
  return 0;
}

I'm having troubles with line 12. I'm sure I'm not writing it good, but I don't know how to.

Can anybody help me?

Thank you very much.

edit retag flag offensive close merge delete

1 Answer

Sort by ยป oldest newest most voted
4

answered 2018-04-06 10:16:39 -0500

Jasmin gravatar image

updated 2018-04-08 08:19:57 -0500

It would be really helpful to share the error message, but I guess that it is related to a type issue since the msg->data is an int8[] not a nav_msgs::OccupancyGrid.

Please refer to OccupancyGrid message definition and see this occupancygrid.cpp code The mapCallback function in it would be very useful as example and since you need to loop over cells note that you can use msg->data[x+ info.width * y] and not data(x,y)

Edit1:

When looping over the width then height values,

index=x+info.width*y is a formula that will give you access to all the cells of the map column by column when you loop over x , y. for example for x=0 you will have [0,1,...,info.height], for x=1 , [info.height+1,...,2*info.height] and so on ... finally we will get [0,...,info.width*info.height].

This way to access cells is more useful if we have a special computation where we will make use of the current width and height (x,y) but in your case you can just use a simple loop since you just need to count the unexplored cells,

for(int i=0; i<info.height*info.width; i++)
    {
        if(msg->data[i]==-1)
            Ngray++;
    }

Good luck!

edit flag offensive delete link more

Comments

Thank you, Jasmin. The code I posted was based on that one that you said. What I want to do with the data from /map is to segment the map into zones explored and non-explored. So, if I want to read the value of one cell in the map, how could I write it?

JaimeGJ gravatar image JaimeGJ  ( 2018-04-07 11:31:53 -0500 )edit

Hey @JaimeGJ , I Edited my answer with some corrections and a more detailed explanation, please check it out and tell me if something is still unclear.

Jasmin gravatar image Jasmin  ( 2018-04-08 08:22:39 -0500 )edit

Question Tools

1 follower

Stats

Asked: 2018-04-06 05:12:19 -0500

Seen: 4,619 times

Last updated: Apr 08 '18