Ask Your Question
0

Problem with publishing Twist message from a node

asked 2016-02-11 19:02:33 -0600

percy_liu gravatar image

updated 2016-02-12 09:48:11 -0600

I wrote a piece of C++ code which should publish twist message and make a fake turtlebot start moving in Rviz. But after running it, the robot had no reaction.Here is my code.

#define PI 3.14
#include "ros/ros.h"
#include "geometry_msgs/Twist.h"

int main(int argc, char **argv)
{
ros::init(argc, argv, "test");
ros::NodeHandle n;
ros::Publisher cmd_vel_pub=
    n.advertise<geometry_msgs::Twist>("cmd_vel",5);
    int rate=50;
ros::Rate loop_rate(rate);

double linear_speed=0.2;
double goal_distance=1.0;
double linear_duration=goal_distance/linear_speed;
double angular_speed=1.0;
double goal_angle=PI;
double angular_duration=goal_angel/angular_speed;
geometry_msgs::Twist twist_msg;
twist_msg.linear.x=twist_msg.linear.y=twist_msg.linear.z=0;
twist_msg.angular.x=twist_msg.angular.y=twist_msg.angular.z=0;

while(ros::ok())
{
    twist_msg.linear.x=linear_speed;
    int ticks=static_cast<int>(linear_duration*rate);
    for(int j=0;j<ticks;j++)
    {
        cmd_vel_pub.publish(twist_msg);
        loop_rate.sleep();
    }

    twist_msg.linear.x=0;
    cmd_vel_pub.publish(twist_msg);
    ros::Duration(1).sleep();

    twist_msg.angular.z=angular_speed;
    ticks=static_cast<int>(goal_angle*rate);
    for(int j=0;j<ticks;j++)
    {
        cmd_vel_pub.publish(twist_msg);
        loop_rate.sleep();
    }

    twist_msg.angular.z=0;
    cmd_vel_pub.publish(twist_msg);
    ros::Duration(1).sleep();

}

twist_msg.linear.x=0;
twist_msg.angular.z=0;
cmd_vel_pub.publish(twist_msg);
ros::Duration(1).sleep();

return 0;

}

I also add the following code in CMakeList.txt

include_directories(include ${catkin_INCLUDE_DIRS})
add_executable(test src/talker.cpp)
target_link_libraries(test ${catkin_LIBRARIES})

Add this to package.xml

<build_depend>geometry_msgs</build_depend>
<run_depend>geometry_msgs</run_depend>
edit retag flag offensive close merge delete

2 Answers

Sort by ยป oldest newest most voted
0

answered 2016-02-11 20:30:13 -0600

Willson Amalraj gravatar image

The problem could be because of the incorrect topic name. As far as I remember, the correct topic for moving a turtlebot would be /cmd_vel_mux/input/teleop . Try changing cmd_vel to /cmd_vel_mux/input/teleop in your code.

edit flag offensive delete link more

Comments

There was no error reported when catkin_ws wan running, but no executable file was generated. I don't know how did that happen.

percy_liu gravatar imagepercy_liu ( 2016-02-12 02:09:24 -0600 )edit

If talker.cpp is the name of you cpp file, once you call catkin_make, if there is no error, the executable will be generated in your_workspace/devel/lib. BTW you need to add find_package(geometry_msgs) in your CMakeList.txt

Willson Amalraj gravatar imageWillson Amalraj ( 2016-02-12 04:20:41 -0600 )edit

I added the sentence as you suggested. And no error reported during catkin_make, but still no executable file. Anyway, thanks for your help!

percy_liu gravatar imagepercy_liu ( 2016-02-12 04:39:46 -0600 )edit

what is the output of rospack find your_package_name_here ?

Willson Amalraj gravatar imageWillson Amalraj ( 2016-02-12 05:13:25 -0600 )edit
1

answered 2016-02-12 19:48:57 -0600

First you should monitor the cmd_vel topic from the command line using "rostopic echo /cmd_vel" or using the rqt Topic Monitor plugin. After verifying that no messages are showing up on that topic you can look at the code to see why.

Some new users think that the "cmd_vel_pub.publish(twist_msg)" calls will actually "publish" their data. That call actually queues up the data for publication. You need at least one "ros::spinOnce()" call inside the while loop which will send out the queued messages.

edit flag offensive delete link more

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account.

Add Answer

Question Tools

1 follower

Stats

Asked: 2016-02-11 19:01:22 -0600

Seen: 1,275 times

Last updated: Feb 12 '16