[ROS] Practical operation_topic release

Requirement description: Coding realizes turtle motion control, and allows the turtle to make circular motions.

Result demonstration:

img

Implementation analysis:

  1. The turtle motion control is implemented. There are two key nodes, one is the turtle motion display node turtlesim_node, and the other is the control node. The two are subscribed to publish mode to achieve communication . The turtle motion display node can be directly called, and the motion control node is used before The turtle_teleop_key is controlled by the keyboard, and now you need to customize the control node.

  2. When the control node is self-implemented, it is first necessary to understand the topics and messages used in the communication between the control node and the display node, which can be obtained by using the ros command in combination with the calculation graph.

  3. After understanding the topic and news, you can write the motion control node in C++ or Python, and publish the news according to certain logic through the specified topic.

Implementation process:

  1. Get topic and message information through the calculation graph combined with the ros command.

  2. Coding realizes the motion control node.

  3. Start roscore, turtlesim_node and custom control nodes, and view the running results.

1. Topic and news acquisition

Preparation: First start the keyboard control tortoise movement case.

1.1 topic acquisition

View topics and messages between the keyboard control and the turtle GUI.

Get topic: /turtle1/cmd_vel

View the topic through the calculation graph and start the calculation graph:

rqt_graph

Or list topics through rostopic:

rostopic list

1.2 Message acquisition

Get message type: geometry_msgs/Twist

rostopic type /turtle1/cmd_vel
或
rostopic info /turtle1/cmd_vel

Get the message format:

rosmsg info geometry_msgs/Twist

Response result:

geometry_msgs/Vector3 linear
  float64 x
  float64 y
  float64 z
geometry_msgs/Vector3 angular
  float64 x
  float64 y
  float64 z

The xyz under linear (linear velocity) correspond to the velocity in the x, y and z directions (unit is m/s);

The xyz under angular (angular velocity) correspond to the speed of roll on the x axis, pitch on the y axis and yaw on the z axis (unit is rad/s).

additional materials:

Radian: The unit radian is defined as the central angle when the arc length is equal to the radius.

Yaw, roll and pitch

Coordinate system diagram:

img

Yaw: [turn around the z axis]

img

Pitch: [Rotate around the y axis]

img

Rolling: [turn around the x axis]

img

 geometry_msgs/Vector3 linear
   float64 x
   float64 y
   float64 z
 geometry_msgs/Vector3 angular
   float64 x
   float64 y
   float64 z
For the tortoise in this case, only the x direction of the linear velocity and the z reverse of the angular velocity change

Verification, print message

rostopic echo /turtle1/cmd_vel

Control turtle movement

rostopic pub -r 10 /turtle1/cmd_vel geometry_msgs/Twist "linear:
  x: 2.0
  y: 0.0
  z: 0.0
angular:
  x: 0.0
  y: 0.0
  z: 2.0" 

 

2. Implement the release node

The function package that needs to be relied on to create the function package: roscpp rospy std_msgs geometry_msgs

Implementation plan A: C++

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

/*
    需求:发布速度消息  
            话题:/turtle1/cmd_vel
            消息:geometry_msgs/Twist

        1 包含头文件
        2 初始化ros节点
        3 创建节点句柄
        4 创建发布对象
        5 发布逻辑实现
        6 回旋函数【可选】 spinOnce()

*/


int main(int argc, char  *argv[])
{
        setlocale(LC_ALL,"");

       // 2 初始化ros节点
        ros::init(argc,argv,"my_control");

       // 3 创建节点句柄
       ros::NodeHandle nh;

       // 4 创建发布对象
       ros::Publisher pub = nh.advertise<geometry_msgs::Twist>("/turtle1/cmd_vel",10);

       // 5 发布逻辑实现
        ros::Rate rate(10);             //设置发布频率
        //组织被发布的消息
        geometry_msgs::Twist twist;
        //线速度
        twist.linear.x = 1.0;     //浮点类型
        twist.linear.y = 0.0 ;
        twist.linear.z = 0.0;

        //角速度
        twist.angular.x = 0.0;
        twist.angular. y =0.0;
        twist.angular.z = 1.0;

        //循环发布
        while(ros::ok())
        {
            pub.publish(twist);
            //休眠
            rate.sleep();
            // 6 回旋函数【可选】 spinOnce()
            ros::spinOnce();
        }
    return 0;
}

Configuration file

Compile

run

roscore
rosrun turtlesim turtlesim_node 

source ./devel/setup.bash
rosrun plumbing_test test01_pub_twist

to sum up:

This case uses a publish-subscribe method. The publisher is the test01_pub_twist node. Its purpose is to make the turtle move in a circle; the subscriber is the turtle GUI.

First, you need to know the topic between the turtle GUI and the keyboard control node, so that test01_pub_twist "replaces" the keyboard control node; [check with rqt_graph or rostopic list]

Then use the topic to obtain the message type [using rostopic type topic name or rostopic info topic name], and use the message type to obtain the message format [rosmsg info message type];

Finally, modify the data in the message format to complete the corresponding functions.

Guess you like

Origin blog.csdn.net/Zhouzi_heng/article/details/114691770