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

ROS publishing "/initialpose" topic in code

asked 2019-01-23 02:16:50 -0500

zinuok gravatar image

updated 2019-01-23 02:46:31 -0500

gvdhoorn gravatar image

Hello, I'm trying to publishing the topic "initialpose" whose type is geometry_msgs/PoseWithCovarianceStamped to amcl node.

I successfully made a node from following c++ source code (modified from rviz initial pose tool) The problem is that it does not publish any topic.

What's the problem?

#include <tf/transform_listener.h>
#include <geometry_msgs/PoseWithCovarianceStamped.h>

double pose(int flag);
void onPoseSet(double x, double y, double theta);


void onPoseSet(double x, double y, double theta)
{   
    ros::NodeHandle nh_;
    ros::Publisher pub_ = nh_.advertise<geometry_msgs::PoseWithCovarianceStamped> ("/initialpose", 1);
    ros::Rate loop_rate(10); // 10HZ = 0.1s
    ros::spinOnce();

    std::string fixed_frame = "map";
    geometry_msgs::PoseWithCovarianceStamped pose;
    pose.header.frame_id = fixed_frame;
    pose.header.stamp = ros::Time::now();

    // set x,y coord
    pose.pose.pose.position.x = x;
    pose.pose.pose.position.y = y;
    pose.pose.pose.position.z = 0.0;

    // set theta
    tf::Quaternion quat;
    quat.setRPY(0.0, 0.0, theta);
    tf::quaternionTFToMsg(quat, pose.pose.pose.orientation);
    pose.pose.covariance[6*0+0] = 0.5 * 0.5;
    pose.pose.covariance[6*1+1] = 0.5 * 0.5;
    pose.pose.covariance[6*5+5] = M_PI/12.0 * M_PI/12.0;

    // publish
    ROS_INFO("x: %f, y: %f, z: 0.0, theta: %f",x,y,theta);
    pub_.publish(pose);
}

int main(int argc, char** argv)
{
    // setting
    ros::init(argc, argv, "set_pose_node");

    onPoseSet(0.968507917975, -4.61491396765, 1);   
    return 0;
}
edit retag flag offensive close merge delete

1 Answer

Sort by ยป oldest newest most voted
0

answered 2019-01-23 03:57:08 -0500

Hamid Didari gravatar image

in your code onPoseSet func run for once and then your app terminate by return you can change your code like this:

#include <tf/transform_listener.h>
#include <geometry_msgs/PoseWithCovarianceStamped.h>

double pose(int flag);
void onPoseSet(double x, double y, double theta);


void onPoseSet(double x, double y, double theta)
{   
    ros::NodeHandle nh_;
    ros::Publisher pub_ = nh_.advertise<geometry_msgs::PoseWithCovarianceStamped> ("/initialpose", 1);
    ros::Rate loop_rate(10); // 10HZ = 0.1s
    ros::spinOnce();

    std::string fixed_frame = "map";
    geometry_msgs::PoseWithCovarianceStamped pose;
    pose.header.frame_id = fixed_frame;
    pose.header.stamp = ros::Time::now();

    // set x,y coord
    pose.pose.pose.position.x = x;
    pose.pose.pose.position.y = y;
    pose.pose.pose.position.z = 0.0;

    // set theta
    tf::Quaternion quat;
    quat.setRPY(0.0, 0.0, theta);
    tf::quaternionTFToMsg(quat, pose.pose.pose.orientation);
    pose.pose.covariance[6*0+0] = 0.5 * 0.5;
    pose.pose.covariance[6*1+1] = 0.5 * 0.5;
    pose.pose.covariance[6*5+5] = M_PI/12.0 * M_PI/12.0;

    // publish
    ROS_INFO("x: %f, y: %f, z: 0.0, theta: %f",x,y,theta);
    pub_.publish(pose);
}

int main(int argc, char** argv)
{
    // setting
    ros::init(argc, argv, "set_pose_node");

    ros::Rate loop_rate(10);
    while (ros::ok()){
          onPoseSet(0.968507917975, -4.61491396765, 1); 
          loop_rate.sleep(); 
          }    
   return 0;
}
edit flag offensive delete link more

Question Tools

1 follower

Stats

Asked: 2019-01-23 02:16:50 -0500

Seen: 3,689 times

Last updated: Jan 23 '19