1 #include <mavros_msgs/CommandTOL.h> 2 #include <mavros_msgs/State.h> 3 #include <nav_msgs/Odometry.h> 4 #include <geometry_msgs/Pose.h> 5 #include <geometry_msgs/PoseStamped.h> 9 #include <std_msgs/Float64.h> 10 #include <std_msgs/String.h> 11 #include <mavros_msgs/CommandBool.h> 12 #include <mavros_msgs/SetMode.h> 13 #include <mavros_msgs/PositionTarget.h> 16 #include <ros/duration.h> 57 void state_cb(
const mavros_msgs::State::ConstPtr& msg)
63 float x = current_pose_enu.pose.pose.position.x;
64 float y = current_pose_enu.pose.pose.position.y;
65 float z = current_pose_enu.pose.pose.position.z;
66 float deg2rad = (M_PI/180);
73 void pose_cb(
const nav_msgs::Odometry::ConstPtr& msg)
81 float psi = atan2((2*(q0*q3 + q1*q2)), (1 - 2*(pow(q2,2) + pow(q3,2))) );
98 float yaw = heading*(M_PI/180);
102 float cy = cos(yaw * 0.5);
103 float sy = sin(yaw * 0.5);
104 float cr = cos(roll * 0.5);
105 float sr = sin(roll * 0.5);
106 float cp = cos(pitch * 0.5);
107 float sp = sin(pitch * 0.5);
109 float qw = cy * cr * cp + sy * sr * sp;
110 float qx = cy * sr * cp - sy * cr * sp;
111 float qy = cy * cr * sp + sy * sr * cp;
112 float qz = sy * cr * cp - cy * sr * sp;
129 float deg2rad = (M_PI/180);
137 ROS_INFO(
"Destination set to x: %f y: %f z: %f origin frame", x, y, z);
152 ROS_INFO(
"Waiting for FCU connection");
157 ros::Duration(0.01).sleep();
161 ROS_INFO(
"Connected to FCU");
164 ROS_INFO(
"Error connecting to drone");
178 ROS_INFO(
"Waiting for user to set mode to GUIDED");
182 ros::Duration(0.01).sleep();
186 ROS_INFO(
"Mode set to GUIDED. Mission starting");
189 ROS_INFO(
"Error starting mission!!");
201 ROS_INFO(
"Initializing local coordinate system");
203 for (
int i = 1; i <= 30; i++) {
205 ros::Duration(0.1).sleep();
211 float psi = atan2((2*(q0*q3 + q1*q2)), (1 - 2*(pow(q2,2) + pow(q3,2))) );
217 ROS_INFO(
"Coordinate offset set");
232 for(
int i=0; i<100; i++)
236 ros::Duration(0.01).sleep();
239 ROS_INFO(
"Arming drone");
240 mavros_msgs::CommandBool arm_request;
241 arm_request.request.value =
true;
244 ros::Duration(.1).sleep();
248 if(arm_request.response.success)
250 ROS_INFO(
"Arming Successful");
252 ROS_INFO(
"Arming failed with %d", arm_request.response.success);
258 mavros_msgs::CommandTOL srv_takeoff;
259 srv_takeoff.request.altitude = takeoff_alt;
261 ROS_INFO(
"takeoff sent %d", srv_takeoff.response.success);
263 ROS_ERROR(
"Failed Takeoff");
279 float tollorance = .3;
283 float dMag = sqrt( pow(deltaX, 2) + pow(deltaY, 2) + pow(deltaZ, 2) );
285 if( dMag < tollorance)
300 mavros_msgs::CommandTOL srv_land;
301 if(
land_client.call(srv_land) && srv_land.response.success)
303 ROS_INFO(
"land sent %d", srv_land.response.success);
306 ROS_ERROR(
"Landing failed");
317 local_pos_pub = controlnode.advertise<geometry_msgs::PoseStamped>(
"mavros/setpoint_position/local", 10);
318 currentPos = controlnode.subscribe<nav_msgs::Odometry>(
"mavros/global_position/local", 10,
pose_cb);
319 state_sub = controlnode.subscribe<mavros_msgs::State>(
"mavros/state", 10,
state_cb);
320 arming_client = controlnode.serviceClient<mavros_msgs::CommandBool>(
"mavros/cmd/arming");
321 land_client = controlnode.serviceClient<mavros_msgs::CommandTOL>(
"/mavros/cmd/land");
322 set_mode_client = controlnode.serviceClient<mavros_msgs::SetMode>(
"/mavros/set_mode");
323 takeoff_client = controlnode.serviceClient<mavros_msgs::CommandTOL>(
"/mavros/cmd/takeoff");
float correction_heading_g
ros::Subscriber state_sub
geometry_msgs::Pose correction_vector_g
void set_heading(float heading)
float y
distance in y with respect to your reference frame
float psi
rotation about the third axis of your reference frame
nav_msgs::Odometry current_pose_g
ros::ServiceClient land_client
void enu_2_local(nav_msgs::Odometry current_pose_enu)
void set_destination(float x, float y, float z, float psi)
ros::ServiceClient takeoff_client
int check_waypoint_reached()
int init_publisher_subscriber(ros::NodeHandle controlnode)
ros::ServiceClient set_mode_client
geometry_msgs::PoseStamped waypoint_g
ros::Subscriber currentPos
mavros_msgs::State current_state_g
int takeoff(float takeoff_alt)
void pose_cb(const nav_msgs::Odometry::ConstPtr &msg)
float z
distance in z with respect to your reference frame
ros::Publisher local_pos_pub
int initialize_local_frame()
ros::ServiceClient arming_client
float x
distance in x with respect to your reference frame
void state_cb(const mavros_msgs::State::ConstPtr &msg)