PX4 simulation stack
offb_node.cpp
Go to the documentation of this file.
1 #include <ros/ros.h>
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>
7 
8 mavros_msgs::State current_state;
9 
10 void state_cb(const mavros_msgs::State::ConstPtr& msg){
11  current_state = *msg;
12 }
13 
14 int main(int argc, char **argv){
15  ros::init(argc, argv, "offb_node");
16  ros::NodeHandle nh;
17 
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");
23 
24  ros::Rate rate(20.0);
25 
26  while(ros::ok() && current_state.connected){
27  ros::spinOnce();
28  rate.sleep();
29  }
30 
31  geometry_msgs::PoseStamped pose;
32  pose.pose.position.x = 0;
33  pose.pose.position.y = 0;
34  pose.pose.position.z = 2;
35 
36  for(int i = 100; ros::ok() && i > 0; --i){
37  local_pos_pub.publish(pose);
38  ros::spinOnce();
39  rate.sleep();
40  }
41 
42  mavros_msgs::SetMode offb_set_mode;
43  offb_set_mode.request.custom_mode = "OFFBOARD";
44 
45  mavros_msgs::CommandBool arm_cmd;
46  arm_cmd.request.value = true;
47 
48  ros::Time last_request = ros::Time::now();
49  ros::Time armed_time = ros::Time::now();
50 
51  while(ros::ok()){
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");
55  }
56  last_request = ros::Time::now();
57  }else{
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();
62  }
63  last_request = ros::Time::now();
64  }
65  }
66 
67  local_pos_pub.publish(pose);
68 
69  ros::spinOnce();
70  rate.sleep();
71 
72  if(ros::Time::now()-armed_time > ros::Duration(20)){
73  ROS_INFO("20s elapsed");
74  break;
75  }
76 
77  if(current_state.mode=="GUIDED"){
78  ROS_INFO("RC control enabled.");
79  break;
80  }
81 
82  }
83 
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){
87  }
88 
89  ROS_INFO("Vehicle landing...");
90 
91  ros::Duration(20).sleep();
92 
93  arm_cmd.request.value = false;
94  while(ros::ok() and !arming_client.call(arm_cmd) && arm_cmd.response.success){
95  }
96  ROS_INFO("Vehicle disarmed");
97 
98  return 0;
99 }
100 
mavros_msgs::State current_state
Definition: offb_node.cpp:8
void state_cb(const mavros_msgs::State::ConstPtr &msg)
Definition: offb_node.cpp:10
int main(int argc, char **argv)
Definition: offb_node.cpp:14