7 #ifndef INCLUDED_px4_teleop_cmds_hpp_ 8 #define INCLUDED_px4_teleop_cmds_hpp_ 14 #include <geometry_msgs/TwistStamped.h> 15 #include <geometry_msgs/PoseStamped.h> 18 #include <sensor_msgs/NavSatFix.h> 21 #include <mavros_msgs/CommandBool.h> 22 #include <mavros_msgs/SetMode.h> 23 #include <mavros_msgs/State.h> 24 #include <mavros_msgs/CommandTOL.h> 32 void arm(ros::ServiceClient& client, mavros_msgs::State& state){
38 ROS_WARN(
"Arm Rejected. Already armed.");
41 mavros_msgs::CommandBool arm_cmd;
42 arm_cmd.request.value =
true;
45 while( not(client.call(arm_cmd)) and
46 arm_cmd.response.success){
50 ROS_INFO(
"Vehicle armed.");
63 mavros_msgs::State& state,
64 geometry_msgs::PoseStamped& lpos,
65 sensor_msgs::NavSatFix& hp,
71 ROS_WARN(
"Takeoff rejected. Arm first.");
75 if(lpos.pose.position.z > 1.0){
76 ROS_WARN(
"Takeoff rejected. Already took off.");
80 double home_alt = hp.altitude;
81 double home_lon = hp.longitude;
82 double home_lat = hp.latitude;
84 mavros_msgs::CommandTOL takeoff_cmd;
85 takeoff_cmd.request.altitude = home_alt + height;
86 takeoff_cmd.request.longitude = home_lon;
87 takeoff_cmd.request.latitude = home_lat;
89 ros::Time takeoff_last_request = ros::Time::now();
91 ROS_DEBUG(
"set lat: %f, lon: %f, alt: %f", home_lat, home_lon, home_alt);
93 while( not(client.call(takeoff_cmd)) and
94 takeoff_cmd.response.success){
97 if(ros::Time::now()-takeoff_last_request>ros::Duration(3.0)){
98 ROS_WARN(
"Takeoff service call failed.");
103 takeoff_last_request = ros::Time::now();
105 while(lpos.pose.position.z < height-0.1){
108 if(ros::Time::now()-takeoff_last_request>ros::Duration(3.0)){
109 ROS_WARN(
"Takeoff failed.");
113 ROS_INFO(
"Vehicle tookoff.");
124 void land(ros::ServiceClient& client,
125 geometry_msgs::PoseStamped& lpos,
126 sensor_msgs::NavSatFix& gpos,
127 sensor_msgs::NavSatFix& hp){
131 double curr_alt = gpos.altitude;
132 double curr_lon = gpos.longitude;
133 double curr_lat = gpos.latitude;
134 double home_alt = hp.altitude;
136 mavros_msgs::CommandTOL landing_cmd;
137 landing_cmd.request.altitude = curr_alt - (curr_alt-home_alt) + 0.5;
138 landing_cmd.request.longitude = curr_lon;
139 landing_cmd.request.latitude = curr_lat;
141 while( not(client.call(landing_cmd)) and
142 landing_cmd.response.success){
146 ROS_INFO(
"Vehicle landing...");
148 while(lpos.pose.position.z > 0.1){
152 ROS_INFO(
"Vehicle landed.");
164 mavros_msgs::CommandBool arm_cmd;
165 arm_cmd.request.value =
false;
166 while(!client.call(arm_cmd) && arm_cmd.response.success){
168 ROS_INFO(
"Vehicle disarmed");
184 ros::ServiceClient& client,
185 mavros_msgs::State& state,
186 double dt,
double vx,
double vy,
double vz,
double ang_z){
190 ros::Time start = ros::Time::now();
191 ros::Time last_request = ros::Time::now();
193 mavros_msgs::SetMode offb_set_mode;
194 offb_set_mode.request.custom_mode =
"OFFBOARD";
196 geometry_msgs::TwistStamped target_vel_msg;
197 target_vel_msg.twist.linear.x = vx;
198 target_vel_msg.twist.linear.y = vy;
199 target_vel_msg.twist.linear.z = vz;
200 target_vel_msg.twist.angular.z = ang_z;
202 ROS_INFO(
"Vehicle moving ...");
204 while (ros::Time::now() - start < ros::Duration(dt)){
205 pub.publish(target_vel_msg);
207 if(state.mode!=
"OFFBOARD" and
208 (ros::Time::now() - last_request > ros::Duration(0.1))){
209 if((client.call(offb_set_mode))){
210 ROS_INFO(
"Offboard enabled.");
212 last_request = ros::Time::now();
217 ROS_INFO(
"Vehicle arrived destination.");
220 mavros_msgs::SetMode loiter_set_mode;
221 loiter_set_mode.request.custom_mode =
"AUTO.LOITER";
222 while( not(client.call(loiter_set_mode))){
226 ROS_INFO(
"Vehicle hovering.");
void disarm(ros::ServiceClient &client)
Disarm vehicle.
void takeoff(ros::ServiceClient &client, mavros_msgs::State &state, geometry_msgs::PoseStamped &lpos, sensor_msgs::NavSatFix &hp, double height)
Take off vehicle.
void land(ros::ServiceClient &client, geometry_msgs::PoseStamped &lpos, sensor_msgs::NavSatFix &gpos, sensor_msgs::NavSatFix &hp)
Land vehicle.
void arm(ros::ServiceClient &client, mavros_msgs::State &state)
Arm vehicle.
void cmd_vel(ros::Publisher &pub, ros::ServiceClient &client, mavros_msgs::State &state, double dt, double vx, double vy, double vz, double ang_z)
Send velocity command to vehicle.