赞
踩
cd /Firmware #进入PX4文件夹内
make px4_sitl_default gazebo # 编译固件并加载仿真环境
如果想进行自动起飞,可运行以下命令
commander takeoff
老规矩先加载ROS,运行命令
roscore
出现红色警告,说明ROS本地缓存大于1GB,可以忽视,如果需要清理可以使用
rosclean purge
在PX4文件夹内用mavros连接到本地ros,运行以下命令打开gazebo仿真环境
roslaunch px4 mavros_posix_sitl.launch
首先需要在ROS工作空间下,在src目录下新建功能包
cd ~/catkin_ws/src
catkin_create_pkg offboard roscpp mavros geometry_msgs
//catkin_create_pkg 是ROS命令,新建一个功能包
//offboard 功能包名字
//roscpp mavros geometry_msgs功能包依赖
//命令格式catkin_create_pkg <package_name> [depend1] [depend2] [depend3]
在offboard功能包src目录下新建cpp文件,将外部程序控制文件写入保存
新建cpp文件
touch offboard_node.cpp
将以下程序写入到cpp文件,此程序参考的是PX4官网给出的例程。
/** * @file offb_node.cpp * @brief Offboard control example node, written with MAVROS version 0.19.x, PX4 Pro Flight * Stack and tested in Gazebo SITL */ #include <ros/ros.h> #include <geometry_msgs/PoseStamped.h> #include <mavros_msgs/CommandBool.h> #include <mavros_msgs/SetMode.h> #include <mavros_msgs/State.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, "offb_node"); ros::NodeHandle nh; //订阅mavros状态 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"); //定义设置模式服务客户端(设置offboard模式) ros::ServiceClient set_mode_client = nh.serviceClient<mavros_msgs::SetMode> ("mavros/set_mode"); //the setpoint publishing rate MUST be faster than 2Hz ros::Rate rate(20.0); // wait for FCU connection 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 = 10; //send a few setpoints before starting 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; }
在offboard目录下找到Cmakelists.txt文件,并设置程序运行节点,不然rosrun命令无法找到该节点,从而报错
输入以下除蓝色字体的命令 - 编辑好后即可进行编译
进入到catkin_ws目录下,运行终端
cd /catkin_ws
catkin_make
如上图显示编译成功
运行程序包
rosrun offboard offboard_node
无人机实现自动起飞10米高,程序运行成功。
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。