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

131

Categories

6

Tags

105

Follow

Archives

  • February 20268
  • November 20253
  • July 20252
  • May 20252
  • April 20259
  • March 202540
  • February 20259
  • January 202512
  • December 20246
  • November 20242
  • October 20244
  • September 20246
  • August 20241
  • July 20241
  • June 20241
  • May 20241
  • April 20244
  • March 20241
  • January 20241
  • December 20231
  • May 20231
  • August 20221
  • May 20226
  • April 20229

Recents

exist_label

2026-02-14

exist_label

Note

BAGEL-Unified-Multimodal-Pretraining

2026-02-06

BAGEL-Unified-Multimodal-Pretraining

Review

LingBot-VLA

2026-02-05

LingBot-VLA

Review

Mixture-of-Experts-Survey

2026-02-05

Mixture-of-Experts-Survey

Review

UniDiffuser

2026-02-03

UniDiffuser

Review

Tags

3D-Scene17
Atlas1
CADC1
CLIP11
CNN1
CV56
Chemistry1
Contrastive-Learning5
Csharp1
DINO3
DT1
Debate2
Diffusion2
DiffusionModel4
Discrete-Mathematics1
Embodied-AI18
Emoation1
Emotion9
FL1
FPN2
Foundation1
FoundationModel4
Functional programming1
Game1
Gated-NN3
Github1
HRI2
Haskell1
Hexo4
Hierarchical4
Html1
HumanoidRobot1
Image-Grounding2
Image-Text4
Image-generation2
Image2Text7
ImgGen3
ImitationLearning5
LLM15
LatentAction1
Latex1
Love2
ML8
MR/AR3
Message-Passing2
MoE2
Mod1
Multi-modal14
Multi-view1
MultiModal5
NLP6
NN7
Nodejs1
Object-Detection9
Open-Vocabulary11
OpenCV1
Panoptic1
Physical-Scene4
Plugin1
PoseEstimation3
Probability1
Promise1
Python1
Pytorch1
QML1
Quantum1
RL3
RNN3
ROS3
Reading3
Real2Sim2
Reconstruct13
Representation-Learning5
Research-paper97
RobotLearning13
Robotics29
SJTU-Lecture1
Scalability2
Scene-graph31
Scene-synthesis2
Segmentation7
Semantic14
Signals and Systems1
Sim2Real1
Snippets1
Subgraph1
Survey4
Task-Planning9
Tech Communication1
Transformer20
Translation-Embedding2
Travel1
Unified-Multimodal1
Unity1
VAE1
VLA2
VLM8
VLP5
VQ-VAE1
ViT5
Vim1
Visual-Relation23
WSL1
Web1
WorldModel2
Chen Yulin's BlogChen Yulin's Blog

© 2026 Chen Yulin  Powered by Hexo & Icarus

×