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

Help publishing to cmd_vel and subscribing to LaserScan

asked 2011-10-24 02:03:55 -0600

jlo gravatar image

Hey guys, I am trying to program a publisher for cmd_vel and a subscriber of base_scan (sensor_msgs/LaserScan, specifically the array ranges). For the first one I know I have to give a command like this:

rostopic pub /cmd_vel geometry_msgs/Twist -r 10000 '[10, 0, 0]' '[0, 0, 5]'

I have tried giving it to the command prompt, and it works. Now, including that in a publisher (I have tried modifying the "talker" one of the tutorials - from here). The thing is I don't know how to modify it to include the command I want to give to it. I tried changing the following line to:

ss << "/cmd_vel geometry_msgs/Twist -r 10000 '[10, 0, 0]' '[0, 0, 5]'";

that belongs to this loop

  while (ros::ok())
  { std_msgs::String msg;
    std::stringstream ss;
    ss << "/cmd_vel geometry_msgs/Twist -r 10000 '[10, 0, 0]' '[0, 0, 5]'";
    msg.data = ss.str();
    ROS_INFO("%s", msg.data.c_str());
    chatter_pub.publish(msg);
    ros::spinOnce();
    loop_rate.sleep();
    ++count;
  }

But as expected, changing just the string to the command I want to give didn't work. There probably is a really simple way of doing it, but I don't know how.

As for the second task, the subscriber to LaserScan, I know that I have to use the ranges array to visualize the meters to the obstacles, but first of all I don't know how to visualize the array in the command prompt. Once I know that with the previous task answered, I may be able to find out how to do it.

Thanks a lot for all the help and support! (not only here but in all the questions I've been having lately!)

edit retag flag offensive close merge delete

3 Answers

Sort by » oldest newest most voted
5

answered 2011-10-24 02:38:04 -0600

updated 2011-10-24 02:39:31 -0600

I think the confusion is that in the publisher/subscriber tutorial, the message type just happens to be a string, whereas the message type you want to be publishing is not a string, but a geometry_msgs/Twist. At the top of your while loop, you should have something like

geometry_msgs::Twist msg;

then you need to fill in the message's data fields:

msg.linear.x = 10;
msg.angular.z = 5;

The syntax you're using is only for the command line tool rostopic pub.

To visualize a laser scan, you should check out rviz.

edit flag offensive delete link more
2

answered 2011-10-24 23:26:11 -0600

jlo gravatar image

Thanks, that clears a lot, I'll give it a try. For the laser scan, I have tried rviz, but what I'd like to do is to access the 'ranges' array within sensor_msgs/LaserScan. How could I do that? I want to access those numbers to program the case when an obstacle is too close (let's say, 2 meters) and then the robot should turn left (for instance).

I changed the code adapting it to my case to this:

#include "ros/ros.h"
//#include "geometry_msgs/Twist.h"
#include "std_msgs/String.h"

#include <sstream>

int main(int argc, char **argv)
{
  ros::init(argc, argv, "talker");
  ros::NodeHandle n;
  ros::Publisher chatter_pub = n.advertise<geometry_msgs::Twist>("cmd_vel", 1000);   
  ros::Rate loop_rate(10);

 int count = 0;
  while (ros::ok())
  {
    geometry_msgs::Twist msg;
    msg.linear.x = 10;
    msg.angular.z = 5;    
    chatter_pub.publish(msg);
    ros::spinOnce();
    loop_rate.sleep();
    ++count;
  }
  return 0;
}

But this is the problems I get to compile.

  /home/alonsoj1/ros_workspace/prueba/src/publisher.cpp: In function ‘int main(int, char**)’:
  /home/alonsoj1/ros_workspace/prueba/src/publisher.cpp:12: error: ‘geometry_msgs’ was not declared in this scope
  /home/alonsoj1/ros_workspace/prueba/src/publisher.cpp:12: error: parse error in template argument list
  /home/alonsoj1/ros_workspace/prueba/src/publisher.cpp:12: error: no matching function for call to ‘ros::NodeHandle::advertise(const char [8], int)’
  /home/alonsoj1/ros_workspace/prueba/src/publisher.cpp:20: error: ‘geometry_msgs’ is not a class or namespace
  /home/alonsoj1/ros_workspace/prueba/src/publisher.cpp:20: error: expected ‘;’ before ‘msg’
  /home/alonsoj1/ros_workspace/prueba/src/publisher.cpp:21: error: ‘msg’ was not declared in this scope

I get I have to define geometry_msgs, but how can I know with '#include' to put in the beginning of the code?

Thanks!

edit flag offensive delete link more
0

answered 2017-03-30 13:02:09 -0600

grifo gravatar image

Hey!

Uncomment your second include and change it to:

#include <geometry_msgs/Twist.h>
edit flag offensive delete link more

Question Tools

2 followers

Stats

Asked: 2011-10-24 02:03:55 -0600

Seen: 18,315 times

Last updated: Mar 30 '17