分享

刚下好ROS,如何从零开始实现px4的无人机仿真

 netouch 2023-05-18 发布于北京

刚刚入门ROS开发,作为刚刚挺过艰难的ubuntu安装、ros安装的萌新来说,你应该正处于ROS开发的初始过渡阶段。一方面,你庆幸你解决了许多安装新系统的许多bug,跨过了阻拦了无数人入门ROS的一道坎,另一方面,你开始惆怅,面对着空荡荡的新系统的新桌面,你不知道接下来的一步该怎么走,该如何学习。

如果你想学习无人机仿真,可以接着往下看,我带你从下好ROS之后空空如也的新系统,一步一步地实现无人机的仿真飞行。

第一步:配置仿真平台

我们要依次进行gazebo安装(仿真平台)、mavros安装(无人机通信模块)、px4配置、地面站QGroundControl安装。

此处仿真平台的搭建比较复杂,上链接:仿真平台基础配置 · 语雀

链接的步骤介绍已经十分详细,如果还有哪里不懂,可以跟着b站的一个up主的视频一步一步慢慢搭建

【【XTDrone】无人机仿真平台基础配置(基于ROS和PX4的无人机仿真平台的基础配置搭建)-哔哩哔哩】 https:///txNJO6P

第二步:创建工作空间与功能包,写入飞行代码

1、创建工作空间

  1. #创建工作空间
  2. midir catkin_ws/
  3. cd catkin_ws/
  4. midir src
  5. cd src/
  6. catkin_init_workspace
  7. #编译工作空间
  8. cd..
  9. catkin_make
  10. #设置环境变量
  11. source devel/setup.bash
  12. #检查环境变量
  13. echo $ROS_PACKAGE_PATH

2、创建功能包

功能包是放在工作空间src文件夹中实现具体功能的特殊文件夹,是放置ROS源码的最小单元

  1. #创建功能包(offboard_sin是功能包名)
  2. $ cd ~/catkin_ws/src
  3. $ catkin_create_pkg offboard_sin std_msgs rospy roscpp
  4. #编译功能包
  5. $ cd ~/catkin_ws
  6. $ catkin_make
  7. $ source ~/catkin_ws/devel/setup.bash

3、写入飞行代码

在功能包的src路径下创建offboard_sin_node.cpp文件,并写入如下沿着sin路径飞行的代码 

 (根据官方offboard代码示例改写)

  1. /**
  2. * @file offb_node.cpp
  3. * @brief Offboard control example node, written with MAVROS version 0.19.x, PX4 Pro Flight
  4. * Stack and tested in Gazebo SITL
  5. */
  6. #include <ros/ros.h>
  7. #include <geometry_msgs/PoseStamped.h>
  8. #include <geometry_msgs/Vector3.h>
  9. #include <mavros_msgs/CommandBool.h>
  10. #include <mavros_msgs/SetMode.h>
  11. #include <mavros_msgs/State.h>
  12. #define PI acos(-1)
  13. mavros_msgs::State current_state;
  14. geometry_msgs::PoseStamped current_position;
  15. void state_cb(const mavros_msgs::State::ConstPtr& msg){
  16. current_state = *msg;
  17. }
  18. void getpointfdb(const geometry_msgs::PoseStamped::ConstPtr& msg){
  19. ROS_INFO('x: [%f]', msg->pose.position.x);
  20. ROS_INFO('y: [%f]', msg->pose.position.y);
  21. ROS_INFO('z: [%f]', msg->pose.position.z);
  22. current_position = *msg;
  23. }
  24. int main(int argc, char **argv)
  25. {
  26. ros::init(argc, argv, 'offb_node');
  27. ros::NodeHandle nh;
  28. ros::Subscriber state_sub = nh.subscribe<mavros_msgs::State>
  29. ('mavros/state', 10, state_cb);
  30. ros::Subscriber get_point = nh.subscribe<geometry_msgs::PoseStamped>
  31. ('mavros/local_position/pose', 10, getpointfdb);
  32. ros::Publisher local_pos_pub = nh.advertise<geometry_msgs::PoseStamped>
  33. ('mavros/setpoint_position/local', 10);
  34. ros::ServiceClient arming_client = nh.serviceClient<mavros_msgs::CommandBool>
  35. ('mavros/cmd/arming');
  36. ros::ServiceClient set_mode_client = nh.serviceClient<mavros_msgs::SetMode>
  37. ('mavros/set_mode');
  38. //the setpoint publishing rate MUST be faster than 2Hz
  39. ros::Rate rate(20.0f);
  40. // wait for FCU connection
  41. while(ros::ok() && !current_state.connected){
  42. ros::spinOnce();
  43. rate.sleep();
  44. }
  45. geometry_msgs::PoseStamped pose;
  46. pose.pose.position.x = 0;
  47. pose.pose.position.y = 0;
  48. pose.pose.position.z = 3;
  49. //send a few setpoints before starting
  50. for(int i = 100; ros::ok() && i > 0; --i){
  51. local_pos_pub.publish(pose);
  52. ros::spinOnce();
  53. rate.sleep();
  54. }
  55. mavros_msgs::SetMode offb_set_mode;
  56. offb_set_mode.request.custom_mode = 'OFFBOARD';
  57. mavros_msgs::CommandBool arm_cmd;
  58. arm_cmd.request.value = true;
  59. ros::Time last_request = ros::Time::now();
  60. while(ros::ok()){
  61. if( current_state.mode != 'OFFBOARD' &&
  62. (ros::Time::now() - last_request > ros::Duration(5.0f))){
  63. if( set_mode_client.call(offb_set_mode) &&
  64. offb_set_mode.response.mode_sent){
  65. ROS_INFO('Offboard enabled');
  66. }
  67. last_request = ros::Time::now();
  68. } else {
  69. if( !current_state.armed &&
  70. (ros::Time::now() - last_request > ros::Duration(5.0f))){
  71. if( arming_client.call(arm_cmd) &&
  72. arm_cmd.response.success){
  73. ROS_INFO('Vehicle armed');
  74. }
  75. last_request = ros::Time::now();
  76. }
  77. }
  78. if((abs(current_position.pose.position.x-pose.pose.position.x)<0.5f)&&(abs(current_position.pose.position.y-pose.pose.position.y)<0.5f)&&(abs(current_position.pose.position.y-pose.pose.position.y)<0.5f))
  79. {
  80. pose.pose.position.x += 5;
  81. pose.pose.position.y = 20*sin(pose.pose.position.x/40*PI);
  82. pose.pose.position.z = 3;
  83. }
  84. local_pos_pub.publish(pose);
  85. ros::spinOnce();
  86. rate.sleep();
  87. }
  88. return 0;
  89. }

 4、修改CMakeLists.txt文件

加入下面两行

  1. add_executable(offboard_sin_node src/offboard_sin_node.cpp)
  2. target_link_libraries(offboard_sin_node ${catkin_LIBRARIES})

第三步:实现飞行仿真

打开终端,输入

roslaunch px4 mavros_posix_sitl.launch

此launch文件会打开你的gazebo启动无人机模型并建立好mavros通信

同时,打开你的QGroundControl,其会自动与你gazebo里的无人机进行通信连接

然后打开新终端,输入

  1. rosrun offboard_sin offboard_sin_node
  2. #rosrun+功能包+功能包内节点文件

实现效果 

 至此,第一次简单的无人机飞行仿真结束。

参考文章:(8条消息) 使用PX4+mavros+gazebo实现无人机offboard控制仿真_sdhdwyx的博客-CSDN博客_offboard 无人机https://blog.csdn.net/qq_42680785/article/details/118853000?utm_source=app&app_version=5.3.0&code=app_1562916241&uLinkId=usr1mkqgl919blen

    本站是提供个人知识管理的网络存储空间,所有内容均由用户发布,不代表本站观点。请注意甄别内容中的联系方式、诱导购买等信息,谨防诈骗。如发现有害或侵权内容,请点击一键举报。
    转藏 分享 献花(0

    0条评论

    发表

    请遵守用户 评论公约