安装PX4
下载源码
git clone https://github.com/PX4/PX4-Autopilot.git --recursive
进入PX4-Autopilot文件夹,继续下载未下载的组件
cd PX4-Autopilot/
git submodule update --init --recursive
执行Ubuntun.sh脚本
bash ./PX4-Autopilot/Tools/setup/ubuntu.sh
出现python部分包的版本问题
python3 -m pip install --upgrade pip
python3 -m pip install --upgrade Pillow
再执行
bash ./PX4-Autopilot/Tools/setup/ubuntu.sh --fix-missing
reboot系统
测试进入PX4-Autopilot文件夹内
make px4_sitl_default gazebo
添加到环境变量
source ~/PX4-Autopilot/Tools/simulation/gazebo-classic/setup_gazebo.bash ~/PX4-Autopilot ~/PX4-Autopilot/build/px4_sitl_default
export ROS_PACKAGE_PATH=$ROS_PACKAGE_PATH:~/PX4-Autopilot
export ROS_PACKAGE_PATH=$ROS_PACKAGE_PATH:~/PX4-Autopilot/Tools/simulation/gazebo-classic/sitl_gazebo-classic
安装MAVROS
sudo apt-get install ros-noetic-mavros ros-noetic-mavros-extras
mavlink
wget https://raw.githubusercontent.com/mavlink/mavros/master/mavros/scripts/install_geographiclib_datasets.sh
chmod +x install_geographiclib_datasets.sh
./install_geographiclib_datasets.sh
启动PX4和MAVROS之间的连接
roslaunch mavros px4.launch fcu_url:="udp://:14540@127.0.0.1:14557"#gazebo方针的端口号
出现下面,表示成功启动
ROS程序测试
创建工程
#创建工作空间
mkdir catkin_ws
cd catkin_ws
mkdir src
cd src
catkin_init_workspace
#编译工作空间
cd ..
catkin_make
#设置环境变量
source devel/setup.bash
#创建功能包
cd catkin_ws/src
catkin_create_pkg offboard roscpp mavros geometry_msgs
cd ../
catkin_make
#编译成功则完成
位置输入悬停代码
#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;
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);
// 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 offboard_set_mode;
offboard_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(offboard_set_mode)&&offboard_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;
}
启动流程
#启动gazebo方针
cd ~/PX4-Autopilot
make px4_sitl_default gazebo
#启动mavlink通信
roslaunch mavros px4.launch fcu_url:="udp://:14540@127.0.0.1:14557"
#运行节点
rosrun offboard offboard_node
#查看模式
rostopic echo /mavros/state
终端开启仿真
gedit ~/.bashrc
#放入以下的内容
source ~/catkin_ws/devel/setup.bash
source ~/PX4_Firmware/Tools/setup_gazebo.bash ~/PX4_Firmware/ ~/PX4_Firmware/build/px4_sitl_default
export ROS_PACKAGE_PATH=$ROS_PACKAGE_PATH:~/PX4_Firmware
export ROS_PACKAGE_PATH=$ROS_PACKAGE_PATH:~/PX4_Firmware/Tools/sitl_gazebo
#关闭
source ~/.bashrc
启动:
roslaunch px4 mavros_posix_sitl.launch
当出现gazebo进程报错,大概率是之前的gazebo进程未完全关闭,可以输入
killall gzserver