roslaunch mavros px4.launch fcu_url:=“udp://:[email protected]:14557”
安装好mavros后,需要添加一下px4源码的路径,否则不能使用px4的launch文件
gedit .bashrc
如果Firmware在home目录下,则添加如下
source ~/Firmware/Tools/setup_gazebo.bash ~/Firmware/ ~/Firmware/build/px4_sitl_default
export ROS_PACKAGE_PATH=ROSPACKAGEPATH: /FirmwareexportROSPACKAGEPATH=ROSPACKAGEPATH: /FirmwareexportROSPACKAGEPATH=ROS_PACKAGE_PATH:~/Firmware/Tools/sitl_gazebo
保存后 source .bashrc
然后运行roslaunch px4 mavros_posix_sitl.launch
连接成功的话会弹出无人机的id等信息
如果是二进制安装的mavros
则在~/.bashrc中添加source /opt/ros/melodic/setup.bash
如果出现ERROR: cannot launch node of type [mavros/mavros_node]错误,则把catkin_ws里的内容删掉重新编译一下工作空间。
或者把原来的catkin_ws删掉
重新创建
mkdir -p ~/catkin_ws/src
创建工作空间
$ cd ~/catkin_ws/src
$ catkin_init_workspace
$ cd ~/catkin_ws/
$ catkin_make
$ source devel/setup.bash
$ echo $ROS_PACKAGE_PATH
创建功能包
编译功能包
$ cd ~/catkin_ws/src
$ catkin_create_pkg test_pkg std_msgs rospy roscpp
$ cd ~/catkin_ws
$ catkin_make
$ source ~/catkin_ws/devel/setup.bash
$ cd ~/catkin_ws/src
$ catkin_create_pkg offboard_node roscpp rospy std_msgs
在offboard_node/src下添加offboard_node.cpp
/**
#include
#include
#include
#include
#include
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/state", 10, state_cb);
ros::Publisher local_pos_pub = nh.advertise
("mavros/setpoint_position/local", 10);
ros::ServiceClient arming_client = nh.serviceClient
("mavros/cmd/arming");
ros::ServiceClient set_mode_client = nh.serviceClient
("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;
}
在offboard_node目录下的cmakelist.txt的最后添加
add_executable(aa src/offboard_node.cpp)
target_link_libraries(aa ${catkin_LIBRARIES})
在重新编译工作空间
然后先
xj@xj:~$ roslaunch px4 mavros_posix_sitl.launch
再
xj@xj:~$ rosrun offboard_node aa
正常的话无人机会起飞至2米