当前位置: 首页 > news >正文

第一次通过通讯节点连接实现无人机仿真模拟(ROS1 + C++ + PX4)

通过与chatGPT之间的交互,让其辅助我进行代码编写

+-------------------+
| C++ 控制节点 |
| (自主起飞/降落) |
+---------+---------+
|
ROS Topic
|
+------v------+
| MAVROS |
+------ +-----+
|
MAVLink
|
+------v------+
| PX4 |
| 飞控 SITL |
+------+------+
|
仿真物理接口
|
+------v------+
| Gazebo |
| 无人机模型 |
+-------------+

任务顺序:

  • PX4 仿真启动
  • Gazebo 中生成无人机
  • ROS 与 PX4 通信
  • 查看无人机状态
  • C++ 发布控制指令
  • 自动解锁
  • 自动起飞
  • 定点悬停
  • 自动降落

一、环境配置

使用vmware虚拟机进行操作,系统为ubuntu20.04版本,确保 VMware 能顺利运行 PX4 + Gazebo + ROS,检查 MAVROS 通信是否稳定,创建新的工作空间来存放新的包

二、ROS与MAVROS通信

(1)启动PX4 SITL

cd ~/PX4-Autopilot
make px4_sitl gazebo

(2) 启动MAVROS

source /opt/ros/noetic/setup.bash

roslaunch mavros px4.launch fcu_url:="udp://:14540@127.0.0.1:14557"

正常情况下你会看到:

类似:

FCU: connected

这表示:

ROS 已经成功接管 PX4

rostopic echo /mavros/state

如果成功,你会看到类似:

connected: True armed: False guided: True mode: "AUTO.LOITER"

三、编写节点通信代码

cd ~/catkin_ws/src

catkin_create_pkg offboard_pkg roscpp std_msgs geometry_msgs mavros_msgs

打开vscode,在offboard_pkg包下的src文件夹创建offboard_node.cpp

#include <ros/ros.h> #include <geometry_msgs/PoseStamped.h> #include <mavros_msgs/State.h> #include <mavros_msgs/CommandBool.h> #include <mavros_msgs/SetMode.h> mavros_msgs::State current_state; void state_cb(const mavros_msgs::State::ConstPtr& msg) { current_state = *msg; } int main(int argc, char **argv) { ros::init(argc, argv, "offboard_node"); ros::NodeHandle nh; ros::Subscriber state_sub = nh.subscribe<mavros_msgs::State> ("/mavros/state", 10, state_cb); ros::Publisher local_pos_pub = nh.advertise<geometry_msgs::PoseStamped> ("/mavros/setpoint_position/local", 10); ros::ServiceClient arming_client = nh.serviceClient<mavros_msgs::CommandBool> ("/mavros/cmd/arming"); ros::ServiceClient set_mode_client = nh.serviceClient<mavros_msgs::SetMode> ("/mavros/set_mode"); ros::Rate rate(20.0); while(ros::ok() && !current_state.connected) { ros::spinOnce(); rate.sleep(); } geometry_msgs::PoseStamped pose; pose.pose.position.x = 0; pose.pose.position.y = 0; pose.pose.position.z = 2; for(int i = 100; ros::ok() && i > 0; --i) { local_pos_pub.publish(pose); ros::spinOnce(); rate.sleep(); } mavros_msgs::SetMode offb_set_mode; offb_set_mode.request.custom_mode = "OFFBOARD"; mavros_msgs::CommandBool arm_cmd; arm_cmd.request.value = true; ros::Time last_request = ros::Time::now(); while(ros::ok()) { if(current_state.mode != "OFFBOARD" && (ros::Time::now() - last_request > ros::Duration(5.0))) { if(set_mode_client.call(offb_set_mode) && offb_set_mode.response.mode_sent) { ROS_INFO("OFFBOARD enabled"); } last_request = ros::Time::now(); } else { if(!current_state.armed && (ros::Time::now() - last_request > ros::Duration(5.0))) { if(arming_client.call(arm_cmd) && arm_cmd.response.success) { ROS_INFO("Vehicle armed"); } last_request = ros::Time::now(); } } local_pos_pub.publish(pose); ros::spinOnce(); rate.sleep(); } return 0; }

顺便修改Cmakelist.txt文件

add_executable(offboard_node src/offboard_node.cpp)

target_link_libraries(offboard_node
${catkin_LIBRARIES}
)

ctrl+s保存,catkin_make进行编译

四、运行

cd ~/PX4-Autopilot
make px4_sitl gazebo

roslaunch mavros px4.launch \
fcu_url:="udp://:14540@127.0.0.1:14557"

source ~/catkin_ws/devel/setup.bash

rosrun offboard_pkg offboard_node

你会看到:

OFFBOARD enabled Vehicle armed

然后:

无人机自动起飞到2米

个人学习总结,内容含AI生成,仅供参考

http://www.jsqmd.com/news/871110/

相关文章:

  • 三分钟掌握Twine.js:零代码创建交互式非线性故事
  • 如何用Akagi麻雀助手快速提升雀魂游戏水平:3个核心技巧
  • TegraRcmGUI:Windows平台Nintendo Switch注入的终极图形化解决方案
  • 文峰购物卡回收:如何实现高效安全变现 - 购物卡回收找京尔回收
  • python运行提速方案全解
  • 观察使用Taotoken聚合调用在不同时段模型响应的延迟表现
  • 2026年必看:Trae对比Claude Code详细评测,热门AI编程工具横评
  • 面向银发消费品的客服系统如何做方言适配?从普通话到各地方言的语音识别方案
  • 杭州西装定制头部品牌权威推荐榜 - 西装爱好者
  • GPU+FPGA-满足你对低延迟与大算力的双重幻想
  • Gearboy终极指南:跨平台Game Boy模拟器与AI辅助调试
  • 如何打造移动工作站:3步实现操作系统随身携带
  • Claude如何3秒定位《民法典》第584条关联条款?——基于172份裁判文书验证的法律语义映射模型
  • Cursor Pro破解工具终极指南:5步解锁AI编程助手完整功能
  • 维小达全品类上门维修 用心服务民生 守护万家安心 - 维小达科技
  • 深度解析ANTs图像配准架构设计:医学影像标准化核心技术实现
  • 基于Go+Wails的智能网络资源嗅探与下载工具:实现全平台自动化资源捕获
  • 5个维度深度解析洛雪音乐音源:从技术实现到高效部署的完整指南
  • 我为什么写了一个 Syslog 查看器
  • IDM永久激活指南:免费解锁下载神器,告别30天试用限制
  • 杰理之使用蓝牙测试盒出现升级失败问题【篇】
  • 为Claude Code配置Taotoken稳定通道避免封号与Token不足
  • 2026年期货回测防过拟合:主流平台样本外与验证工具对比
  • 告别手动抢茅台!Campus-imaotai智能预约系统终极指南
  • 2026年企业账号矩阵第一步:适配性诊断+资源盘点
  • 开启防火墙后,不能ping通的解决办法
  • 杰理之关机功耗高问题【篇】
  • 【AI Agent物流行业落地实战指南】:20年专家亲授3大不可绕过的失败陷阱与5步标准化部署法
  • 金融舆情如何从“事后灭火”转向“事前预警”,看看这家AI公司怎么做
  • GHelper技术解析:基于ACPI直接通信的华硕笔记本轻量级控制方案