error: ‘rclcpp::executor’ has not been declared思考与ROS2的版本号

举报
zhangrelay 发表于 2022/06/20 22:15:49 2022/06/20
【摘要】 这个其实是各版本之间不停的改动导致的。 foxy: That means replace the rclcpp::FutureReturnCode::SUCCESS with rclcpp::executor::FutureReturnCode::SUCCESS. 然后: galactic: rclcpp::FutureRet...

这个其实是各版本之间不停的改动导致的。

foxy:

That means replace the rclcpp::FutureReturnCode::SUCCESS with rclcpp::executor::FutureReturnCode::SUCCESS.

然后:

galactic:

rclcpp::FutureReturnCode::SUCCESS

humble:

rclcpp::FutureReturnCode::SUCCESS

那么针对如下出错信息:

修改对应源代码:

/home/ros/RobCode/mobot/src/mobot/src/send_client.cpp:38:13:

将:


  
  1. if (rclcpp::spin_until_future_complete(node, result) ==
  2. rclcpp::executor::FutureReturnCode::SUCCESS)

修改为:


  
  1. if (rclcpp::spin_until_future_complete(node, result) ==
  2. rclcpp::FutureReturnCode::SUCCESS)

然后就一切ok啦。

全部记录:


  
  1. ros@ros:~/RobCode/mobot$ colcon build
  2. Starting >>> teleop_tools_msgs
  3. Starting >>> key_teleop
  4. Starting >>> mobot
  5. Starting >>> mobot_follow
  6. Starting >>> mouse_teleop
  7. Finished <<< key_teleop [2.30s]
  8. Finished <<< mouse_teleop [2.36s]
  9. Finished <<< teleop_tools_msgs [12.8s]
  10. Starting >>> joy_teleop
  11. Finished <<< joy_teleop [3.74s]
  12. Starting >>> teleop_tools
  13. Finished <<< teleop_tools [0.74s]
  14. Finished <<< mobot_follow [19.8s]
  15. --- stderr: mobot
  16. /home/ros/RobCode/mobot/src/mobot/src/send_client.cpp: In function ‘int main(int, char**)’:
  17. /home/ros/RobCode/mobot/src/mobot/src/send_client.cpp:38:13: error: ‘rclcpp::executor’ has not been declared
  18. 38 | rclcpp::executor::FutureReturnCode::SUCCESS)
  19. | ^~~~~~~~
  20. make[2]: *** [CMakeFiles/send_client.dir/build.make:63:CMakeFiles/send_client.dir/src/send_client.cpp.o] 错误 1
  21. make[1]: *** [CMakeFiles/Makefile2:671:CMakeFiles/send_client.dir/all] 错误 2
  22. make[1]: *** 正在等待未完成的任务....
  23. make: *** [Makefile:141:all] 错误 2
  24. ---
  25. Failed <<< mobot [25.3s, exited with code 2]
  26. Summary: 6 packages finished [25.6s]
  27. 1 package failed: mobot
  28. 1 package had stderr output: mobot
  29. ros@ros:~/RobCode/mobot$ colcon build
  30. Starting >>> teleop_tools_msgs
  31. Starting >>> key_teleop
  32. Starting >>> mobot
  33. Starting >>> mobot_follow
  34. Starting >>> mouse_teleop
  35. Finished <<< mobot_follow [0.90s]
  36. Finished <<< teleop_tools_msgs [1.00s]
  37. Starting >>> joy_teleop
  38. Finished <<< key_teleop [1.99s]
  39. Finished <<< mouse_teleop [2.07s]
  40. Finished <<< joy_teleop [1.48s]
  41. Starting >>> teleop_tools
  42. Finished <<< teleop_tools [0.12s]
  43. Finished <<< mobot [4.66s]
  44. Summary: 7 packages finished [4.84s]
  45. ros@ros:~/RobCode/mobot$

当节点使用服务进行通信时,发送数据请求的节点称为客户端节点,响应请求的节点称为服务节点。 请求和响应的结构由 .srv 文件确定。

When nodes communicate using services, the node that sends a request for data is called the client node, and the one that responds to the request is the service node. The structure of the request and response is determined by a .srv file.

这里使用的例子是一个简单的目标前进系统; 一个节点目标前进服务器,另一个客户端接收对应坐标。

到达目标点附近,任务完成。

这个比导航行动要简单,但是比速度控制反馈等要复杂一些。

给定目标点,到达目标点。

当然也可以用行动来完成,显示距离目标点完成的百分比。


服务器端:


  
  1. #include <iostream>
  2. #include <chrono>
  3. #include <memory>
  4. #include "rclcpp/rclcpp.hpp"
  5. #include "mobot/srv/drivegoalsrv.hpp"
  6. #include <geometry_msgs/msg/twist.hpp>
  7. #include "nav_msgs/msg/odometry.hpp"
  8. using namespace std::chrono_literals;
  9. bool drive_flag=0;
  10. float goal_x=0;
  11. float goal_y=0;
  12. float vel_x=0;
  13. float vel_z=0;
  14. float pid_z=2.0;
  15. void get(const std::shared_ptr<mobot::srv::Drivegoalsrv::Request> request,
  16. std::shared_ptr<mobot::srv::Drivegoalsrv::Response> response)
  17. {
  18. if(request->x>0.0)
  19. {
  20. drive_flag=1;
  21. response->success=drive_flag;
  22. goal_x=request->x;
  23. goal_y=request->y;
  24. RCLCPP_INFO(rclcpp::get_logger("service"), "Driving");
  25. //RCLCPP_INFO(rclcpp::get_logger("service"), "Get Goal\nx: %lf" " y: %lf",
  26. // request->x, request->y);
  27. //RCLCPP_INFO(rclcpp::get_logger("service"), "Flag: [%d]", response->success);
  28. }
  29. else
  30. {
  31. drive_flag=0;
  32. response->success=drive_flag;
  33. goal_x=request->x;
  34. goal_y=request->y;
  35. //RCLCPP_INFO(rclcpp::get_logger("service"), "Error, x>0!!!\nx: %lf" " y: %lf",
  36. // request->x, request->y);
  37. //RCLCPP_INFO(rclcpp::get_logger("service"), "Flag: [%d]", response->success);
  38. }
  39. }
  40. void odom_callback(const nav_msgs::msg::Odometry::SharedPtr msg)
  41. {
  42. if(drive_flag)
  43. {
  44. vel_x=goal_x-msg->pose.pose.position.x;
  45. vel_z=pid_z*(goal_y-msg->pose.pose.position.y)*vel_x*vel_x;
  46. if(vel_z>0.5)
  47. {
  48. vel_z=0.5;
  49. }
  50. if(vel_z<-0.5)
  51. {
  52. vel_z=-0.5;
  53. }
  54. if(vel_x>1.0)
  55. {
  56. vel_x=1.0;
  57. }
  58. if(vel_x<-1.0)
  59. {
  60. vel_x=-1.0;
  61. }
  62. if((vel_x<0.05)&&(vel_x>-0.05))
  63. if((vel_z<0.05)&&(vel_z>-0.05))
  64. {
  65. RCLCPP_INFO(rclcpp::get_logger("service"), "Mission complete!");
  66. RCLCPP_INFO(rclcpp::get_logger("service"), "Ready to get goal.");
  67. drive_flag=0;
  68. }
  69. }
  70. else
  71. {
  72. vel_x=0;
  73. vel_z=0;
  74. }
  75. // RCLCPP_INFO(rclcpp::get_logger("odom_sub"), "I heard: mobot odom position(x,y)='%f','%f'", msg->pose.pose.position.x, msg->pose.pose.position.y);
  76. }
  77. int main(int argc, char **argv)
  78. {
  79. rclcpp::init(argc, argv);
  80. std::shared_ptr<rclcpp::Node> node = rclcpp::Node::make_shared("get_goal_server");
  81. rclcpp::Service<mobot::srv::Drivegoalsrv>::SharedPtr service =
  82. node->create_service<mobot::srv::Drivegoalsrv>("get_goal", &get);
  83. rclcpp::Publisher<geometry_msgs::msg::Twist>::SharedPtr vel_pub =
  84. node->create_publisher<geometry_msgs::msg::Twist>("mobot/cmd_vel", 10);
  85. geometry_msgs::msg::Twist mobot_vel;
  86. rclcpp::Subscription<nav_msgs::msg::Odometry>::SharedPtr odom_sub =
  87. node->create_subscription<nav_msgs::msg::Odometry>("/mobot/odom", 100, odom_callback);
  88. RCLCPP_INFO(rclcpp::get_logger("service"), "Ready to get goal.");
  89. rclcpp::WallRate loop_rate(100ms);
  90. while (rclcpp::ok()) {
  91. mobot_vel.linear.x = vel_x;
  92. mobot_vel.angular.z = vel_z;
  93. //RCLCPP_INFO(rclcpp::get_logger("vel_pub"), "Publishing mobot cmd_vel : linear='%f',angular='%f'", mobot_vel.linear.x, mobot_vel.angular.z);
  94. vel_pub->publish(mobot_vel);
  95. rclcpp::spin_some(node);
  96. loop_rate.sleep();
  97. }
  98. rclcpp::shutdown();
  99. return 0;
  100. }

客户端: 


  
  1. #include "rclcpp/rclcpp.hpp"
  2. #include "mobot/srv/drivegoalsrv.hpp"
  3. #include <chrono>
  4. #include <cstdlib>
  5. #include <memory>
  6. using namespace std::chrono_literals;
  7. int main(int argc, char **argv)
  8. {
  9. rclcpp::init(argc, argv);
  10. if (argc != 3) {
  11. RCLCPP_INFO(rclcpp::get_logger("rclcpp"), "usage: send_goal_position_client X Y");
  12. return 1;
  13. }
  14. std::shared_ptr<rclcpp::Node> node = rclcpp::Node::make_shared("send_goal_client");
  15. rclcpp::Client<mobot::srv::Drivegoalsrv>::SharedPtr client =
  16. node->create_client<mobot::srv::Drivegoalsrv>("get_goal");
  17. auto request = std::make_shared<mobot::srv::Drivegoalsrv::Request>();
  18. request->x = atoll(argv[1]);
  19. request->y = atoll(argv[2]);
  20. while (!client->wait_for_service(1s)) {
  21. if (!rclcpp::ok()) {
  22. RCLCPP_ERROR(rclcpp::get_logger("rclcpp"), "Interrupted while waiting for the service. Exiting.");
  23. return 0;
  24. }
  25. RCLCPP_INFO(rclcpp::get_logger("rclcpp"), "service not available, waiting again...");
  26. }
  27. auto result = client->async_send_request(request);
  28. // Wait for the result.
  29. if (rclcpp::spin_until_future_complete(node, result) ==
  30. rclcpp::FutureReturnCode::SUCCESS)
  31. {
  32. RCLCPP_INFO(rclcpp::get_logger("rclcpp"), "Flag: %d", result.get()->success);
  33. } else {
  34. RCLCPP_ERROR(rclcpp::get_logger("rclcpp"), "Failed to call service get_goal");
  35. }
  36. rclcpp::shutdown();
  37. return 0;
  38. }

 

文章来源: zhangrelay.blog.csdn.net,作者:zhangrelay,版权归原作者所有,如需转载,请联系作者。

原文链接:zhangrelay.blog.csdn.net/article/details/125380659

【版权声明】本文为华为云社区用户转载文章,如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱: cloudbbs@huaweicloud.com
  • 点赞
  • 收藏
  • 关注作者

评论(0

0/1000
抱歉,系统识别当前为高风险访问,暂不支持该操作

全部回复

上滑加载中

设置昵称

在此一键设置昵称,即可参与社区互动!

*长度不超过10个汉字或20个英文字符,设置后3个月内不可修改。

*长度不超过10个汉字或20个英文字符,设置后3个月内不可修改。