创建工作空间:
mkdir -p ~/myPX4_ws/src
cd ~/myPX4_wss/
catkin_make
source devel/setup.bash
echo "source ~/myPX4_wss/devel/setup.bash" >> ~/.bashrc
source ~/.bashrc
将offboard程序放置在src中。
启动gazebo仿真环境:
roslaunch px4 fast_test.launch
启动QGC。
运行飞行demo:
rosrun offboard_pkg offboard_node
程序范例(控制起飞):
// :: 表示作用域,如果想在类的外部引用静态成员函数,或在类的外部定义成员函数都要用到
#include <ros/ros.h>
#include <geometry_msgs/PoseStamped.h> //发布的消息体对应的头文件,该消息体的类型为geometry_msgs::PoseStamped 本地位置
#include <mavros_msgs/CommandBool.h>
#include <mavros_msgs/SetMode.h>
#include <mavros_msgs/State.h> //订阅的消息体的头文件,该消息体的类型为mavros_msgs::State
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"); //初始化节点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");
//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 = 2;
//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;
}
范例2(指点飞行循环):
#include <ros/ros.h>
#include <geometry_msgs/PoseStamped.h>
#include <mavros_msgs/CommandBool.h>
#include <mavros_msgs/SetMode.h>
#include <mavros_msgs/State.h>
#include <geometry_msgs/TwistStamped.h>
#include <string>
#include <sstream>
#include <iostream>
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, "position");
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::Publisher velocity_pub = nh.advertise<geometry_msgs::TwistStamped>
// ("mavros/setpoint_velocity/cmd_vel", 10);
//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 = 1.5;
mavros_msgs::SetMode offb_set_mode;
offb_set_mode.request.custom_mode = "OFFBOARD";
mavros_msgs::CommandBool arm_cmd;
arm_cmd.request.value = true;
int state = 3;
ros::Time last_request = ros::Time::now();
while(ros::ok() ){
if( !current_state.armed ){
if( arming_client.call(arm_cmd) &&
arm_cmd.response.success){
ROS_INFO("Vehicle armed");
}
}
if( current_state.mode != "OFFBOARD"){
if( set_mode_client.call(offb_set_mode) &&
offb_set_mode.response.mode_sent){
ROS_INFO("Offboard enabled");
}
}
if( (ros::Time::now() - last_request > ros::Duration(5.0))) break;
ros::spinOnce();
rate.sleep();
}
while(state--) {
last_request = ros::Time::now();
while(ros::ok()) {
if( (ros::Time::now() - last_request > ros::Duration(5.0))) break;
pose.pose.position.x = 0;
pose.pose.position.y = 0;
local_pos_pub.publish(pose);
ROS_INFO("SUCCESS0");
ros::spinOnce();
rate.sleep();
}
last_request = ros::Time::now();
while(ros::ok()) {
if( (ros::Time::now() - last_request > ros::Duration(5.0))) break;
pose.pose.position.x = 2;
pose.pose.position.y = 2;
local_pos_pub.publish(pose);
ROS_INFO("SUCCESS1");
ros::spinOnce();
rate.sleep();
}
ROS_INFO_STREAM("state="<<state);
}
// offb_set_mode.request.custom_mode = "AUTO.LAND";
// if( set_mode_client.call(offb_set_mode) && offb_set_mode.response.mode_sent)
// {
// ROS_INFO("AUTO.LAND enabled");
// last_request = ros::Time::now();
// }
return 0;
}