3 #include <geometry_msgs/PoseStamped.h> 4 #include <nav_msgs/Odometry.h> 5 #include "std_msgs/Float64.h" 6 #include "std_msgs/String.h" 7 #include <mavros_msgs/CommandBool.h> 8 #include <mavros_msgs/SetMode.h> 9 #include <mavros_msgs/State.h> 10 #include <mavros_msgs/PositionTarget.h> 12 #include <geometry_msgs/Pose2D.h> 13 #include <mavros_msgs/CommandTOL.h> 14 #include <sensor_msgs/LaserScan.h> 18 #include <ros/duration.h> 42 void state_cb(
const mavros_msgs::State::ConstPtr& msg)
47 void pose_cb(
const nav_msgs::Odometry::ConstPtr& msg)
54 float psi = atan2((2*(q0*q3 + q1*q2)), (1 - 2*(pow(q2,2) + pow(q3,2))) );
59 void mode_cb(
const std_msgs::String::ConstPtr& msg)
64 void scan_cb(
const sensor_msgs::LaserScan::ConstPtr& msg)
72 float yaw = heading*(M_PI/180);
76 float cy = cos(yaw * 0.5);
77 float sy = sin(yaw * 0.5);
78 float cr = cos(roll * 0.5);
79 float sr = sin(roll * 0.5);
80 float cp = cos(pitch * 0.5);
81 float sp = sin(pitch * 0.5);
83 float qw = cy * cr * cp + sy * sr * sp;
84 float qx = cy * sr * cp - sy * cr * sp;
85 float qy = cy * cr * sp + sy * sr * cp;
86 float qz = sy * cr * cp - cy * sr * sp;
102 float deg2rad = (M_PI/180);
110 cout <<
"Destination set to x: " << X <<
" y: " << Y <<
" z: " << Z << endl;
116 ROS_INFO(
"INITIALIZING ROS");
117 for(
int i=0; i<100; i++)
120 ros::Duration(0.01).sleep();
127 geometry_msgs::PoseStamped waypoint_eval;
128 waypoint_eval = *msg;
129 float way_x = waypoint_eval.pose.position.x;
130 float way_y = waypoint_eval.pose.position.y;
131 float way_z = waypoint_eval.pose.position.z;
135 if(way_z < z_max && way_z >
z_min && way_x < x_max && way_x >
x_min && way_y < y_max && way_y >
y_min)
144 ROS_INFO(
"The waypoint passed is out of bounds at x,y,z = %f,%f,%f",way_x,way_y,way_z);
149 ROS_INFO(
"Waypoint ignored. Obs avoidance in progress...");
153 int main(
int argc,
char** argv)
155 ros::init(argc, argv,
"offb_node");
156 ros::NodeHandle controlnode;
158 ros::param::get(
"/contols_params/safety_limits/xlim/min",
x_min);
159 ros::param::get(
"/contols_params/safety_limits/xlim/max",
x_max);
160 ros::param::get(
"/contols_params/safety_limits/ylim/min",
y_min);
161 ros::param::get(
"/contols_params/safety_limits/ylim/max",
y_max);
162 ros::param::get(
"/contols_params/safety_limits/xlim/min",
z_min);
163 ros::param::get(
"/contols_params/safety_limits/xlim/max",
z_max);
166 ros::Publisher gym_offset_pub = controlnode.advertise<std_msgs::Float64>(
"gymOffset", 1);
167 ros::Publisher
local_pos_pub = controlnode.advertise<geometry_msgs::PoseStamped>(
"mavros/setpoint_position/local", 10);
168 ros::Publisher run_start_pub = controlnode.advertise<std_msgs::Float64>(
"runStartTime", 1);
169 ros::Subscriber collision_sub = controlnode.subscribe<sensor_msgs::LaserScan>(
"/scan", 1,
scan_cb);
170 ros::Subscriber
currentPos = controlnode.subscribe<nav_msgs::Odometry>(
"mavros/global_position/local", 10,
pose_cb);
171 ros::Subscriber heading_pub = controlnode.subscribe<std_msgs::Float64>(
"setHeading", 1,
setHeading_cb);
172 ros::Subscriber mode_sub = controlnode.subscribe<std_msgs::String>(
"mode", 10,
mode_cb);
173 ros::Subscriber
state_sub = controlnode.subscribe<mavros_msgs::State>(
"mavros/state", 10,
state_cb);
174 ros::Subscriber waypointSubscrib = controlnode.subscribe<geometry_msgs::PoseStamped>(
"TARwaypoint", 10,
waypoint_update);
175 ros::ServiceClient
arming_client = controlnode.serviceClient<mavros_msgs::CommandBool>(
"mavros/cmd/arming");
176 ros::ServiceClient
land_client = controlnode.serviceClient<mavros_msgs::CommandTOL>(
"/mavros/cmd/land");
177 ros::ServiceClient
set_mode_client = controlnode.serviceClient<mavros_msgs::SetMode>(
"/mavros/set_mode");
178 ros::ServiceClient
takeoff_client = controlnode.serviceClient<mavros_msgs::CommandTOL>(
"/mavros/cmd/takeoff");
186 ROS_INFO(
"Connected to FCU");
196 std_msgs::Float64 runStartTime;
198 run_start_pub.publish(runStartTime);
200 ROS_INFO(
"Mode set to GUIDED");
204 for (
int i = 1; i <= 30; ++i) {
206 ros::Duration(0.1).sleep();
212 float psi = atan2((2*(q0*q3 + q1*q2)), (1 - 2*(pow(q2,2) + pow(q3,2))) );
219 ROS_INFO(
"the N' axis is facing: %f",
GYM_OFFSET);
221 std_msgs::Float64 gymOffset;
223 gym_offset_pub.publish(gymOffset);
226 mavros_msgs::CommandBool arm_request;
227 arm_request.request.value =
true;
228 while (!
current_state.armed && !arm_request.response.success)
230 ros::Duration(.1).sleep();
233 ROS_INFO(
"ARM sent %d", arm_request.response.success);
237 mavros_msgs::CommandTOL takeoff_request;
238 takeoff_request.request.altitude = 1.5;
240 while (!takeoff_request.response.success)
242 ros::Duration(.1).sleep();
245 ROS_INFO(
"Takeoff initialized");
252 float tollorance = .35;
253 float scanMinRange = .45;
254 float scanMaxRange = 1.75;
262 int scanRayIndex = 2;
280 ROS_INFO(
"Index[%d]: %f", scanRayIndex,
current_2D_scan.ranges[scanRayIndex]);
289 double angle_of_obstacle_RAD =
current_2D_scan.angle_increment*scanRayIndex;
290 ROS_INFO(
"Obstacle sighted @%f (current_2D_scan.angle_increment: %f * scanRayIndex: %d)", angle_of_obstacle_RAD,
current_2D_scan.angle_increment, scanRayIndex);
291 ROS_INFO(
"-------------------SKRTT SKRTT-------------------");
294 float deg2rad = (M_PI/180);
298 ROS_INFO(
"x: %f y: %f z: %f", X, Y, Z);
301 double why = radius * sin(angle_of_obstacle_RAD);
302 double ex = radius * cos(angle_of_obstacle_RAD);
304 ROS_INFO(
"radius: %f, ex: %f, why: %f", radius, ex, why);
307 float avoidXdrone = (ex/radius)*(-1.328);
308 float avoidYdrone = (why/radius)*(-1.328);
314 ROS_INFO(
"goto x: %f, y: %f", avoidXgym, avoidYgym);
327 mavros_msgs::CommandTOL srv_land;
328 if(
land_client.call(srv_land) && srv_land.response.success)
329 ROS_INFO(
"land sent %d", srv_land.response.success);
332 ROS_ERROR(
"Landing failed");
335 else if(
MODE.data ==
"TAKEOFF")
342 ros::Duration(5).sleep();
347 mavros_msgs::SetMode srv_setMode;
348 srv_setMode.request.base_mode = 0;
349 srv_setMode.request.custom_mode =
"GUIDED";
351 ROS_INFO(
"setmode send ok");
353 ROS_ERROR(
"Failed SetMode");
356 ros::Duration(.5).sleep();
360 mavros_msgs::CommandBool arm_request;
361 arm_request.request.value =
true;
365 ros::Duration(.1).sleep();
368 ROS_INFO(
"ARM sent %d", arm_request.response.success);
369 mavros_msgs::CommandTOL takeoff_request;
370 takeoff_request.request.altitude = 1.5;
372 while (!takeoff_request.response.success &&
current_state.mode ==
"GUIDED")
375 ros::Duration(.1).sleep();
378 ROS_INFO(
"Takeoff sent");
379 ros::Duration(5).sleep();
383 gym_offset_pub.publish(gymOffset);
sensor_msgs::LaserScan current_2D_scan
void pose_cb(const nav_msgs::Odometry::ConstPtr &msg)
ros::Subscriber state_sub
void set_heading(float heading)
ros::ServiceClient land_client
mavros_msgs::State current_state
ros::ServiceClient takeoff_client
ros::ServiceClient set_mode_client
void mode_cb(const std_msgs::String::ConstPtr &msg)
ros::Subscriber currentPos
void scan_cb(const sensor_msgs::LaserScan::ConstPtr &msg)
void state_cb(const mavros_msgs::State::ConstPtr &msg)
nav_msgs::Odometry current_pose
void setHeading_cb(const std_msgs::Float64::ConstPtr &msg)
ros::Publisher local_pos_pub
void setHeading(float heading)
void setDestination(float x, float y, float z)
ros::ServiceClient arming_client
void waypoint_update(const geometry_msgs::PoseStamped::ConstPtr &msg)
int main(int argc, char **argv)
geometry_msgs::PoseStamped waypoint