The turtle in ros makes a circular motion and subscribes to the turtle’s position information

一.Based on the turtle display node, through topic publishing and coding, the small turtle can be controlled to make circular motion

  • Open Terminal 1 and enter the workspace ros_ws
cd ros_ws
  • Start the node (ros server)
roscore

  • Open a new terminal2, start the turtle node (turtlesim)
rosrun turtlesim turtlesim_node

The external link image transfer failed. The source site may have an anti-leeching mechanism. It is recommended to save the image. Come down and upload directly

  • Open Terminal 3 and check the started node information
rosnode list

  • The turtle’s node is turtlesim, and check the node’s information to find the subscriber topic /turtle1/cmd_vel
rosnode info turtlesim

  • View the information of the /turtle1/cmd_vel topic. You can find the message type of the topic as geometry_msgs/Twist
rostopic info /turtle1/cmd_vel

  • View the contents of the geometry_msgs/Twist message
rosmsg show geometry_msgs/Twist

  • Enter the src folder of the workspace ros_ws and create a new function package wugui_topic
cd ros_ws/src
catkin_create_pkg wugui_topic roscpp rospy std_msgs
  • Enter the function package wugui_topic and create the scripts folder
cd wugui_topic
mkdir scripts
  • Open vscode

  • Create the python file wu_tal.py under scripts and write the program

#! /usr/bin/env python
"""
    Write ROS nodes to control the little turtle to draw a circle
    Preparation:
        1. Get topic: /turtle1/cmd_vel
        2. Get the message type: geometry_msgs/Twist
        3. Before running, be sure to start the turtlesim_node node first
    Implementation process:
        1. Guide package
        2. Initialize ROS nodes
        3. Create a publisher object
        4. Create frequency object 10HZ
        5. Create message object
        6. Create published message content
"""
 
import rospy
from geometry_msgs.msg import Twist
 
if __name__ == "__main__":
    #Initialize ROS node
    rospy.init_node("wu_tal_p")
    
    #Create publisher object
    pub = rospy.Publisher("/turtle1/cmd_vel",Twist,queue_size=10)

    #Create frequency object (10HZ)
    rate = rospy.Rate(10)

    #Create message object
    message = Twist()

    # Create published message content
    message.linear.x = 2.0
    message.linear.y = 1.0
    message.linear.z = 0.0
    # Yaw angle in radians
    message.angular.x = 0.0
    message.angular.y = 0.0
    message.angular.z = 1.0
 
    while not rospy.is_shutdown(): #Determine whether rospy is closed, if not run the following code

        pub.publish(message)
        rate.sleep()
  • Uncomment lines 162-165 in the CMakeLists.txt file in wugui_topic in vscode and modify them to scripts/wu_tal.py

  • Enter the scripts folder and add executable permissions to the python file
chmod + x *.py
  • Return to Terminal 1, first close the roscore node manager in Terminal 1, and then open the node manager after compiling the workspace.
catkin_make
roscore
  • Terminal 2, restart the turtle node (turtlesim)
rosrun turtlesim turtlesim_node
  • Open Terminal 4 and enter the ros_ws workspace
cd ros_ws
  • Refresh and run the program
source ./devel/setup.bash
rosrun wugui_topic wu_tal.py

2.On the basis of keyboard control of turtle movement, coding realizes topic subscription and prints real-time position information of turtle

  • Open Terminal 1 to start the roscore node manager, and open a new Terminal 2 to start the turtle node
roscore # Terminal 1 starts
rosrun turtlesim turtlesim_node # Terminal 2 startup
  • Open Terminal 3 to view the information of /turtlesim node

    The publisher of this node information has three topics

    /rosout: For logging and debugging in ROS.

    /turtle1/color_sensor: used to obtain the color sensor information of the turtle1 turtle in the turtlesim package

    /turtle1/pose: Represents the posture information of the turtle

rosnode info /turtlesim

  • Check the information of the pose topic /turtle1/pose and you can find the topic type turtlesim/Pose
rostopic info /turtle1/pose

  • View turtlesim/Pose content
rosmsg show turtlesim/Pose

  • Create the python file wu_lis.py in the scripts folder of the function package wugui_topic and write the program
#! /usr/bin/env python
"""
    Get Little Turtle’s status
    Preparation:
        1. Get topic: /turtle1/pose
        2. Get the message type: turtlesim/Pose
        3. Before running, be sure to start the turtlesim_node node first
    Implementation process:
        1. Guide package
        2. Initialize ROS nodes
        3. Create service objects
        4. The callback function processes the request and generates a response
        5.spin function
"""


import rospy
from turtlesim.msg import Pose

# Callback
def coord(msg):
    rospy.loginfo("Turtle pose information: coordinates (%.2f,%.2f), orientation: %.2f, linear velocity: %.2f, angular velocity: %.2f",
                    msg.x,msg.y,msg.theta,msg.linear_velocity,msg.angular_velocity)

if __name__ == '__main__':

    #Initialize node
    rospy.init_node("wu_lis_p")

    #Create service object
    sub = rospy.Subscriber("/turtle1/pose",Pose,coord,queue_size=10)

    # blocking function
    rospy.spin()
  • Uncomment lines 162-165 in the CMakeLists.txt file in wugui_topic in vscode and add it as scripts/wu_lis.py

  • Return to Terminal 1, first close the roscore node manager in Terminal 1, and then open the node manager after compiling the workspace.
catkin_make
roscore
  • Terminal 2, restart the turtle node (turtlesim)
rosrun turtlesim turtlesim_node
  • Open a new terminal 4 and start the turtle control node
rosrun turtlesim turtle_teleop_key

  • Open terminal 5 and enter the ros_ws workspace
cd ros_ws
  • Refresh and run the program
source ./devel/setup.bash
rosrun wugui_topic wu_lis.py