test_fly_find_K.cpp
Go to the documentation of this file.
00001 /*
00002 Parker Conroy
00003 ARLab
00004 
00005 */
00006 #include <ros/ros.h>
00007 #include <std_msgs/Empty.h>
00008 #include <geometry_msgs/Twist.h>
00009 #include <ardrone_autonomy/Navdata.h>
00010 
00011         geometry_msgs::Twist twist_msg;
00012         geometry_msgs::Twist twist_msg_hover;
00013         geometry_msgs::Twist twist_msg_neg;
00014         geometry_msgs::Twist twist_msg_pshover;
00015         geometry_msgs::Twist twist_msg_up;
00016         std_msgs::Empty emp_msg;
00017         double vx_=0.0;
00018         double vy_=0.0;
00019         double vz_=0.0;
00020         float takeoff_time=8.0;
00021         float fly_time=3.0;
00022         float land_time=3.0;
00023         float kill_time =4.0;   
00024                         
00025 void nav_callback(const ardrone_autonomy::Navdata& msg_in)
00026 {
00027         //Take in state of ardrone      
00028         vx_=msg_in.vx*0.001;
00029         vy_=msg_in.vy*0.001;    
00030         vz_=msg_in.vz*0.001;    
00031         //ROS_INFO("getting sensor reading");   
00032 }
00033 
00034 geometry_msgs::Twist test_controller(double vx_des,double vy_des,double vz_des,double K)
00035 {
00036                 geometry_msgs::Twist twist_msg_gen;
00037         
00038                 twist_msg_gen.linear.x=K*(vx_des-vx_); //{-1 to 1}=K*( m/s - m/s)
00039                 //ROS_INFO("vx des- vx sensor &f",vx_des-vx_);
00040                 twist_msg_gen.linear.y=K*(vy_des-vy_); 
00041                 twist_msg_gen.linear.z=K*(vz_des-vz_);
00042                 twist_msg_gen.angular.x=1.0; 
00043                 twist_msg_gen.angular.y=1.0;
00044                 twist_msg_gen.angular.z=0.0;
00045                 return twist_msg_gen;
00046 }
00047 
00048 int main(int argc, char** argv)
00049 {
00050 
00051         printf("Manual Test Node Starting");
00052         ros::init(argc, argv,"ARDrone_manual_test");
00053     ros::NodeHandle node;
00054     ros::Rate loop_rate(50);
00055 
00056         ros::Publisher pub_empty_land;
00057         ros::Publisher pub_twist;
00058         ros::Publisher pub_empty_takeoff;
00059         ros::Publisher pub_empty_reset;
00060         ros::Subscriber nav_sub;
00061         double start_time;
00062 
00063 //hover message
00064                         twist_msg_hover.linear.x=0.0; 
00065                         twist_msg_hover.linear.y=0.0;
00066                         twist_msg_hover.linear.z=0.0;
00067                         twist_msg_hover.angular.x=0.0; 
00068                         twist_msg_hover.angular.y=0.0;
00069                         twist_msg_hover.angular.z=0.0;  
00070 //fly up
00071                         twist_msg_up.linear.x=0.0; 
00072                         twist_msg_up.linear.y=0.0;
00073                         twist_msg_up.linear.z=0.5;
00074                         twist_msg_up.angular.x=0.0; 
00075                         twist_msg_up.angular.y=0.0;
00076                         twist_msg_up.angular.z=0.0;                     
00077 //command message
00078                         twist_msg.linear.x=0.0; 
00079                         twist_msg.linear.y=0.0;
00080                         twist_msg.linear.z=0.0;
00081                         twist_msg.angular.x=0.0; 
00082                         twist_msg.angular.y=0.0;
00083                         twist_msg.angular.z=0.0;
00084 
00085 
00086         nav_sub = node.subscribe("/ardrone/navdata", 1, nav_callback);  
00087         pub_twist = node.advertise<geometry_msgs::Twist>("/cmd_vel", 1); 
00088         pub_empty_takeoff = node.advertise<std_msgs::Empty>("/ardrone/takeoff", 1); 
00089         pub_empty_land = node.advertise<std_msgs::Empty>("/ardrone/land", 1); 
00090         pub_empty_reset = node.advertise<std_msgs::Empty>("/ardrone/reset", 1);
00091         
00092         start_time =(double)ros::Time::now().toSec();   
00093         ROS_INFO("Starting ARdrone_test loop");
00094 
00095         double desired_vx=0.75; // [m/s]
00096         //double desired_vx=0.0; // [m/s]
00097         double K = .75; // []
00098 
00099 while (ros::ok()) {
00100                 while ((double)ros::Time::now().toSec()< start_time+takeoff_time){ //takeoff
00101                 
00102                         pub_empty_takeoff.publish(emp_msg); //launches the drone
00103                                 pub_twist.publish(twist_msg_hover); //drone is flat
00104                         ROS_INFO("Taking off");
00105                         ros::spinOnce();
00106                         loop_rate.sleep();
00107                         }//while takeoff
00108 
00109                 while  ((double)ros::Time::now().toSec()> start_time+takeoff_time+fly_time){
00110                 
00111                         pub_twist.publish(twist_msg_hover); //drone is flat
00112                 
00113                         ROS_INFO("Landing");
00114                         
00115                                         
00116                         if ((double)ros::Time::now().toSec()> takeoff_time+start_time+fly_time+land_time+kill_time){
00117                         pub_empty_land.publish(emp_msg); //lands the drone
00118                                 ROS_INFO("Closing Node");
00119                                 exit(0);        }//kill node
00120                         ros::spinOnce();
00121                         loop_rate.sleep();                      
00122 }//while land
00123 
00124                 while ( (double)ros::Time::now().toSec()> start_time+takeoff_time && (double)ros::Time::now().toSec()< start_time+takeoff_time+fly_time){       
00125                 
00126                         twist_msg=test_controller(desired_vx,0.0,0.0,K);
00127 
00128                         if((double)ros::Time::now().toSec()< start_time+takeoff_time+fly_time){
00129                         pub_twist.publish(twist_msg);
00130                         ROS_INFO("Flying +ve");
00131 
00132                         }//fly according to desired twist
00133                         
00134                         if((double)ros::Time::now().toSec()> start_time+takeoff_time+fly_time){
00135                         
00136                         desired_vx=-desired_vx;
00137                         pub_twist.publish(twist_msg);
00138                         ROS_INFO("Flying -ve");
00139 
00140                         }//fly according to desired twist
00141                         
00142                         ros::spinOnce();
00143                         loop_rate.sleep();
00144                         }
00145 
00146         ros::spinOnce();
00147         loop_rate.sleep();
00148 
00149 }//ros::ok
00150 
00151 }//main


arl_ardrone_examples
Author(s): parcon
autogenerated on Mon Jan 6 2014 11:03:12