ROS learning notes ---- topic programming

Keywords: ROS ARM

catalogue

1, Basic knowledge

2, Workspaces and feature packs

3, Programming implementation of publisher

4, Programming implementation of subscriber

1, Basic knowledge

  • Topic -- asynchronous communication programming
    • An important bus used to transmit data between nodes;
    • use Publish / subscribe Model, the data is transmitted from the publisher to the subscriber, and the subscriber or publisher of the same topic can Not unique .
  • Message -- topic data
    • It has certain types and data structures, including standard types provided by ROS and user-defined types;
    • Use the programming language independent. msg file definition to generate the corresponding code file during compilation.
  • Service -- synchronous communication mechanism
    • Using the client / server (C/S) model, the client sends the request data, and the server returns the response data after processing;
    • The request and response data structures are defined by using the. srv file independent of the programming language, and the corresponding code files are generated during the compilation process.
  • Parameter -- global shared dictionary
  • Function package
    • The basic unit in ROS software, including node source code, configuration file, data definition, etc.
  • Package manifest
    • Record the basic information of the function package, including author information, license information, dependency options, compilation flags, etc.
  • Meta packages
    • Organize multiple function packages for the same purpose

  • ROS common commands (complete the commands with tab)
    • roscore ------ start ROSMaster
    • rosrun ------ run a node in the function package
      • //       Function package name a node in the function package  
        $rosrun turtlesim turtlesim_node
               
    • rostopic
      •  //         10 times a second (frequency) topic name message data type
        $rostopic pub -r 10 /turtle1/cmd_vel geometry_msgs/Twist "Message structure specific data" 
                    
        ros Speed in miles m/s,Angular velocity unit rad/s
    • rosservice
      • //The terminal can request a service as a client
        $rosservice list
        
        //Publish service request
        $rosservice call/spawn "service content "
    • rosnode
      • $rosnode list //View all topic nodes. Note: / rosout is the ros default topic
        $rosnode info /turtlesim //View the specific information of a node, such as which topics are published and which topics are subscribed to.
    • rosparam
    • rosmsg
    • rossrv
    • rqt_graph - visual tool to view the calculation diagram running in the system
    • rosbag
      • //Topic record
        $rosbag record -a -O cmd_record
        
        //Topic recurrence
        $rosbag play cmd_record.bag

2, Workspaces and feature packs

Workspace:

  • src: code space
  • build: compile space
  • devel: development space
  • install: installation space

1. Create workspace

$mkdir -p ~/catkin_ws/src
$cd ~/catkin/src
$catkin_init_workspace

2. Compile workspace

$cd ~/catkin_ws/
$catkin_make

//When generating the install space
$catkin_make install

3. Set environment variables

$source devel/setup.bash

4. Check environment variables

$echo $ROS_PACKAGE_PATH

Feature Pack:

Note: the function package is the smallest unit to place the source code. You cannot directly place the source code in src.

1. Create function package

$cd ~/catkin_ws/src
$catkin_create_pkg test_pkg std_msgs rospy roscpp

2. Compile function package

$cd ~/catkin_ws
$catkin_make
$source ~/catkin_ws/devel/setup.bash

3, Programming implementation of publisher

  1. Create Feature Pack

$cd ~/catkin_ws/src
$catkin_create_pkg learning_topic rosscpp rospy std_msgs geometry_msgs turtlesim

2. Create publisher

  • Initialize ROS node
  • Register the node information with the ROS Master, including the published topic name and the message type in the topic
  • Create message data
  • Circularly publish messages at a certain frequency

Put the prepared publisher's source code in ~ / catkin_ws/src/learning_topic/src

 velocity_publisher.cpp

#include <ros/ros.h>
#include <geometry_msgs/Twist.h>

int main(int argc, char **argv)
{
	// ROS node initialization
	ros::init(argc, argv, "velocity_publisher");

	// Create node handle
	ros::NodeHandle n;

	// Create a Publisher with the publication name / turnle1 / CMD_ The topic of vel and the message type is geometry_msgs::Twist, queue length 10
	ros::Publisher turtle_vel_pub = n.advertise<geometry_msgs::Twist>("/turtle1/cmd_vel", 10);

	// Sets the frequency of the cycle
	ros::Rate loop_rate(10);

	int count = 0;
	while (ros::ok())
	{
	    // Initialize geometry_msgs::Twist type message
		geometry_msgs::Twist vel_msg;
		vel_msg.linear.x = 0.5;
		vel_msg.angular.z = 0.2;

	    // Release news
		turtle_vel_pub.publish(vel_msg);
		ROS_INFO("Publsh turtle velocity command[%0.2f m/s, %0.2f rad/s]", 
				vel_msg.linear.x, vel_msg.angular.z);

	    // Delay according to cycle frequency
	    loop_rate.sleep();
	}

	return 0;
}

Write Compilation Rules

  •   Set the code to be compiled and the generated executable
  • Set up link library
CMakeLists.txt
#########
##build##
#########

add_executable(velocity_publisher src/velocity_publisher.cpp)
target_link_libraries(velocity_publisher ${catkin_LIBRARIES})

Compile and run publisher

$cd ~/catkin_ws
$catkin_make
//Environment variables must be set before executing the program
$source devel/setup.bash
$roscore
$rosrun turtlesim turtlesim_node
$rosrun learning_topic velocity_publisher

4, Programming implementation of subscriber

  1. Implementation subscriber

  • Initialize ROS node
  • Subscribe to required topics
  • The loop waits for the topic message and enters the callback function after receiving the message
  • Complete message processing in callback function

pose_subscriber.cpp

/**
 * This routine will subscribe to / turnle1 / pose topic, and the message type is turnlesim:: pose
 */
 
#include <ros/ros.h>
#include "turtlesim/Pose.h"

// After receiving the subscribed message, it will enter the message callback function
void poseCallback(const turtlesim::Pose::ConstPtr& msg)
{
    // Print the received message
    ROS_INFO("Turtle pose: x:%0.6f, y:%0.6f", msg->x, msg->y);
}

int main(int argc, char **argv)
{
    // Initialize ROS node
    ros::init(argc, argv, "pose_subscriber");

    // Create node handle
    ros::NodeHandle n;

    // Create a Subscriber, subscribe to the topic named / turnle1 / pose, and register the callback function poseCallback
    ros::Subscriber pose_sub = n.subscribe("/turtle1/pose", 10, poseCallback);

    // Loop waiting callback function
    ros::spin();

    return 0;
}

  Write Compilation Rules

CMakeLists.txt
#########
##build##
#########

add_executable(pose_subscriber src/pose_subscriber.cpp)
target_link_libraries(pose_subscriber ${catkin_LIBRARIES})

Compile and run publisher  

$cd ~/catkin_ws
$catkin_make
//Environment variables must be set before executing the program
$source devel/setup.bash
$roscore
$rosrun turtlesim turtlesim_node
$rosrun learning_topic pose_subscriber

 

Posted by clay1 on Fri, 19 Nov 2021 00:04:48 -0800