This shows you the differences between two versions of the page.
| Both sides previous revisionPrevious revisionNext revision | Previous revision | ||
| en:ros:subscribepublish [2020/03/20 09:06] – tomykalm | en:ros:subscribepublish [Unknown date] (current) – external edit (Unknown date) 127.0.0.1 | ||
|---|---|---|---|
| Line 1: | Line 1: | ||
| + | ===== ROS Packages ===== | ||
| + | |||
| + | ROS has software for organized packages. The package may contain ROS nodes, data sets, configuration files, third-party software, or any other program needed to run the program. The purpose of libraries is to provide useful functions that can be easily used for different purposes. | ||
| + | |||
| + | The libraries can be installed using the Linux package manager //apt-get// or placed in the //src// directory of your workspace. The package called // | ||
| + | |||
| + | Install the package named // | ||
| + | |||
| + | $ sudo apt-get install ros-melodic-ros-tutorials | ||
| + | |||
| + | Once the package is successfully installed, the working environment must be recompiled: | ||
| + | |||
| + | $ catkin_make #compile the computing environment | ||
| + | |||
| + | ===== Ros Master ===== | ||
| + | |||
| + | ROS Master manages the communication between nodes. Every node registers at startup with the master. The //ros master// node must work. This node can be started with // | ||
| + | |||
| + | Open a new terminal window (Ctrl + Alt + T) and launch // | ||
| + | |||
| + | $ roscore | ||
| + | If everything is set up correctly, something should appear on the terminal: | ||
| + | |||
| + | ... logging to / | ||
| + | Checking log directory for disk usage. This may take a while. | ||
| + | Press Ctrl-C to interrupt | ||
| + | Done checking log file disk usage. Usage is <1GB. | ||
| + | | ||
| + | started roslaunch server http:// | ||
| + | ros_comm version 1.14.6 | ||
| + | | ||
| + | | ||
| + | SUMMARY | ||
| + | ======== | ||
| + | | ||
| + | PARAMETERS | ||
| + | * /rosdistro: melodic | ||
| + | * / | ||
| + | | ||
| + | NODES | ||
| + | | ||
| + | auto-starting new master | ||
| + | process[master]: | ||
| + | ROS_MASTER_URI=http:// | ||
| + | | ||
| + | setting /run_id to 83e74152-7a80-11eb-b6ad-ccaf78b12fc1 | ||
| + | process[rosout-1]: | ||
| + | started core service [/rosout] | ||
| + | | ||
| + | |||
| + | Only one //roscore// can be run at a time. Now ROS nodes can communicate with each other. | ||
| + | |||
| + | ===== ROS Nodes ===== | ||
| + | ROS nodes are actually programs that run on your PC. It's a single-purpose and executable program that can be compiled, run, and managed individually. Nodes are like building blocks of your program. Nodes are organized in packages. Each package may include several nodes. Each node should be registered with the ROS master: | ||
| + | |||
| + | {{ : | ||
| + | |||
| + | To run a node: | ||
| + | $ rosrun package_name node_name | ||
| + | To see the list of current active node: | ||
| + | $ rosnode list | ||
| + | If you need to get some information about a specific node: | ||
| + | $ rosnode info node_name | ||
| + | |||
| + | ===== ROS Topics ===== | ||
| + | Nodes communicate with each other through ROS topics. One node can publish and subscribes to topics. Actually, topics are a stream of messages. The figure below shows how a topic transfer message between two nodes while one is publishing and the other is subscribing to the topic. | ||
| + | |||
| + | {{ : | ||
| + | |||
| + | To see the list of active topics: | ||
| + | $ rostopic list | ||
| + | Subscribe and print the message inside a topic: | ||
| + | $ rostopic echo /topic_name | ||
| + | To get the information about a topic such as a publisher and subscribers and the type of the message: | ||
| + | $ rostopic info /topic_name | ||
| + | | ||
| + | ===== ROS Messages ===== | ||
| + | Nodes publish messages over topics. Messages define the data structure of the information that flows from one node to the other. It includes simple data structures such as integers, floats, booleans, string, etc. They are defined in the *.msg file. You can see the message type inside a topic by running //rostopic info / | ||
| + | To see the message structure of a specific type you need to run: | ||
| + | $ rosmsg show message_type | ||
| + | for example : | ||
| + | $ rosmsg show geometry_msgs/ | ||
| + | geometry_msgs/ | ||
| + | float64 x | ||
| + | float64 y | ||
| + | float64 z | ||
| + | geometry_msgs/ | ||
| + | float64 x | ||
| + | float64 y | ||
| + | float64 z | ||
| + | ===== Sample Publisher and Subscriber ===== | ||
| + | Above you installed a package called // | ||
| + | |||
| + | We use //rosrun// to execute the node. | ||
| + | |||
| + | Open a new terminal window (Ctrl + Alt + T) and run a node called //talker// there: | ||
| + | |||
| + | $ rosrun rospy_tutorials talker | ||
| + | Make sure //roscore// is running, you can't start a node without it. If the node is successfully started, the terminal should appear: | ||
| + | |||
| + | | ||
| + | | ||
| + | | ||
| + | | ||
| + | | ||
| + | | ||
| + | The publisher goes and publishes messages to /// | ||
| + | |||
| + | The second node is called // | ||
| + | |||
| + | Open a new terminal window (Ctrl + Alt + T) and execute a subscriber named // | ||
| + | |||
| + | $ rosrun rospy_tutorials listener | ||
| + | When messages sent by //talker// appear on the terminal, the node started successfully, | ||
| + | |||
| + | | ||
| + | | ||
| + | | ||
| + | | ||
| + | | ||
| + | | ||
| + | | ||
| + | | ||
| + | | ||
| + | | ||
| + | | ||
| + | | ||
| + | | ||
| + | | ||
| + | ROS-based robots usually have a large number of nodes, each with its own specific function. Nodes can be located on different computers and communicate through different protocols. But now we're getting to know the tools that come with ROS, which allow us to track and manage nodes and topics. | ||
| + | ==== Topics ==== | ||
| + | |||
| + | Entering the // | ||
| + | |||
| + | | ||
| + | | ||
| + | | ||
| + | | ||
| + | | ||
| + | | ||
| + | | ||
| + | Make sure roscore, talker, and listener work in the background. | ||
| + | |||
| + | We will display all the topics currently in use: | ||
| + | |||
| + | | ||
| + | / | ||
| + | / | ||
| + | / | ||
| + | /// | ||
| + | |||
| + | We use the //rostopic info// command to display the necessary information about the topic: | ||
| + | |||
| + | $ rostopic info/ | ||
| + | Type: std_msgs/ | ||
| + | |||
| + | | ||
| + | * / | ||
| + | |||
| + | | ||
| + | * / | ||
| + | You will see which nodes have subscribed to the topic and which ones are publishing to the topic, as well as the message type, which is // | ||
| + | |||
| + | We use //rostopic hz// to see the frequency of messages: | ||
| + | |||
| + | $ rostopic hz/chatter | ||
| + | | ||
| + | | ||
| + | min: 0.100s max: 0.100s std dev: 0.00017s window: 10 | ||
| + | We see that the average frequency of messages is about 10 Hz (Ctrl + C aborts the process). | ||
| + | |||
| + | We use //echo// to display messages sent to the topic: | ||
| + | |||
| + | $ rostopic echo /chatter | ||
| + | date: "hello world 1542817743.91" | ||
| + | --- | ||
| + | date: "hello world 1542817744.01" | ||
| + | --- | ||
| + | date: "hello world 1542817744.11" | ||
| + | --- | ||
| + | date: "hello world 1542817744.21" | ||
| + | --- | ||
| + | date: "hello world 1542817744.31" | ||
| + | --- | ||
| + | We use //pub// to post a message to: | ||
| + | |||
| + | $ rostopic pub /chatter std_msgs /String "data: 'hello world'" | ||
| + | | ||
| + | While listening to the subject at the same time, we see that the message we sent was published: | ||
| + | |||
| + | date: "hello world 1542817787.91" | ||
| + | --- | ||
| + | date: "hello world 1542817788.01" | ||
| + | --- | ||
| + | date: "hello world 1542817788.11" | ||
| + | --- | ||
| + | data: "hello world" | ||
| + | --- | ||
| + | date: "hello world 1542817788.21" | ||
| + | --- | ||
| + | date: "hello world 1542817788.31" | ||
| + | --- | ||
| + | date: "hello world 1542817788.41" | ||
| + | |||
| + | ==== Nodes ==== | ||
| + | |||
| + | We use //rosnode list// to display all currently running nodes. | ||
| + | |||
| + | $ rosnode list | ||
| + | / | ||
| + | / | ||
| + | / | ||
| + | We use //rosnode info// to display the necessary information about the node: | ||
| + | |||
| + | $ rosnode info / | ||
| + | | ||
| + | Node [/ | ||
| + | | ||
| + | * /chatter [std_msgs /String] | ||
| + | * /rosout [rosgraph_msgs /Log] | ||
| + | |||
| + | | ||
| + | |||
| + | | ||
| + | * / | ||
| + | * / | ||
| + | |||
| + | |||
| + | | ||
| + | Hold: 12,621 | ||
| + | | ||
| + | * topic: /chatter | ||
| + | | ||
| + | | ||
| + | | ||
| + | * topic: /rosout | ||
| + | | ||
| + | | ||
| + | | ||
| + | We can see which computer the node is running on and through which protocol it is connected to the ROS. | ||
| + | |||