赞
踩
- cd ~
- mkdir catkin_ws
- cd catkin_ws
- mkdir src
- cd src
- catkin_init_workspace #初始化工作空间
- cd ~/catkin_ws
- catkin_make
- source ~/catkin_ws/devel/setup.bash #将工作空间添加到环境变量
- cd ~/catkin_ws/src
- catkin_create_pkg mytest std_msgs roscpp
- cd ~/catkin_ws/src/mytest/src
- touch subscriber.cpp
- #include<ros/ros.h>
- #include<tf/transform_broadcaster.h>
- #include<nav_msgs/Odometry.h>
- #include<geometry_msgs/Twist.h>
- #include<iostream>
-
- void callback(const geometry_msgs::Twist& cmd_vel)
- {
- ROS_INFO("Received a /cmd_vel message!");
- ROS_INFO("Linear Components:[%f,%f,%f]",cmd_vel.linear.x,cmd_vel.linear.y,cmd_vel.linear.z);
- ROS_INFO("Angular Components:[%f,%f,%f]",cmd_vel.angular.x,cmd_vel.angular.y,cmd_vel.angular.z);
- // Do velocity processing here:
- // Use the kinematics of your robot to map linear and angular velocities into motor commands
- // v_l = ...
- // v_r = ...
-
- // Then set your wheel speeds (using wheel_left and wheel_right as examples)
- // wheel_left.set_speed(v_l)
- // wheel_right.set_speed(v_r)
- }
-
- int main(int argc, char** argv)
- {
- ros::init(argc, argv, "cmd_vel_listener");
- ros::NodeHandle n;
- ros::Subscriber sub = n.subscribe("cmd_vel", 1000, callback); //订阅cmd_vel话题
- ros::spin();
-
- /*
- //http://answers.ros.org/question/129506/subscriber-not-seeing-cmd_vel-messages/
- ros::Rate loop_rate(10);
- while( n.ok() )
- {
- ros::spin();
- }
- */
- return 1;
- }
- vim ~/catkin_ws/src/mytest/CMakeLists.txt
- 在文件末尾添加如下语句
- add_executable(subscriber src/node_subscriber.cpp)
- target_link_libraries(subscriber ${catkin_LIBRARIES})
- cd ~/catkin_ws
- catkin_make
打开一个终端
roscore
打开另一个终端
rosrun mytest subscirber
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。