Chen Yulin's BlogChen Yulin's Blog
HomeArchivesCategoriesTagsAbout
ROS-Publisher
Posted 2022-04-15Updated 2024-11-02Note3 minutes read (About 378 words)   visits

ROS-Publisher

Study note for ROS publisher.

Model of Topic

  • Publisher(turtle Velocity)
    • –[Message(geometry_msgs::Twist)]–>
  • Topic (/turtle1/cmd_vel)
    • –[Message(geometry_msgs::Twist)]–>
  • Subscriber(turtlesim)

Programming realization

Take turtlesim as the example.
We write a program acting like a publisher and sending controlling messages.

Create package

1
2
$ cd catkin_ws/src
$ catkin_create_pkg learning_topic roscpp rospy std_msgs geometry_msgs turtlesim

can see:

1
2
$ ls
CMakeLists.txt include package.xml src

How to realize a publisher?

  • Initialize ROS node
  • register node info from ROS Master, including name of the topic and msg type
  • create message data
  • publish message at a specific frequency

Code

create velocity_publisher.cpp file in learning_topic/src

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
#include <ros/ros.h>
#include <geometry_msgs/Twist.h>

int main(int argc, char *argv[])
{
//init the node
ros::init(argc,argv,"velocitypublisher");

//create node handle
ros::NodeHandle n;

//create a publisher, who publish topic names '/turtle1/cmd_vel', message type 'geometry_msgs:Twist', queue length 10
ros::Publisher turtle_vel_pub = n.advertise<geometry_msgs::Twist>("/turtle1/cmd_vel",10);

//set loop rate
ros::Rate loop_rate(10);
while (ros::ok()) {
//initialize geometry_msgs:Twist msg
geometry_msgs::Twist vel_msg;
vel_msg.linear.x = 0.5;
vel_msg.angular.z = 0.2;

//publish msgs
turtle_vel_pub.publish(vel_msg);
ROS_INFO("Publish turtle velocity command at %0.2f m/s, %0.2f rad/s",
vel_msg.linear.x,vel_msg.angular.z);

//delay
loop_rate.sleep();
}
return 0;
}

What is queue length?

  • msgs can be published very fast while the data transmission rate is limited, so the queue act as a buffer
  • first in, first thrown, keeping the remained msgs in the queue relatively new

Configure the compile rule of publisher

Steps:

  • set the code need to be complied and the generated excutable file
  • set the link library

Add two lines to learning\_topic/CMakeLists.txt (in build part):

1
2
add_executable(velocity_publisher src/velocity_publisher.cpp)
target_link_libraries(velocity_publisher ${catkin_LIBRARIES})

Compile

1
2
3
4
5
6
$ cd ~/ROS_study/catkin_ws
$ catkin_make
$ source ./devel/setup.zsh
$ roscore
$ rosrun turtlesim turtlesim_node
$ rosrun learning_topic velocity_publisher

Then, you can see turtle:

And outputs:

1
2
3
4
5
...
[ INFO] [1650088100.376299177]: Publish turtle velocity command at 0.50 m/s, 0.20 rad/s
[ INFO] [1650088100.476312726]: Publish turtle velocity command at 0.50 m/s, 0.20 rad/s
[ INFO] [1650088100.576304995]: Publish turtle velocity command at 0.50 m/s, 0.20 rad/s
...

ROS-Publisher

http://chen-yulin.github.io/2022/04/15/ROS-Publisher/

Author

Chen Yulin

Posted on

2022-04-15

Updated on

2024-11-02

Licensed under

#ROS
ROS-Subscriber
Regular-Expression

Comments

Chen Yulin

Chen Yulin

SJTU student

Manchester by the Sea

Posts

261

Categories

8

Tags

187

Follow

Archives

  • May 20257
  • April 202517
  • March 202545
  • February 202512
  • January 202513
  • December 202412
  • November 20244
  • October 202418
  • September 202417
  • August 202413
  • July 20243
  • June 20245
  • May 202413
  • April 202417
  • March 20241
  • January 20241
  • December 20231
  • May 202346
  • August 20221
  • May 20226
  • April 20229

Recents

Matlab on Archlinux

2025-05-13

Matlab on Archlinux

Note

2025-05-13

Part-level Dataset Available for Augmentation

Note

Feature Pyramid Networks for Object Detection

2025-05-08

Feature Pyramid Networks for Object Detection

Review

Write Latex in Neovim on Archlinux

2025-05-07

Write Latex in Neovim on Archlinux

Note

Davinci-resolve on Archlinux

2025-05-07

Davinci-resolve on Archlinux

Note

Tags

3D-Scene4
6-D3
AI10
AIGC1
AR2
Academic1
Algorithm1
Aliyun1
App2
Atlas1
BS41
Beautify1
Behaviorism1
Business1
C1
CADC1
CD1
CLIP5
CNN1
CV28
Capstone10
Communication2
Contrastive-Learning3
Control2
Csharp9
Css1
Cuda3
DD1
DINO4
DT1
Dataframe1
Debate5
Debugger1
Diffusion1
Discrete-Mathematics1
Docker1
Docs2
Dynamic-programming1
ESP322
Education1
Embeded-System9
Embodied-AI8
Emoation1
Emotion12
Ethic1
FL1
FPN2
Family1
Federated-Learning1
Foundation1
Functional programming1
GPT3
Game5
Gated-NN2
Git7
Github1
Godot3
HPC1
HRI2
Haskell1
Health2
Hexo10
Hierarchical1
Html5
Humanism1
Hyprland2
IK1
Image-Grounding1
Image-Text5
Image-generation1
ImitationLearning3
Jolt1
Json1
LLM12
LSP2
Latex2
Life4
LinearAlgebra1
Linux21
Live2d1
Love3
Lua1
MBTI1
ML5
MR/AR3
Mason1
Math3
Meme1
Message-Passing1
Mod3
Motivation1
Movie1
Multi-modal6
Multi-view1
Music5
NLP4
NN4
Network2
Nodejs5
Numpy1
Nvim9
Object-Detection4
Open-Vocabulary9
OpenCV1
Oral1
PHD1
PSY5
Pandas2
Panoptic1
Path1
Philosophy3
PhysX1
Physical-Scene4
Physics-engine1
Pio2
Planning1
Plugin8
PoseEstimation3
Postgraduate1
Prefab1
Probability1
Python26
Pytorch1
QML1
Quantum1
RNN4
ROS3
Reading19
Real2Sim1
Reconstruct9
Regex2
Reinforcement-learning1
Repository5
Representation-Learning1
Research-paper86
Robot1
Robotics16
SJTU-Lecture1
SQL2
SSH2
Scene-graph29
Scene-synthesis1
Science-fiction1
Scrap1
Script2
Segmentation7
Semantic12
Shader3
Shell4
Signals and Systems1
Sim2Real1
Sklearn1
Snippets1
Society4
Star-rail1
Subgraph1
Submodule1
Supervised-learning2
Survey3
TC1
TOEFL1
Task-Planning6
Tasks4
Tech Communication1
Torch4
Transformer11
Translation-Embedding2
Travel2
Unity20
Unsupervised-learning1
VLM5
VLP2
Version-management1
ViT4
VideoEditing2
Vim1
Visual-Relation20
WSL1
Waybar1
Wayland1
Web1
Website1
Well-being1
Window-manager2
YKLL3
Zen2
🐱1
Chen Yulin's BlogChen Yulin's Blog

© 2025 Chen Yulin  Powered by Hexo & Icarus

×