机器人编程趣味实践15-遥控到自动(AutoAvoidObstacles)

举报
zhangrelay 发表于 2021/07/15 00:16:46 2021/07/15
【摘要】 之前,不管是二维平台,还是三维平台,都是用键盘遥控,对于turtlebot3机器人装配了激光传感器,可以测量周围360度障碍物的距离,这就非常方便使用其进行避开障碍物的自主行驶。 这里的自主行使是最基础的功能即在环境中避开障碍物在空旷处随机行驶。 机器人选择: export TURTLEBOT3_MODEL=burgerexport TURTLEBOT3_...

之前,不管是二维平台,还是三维平台,都是用键盘遥控,对于turtlebot3机器人装配了激光传感器,可以测量周围360度障碍物的距离,这就非常方便使用其进行避开障碍物的自主行驶。

这里的自主行使是最基础的功能即在环境中避开障碍物在空旷处随机行驶。

机器人选择:

  1. export TURTLEBOT3_MODEL=burger
  2. export TURTLEBOT3_MODEL=waffle_pi

二选一即可,然后不用启动键盘遥控节点,改为如下节点:

  • ros2 run turtlebot3_gazebo turtlebot3_drive

接着,继续开启三维可视化:

  • ros2 launch turtlebot3_bringup rviz2.launch.py

仿真软件是Gazebo,可视化工具是rviz,不要用错了哦^_^

预备

需要ROS2+TurtleBot3仿真包。

实践

1 基本命令

需要掌握:

  1. ros2 run
  2. ros2 launch

开启仿真环境和避障行驶节点。

2 rqt工具

使用rqt_graph等查看,节点信息流。

3 源码阅读

launch


  
  1. import os
  2. from ament_index_python.packages import get_package_share_directory
  3. from launch import LaunchDescription
  4. from launch.actions import IncludeLaunchDescription
  5. from launch.launch_description_sources import PythonLaunchDescriptionSource
  6. from launch.substitutions import LaunchConfiguration
  7. TURTLEBOT3_MODEL = os.environ['TURTLEBOT3_MODEL']
  8. def generate_launch_description():
  9. use_sim_time = LaunchConfiguration('use_sim_time', default='true')
  10. world_file_name = 'turtlebot3_worlds/' + TURTLEBOT3_MODEL + '.model'
  11. world = os.path.join(get_package_share_directory('turtlebot3_gazebo'),
  12. 'worlds', world_file_name)
  13. launch_file_dir = os.path.join(get_package_share_directory('turtlebot3_gazebo'), 'launch')
  14. pkg_gazebo_ros = get_package_share_directory('gazebo_ros')
  15. return LaunchDescription([
  16. IncludeLaunchDescription(
  17. PythonLaunchDescriptionSource(
  18. os.path.join(pkg_gazebo_ros, 'launch', 'gzserver.launch.py')
  19. ),
  20. launch_arguments={'world': world}.items(),
  21. ),
  22. IncludeLaunchDescription(
  23. PythonLaunchDescriptionSource(
  24. os.path.join(pkg_gazebo_ros, 'launch', 'gzclient.launch.py')
  25. ),
  26. ),
  27. IncludeLaunchDescription(
  28. PythonLaunchDescriptionSource([launch_file_dir, '/robot_state_publisher.launch.py']),
  29. launch_arguments={'use_sim_time': use_sim_time}.items(),
  30. ),
  31. ])

drive


  
  1. #include "turtlebot3_gazebo/turtlebot3_drive.hpp"
  2. #include <memory>
  3. using namespace std::chrono_literals;
  4. Turtlebot3Drive::Turtlebot3Drive()
  5. : Node("turtlebot3_drive_node")
  6. {
  7. /************************************************************
  8. ** Initialise variables
  9. ************************************************************/
  10. scan_data_[0] = 0.0;
  11. scan_data_[1] = 0.0;
  12. scan_data_[2] = 0.0;
  13. robot_pose_ = 0.0;
  14. prev_robot_pose_ = 0.0;
  15. /************************************************************
  16. ** Initialise ROS publishers and subscribers
  17. ************************************************************/
  18. auto qos = rclcpp::QoS(rclcpp::KeepLast(10));
  19. // Initialise publishers
  20. cmd_vel_pub_ = this->create_publisher<geometry_msgs::msg::Twist>("cmd_vel", qos);
  21. // Initialise subscribers
  22. scan_sub_ = this->create_subscription<sensor_msgs::msg::LaserScan>(
  23. "scan", \
  24. rclcpp::SensorDataQoS(), \
  25. std::bind(
  26. &Turtlebot3Drive::scan_callback, \
  27. this, \
  28. std::placeholders::_1));
  29. odom_sub_ = this->create_subscription<nav_msgs::msg::Odometry>(
  30. "odom", qos, std::bind(&Turtlebot3Drive::odom_callback, this, std::placeholders::_1));
  31. /************************************************************
  32. ** Initialise ROS timers
  33. ************************************************************/
  34. update_timer_ = this->create_wall_timer(10ms, std::bind(&Turtlebot3Drive::update_callback, this));
  35. RCLCPP_INFO(this->get_logger(), "Turtlebot3 simulation node has been initialised");
  36. }
  37. Turtlebot3Drive::~Turtlebot3Drive()
  38. {
  39. RCLCPP_INFO(this->get_logger(), "Turtlebot3 simulation node has been terminated");
  40. }
  41. /********************************************************************************
  42. ** Callback functions for ROS subscribers
  43. ********************************************************************************/
  44. void Turtlebot3Drive::odom_callback(const nav_msgs::msg::Odometry::SharedPtr msg)
  45. {
  46. tf2::Quaternion q(
  47. msg->pose.pose.orientation.x,
  48. msg->pose.pose.orientation.y,
  49. msg->pose.pose.orientation.z,
  50. msg->pose.pose.orientation.w);
  51. tf2::Matrix3x3 m(q);
  52. double roll, pitch, yaw;
  53. m.getRPY(roll, pitch, yaw);
  54. robot_pose_ = yaw;
  55. }
  56. void Turtlebot3Drive::scan_callback(const sensor_msgs::msg::LaserScan::SharedPtr msg)
  57. {
  58. uint16_t scan_angle[3] = {0, 30, 330};
  59. for (int num = 0; num < 3; num++) {
  60. if (std::isinf(msg->ranges.at(scan_angle[num]))) {
  61. scan_data_[num] = msg->range_max;
  62. } else {
  63. scan_data_[num] = msg->ranges.at(scan_angle[num]);
  64. }
  65. }
  66. }
  67. void Turtlebot3Drive::update_cmd_vel(double linear, double angular)
  68. {
  69. geometry_msgs::msg::Twist cmd_vel;
  70. cmd_vel.linear.x = linear;
  71. cmd_vel.angular.z = angular;
  72. cmd_vel_pub_->publish(cmd_vel);
  73. }
  74. /********************************************************************************
  75. ** Update functions
  76. ********************************************************************************/
  77. void Turtlebot3Drive::update_callback()
  78. {
  79. static uint8_t turtlebot3_state_num = 0;
  80. double escape_range = 30.0 * DEG2RAD;
  81. double check_forward_dist = 0.7;
  82. double check_side_dist = 0.6;
  83. switch (turtlebot3_state_num) {
  84. case GET_TB3_DIRECTION:
  85. if (scan_data_[CENTER] > check_forward_dist) {
  86. if (scan_data_[LEFT] < check_side_dist) {
  87. prev_robot_pose_ = robot_pose_;
  88. turtlebot3_state_num = TB3_RIGHT_TURN;
  89. } else if (scan_data_[RIGHT] < check_side_dist) {
  90. prev_robot_pose_ = robot_pose_;
  91. turtlebot3_state_num = TB3_LEFT_TURN;
  92. } else {
  93. turtlebot3_state_num = TB3_DRIVE_FORWARD;
  94. }
  95. }
  96. if (scan_data_[CENTER] < check_forward_dist) {
  97. prev_robot_pose_ = robot_pose_;
  98. turtlebot3_state_num = TB3_RIGHT_TURN;
  99. }
  100. break;
  101. case TB3_DRIVE_FORWARD:
  102. update_cmd_vel(LINEAR_VELOCITY, 0.0);
  103. turtlebot3_state_num = GET_TB3_DIRECTION;
  104. break;
  105. case TB3_RIGHT_TURN:
  106. if (fabs(prev_robot_pose_ - robot_pose_) >= escape_range) {
  107. turtlebot3_state_num = GET_TB3_DIRECTION;
  108. } else {
  109. update_cmd_vel(0.0, -1 * ANGULAR_VELOCITY);
  110. }
  111. break;
  112. case TB3_LEFT_TURN:
  113. if (fabs(prev_robot_pose_ - robot_pose_) >= escape_range) {
  114. turtlebot3_state_num = GET_TB3_DIRECTION;
  115. } else {
  116. update_cmd_vel(0.0, ANGULAR_VELOCITY);
  117. }
  118. break;
  119. default:
  120. turtlebot3_state_num = GET_TB3_DIRECTION;
  121. break;
  122. }
  123. }
  124. /*******************************************************************************
  125. ** Main
  126. *******************************************************************************/
  127. int main(int argc, char ** argv)
  128. {
  129. rclcpp::init(argc, argv);
  130. rclcpp::spin(std::make_shared<Turtlebot3Drive>());
  131. rclcpp::shutdown();
  132. return 0;
  133. }

这里面涉及一些读取传感器关键,0度,30度,-30度的距离。

然后有一些初值需要查看头文件:


  
  1. #ifndef TURTLEBOT3_GAZEBO__TURTLEBOT3_DRIVE_HPP_
  2. #define TURTLEBOT3_GAZEBO__TURTLEBOT3_DRIVE_HPP_
  3. #include <geometry_msgs/msg/twist.hpp>
  4. #include <nav_msgs/msg/odometry.hpp>
  5. #include <rclcpp/rclcpp.hpp>
  6. #include <sensor_msgs/msg/laser_scan.hpp>
  7. #include <tf2/LinearMath/Matrix3x3.h>
  8. #include <tf2/LinearMath/Quaternion.h>
  9. #define DEG2RAD (M_PI / 180.0)
  10. #define RAD2DEG (180.0 / M_PI)
  11. #define CENTER 0
  12. #define LEFT 1
  13. #define RIGHT 2
  14. #define LINEAR_VELOCITY 0.3
  15. #define ANGULAR_VELOCITY 1.5
  16. #define GET_TB3_DIRECTION 0
  17. #define TB3_DRIVE_FORWARD 1
  18. #define TB3_RIGHT_TURN 2
  19. #define TB3_LEFT_TURN 3
  20. class Turtlebot3Drive : public rclcpp::Node
  21. {
  22. public:
  23. Turtlebot3Drive();
  24. ~Turtlebot3Drive();
  25. private:
  26. // ROS topic publishers
  27. rclcpp::Publisher<geometry_msgs::msg::Twist>::SharedPtr cmd_vel_pub_;
  28. // ROS topic subscribers
  29. rclcpp::Subscription<sensor_msgs::msg::LaserScan>::SharedPtr scan_sub_;
  30. rclcpp::Subscription<nav_msgs::msg::Odometry>::SharedPtr odom_sub_;
  31. // Variables
  32. double robot_pose_;
  33. double prev_robot_pose_;
  34. double scan_data_[3];
  35. // ROS timer
  36. rclcpp::TimerBase::SharedPtr update_timer_;
  37. // Function prototypes
  38. void update_callback();
  39. void update_cmd_vel(double linear, double angular);
  40. void scan_callback(const sensor_msgs::msg::LaserScan::SharedPtr msg);
  41. void odom_callback(const nav_msgs::msg::Odometry::SharedPtr msg);
  42. };
  43. #endif // TURTLEBOT3_GAZEBO__TURTLEBOT3_DRIVE_HPP_

这是一个通用的简易避障行驶代码,只要是激光传感器两轮差动小车都适用。


 

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

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

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

评论(0

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

全部回复

上滑加载中

设置昵称

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

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

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