当前位置:网站首页>Topic creation and running example of ROS
Topic creation and running example of ROS
2022-06-13 01:39:00 【lainegates】
Reference resources
ROS official wiki— establish ROS Messages and services
ROS official wiki— Write simple publishers and subscribers (C++)
ROS official wiki— Verify simple publishers and subscribers
Pre blog post
ROS Logical view of
ROS Working space for
List of articles
Definition of message
First, enter the created package
$ roscd beginner_tutorials
$ mkdir srv
Message definition file (*.msg)
Define a string message
$ echo "int64 num" > msg/Num.msg
Set up *.msg
File compilation information
stay packages.xml
Add the configuration
<build_depend>message_generation</build_depend> # Compile time dependent Libraries
<exec_depend>message_runtime</exec_depend> # Runtime dependent Libraries
stay CMakeLists.txt Make the following settings in
# add to ROS Dependency Library
find_package(catkin REQUIRED COMPONENTS
roscpp
rospy
std_msgs # Standard information , Basic data supported by the system , Such as int,float,...
message_generation # take *.srv The file to C++/Python Code
# stay packages.xml Has added
)
# Add just written *.msg file
add_message_files( # ROS Defined cmake command , inform ROS Custom files
FILES
Num.msg
)
# ROS Defined cmake command , inform ROS Generate (C++/Python) Message class dependency Library
generate_messages(
DEPENDENCIES
std_msgs
)
View the generated msg Information
# rosmsg show [message type]
$ rosmsg show beginner_tutorials/Num
msg Any... In the directory .msg Files will generate code for all supported languages .
- C++ The header file of the message will be generated in ~/catkin_ws/devel/include/beginner_tutorials/
.
- Python The script will be created in ~/catkin_ws/devel/lib/python2.7/dist-packages/beginner_tutorials/msg
.
Publisher code
#include "ros/ros.h" // ROS The header file
#include "std_msgs/String.h" // System defined string messages
#include <sstream>
int main(int argc, char **argv)
{
// The first two command line arguments
// The third node name , Node names cannot conflict !!!
ros::init(argc, argv, "talker");
// ROS System handle
// The first to apply for resources , The last handle destroys all requested resources
ros::NodeHandle n;
// The first parameter ,Topic Name ,roscore The publisher will be assigned to the corresponding subscriber by name , Then the two communicate point-to-point
// The second parameter ,Topic Cache size of , Those in excess will be discarded
ros::Publisher chatter_pub = n.advertise<std_msgs::String>("chatter", 1000);
// With 10 Hz Frequency of news release
ros::Rate loop_rate(10);
while (ros::ok())
{
std_msgs::String msg; // Message object
std::stringstream ss;
ss << "hello world " << count;
msg.data = ss.str();
ROS_INFO("%s", msg.data.c_str());
// Release the news
chatter_pub.publish(msg);
// Used to receive messages from other publishers , It can be omitted here
ros::spinOnce();
loop_rate.sleep();
}
return 0;
}
Subscriber code
#include "ros/ros.h"
#include "std_msgs/String.h"
// Callback function , How to handle the received message
void chatterCallback(const std_msgs::String::ConstPtr& msg)
{
ROS_INFO("I heard: [%s]", msg->data.c_str());
}
int main(int argc, char **argv)
{
// The first two command line arguments
// The third node name , Node names cannot conflict !!!
ros::init(argc, argv, "listener");
ros::NodeHandle n;
// The first parameter , The name of the publisher
// The second parameter , Number of cached messages
// The third parameter , Callback function for processing messages
ros::Subscriber sub = n.subscribe("chatter", 1000, chatterCallback);
// Call the message callback function upon receiving the message
// once ros::ok() return false,ros::spin() You're going to quit
ros::spin();
return 0;
}
Build the program
following cmake The script beginner_tutorials_generate_messages_cpp
see
cmake_minimum_required(VERSION 2.8.3)
project(beginner_tutorials)
find_package(catkin REQUIRED COMPONENTS roscpp rospy std_msgs genmsg)
## Declare as catkin package
catkin_package()
include_directories(include ${catkin_INCLUDE_DIRS})
# Publisher talker
add_executable(talker src/talker.cpp)
target_link_libraries(talker ${catkin_LIBRARIES})
add_dependencies(talker beginner_tutorials_generate_messages_cpp) # add_dependencies yes ROS Defined commands
# subscriber listener
add_executable(listener src/listener.cpp)
target_link_libraries(listener ${catkin_LIBRARIES})
add_dependencies(listener beginner_tutorials_generate_messages_cpp) # add_dependencies yes ROS Defined commands
Created executable talker and listener, By default, it will be placed in the package directory devel
In the space , namely ~/catkin_ws/devel/lib/<package name>
.
If you add a new package , You may need to specify –force-cmake Parameters to make catkin Force generation . See Use the workspace .
Verify the compiled program
$ cd ~/catkin_ws
$ catkin_make # Compile code
$ roscore # ROS System
$ source ./devel/setup.bash # Every time catkin_make After execution
Start publisher talker
$ rosrun beginner_tutorials talker # (C++)
You can see messages like the following :
[INFO] [WallTime: 1314931831.774057] hello world 1314931831.77
[INFO] [WallTime: 1314931832.775497] hello world 1314931832.77
Start the subscriber listener
$ rosrun beginner_tutorials listener # (C++)
You can see messages like the following :
[INFO] [WallTime: 1314931969.258941] /listener_17657_1314931968795I heard hello world 1314931969.26
[INFO] [WallTime: 1314931970.262246] /listener_17657_1314931968795I heard hello world 1314931970.26
边栏推荐
- Run Presto under docker to access redis and Bi presentation
- MySQL related summary
- [learn FPGA programming from scratch -21]: Advanced - Architecture - VerilogHDL coding specification
- Minimum score of one question per day
- Project training (XVII) -- personal work summary
- Rasa dialogue robot helpdesk (III)
- Golang inline mechanism & go development test
- Should the audience choose observation mode or positioning mode?
- Using OpenCV in go
- 五、库存查询功能的完善
猜你喜欢
The storage structure of a tree can adopt the parent representation, that is, the parent pointer array representation. Try to give the corresponding class definition. Each tree node contains two membe
September 3, 2021 visual notes
Record the VMware installation process of VMware Tools and some problems encountered
Introduction to convolutional neural network
Install pycharm process
Create a simple game interface using pyGame
Startup, connection and stop of MySQL service
ES6解构赋值
Memory learning book reference
Page optimization - Notes
随机推荐
This of phaser3 add. add. image
Facial expression recognition dataset
Minimum score of one question per day
Use koa to mock data and set cross domain issues
How does Apple add QQ email?
他山之石:a16z 的 Web3 投资版图
Wangdao machine test - Chapter 6 - maximum common divisor GCD and minimum common multiple LCM
FSOs forest simulation optimization model learning notes
Tkinter library installation
About constructive code blocks, static code blocks, and constructor execution order
Realization of flip animation
受众群体应该选择观察模式还是定位模式?
[soft test] software designer knowledge points sorting (to be updated)
#pragma comment(lib,“urlmon.lib“)
Anims of phaser3
MySQL - use field alias after where
Leetcode question brushing 03 stack
Tweens of phaser3
How to turn on the hotspot for the mobile phone after the computer is connected to the network cable
兴趣相似的受众群体