【ROS学习】初学ROS的小项目

ROS学习:turtle控制

img

turtle旋转

首先查看turtlesim里面的节点,使用rqt_graph查看节点流图rqt_graph

结果展示

/*
 * @Description: 这是一个让turtle旋转的ros publisher
 * @Version: 
 * @Autor: copy
 * @Date: 2022-03-12 17:28:55
 * @LastEditors: copy
 * @LastEditTime: 2022-03-12 17:42:00
 */
#include <ros/ros.h>
#include "geometry_msgs/Twist.h"

#define PI 3.141592653
int main(int argc,char** argv)
{
    ros::init(argc,argv,"turtle_twist");
    ros::NodeHandle nh;
    ros::Publisher Pub = nh.advervise<geometry_msgs::Twist>("/turtle1/cmd_vel",10);
    geometry_msgs::Twist twist;
    twist.linear.x = 1;
    twist.angular.z = PI;
    ROS_INFO("\t现在的速度是%f,偏转速度是%f\t",twist.linear.x,twist.angular.z = PI);
    ros::Rate rate(10);//10Hz
    while(ros::OK)
    {
        Pub.publish(twist);

        rate.sleep();
        ros::spin();            //后面的代码不再执行ros::spinOnce();
    }

    return 0;
}

接收turtle位置

使用rostopic list 和rostopic info [topic]查看turtle的topic,image-20220314110604770

  • 结果展示

    image-20220315120427620

跟踪turtle

使用rosservice list 和 rosservice info [service]查看turtle的service-----/Spawn。image-20220314145138555

  • rospackage

    • rospy
    • roscpp
    • std_msgs
    • turtlesim
    • tf
  • turtle_tf_broadcaster.cpp

    turtle_tf_broadcaster

  • turtle_tf_listener.cpp

    turtle_tf_listener

  • start_demo_with_listener.launch

    image-20220315115814890

  • 结果展示

直接控制image-20220315115934120无超调,但是反应慢,稳态误差大

PID控制

image-20220315161802801

image-20220315120111915超调量大,但是反应迅速,稳态误差较小

相关代码在我的github的turtle库中

posted @ 2023-06-03 23:15  cccopy  阅读(57)  评论(0编辑  收藏  举报