Establishment of workspace and function package under ROS, and creation of nodes to realize communication between nodes

I am a beginner in ROS, and I learned it completely from the book "Efficient Programming for ROS Robots", the third edition. When I study Section 2.4, some inexplicable problems often appear, so I searched the Internet through various kinds of information, and finally completed the book. programming on! The following is my summary of the establishment of the ROS workspace, the creation of nodes, and the implementation of communication

   Step 1: Create a ROS workspace

   We create multi-level directories directly via mkdir -p.

   $ mkdir -p ~/dev/catkin_ws/src //catkin_ws is the workspace directory, the original file directory under the src workspace

   $ cd  ~/dev/catkin_ws/src

   $ catkin_init_workspace //Create a CMakeList.txt file in the workspace, which is built in the ~/dev/catkin_ws/src directory

   $ cd ~/dev/catkin_ws //Move to the workspace directory to compile the workspace

   $ catkin_make //Compile the workspace, only in the workspace directory

   At this point, if you enter the ll command, you can see that new files are automatically created in the workspace directory, namely the build and devel folders.

   $ source devel/setup.bash // Reload the setup.bash file, you can also reload by closing the terminal and reopening

   Finally, add a command line at the end of the ~/.bashrc file to configure our environment $ echo "source /opt/ros/kinetic/setup.bash" >> ~/.bashrc If it has been configured when installing ros, Then there is no need to configure it. The ROS version we use is ROS kinetic. If you want to use other versions, please change the configuration statement yourself!

   Step 2: Create ROS package and meta package

   $ cd ~/dev/catkin_ws/src

   $ catkin_create_pkg chapter2_tutorials std_msgs roscpp //chapter2_tutorials is the function package name, std_msgs and roscpp are dependencies, std_msgs contains common message types, representing basic data types and other basic message structures, such as multidimensional arrays. roscpp uses C++ to implement various functions of ROS. If you also want to use the Python library, you also need to add rospy at the end

   Step 3: Compile the ROS function package

    $ cd  ~/dev/catkin_ws

    $ catkin_make //Compile all packages in the workspace, still need to compile in the workspace

   Step 4: Create Nodes

    $ roscd chapter2_tutorials/src //The node is to be created under the source file under the function package, so it must be moved to this directory first. If the execution of the statement fails, you can use $ cd ~/dev/catkin_ws/src/chapter2_tutorials/src instead .

   Then create two files, named example1_a.cpp and example1_b.cpp, the first file will send the data with the node name, and the second file will accept the data and display it on the command line.

    $ vim example1_a.cpp //Create and open the example1_a.cpp file

    Copy the following into this file:

 #include "ros/ros.h"
#include "std_msgs/String.h"//  
#include <sstream>///////////include the file type to use

int main(int argc, char * *argv)
{
  ros::init(argc, argv, "example1_a");//Start the node and set its name, the name must be unique  
  ros::NodeHandle n;//Set the handle of the node process  
  ros::Publisher pub = n.advertise<std_msgs::String>("message", 1000);//Set the node as the publisher, and inform the node manager of the type and name of the published topic. The first parameter is the name of the message: message, and the second is the buffer track size. If the topic publishes data quickly
, set the buffer to 1000 messages.  
  ros::Rate loop_rate(10);//Set the sending data channel frequency to 10Hz  
  while (ros::ok())//When receiving a key message of Ctrl+C or ROS stops running the current node, ros::ok () The library will execute the stop node running command  
  {
//////Create a message variable, the variable type must meet the requirements of sending data  
    std_msgs::String msg;
    std::stringstream ss;
    ss << "
    msg.data = ss.str();
    //ROS_INFO("%s", msg.data.c_str());  
    pub.publish(msg);//The message is published  
    ros::spinOnce();//If When a subscriber appears, ROS will update and read all topics  
    loop_rate.sleep();//Suspend the program at a frequency of 10Hz  
  }
  return 0;
}
     After saving, create and open the second file

     $ vim example1_b.cpp

     Copy the following:

#include "ros/ros.h"
#include "std_msgs/String.h"

void messageCallback(const std_msgs::String::ConstPtr& msg)//This function is called every time the node receives a message, you can use or
process data. In this example, the received data will be output in the command window to  
{
  ROS_INFO("I heard: [%s]", msg->data.c_str());//used to output data in the command line  
}

int main(int argc, char **argv)
{
   ros::init(argc, argv, "example1_b");
   ros::NodeHandle n;
    /////Create a subscriber and get the message named message from the topic message data. Set the buffer to 1000 messages, and handle the callback function of the message handle > messageCallback  
   ros::Subscriber sub = n.subscribe("message", 1000, messageCallback);
     //// The ros::spin() library is a node read Take the data channel message response loop, when the message arrives, the callback function will be called. When Ctrl+C is pressed, the node
exits the message loop and the loop ends.  
   ros::spin();
   return 0;

    Step 5: Compile the node. In fact, there will be basically no problems in the previous steps, and there may be problems later.

    Next, you need to edit the CMakeList.txt file yourself, and use the following statement to edit it

    $ rosed chapter2_tutorials CMakeList.txt //If there is no error in this step, congratulations, but I have a problem when executing this step: I can't find the chapter2_tutorials package, I searched for a long time and found that before compiling, I need to modify CMakeLists. txt file and manifest.xml file, you need to compile the function package through rosmake

    $ rosmake chapter2_tutorials //After compiling this step, the above problems should not occur, and then perform the previous step to successfully open the CMakeList.txt file

    After opening the CMakeList.txt file, add the following to the end:

include_directories(
include
  ${catkin_INCLUDE_DIRS}
)

add_executable(example1_a src/example1_a.cpp)
add_executable(example1_b src/example1_b.cpp)

add_dependencies(example1_a
chapter2_tutorials_generate_messages_cpp)
add_dependencies(example1_b
chapter2_tutorials_generate_messages_cpp)

target_link_libraries(example1_a ${catkin_LIBRARIES})
target_link_libraries(example1_b ${catkin_LIBRARIES})

   This part of the content must not be wrong. For the first time, I also wrote the last s of LIBRARIES in the last line as lowercase, which caused the second file to be unable to receive information. It took me a long time to find it!

   The next step is to compile the CMakeList.txt file, or you have to go to the workspace before compiling

   $ cd ~/dev/catkin_ws

   $ catkin_make --pkg chapter2_tutorials

   Next, to start ROS on the computer, you need to first call:

   $ roscore

   After starting ros, you need to open another terminal, which we call terminal 2;

   Step 6: Run the Node

   First check whether ROS is running, use the rosnode list command in terminal 2 to query

   $ rosnode list

   Next run the first node in Terminal 2:

   $ rosrun chapter2_tutorials example1_a //There may be a problem here: the chapter2_tutorials package cannot be found, you need to continue editing the chapter2_tutorials package in the terminal, load the setup.bash file, and perform the following two steps

   $ catkin_make --pkg chapter2_tutorials    

   $ source devel/setup.bash

   Then re-run the above step, namely $ rosrun chapter2_tutorials example1_a , a blank line appears after the successful operation, and it is in a waiting state.

   Then go ahead and open another terminal, Terminal 3, and run the second node in this terminal:

   $ rosrun chapter2_tutorials example1_b //If there is a problem that the chapter2_tutorials package cannot be found, please refer to the previous step.

   If it runs successfully, you will find that the command line will keep repeating [ INFO] [************.**********]: I heard : [I am the example1_a node]

   The link is established successfully!

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325385607&siteId=291194637