3 #include <geometry_msgs/PoseStamped.h> 4 #include <nav_msgs/Odometry.h> 5 #include "std_msgs/Float64.h" 6 #include <mavros_msgs/CommandBool.h> 7 #include <mavros_msgs/SetMode.h> 8 #include <mavros_msgs/State.h> 9 #include <mavros_msgs/PositionTarget.h> 11 #include <geometry_msgs/Pose2D.h> 12 #include <mavros_msgs/CommandTOL.h> 16 #include <ros/duration.h> 24 geometry_msgs::PoseStamped
pose;
29 void state_cb(
const mavros_msgs::State::ConstPtr& msg)
36 void pose_cb(
const geometry_msgs::PoseStamped::ConstPtr& msg)
58 float yaw = heading*(M_PI/180);
62 float cy = cos(yaw * 0.5);
63 float sy = sin(yaw * 0.5);
64 float cr = cos(roll * 0.5);
65 float sr = sin(roll * 0.5);
66 float cp = cos(pitch * 0.5);
67 float sp = sin(pitch * 0.5);
69 float qw = cy * cr * cp + sy * sr * sp;
70 float qx = cy * sr * cp - sy * cr * sp;
71 float qy = cy * cr * sp + sy * sr * cp;
72 float qz = sy * cr * cp - cy * sr * sp;
74 pose.pose.orientation.w = qw;
75 pose.pose.orientation.x = qx;
76 pose.pose.orientation.y = qy;
77 pose.pose.orientation.z = qz;
83 float deg2rad = (M_PI/180);
87 pose.pose.position.x = X;
88 pose.pose.position.y = Y;
89 pose.pose.position.z = Z;
90 ROS_INFO(
"Destination set to x: %f y: %f z %f", X, Y, Z);
93 int main(
int argc,
char** argv)
95 ros::init(argc, argv,
"offb_node");
100 ros::Subscriber
state_sub = nh.subscribe<mavros_msgs::State>(
"mavros/state", 10,
state_cb);
101 ros::Publisher set_vel_pub = nh.advertise<mavros_msgs::PositionTarget>(
"mavros/setpoint_raw/local", 10);
102 ros::Publisher
local_pos_pub = nh.advertise<geometry_msgs::PoseStamped>(
"mavros/setpoint_position/local", 10);
103 ros::Subscriber
currentPos = nh.subscribe<geometry_msgs::PoseStamped>(
"/mavros/global_position/pose", 10,
pose_cb);
104 ros::Subscriber currentHeading = nh.subscribe<std_msgs::Float64>(
"/mavros/global_position/compass_hdg", 10,
heading_cb);
107 ROS_INFO(
"INITILIZING...");
108 for(
int i=0; i<100; i++)
111 ros::Duration(0.01).sleep();
116 ros::Duration(0.01).sleep();
121 for (
int i = 1; i <= 30; ++i) {
123 ros::Duration(0.1).sleep();
125 ROS_INFO(
"current heading%d: %f", i,
GYM_OFFSET/i);
128 ROS_INFO(
"the N' axis is facing: %f",
GYM_OFFSET);
139 ros::ServiceClient arming_client_i = nh.serviceClient<mavros_msgs::CommandBool>(
"mavros/cmd/arming");
140 mavros_msgs::CommandBool srv_arm_i;
141 srv_arm_i.request.value =
true;
142 if (arming_client_i.call(srv_arm_i) && srv_arm_i.response.success)
143 ROS_INFO(
"ARM sent %d", srv_arm_i.response.success);
146 ROS_ERROR(
"Failed arming");
152 ros::ServiceClient takeoff_cl = nh.serviceClient<mavros_msgs::CommandTOL>(
"/mavros/cmd/takeoff");
153 mavros_msgs::CommandTOL srv_takeoff;
154 srv_takeoff.request.altitude = 1.5;
155 if(takeoff_cl.call(srv_takeoff)){
156 ROS_INFO(
"takeoff sent %d", srv_takeoff.response.success);
158 ROS_ERROR(
"Failed Takeoff");
168 float tollorance = .35;
172 for (
int i = 10000; ros::ok() && i > 0; --i)
188 float dMag = sqrt( pow(deltaX, 2) + pow(deltaY, 2) + pow(deltaZ, 2) );
189 cout << dMag << endl;
190 if( dMag < tollorance)
195 ros::Duration(0.5).sleep();
198 ROS_INFO(
"Failed to reach destination. Stepping to next task.");
201 ROS_INFO(
"Done moving foreward.");
205 ros::ServiceClient
land_client = nh.serviceClient<mavros_msgs::CommandTOL>(
"/mavros/cmd/land");
206 mavros_msgs::CommandTOL srv_land;
207 if (
land_client.call(srv_land) && srv_land.response.success)
208 ROS_INFO(
"land sent %d", srv_land.response.success);
211 ROS_ERROR(
"Landing failed");
std_msgs::Float64 current_heading
geometry_msgs::PoseStamped current_pose
ros::Subscriber state_sub
void setHeading(float heading)
void heading_cb(const std_msgs::Float64::ConstPtr &msg)
mavros_msgs::State current_state
ros::ServiceClient land_client
ros::Subscriber currentPos
void state_cb(const mavros_msgs::State::ConstPtr &msg)
ros::Publisher local_pos_pub
void setDestination(float x, float y, float z)
geometry_msgs::PoseStamped pose
void pose_cb(const geometry_msgs::PoseStamped::ConstPtr &msg)
int main(int argc, char **argv)