Implement Subscriber 1. code part (1) python 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 import rospyfrom turtlesim.msg import Posedef poseCallback (msg ): rospy.loginfo("Turtle pose: x:%0.6f, y:%0.6f" , msg.x, msg.y) def pose_subscriber (): rospy.init_node('pose_subscriber' , anonymous=True ) rospy.Subscriber("/turtle1/pose" , Pose, poseCallback) rospy.spin() if __name__ == '__main__' : pose_subscriber()
(2)C++ 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 33 34 35 #include <ros/ros.h> #include "turtlesim/Pose.h" void poseCallback (const turtlesim::Pose::ConstPtr& msg) { ROS_INFO ("Turtle pose: x:%0.6f, y:%0.6f" , msg->x, msg->y); } int main (int argc, char **argv) { ros::init (argc, argv, "pose_subscriber" ); ros::NodeHandle n; ros::Subscriber pose_sub = n.subscribe ("/turtle1/pose" , 10 , poseCallback); ros::spin (); return 0 ; }
2. set up env (1) C++ go to the CMakeLists.txt
file, add these two command here
1 2 add_executable(pose_subscriber src/pose_subscriber.cpp) target_link_libraries(pose_subscriber ${catkin_LIBRARIES})