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

error: ‘map_pub’ not declared in this scope

asked 2019-11-11 03:58:21 -0500

mohamed19977 gravatar image

updated 2019-11-11 04:02:33 -0500

gvdhoorn gravatar image

i keep having this error ‘map_pub’ was not declared in this scope map_pub.publish(new_grid); / ‘publisher’ is not a member of ‘ros’ ros::publisher map_pub = n.advertise<nav_msgs::occupancygrid>("map",100);

Code:

#include "ros/ros.h"
#include "sensor_msgs/LaserScan.h"
#include "geometry_msgs/Twist.h"
#include <nav_msgs/GetMap.h>
#include <vector>
#include <nav_msgs/OccupancyGrid.h>
#include <nav_msgs/MapMetaData.h>
#include<geometry_msgs/Pose.h>
#include <std_msgs/Header.h>
#include <map>
#include <std_msgs/String.h>





//some global variables to "perceive" the world around the robot:
double front;
double right;
double left; 
bool robot_stopped;

// grid map

int rows;
int cols;
double mapResolution;
int currCell = 0;
std::vector<std::vector<bool> > grid;    //vector of vectors 





//...

void laserCallback(const sensor_msgs::LaserScan::ConstPtr& msg)
{
    ROS_INFO("Received a LaserScan with %i samples", (int) msg->ranges.size() );

    //For simplicity, let's save the distance of the closer obstacle to the robot:
    front = *std::min_element (msg->ranges.begin()+80, msg->ranges.begin()+159);
    left = *std::min_element (msg->ranges.begin(), msg->ranges.begin()+79);
    right = *std::min_element (msg->ranges.begin()+160, msg->ranges.end());
    ROS_INFO("minimum front distance to obstacle: %f", front);
    ROS_INFO("minimum left distance to obstacle: %f", left);
    ROS_INFO("minimum right distance to obstacle: %f", right);
  }  




void mapCallback(const nav_msgs::OccupancyGrid::ConstPtr& msg)
{

// set Header that belong to std_msgs to header that exists in the supllied reference msg
  std_msgs::Header header = msg->header;

// set MapMeta Data that belong to std_msgs to information that exists in the supllied reference msg
  nav_msgs::MapMetaData info = msg->info;


  ROS_INFO("Received a %d X %d map @ %.3f m/px\n",info.width,info.height,info.resolution);

//assigning data to varibles 
  rows=info.width;
  cols=info.height;
  mapResolution=info.resolution;
  nav_msgs::OccupancyGrid* new_grid = new nav_msgs::OccupancyGrid();

// Dynamically resize the grid from vector class it is resizing it accourding to laser range to make it rows = to cols
  grid.resize(rows);

   for(int i = 0; i < rows; i++) 
    {
      grid[i].resize(cols);
    }


//loop on all elements in the grid in order to allocate values due to data conversion   

    for(int i=0; i<info.width ; i++)
     {
      for(int j=0; i<info.height ; j++)
       {

       if(msg->data[currCell] == 0)

      // unoccupied cell
        grid[i][j] = false;

     else

      //occupied or unknown  
        grid[i][j] = true; 

        currCell++;

        grid[i][j]=new_grid;

     map_pub.publish(new_grid);

       }
     }

}

int main(int argc, char **argv){

  ros::init(argc, argv, "karina"); 

  ros::NodeHandle n;


  //Publisher for /cmd_vel and /map topics 
  ros::Publisher cmd_vel_pub = n.advertise<geometry_msgs::Twist>("cmd_vel", 100);
  ros::publisher map_pub = n.advertise<nav_msgs::OccupancyGrid>("map",100);

  //Subscriber for /basescan which contains laserdata to drive robot and publish map
  ros::Subscriber laser_sub = n.subscribe("base_scan", 100, laserCallback);
  ros::Subscriber map_sub = n.subscribe("base_scan",100,mapCallback);



  ros::Rate loop_rate(10); //10 Hz

  //initializations:
  geometry_msgs::Twist cmd_vel_msg;
  robot_stopped = false;
  front = 0.1;
  right =0.1;
  left = 0.1;
  int count = 0;
  while (ros::ok())
{

    //fill the "cmd_vel_msg" data according to some conditions (depending on laser data)
      ROS_INFO("Read: %f", front);


    //publish velocity commands:
    cmd_vel_pub.publish(cmd_vel_msg);

    ros::spinOnce();

    loop_rate.sleep();
  }

  return 0;
}
edit retag flag offensive close merge delete

1 Answer

Sort by » oldest newest most voted
1

answered 2019-11-15 10:15:58 -0500

cat_in_box gravatar image
  1. In main, change ros::publisher map_pub to ros::Publisher map_pub
  2. In mapCallback, when you loop over all elements of the grid, you should wrap the else in brackets like so:

    else { //occupied or unknown grid[i][j] = true; currCell++; grid[i][j]=new_grid; map_pub.publish(new_grid); }

edit flag offensive delete link more

Question Tools

Stats

Asked: 2019-11-11 03:58:21 -0500

Seen: 1,141 times

Last updated: Nov 15 '19