当前位置:网站首页>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
边栏推荐
- Golang learning essay
- 【MathType】利用MathType输出LaTex样式的公式
- About inquirerjs
- Minimum score of one question per day
- The interviewer only asked me five questions and the interview was over
- About the proposed signature file migration to industry standard format pkcs12
- Wildcard usage of go standard library FMT
- How to solve the problems when using TV focusable to package APK in uni app
- Stone from another mountain: Web3 investment territory of a16z
- 服务器安装jupyterlab以及远程登录配置
猜你喜欢

V-inline-date, similar to Ctrip, flying pig, time selection with price

Project training (XVII) -- personal work summary

MySQL download and installation

How to print infinite symbol in WPS

关于tkinter.Canvas 不显示图片的问题

30: Kakfa simulates JSON data generation and transmission

C language implementation of the classic eight queens problem

Introduction to convolutional neural network

pycharm add configutions

matplotlib画图中文乱码
随机推荐
How to turn on the hotspot for the mobile phone after the computer is connected to the network cable
Unity jsonutility failed to serialize list
Using OpenCV in go
MySQL - use field alias after where
[Stanford Jiwang cs144 project] lab1: streamreassembler
Quickly set the computer to turn off automatically
csdn涨薪技术之Jmeter接口测试数据库断言的实现与设计
Implementation and design of JMeter interface test database assertion for CSDN salary increase technology
[official document summary] writing standards for academic dissertations of National University of science and technology
Stack and queue practice (C language): Demon King's language
[soft test] software designer knowledge points sorting (to be updated)
MySQL performance optimization
四、入库管理功能的完善
【软考】软件设计师知识点整理(待更新)
Crypto JS reports uglifyjs error
Lecture on Compilation Principles
np. Understanding of axis in concatenate
Audiences with similar interests
Tweens of phaser3
路径字段是什么? ——竞价广告