当前位置:网站首页>ROS "topic" programming implementation
ROS "topic" programming implementation
2022-06-27 16:57:00 【melodic18】
( One ) Publisher (publisher) Programming

Create a workspace and compile
Create Feature Pack :
catkin_create_pkg learning_topic roscpp rospy std_msgs geometry_msgs turtlesim
c++ The code is as follows :
/**
* The routine will publish turtle1/cmd_vel topic of conversation , Message type geometry_msgs::Twist
*/
#include <ros/ros.h>
#include <geometry_msgs/Twist.h>
int main(int argc, char **argv)
{
// ROS Node initialization
ros::init(argc, argv, "velocity_publisher");
// Create node handle
ros::NodeHandle n;
// Create a Publisher, The release is called /turtle1/cmd_vel Of topic, The message type is geometry_msgs::Twist, The queue length 10
ros::Publisher turtle_vel_pub = n.advertise<geometry_msgs::Twist>("/turtle1/cmd_vel", 10);
// Set the frequency of the cycle
ros::Rate loop_rate(10);
int count = 0;
while (ros::ok())
{
// initialization geometry_msgs::Twist Type of message
geometry_msgs::Twist vel_msg;
vel_msg.linear.x = 0.5;
vel_msg.angular.z = 0.2;
// Release the news
turtle_vel_pub.publish(vel_msg);
ROS_INFO("Publsh turtle velocity command[%0.2f m/s, %0.2f rad/s]",
vel_msg.linear.x, vel_msg.angular.z);
// Delay according to the cycle frequency
loop_rate.sleep();
}
return 0;
}
Return to the root directory of the function package , open cmake.list, stay build Write below :
add_executable(velocity_publisher src/velocity_publisher.cpp)
target_link_libraries(velocity_publisher ${catkin_LIBRARIES})
Open the terminal , Run the following command :
roscore
rosrun turtlesim turtlesim_node
rosrun learning_topic velocity_publisher

( Two ) subscriber (subscriber) Programming
The goal is : Subscribe to turtles pose Information ( Postures )
c++ The code is as follows :
/**
* This routine will subscribe to /turtle1/pose topic of conversation , Message type turtlesim::Pose
*/
#include <ros/ros.h>
#include "turtlesim/Pose.h"
// After receiving the subscription message , Will enter the message callback function
void poseCallback(const turtlesim::Pose::ConstPtr& msg)
{
// Print the received message
ROS_INFO("Turtle pose: x:%0.6f, y:%0.6f", msg->x, msg->y);
}
int main(int argc, char **argv)
{
// initialization ROS node
ros::init(argc, argv, "pose_subscriber");
// Create node handle
ros::NodeHandle n;
// Create a Subscriber, The subscription is called /turtle1/pose Of topic, Register callback function poseCallback
ros::Subscriber pose_sub = n.subscribe("/turtle1/pose", 10, poseCallback);
// Loop waiting for callback function
ros::spin();
return 0;
}
Return to the root directory of the function package , open cmake.list, stay build Write below :
add_executable(pose_subscriber src/pose_subscriber.cpp)
target_link_libraries(pose_subscriber ${catkin_LIBRARIES})
After compiling , Enter the following command at the terminal :
roscore
rosrun turtlesim turtlesim_node
We can see , A terminal releases messages , Another subscription message , The turtle is running as shown in the figure :

边栏推荐
- Ti Click: quickly set up tidb online laboratory through browser | ti- team interview can be conducted immediately
- 2/15 topology sorting +dfs (the order of specified directions is very important) +bfs
- Oracle concept 3
- New method of cross domain image measurement style relevance: paper interpretation and code practice
- What is the level 3 password complexity of ISO? How often is it replaced?
- Leetcode daily practice (longest substring without repeated characters)
- 国家食品安全风险评估中心:不要盲目片面追捧标签为“零添加”“纯天然”食品
- Hongmeng makes efforts! HDD Hangzhou station · offline salon invites you to build ecology
- About MySQL: the phenomenon and background of the problem
- tensorflow求解泊松方程
猜你喜欢

字节跳动埋点数据流建设与治理实践

Oracle concept II

d3dx9_ How to repair 40.dll? Win10 system d3dx9_ What if 40.dll is lost?

【多线程】线程通信调度、等待集 wait() 、notify()

Kubernetes基础自学系列 | Ingress API讲解

Alibaba cloud liupeizi: Inspiration from cloud games - innovation on the end

3.4 fixed number of cycles II

Detailed explanation of various GPIO input and output modes (push-pull, open drain, quasi bidirectional port)

Raspberry pie preliminary use

LeetCode 124. Binary tree maximum path sum - binary tree series question 8
随机推荐
The time of localdatetime type (2019-11-19t15:16:17) is queried with the time range of Oracle
#yyds干货盘点#简述chromeV8引擎垃圾回收
Nemo of pulseaudio (22)
Oracle concept 3
What is the level 3 password complexity of ISO? How often is it replaced?
Autodesk NavisWorks 2022 software installation package download and installation tutorial
Under the influence of external factors, how to manage internal resources and reduce costs is the key
Pragma once Usage Summary
事务的隔离级别详解
印象深刻的问题
等保三级密码复杂度是多少?多久更换一次?
【Pygame小遊戲】這款“吃掉一切”遊戲簡直奇葩了?通通都吃掉嘛?(附源碼免費領)
Deeply digitise, lead cloud nativity and serve more developers
LeetCode每日一练(两数之和)
锚文本大量丢失的问题
事件监听机制
Open source 23 things shardingsphere and database mesh have to say
C語言教師工作量管理系統
Array represents a collection of several intervals. Please merge all overlapping intervals and return a non overlapping interval array. The array must exactly cover all the intervals in the input. 【Le
d3dx9_ Where is 35.dll? d3dx9_ Where can I download 35.dll