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

Subscribe and Publish a arbitrary topic!

asked 2016-03-13 21:22:50 -0600

Reza1984 gravatar image

updated 2016-03-13 22:01:22 -0600

Hi

I would like to subscribe to the Laserscan data and process it and publish something. Unfortunately, I keep getting error. I appreciate if you help. here is my code:

#include <ros/ros.h>
#include <iostream>
#include <sensor_msgs/LaserScan.h>
#include <std_msgs/Float32.h>

std_msgs::Float32 minval;

void Callback_laser(const sensor_msgs::LaserScan::ConstPtr& scan)
{

minval = scan->ranges[0];
    for(int i=0;i<scan->ranges.size();i++)
    {
        if(scan->ranges[i]<minval or minval!=minval)
           minval=scan->ranges[i];
    }

}


int main(int argc, char** argv)
{
    ros::init(argc, argv, "subscribing"); 
    ros::NodeHandle n;

    ros::Subscriber laser_subscriber = n.subscribe<sensor_msgs::LaserScan>("scan", 1000, Callback_laser);

    ros::Publisher pub=n.advertise<std_msgs::Float32>("minval", 1000);

    ros::Rate loop_rate(10);

      while (ros::ok())
      {
         pub.publish(minval);
         ros::spinOnce();
         loop_rate.sleep();
      }


     return 0; 
}

Thanks in advance for your help.

edit retag flag offensive close merge delete

Comments

Hi, it would be useful if you could attach any error logs.

Martin Peris gravatar image Martin Peris  ( 2016-03-13 22:00:54 -0600 )edit

1 Answer

Sort by ยป oldest newest most voted
0

answered 2016-03-13 22:02:28 -0600

Reza1984 gravatar image

Thanks I solved my problem. Here is the code:

#include <ros/ros.h>
#include <iostream>
#include <sensor_msgs/LaserScan.h>
#include <std_msgs/Float32.h>

std_msgs::Float32 mindist;

void Callback_laser(const sensor_msgs::LaserScan::ConstPtr& scan)
{

float minval = scan->ranges[0];
    for(int i=0;i<scan->ranges.size();i++)
    {
        if(scan->ranges[i]<minval or minval!=minval)
           minval=scan->ranges[i];
    }

       ROS_INFO("min_range[%f] \n",minval);

       mindist.data=minval;

}



int main(int argc, char** argv)
{
    ros::init(argc, argv, "subscribing"); 

    ros::NodeHandle n;

    ros::Subscriber laser_subscriber = n.subscribe<sensor_msgs::LaserScan>("scan", 1000, Callback_laser);

    ros::Publisher pub=n.advertise<std_msgs::Float32>("mindist", 1000);

    ros::Rate loop_rate(10);

      while (ros::ok())
      {

         pub.publish(mindist);
         ros::spinOnce();
         loop_rate.sleep();
      }

     return 0; 

}
edit flag offensive delete link more

Question Tools

1 follower

Stats

Asked: 2016-03-13 21:22:50 -0600

Seen: 213 times

Last updated: Mar 13 '16