2 #include <geometry_msgs/PoseStamped.h> 3 #include <mavros_msgs/CommandBool.h> 4 #include <mavros_msgs/SetMode.h> 5 #include <mavros_msgs/State.h> 6 #include <mavros_msgs/CommandTOL.h> 10 void state_cb(
const mavros_msgs::State::ConstPtr& msg){
14 int main(
int argc,
char **argv){
15 ros::init(argc, argv,
"offb_node");
18 ros::Subscriber state_sub = nh.subscribe<mavros_msgs::State>(
"mavros/state", 10,
state_cb);
19 ros::Publisher local_pos_pub = nh.advertise<geometry_msgs::PoseStamped>(
"mavros/setpoint_position/local", 10);
20 ros::ServiceClient arming_client = nh.serviceClient<mavros_msgs::CommandBool>(
"mavros/cmd/arming");
21 ros::ServiceClient set_mode_client = nh.serviceClient<mavros_msgs::SetMode>(
"mavros/set_mode");
22 ros::ServiceClient landing_client = nh.serviceClient<mavros_msgs::CommandTOL>(
"mavros/cmd/land");
31 geometry_msgs::PoseStamped pose;
32 pose.pose.position.x = 0;
33 pose.pose.position.y = 0;
34 pose.pose.position.z = 2;
36 for(
int i = 100; ros::ok() && i > 0; --i){
37 local_pos_pub.publish(pose);
42 mavros_msgs::SetMode offb_set_mode;
43 offb_set_mode.request.custom_mode =
"OFFBOARD";
45 mavros_msgs::CommandBool arm_cmd;
46 arm_cmd.request.value =
true;
48 ros::Time last_request = ros::Time::now();
49 ros::Time armed_time = ros::Time::now();
52 if(
current_state.mode !=
"OFFBOARD" && (ros::Time::now() - last_request > ros::Duration(5.0))){
53 if(set_mode_client.call(offb_set_mode)){
54 ROS_INFO(
"Offboard enabled");
56 last_request = ros::Time::now();
58 if(!
current_state.armed && (ros::Time::now() - last_request > ros::Duration(5.0))){
59 if(arming_client.call(arm_cmd) && arm_cmd.response.success){
60 ROS_INFO(
"Vehicle armed");
61 armed_time = ros::Time::now();
63 last_request = ros::Time::now();
67 local_pos_pub.publish(pose);
72 if(ros::Time::now()-armed_time > ros::Duration(20)){
73 ROS_INFO(
"20s elapsed");
78 ROS_INFO(
"RC control enabled.");
84 mavros_msgs::CommandTOL landing_msg;
85 landing_msg.request.altitude = 0;
86 while(ros::ok() and !landing_client.call(landing_msg) && landing_msg.response.success){
89 ROS_INFO(
"Vehicle landing...");
91 ros::Duration(20).sleep();
93 arm_cmd.request.value =
false;
94 while(ros::ok() and !arming_client.call(arm_cmd) && arm_cmd.response.success){
96 ROS_INFO(
"Vehicle disarmed");
mavros_msgs::State current_state
void state_cb(const mavros_msgs::State::ConstPtr &msg)
int main(int argc, char **argv)