目录
- 树莓派控制无人机实现定点降落(四)——gazebo无人机定点降落仿真环境搭建
-
- 1、编译px4_sitl_default
- 2、编译gazebo
- 3、仿真
- 3、尝试offboard_node
- 4、无人机添加单目相机
树莓派控制无人机实现定点降落(四)——gazebo无人机定点降落仿真环境搭建
1、编译px4_sitl_default
#在Firmware路径下
sudo make px4_sitl_default
这个是用于仿真的,而px4_fmu-v5_default可以不编译,但如果没有它的话就需要用实物pixhawk连接电脑然后用mavros连接用于仿真,如果有它的话就不需要接自己的飞控,已经有虚拟飞控了。
2、编译gazebo
在Firmware/Tools/sitl_gazebo路径下执行:
mkdir Build
### 将以下所有的[]都替换成Firmware所在的路径
### 设置插件的路径以便 Gazebo 能找到模型文件
export GAZEBO_PLUGIN_PATH=${GAZEBO_PLUGIN_PATH}:[]/Tools/sitl_gazebo/Build
### 设置模型路径以便 Gazebo 能找到机型
export GAZEBO_MODEL_PATH=${GAZEBO_MODEL_PATH}:[]/Tools/sitl_gazebo/models
### 禁用在线模型查找
export GAZEBO_MODEL_DATABASE_URI=""
### 设置 sitl_gazebo 文件夹的路径
export SITL_GAZEBO_PATH=[]/Tools/sitl_gazebo
### 编译gazebo仿真
cd Build
cmake ..
make
3、仿真
cd src/Firmware
make posix_sitl_default
export FIRMWARE_DIR=[]//[]里添加firmware的路径
source ~/catkin_ws/devel/setup.bash // 只有mavros是用源码编译的才需要
source Tools/setup_gazebo.bash $(FIRMWARE_DIR) $(FIRMWARE_DIR)/build/px4_sitl_default
export ROS_PACKAGE_PATH=$ROS_PACKAGE_PATH:$(FIRMWARE_DIR)
export ROS_PACKAGE_PATH=$ROS_PACKAGE_PATH:$(FIRMWARE_DIR)/Tools/sitl_gazebo
roslaunch px4 mavros_posix_sitl.launch
在执行roslaunch之后将会看到仿真界面就成功了。
为了之后方便可以直接把需要配置的环境加到~/.bashrc里:
sudo vim ~/.bashrc
#在最后添加
export FIRMWARE_DIR=[]//[]里添加firmware的路径
export GAZEBO_PLUGIN_PATH=${GAZEBO_PLUGIN_PATH}:$FIRMWARE_DIR/Tools/sitl_gazebo/build
source $FIRMWARE_DIR/Tools/setup_gazebo.bash $FIRMWARE_DIR $FIRMWARE_DIR/build/px4_sitl_default
export ROS_PACKAGE_PATH=$ROS_PACKAGE_PATH:$FIRMWARE_DIR
export ROS_PACKAGE_PATH=$ROS_PACKAGE_PATH:$FIRMWARE_DIR/Tools/sitl_gazebo
至此gazebo环境就搭建好了。 在命令行的pxh处输入command takeoff可以让它起飞,飞至两米高度,五秒后降落。
3、尝试offboard_node
按照px4官网的方法可以建立一个offboard_node来控制它起飞,方法如下:
#创建包
mkdir -p ~/catkin_ws/src
cd ~/catkin_ws/src
catkin_create_pkg offb roscpp mavros
cd ~/catkin_ws/src/offb/src
vim offb_node.cpp
在文件中复制粘贴
/**
* @file offb_node.cpp
* @brief offboard example node, written with mavros version 0.14.2, px4 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;
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;
}
(代码有pixhawk官网给出) 在CmakeLists中添加
add_executable(offb_node src/offb_node.cpp)
之后回到catkin_ws路径catkin_make
编译然后rosrun offb offb_node
即可看到无人机起飞到两米处。
4、无人机添加单目相机
修改launch文件: 在Firmware/launch
文件下的mavros_posix_sitl.launch
文件,添加一句:
<arg name="cam" default="iris_fpv_cam"/>
之后将
<arg name="sdf" default="$(find mavlink_sitl_gazebo)/models/$(arg vehicle)/$(arg vehicle).sdf"/>
改为
<arg name="sdf" default="$(find mavlink_sitl_gazebo)/models/$(arg cam)/$(arg cam).sdf"/>
这样就可以了,再次执行roslaunch px4 mavros_posix_sitl.launch
可以看到无人机有了前置摄像头,画面将发布到/iris/usb_cam/image_raw
话题,可以通过rqt查看。 改变摄像头姿态: 打开Firmware/Tools/sitl_gazebo/models/iris_fpv_cam/iris_fpv_cam.sdf
文件修改pose标签即可,改为0 0 0 0 1.57 0便是下视摄像头。