Texas Aerial Robotics Documentation
flight.cpp
Go to the documentation of this file.
1 #include <iostream>
2 #include <ros/ros.h>
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>
10 #include <unistd.h>
11 #include <geometry_msgs/Pose2D.h>
12 #include <mavros_msgs/CommandTOL.h>
13 #include <time.h>
14 #include <cmath>
15 #include <math.h>
16 #include <ros/duration.h>
17 
18 using namespace std;
19 
20 //Set global variables
21 mavros_msgs::State current_state;
22 //nav_msgs::Odometry current_pose;
23 geometry_msgs::PoseStamped current_pose;
24 geometry_msgs::PoseStamped pose;
25 std_msgs::Float64 current_heading;
26 float GYM_OFFSET;
27 
28 //get armed state
29 void state_cb(const mavros_msgs::State::ConstPtr& msg)
30 {
31  current_state = *msg;
32  bool connected = current_state.connected;
33  bool armed = current_state.armed;
34 }
35 //get current position of drone
36 void pose_cb(const geometry_msgs::PoseStamped::ConstPtr& msg)
37 {
38  current_pose = *msg;
39  ROS_INFO("x: %f y: %f z: %f", current_pose.pose.position.x, current_pose.pose.position.y, current_pose.pose.position.z);
40  // ROS_INFO("y: %f", current_pose.pose.position.y);
41  // ROS_INFO("z: %f", current_pose.pose.position.z);
42 }
43 // void pose_cb(const nav_msgs::Odometry::ConstPtr& msg)
44 // {
45 // current_pose = *msg;
46 // ROS_INFO("x: %f y: %f z: %f", current_pose.pose.pose.position.x, current_pose.pose.pose.position.y, current_pose.pose.pose.position.z);
47 // }
48 //get compass heading
49 void heading_cb(const std_msgs::Float64::ConstPtr& msg)
50 {
51  current_heading = *msg;
52  //ROS_INFO("current heading: %f", current_heading.data);
53 }
54 //set orientation of the drone (drone should always be level)
55 void setHeading(float heading)
56 {
57  heading = -heading + 90 - GYM_OFFSET;
58  float yaw = heading*(M_PI/180);
59  float pitch = 0;
60  float roll = 0;
61 
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);
68 
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;
73 
74  pose.pose.orientation.w = qw;
75  pose.pose.orientation.x = qx;
76  pose.pose.orientation.y = qy;
77  pose.pose.orientation.z = qz;
78 
79 }
80 // set position to fly to in the gym frame
81 void setDestination(float x, float y, float z)
82 {
83  float deg2rad = (M_PI/180);
84  float X = x*cos(-GYM_OFFSET*deg2rad) - y*sin(-GYM_OFFSET*deg2rad);
85  float Y = x*sin(-GYM_OFFSET*deg2rad) + y*cos(-GYM_OFFSET*deg2rad);
86  float Z = z;
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);
91 }
92 
93 int main(int argc, char** argv)
94 {
95  ros::init(argc, argv, "offb_node");
96  ros::NodeHandle nh;
97 
98  // the setpoint publishing rate MUST be faster than 2Hz
99  ros::Rate rate(20.0);
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);
105 
106  // allow the subscribers to initialize
107  ROS_INFO("INITILIZING...");
108  for(int i=0; i<100; i++)
109  {
110  ros::spinOnce();
111  ros::Duration(0.01).sleep();
112  }
113  while(current_state.mode != "GUIDED")
114  {
115  ros::spinOnce();
116  ros::Duration(0.01).sleep();
117  }
118 
119  //set the orientation of the gym
120  GYM_OFFSET = 0;
121  for (int i = 1; i <= 30; ++i) {
122  ros::spinOnce();
123  ros::Duration(0.1).sleep();
124  GYM_OFFSET += current_heading.data;
125  ROS_INFO("current heading%d: %f", i, GYM_OFFSET/i);
126  }
127  GYM_OFFSET /= 30;
128  ROS_INFO("the N' axis is facing: %f", GYM_OFFSET);
129  cout << GYM_OFFSET << "\n" << endl;
130 
131  // wait for FCU connection
132  while (ros::ok() && !current_state.connected)
133  {
134  ros::spinOnce();
135  rate.sleep();
136  }
137 
138  // arming
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);
144  else
145  {
146  ROS_ERROR("Failed arming");
147  return -1;
148  }
149 
150 
151  //request takeoff
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);
157  }else{
158  ROS_ERROR("Failed Takeoff");
159  return -1;
160  }
161 
162  sleep(10);
163 
164 
165  //move foreward
166  setHeading(0);
167  setDestination(0, 2, 1.5);
168  float tollorance = .35;
169  if (local_pos_pub)
170  {
171 
172  for (int i = 10000; ros::ok() && i > 0; --i)
173  {
174 
175  local_pos_pub.publish(pose);
176  // float percentErrorX = abs((pose.pose.position.x - current_pose.pose.position.x)/(pose.pose.position.x));
177  // float percentErrorY = abs((pose.pose.position.y - current_pose.pose.position.y)/(pose.pose.position.y));
178  // float percentErrorZ = abs((pose.pose.position.z - current_pose.pose.position.z)/(pose.pose.position.z));
179  // cout << " px " << percentErrorX << " py " << percentErrorY << " pz " << percentErrorZ << endl;
180  // if(percentErrorX < tollorance && percentErrorY < tollorance && percentErrorZ < tollorance)
181  // {
182  // break;
183  // }
184  float deltaX = abs(pose.pose.position.x - current_pose.pose.position.x);
185  float deltaY = abs(pose.pose.position.y - current_pose.pose.position.y);
186  float deltaZ = abs(pose.pose.position.z - current_pose.pose.position.z);
187  //cout << " dx " << deltaX << " dy " << deltaY << " dz " << deltaZ << endl;
188  float dMag = sqrt( pow(deltaX, 2) + pow(deltaY, 2) + pow(deltaZ, 2) );
189  cout << dMag << endl;
190  if( dMag < tollorance)
191  {
192  break;
193  }
194  ros::spinOnce();
195  ros::Duration(0.5).sleep();
196  if(i == 1)
197  {
198  ROS_INFO("Failed to reach destination. Stepping to next task.");
199  }
200  }
201  ROS_INFO("Done moving foreward.");
202  }
203 
204  //land
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);
209  else
210  {
211  ROS_ERROR("Landing failed");
212  ros::shutdown();
213  return -1;
214  }
215 
216  while (ros::ok())
217  {
218  ros::spinOnce();
219  rate.sleep();
220  }
221  return 0;
222 }
std_msgs::Float64 current_heading
Definition: flight.cpp:25
geometry_msgs::PoseStamped current_pose
Definition: flight.cpp:23
ros::Subscriber state_sub
void setHeading(float heading)
Definition: flight.cpp:55
void heading_cb(const std_msgs::Float64::ConstPtr &msg)
Definition: flight.cpp:49
mavros_msgs::State current_state
Definition: flight.cpp:21
ros::ServiceClient land_client
ros::Subscriber currentPos
void state_cb(const mavros_msgs::State::ConstPtr &msg)
Definition: flight.cpp:29
ros::Publisher local_pos_pub
float GYM_OFFSET
Definition: flight.cpp:26
void setDestination(float x, float y, float z)
Definition: flight.cpp:81
geometry_msgs::PoseStamped pose
Definition: flight.cpp:24
void pose_cb(const geometry_msgs::PoseStamped::ConstPtr &msg)
Definition: flight.cpp:36
int main(int argc, char **argv)
Definition: flight.cpp:93