借助 ros的可视化工具 显示自己的数据

ROS中显示坐标系
https://blog.csdn.net/ktigerhero3/article/details/88234338

(十二)ROS在rviz中的不同位置显示文字(visualization_msgs::MarkerArray的使用)
https://blog.csdn.net/ktigerhero3/article/details/80483802

https://blog.csdn.net/ktigerhero3/category_6805916.html

http://wiki.ros.org/cn/navigation/Tutorials

https://blog.csdn.net/RainVictor1/article/details/79674863

#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 

main(int argc, char **argv)
{
    /* "showpath" 可以用 rosnode list 看到   */
    ros::init(argc, argv, "odom_tf_path");

    ros::NodeHandle n;
    /* "trajectory" 可以用 rostopic list 看到 */
    ros::Publisher path_pub = n.advertise<nav_msgs::Path>("trajectory", 1, true);

    ros::NodeHandle odom_n;

    ros::Publisher odom_pub = odom_n.advertise<nav_msgs::Odometry>("odom", 50);

    tf::TransformBroadcaster odom_broadcaster;

    double x = 0.0;
    double y = 0.0;
    double th = 0.0;
    double vx = 0.1;
    double vy = -0.1;
    double vth = 0.1;

    ros::Time current_time, last_time;
    current_time = ros::Time::now();
    last_time = ros::Time::now();

    nav_msgs::Path path;
    path.header.stamp = current_time;
    path.header.frame_id = "odom";

    ros::Rate loop_rate(1);
    while (ros::ok())
    {

        current_time = ros::Time::now();
        //compute odometry in a typical way given the velocities of the robot
        double dt = (current_time - last_time).toSec();
        double delta_x = (vx * cos(th) - vy * sin(th)) * dt;
        double delta_y = (vx * sin(th) + vy * cos(th)) * dt;
        double delta_th = vth * dt;

        x += delta_x;
        y += delta_y;
        th += delta_th;

        geometry_msgs::Quaternion odom_quat = tf::createQuaternionMsgFromYaw(th);

        geometry_msgs::TransformStamped odom_trans;
        odom_trans.header.stamp = current_time;
        odom_trans.header.frame_id = "odom";
        odom_trans.child_frame_id = "base_link";

        odom_trans.transform.translation.x = x;
        odom_trans.transform.translation.y = y;
        odom_trans.transform.translation.z = 0.0;
        odom_trans.transform.rotation = odom_quat;

        // send the transform
        odom_broadcaster.sendTransform(odom_trans);

        geometry_msgs::PoseStamped this_pose_stamped;
        this_pose_stamped.pose.position.x = x;
        this_pose_stamped.pose.position.y = y;

        this_pose_stamped.pose.orientation.x = odom_quat.x;
        this_pose_stamped.pose.orientation.y = odom_quat.y;
        this_pose_stamped.pose.orientation.z = odom_quat.z;
        this_pose_stamped.pose.orientation.w = odom_quat.w;

        this_pose_stamped.header.stamp = current_time;
        this_pose_stamped.header.frame_id = "odom";
        path.poses.push_back(this_pose_stamped);

        path_pub.publish(path);
        ros::spinOnce(); // check for incoming messages

        // publish the odometry message over ROS
        // 发布nav_msgs/Odometry消息,使得导航从中获取速度信息。
        nav_msgs::Odometry odom;
        odom.header.stamp = current_time;
        odom.header.frame_id = "odom";
        // child_frame_id -> base_link 坐标系是发送速度信息的坐标系
        odom.child_frame_id = "base_link";

        // set the position
        odom.pose.pose.position.x = x;
        odom.pose.pose.position.y = y;
        odom.pose.pose.position.z = 0.0;
        odom.pose.pose.orientation = odom_quat;

        // set the velocity

        odom.twist.twist.linear.x = vx;
        odom.twist.twist.linear.y = vy;
        odom.twist.twist.angular.z = vth;

        // publish the message
        odom_pub.publish(odom);

        last_time = current_time;
        loop_rate.sleep();
    }

    return 0;
}

你可能感兴趣的:(ROS)