This is a simple example to run publisher and subscriber with ros service in ROS Melodic.
- Simple ROS publisher-subscriber
- Simple ROS Service
- TF Broadcaster
- Gtest and ROSTest
- rosbag
- Ubuntu 18.04 (LTS)
- ROS Melodic
-
Make sure you have ROS Melodic installed in your computer. If not refer to site.
-
Create a workspace:
mkdir -p ~/pubsub_ws/src cd ~/pubsub_ws/src
-
Clone the repository into the workspace:
git clone https://github.com/anubhavparas/beginner_tutorials.git
-
Build the workspace:
cd ~/pubsub_ws catkin_make or catkin build (preferred) source devel/setup.bash
-
Run using launch file: This will spawn:
- a ros service node:
/anubhavp/modify_string_service
- a publisher node:
/anubhavp/talker
- a subscriber node:
/anubhavp/listener
roslaunch beginner_tutorials beginner_tutorials.launch buffer_size:=1 loop_rate:=10 # arguments are: # buffer_size - default=1 # loop_rate - default=10 # is_record_bag - default=false # bagfile - default=recorded_topics.bag
- a ros service node:
-
To enable rosbag recording. By default it is disabled.
- This will store the rosbag recording in a file
home/<username>/.ros/recorded_topics.bag
roslaunch beginner_tutorials beginner_tutorials.launch is_record_bag:=true
- This will store the rosbag recording in a file
-
To call the service:
rosservice call /anubhavp/modify_string_service "inputstring: 'Let us learn ROS.'"
-
/talker
node also broadcasts tf frame information from/world
to/talk
frame. Example.
-
Start ros master node in a separate terminal:
roscore
-
To run the service run the following in a new terminal:
cd ~/pubsub_ws/ source devel/setup.bash rosrun beginner_tutorials modify_string_service
-
To run the publisher run the following in a new terminal:
cd ~/pubsub_ws/ source devel/setup.bash rosrun beginner_tutorials talker 1 10
-
To run the subscriber run the following in a new terminal:
cd ~/pubsub_ws/ source devel/setup.bash rosrun beginner_tutorials listener
-
To call the service:
rosservice call /modify_string_service "inputstring: 'Let us learn ROS.'"
-
To run rosbag recording run the following in a new terminal (assuming the above nodes are running):
cd ~/pubsub_ws/ source devel/setup.bash rosbag record -O record_topics.bag -a --duration 15 # -a: to record all topics # -O record_topics.bag: name of the output bag file # --duration 15: record for 15 sec
-
To verify the recording:
- Examining the bag file:
rosbag info <your bagfile>
- Stop the /talker node and only start the listner node
cd ~/pubsub_ws/ source devel/setup.bash rosrun beginner_tutorials listener
- Replay the bag file:
rosbag play record_topics.bag
- You can verify that the listener is now subscribing to the recorded messages.
- Examining the bag file:
-
Run a sample recorded rosbag file.
- In a new terminal start
rosmaster
:roscore
- In a new terminal start the listner of the
/chatter
topic:cd ~/pubsub_ws/ source devel/setup.bash rosrun beginner_tutorials listener
- In a new terminal replay the bag file. This is a 15 sec recording of many topics including
/chatter
topic.cd ~/pubsub_ws/ source devel/setup.bash roscd beginner_tutorials cd results/bag
- Examining the bag file: Sample output.
rosbag info record_topics.bag
- Play the bag file: Sample output.
rosbag play record_topics.bag
- In a new terminal start
- Build the test:
cd ~/pubsub_ws/ catkin_make catkin_make run_tests (to run all the test executables) (or) catkin_make run_tests talker_test (to run specific executable)
- tests can be run using
rostest
too, once$catkin_make run_tests
have been run once.source devel/setup.bash rostest beginner_tutorials publisher_test.test