Ros
4 May 2023

ROS Publisher

ROS Learning Notes

ROS Learning Notes 02

image

Keypoints:

  • Workspace and package
  • Publisher

Workspace and package

  • Workspace: a folder that contains all the packages
  • Contains:
    • src: source code
    • build: build files
    • devel: executable files
    • install: install files
  • How to create a workspace and package:
    ## create a folder named catkin_ws in home directory
    mkdir -p ~/catkin_ws/src  
    cd ~/catkin_ws/src
    catkin_init_workspace
    
    ## compile the workspace
    cd ~/catkin_ws
    catkin_make ## to compile all the source code in src folder
    catkin_make install ## to install all the executable files in install folder
    
    ## package.xml and CMakeLists.txt are the two files that are used to compile the source code, about the configuration of the workspace and package
    
    ## set up the environment variables
    source devel/setup.bash
    
    ## check the environment variables
    echo $ROS_PACKAGE_PATH
    
    ## create a package
    cd ~/catkin_ws/src
    catkin_create_pkg package_name std_msgs rospy roscpp
    cd ~/catkin_ws
    catkin_make
    

Publisher

  • Publisher: publish messages to a topic.
  • How to create a publisher in C++:
    • Cpp code:
      # include <ros/ros.h> // include the ros library
      # include <geometry_msgs/Twist.h>
      // In ROS, the geometry_msgs/Twist message type is often used to send velocity commands to a robot. This message type contains two vectors, linear and angular, which represent the robot's linear and angular velocities, respectively. By including the Twist.h header file, you are making the geometry_msgs/Twist message type available to your C++ code, allowing you to create and manipulate Twist messages.
      
      int main(int argc, char **argv)
      // argc stands for "argument count" and is an integer representing the number of arguments passed to the program when it is executed. This includes the name of the program itself as the first argument. argv stands for "argument vector" and is an array of strings representing the arguments passed to the program. argv[0] contains the name of the program itself, while argv[1] through argv[argc-1] contain the other arguments passed to the program.Together, int main(int argc, char **argv) specifies the entry point of the program, including any command line arguments that are passed to it.
      
      {
          // ROS Wiki: http://wiki.ros.org/roscpp/Overview/Initialization%20and%20Shutdown
      
          // initialize the ros node: ros::init(argc, argv, "my_node_name");
          ros::init(argc, argv, "velocity_publisher");
      
          // start the ros node: ros::NodeHandle nh;
          ros::NodeHandle nh;
      
          // create a publisher
          // The NodeHandle::advertise() methods are used to create a ros::Publisher which is used to publish on a topic, 
          ros::Publisher turtle_vel_pub = nh.advertise<geometry_msgs::Twist>("/turtle1/cmd_vel", 100); 
          // 100 is the buffer size, "turtle1/cmd_vel" is the topic name, geometry_msgs::Twist is the message type
      
          // set the frequency of the loop
          ros::Rate loop_rate(11);
      
          int count = 0;
          // loop
          while (ros::ok()) 
          // ros::ok() returns false if: a SIGINT is received (Ctrl-C)
          {
              // create a message
              geometry_msgs::Twist msg; // create a message of type geometry_msgs::Twist, consisting of two vectors, linear and angular, which represent the robot's linear and angular velocities, respectively.
              msg.linear.x = 0.5;
              msg.angular.z = 0.2;
      
              // publish the message
              turtle_vel_pub.publish(msg); // publish the message to the topic
              ROS_INFO("Publising: linear.x = %0.2f m/s, angular.z = %0.2f rad/s", msg.linear.x, msg.angular.z);  // print the message to the terminal
      
              loop_rate.sleep();  // extend the loop to keep the frequency
          }
          return 0;
      }
      
    • Edit CMakeLists.txt in Build module:
      add_executable(velocity_publisher src/vel_publisher.cpp) ## add the executable file to CMakeLists.txt
      target_link_libraries(velocity_publisher ${catkin_LIBRARIES}) ## link the executable file to the library
      
    • Compile the source code:
      cd ~/catkin_ws
      catkin_make
      source devel/setup.bash ## or add this line to ~/.bashrc
      rosrun package_name executable_file_name # rosrun topic_learning velocity_publisher
      
    • Python code:
      import rospy
      from geometry_msgs.msg import Twist
      
      def velocity_publisher():
              
          #  Initialize the node with the name "velocity_publisher"
          rospy.init_node('velocity_publisher', anonymous=True)
              
          #  Create a publisher object
          turtle_vel_pub = rospy.Publisher('/turtle1/cmd_vel', Twist, queue_size=10)
              
          # Set the loop rate
          rate = rospy.Rate(10)
              
          while not rospy.is_shutdown():
              # Initialize the geometry_msgs/Twist message
              vel_msg = Twist()
              vel_msg.linear.x = 0.5
              vel_msg.angular.z = 0.2
                  
              # Publish the message
              turtle_vel_pub.publish(vel_msg)
              rospy.loginfo("Publish turtle velocity command[%0.2f m/s, %0.2f rad/s]", vel_msg.linear.x, vel_msg.angular.z)
                  
              # Extend the loop
              rate.sleep()
                  
                  
      if __name__ == '__main__':
          try:
              velocity_publisher()
          except rospy.ROSInterruptException:
              pass
      

Tags:
0 comments