当前位置:   article > 正文

ROS学习笔记-创建订阅cmd_vel话题节点_ros cmd节点

ros cmd节点

创建工作空间

  1. cd ~
  2. mkdir catkin_ws
  3. cd catkin_ws
  4. mkdir src
  5. cd src
  6. catkin_init_workspace #初始化工作空间
  7. cd ~/catkin_ws
  8. catkin_make
  9. source ~/catkin_ws/devel/setup.bash #将工作空间添加到环境变量

创建功能包

  1. cd ~/catkin_ws/src
  2. catkin_create_pkg mytest std_msgs roscpp

创建节点

  1. cd ~/catkin_ws/src/mytest/src
  2. touch subscriber.cpp

订阅者节点cpp

  1. #include<ros/ros.h>
  2. #include<tf/transform_broadcaster.h>
  3. #include<nav_msgs/Odometry.h>
  4. #include<geometry_msgs/Twist.h>
  5. #include<iostream>
  6. void callback(const geometry_msgs::Twist& cmd_vel)
  7. {
  8. ROS_INFO("Received a /cmd_vel message!");
  9. ROS_INFO("Linear Components:[%f,%f,%f]",cmd_vel.linear.x,cmd_vel.linear.y,cmd_vel.linear.z);
  10. ROS_INFO("Angular Components:[%f,%f,%f]",cmd_vel.angular.x,cmd_vel.angular.y,cmd_vel.angular.z);
  11. // Do velocity processing here:
  12. // Use the kinematics of your robot to map linear and angular velocities into motor commands
  13. // v_l = ...
  14. // v_r = ...
  15. // Then set your wheel speeds (using wheel_left and wheel_right as examples)
  16. // wheel_left.set_speed(v_l)
  17. // wheel_right.set_speed(v_r)
  18. }
  19. int main(int argc, char** argv)
  20. {
  21. ros::init(argc, argv, "cmd_vel_listener");
  22. ros::NodeHandle n;
  23. ros::Subscriber sub = n.subscribe("cmd_vel", 1000, callback); //订阅cmd_vel话题
  24. ros::spin();
  25. /*
  26. //http://answers.ros.org/question/129506/subscriber-not-seeing-cmd_vel-messages/
  27. ros::Rate loop_rate(10);
  28. while( n.ok() )
  29. {
  30. ros::spin();
  31. }
  32. */
  33. return 1;
  34. }

修改CMakeLists.txt

  1. vim ~/catkin_ws/src/mytest/CMakeLists.txt
  2. 在文件末尾添加如下语句
  3. add_executable(subscriber src/node_subscriber.cpp)
  4. target_link_libraries(subscriber ${catkin_LIBRARIES})

编译节点

  1. cd ~/catkin_ws
  2. catkin_make

运行测试节点

打开一个终端

roscore

打开另一个终端

rosrun mytest subscirber

参考资料

https://blog.csdn.net/qq_29022265/article/details/93652199

https://blog.csdn.net/scliu12345/article/details/45741893

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/知新_RL/article/detail/674740
推荐阅读
相关标签
  

闽ICP备14008679号