fly_from_joy_test_K.cpp
Go to the documentation of this file.
00001 /*
00002 Parker Conroy
00003 Algorithmic Robotics Lab @ University of Utah
00004 
00005 
00006 This code actuates the ARdrone from a generic joystick message. It is open loop.
00007 It is intended as a simple example for those starting with the AR Drone platform.
00008 */
00009 #include <ros/ros.h>
00010 #include <std_msgs/Empty.h>
00011 #include <geometry_msgs/Twist.h>
00012 #include <sensor_msgs/Joy.h>
00013 #include <geometry_msgs/Vector3.h>
00014 #include <ardrone_autonomy/Navdata.h>
00015         
00016 double max_speed = 0.5; //[m/s]
00017 double Kp= 0.75;
00018 double Kd= 0.75;
00019 
00020 double joy_x_,joy_y_,joy_z_;
00021 int joy_a_,joy_b_,joy_xbox_;
00022 double joy_x,joy_y,joy_z;
00023 int joy_a,joy_b,joy_xbox;
00024 
00025 double drone_vx_, drone_vy_ , drone_vz_;
00026 double drone_ax_, drone_ay_ , drone_az_;
00027 double drone_vx, drone_vy , drone_vz;
00028 double drone_ax, drone_ay , drone_az;
00029 
00030 double cmd_x,cmd_y,cmd_z;
00031 int new_msg=0;
00032 int drone_state =0; 
00033 // state: {0 is failure, 2 is landed, 3 is flying, 4 is hovering, 6 taking off, 8 landing}
00034 float forget =0.99;
00035 //double joy_x_old,joy_y_old,joy_z_old;
00036 geometry_msgs::Twist twist_msg;
00037 std_msgs::Empty emp_msg;
00038 geometry_msgs::Vector3 v3_msg; //[x, y,z]
00039 sensor_msgs::Joy joy_msg_in;
00040 
00041         
00042 void joy_callback(const sensor_msgs::Joy& joy_msg_in)
00043 {
00044         //Take in xbox controller
00045         joy_x_=joy_msg_in.axes[1]; //left stick up-down
00046         joy_y_=joy_msg_in.axes[0]; //left stick left-right
00047         joy_a_=joy_msg_in.buttons[0]; //a button
00048         joy_b_=joy_msg_in.buttons[1]; //b button
00049         joy_xbox_=joy_msg_in.buttons[8]; //xbox button
00050         
00051         //Take in time
00052         //msg_time=(double)ros::Time::now().toNSec();
00053     new_msg=1;
00054 }
00055 
00056 void nav_callback(const ardrone_autonomy::Navdata& msg_in)
00057 {
00058         //Take in state of ardrone      
00059         drone_vx_=msg_in.vx*0.001; //[mm/s] to [m/s]
00060         drone_vy_=msg_in.vy*0.001;      
00061         drone_vz_=msg_in.vz*0.001;
00062         
00063         drone_ax_=msg_in.ax*9.8; //[g] to [m/s2]
00064         drone_ay_=msg_in.ay*9.8;        
00065         drone_az_=msg_in.az*9.8;
00066         
00067         drone_state=msg_in.state;       
00068         //ROS_INFO("getting sensor reading");   
00069 }
00070 
00071 void test_controller(double vx_des,double vy_des,double vz_des,double Kp, double Kd)
00072 {
00073                 geometry_msgs::Twist twist_msg_gen;
00074         
00075                 cmd_x=Kp*(vx_des-drone_vx_); //-Kd *drone_vx_   ; //{-1 to 1}=K*( m/s - m/s)
00076                 cmd_y=Kp*(vy_des-drone_vy_); 
00077                 cmd_z=Kp*(vz_des-drone_vz_);
00078                 twist_msg_gen.angular.x=1.0; 
00079                 twist_msg_gen.angular.y=1.0;
00080                 twist_msg_gen.angular.z=0.0;
00081 }               
00082 
00083 /*
00084 geometry_msgs::Twist test_controller(double vx_des,double vy_des,double vz_des,double K)
00085 {
00086                 geometry_msgs::Twist twist_msg_gen;
00087         
00088                 twist_msg_gen.linear.x=K*(vx_des-drone_vx_); //{-1 to 1}=K*( m/s - m/s)
00089                 twist_msg_gen.linear.y=K*(vy_des-drone_vy_); 
00090                 twist_msg_gen.linear.z=K*(vz_des-drone_vz_);
00091                 twist_msg_gen.angular.x=1.0; 
00092                 twist_msg_gen.angular.y=1.0;
00093                 twist_msg_gen.angular.z=0.0;
00094         
00095                 return twist_msg_gen;
00096 }
00097 */
00098 double map(double value, double in_min, double in_max, double out_min, double out_max) {
00099   return (double)((value - in_min) * (out_max - out_min) / (in_max - in_min) + out_min);
00100 }       
00101 
00102 void merge_new_mgs(void){
00103                 joy_x=joy_x_;
00104                 joy_y=joy_y_;
00105                 joy_z=joy_z_;
00106                 joy_a=joy_a_;
00107                 joy_b=joy_b_;
00108                 joy_xbox=joy_xbox_;
00109                 drone_vx=drone_vx_;
00110                 drone_vy=drone_vy_;
00111                 drone_vz=drone_vz_;
00112                 drone_ax=drone_ax_;
00113                 drone_ay=drone_ay_;
00114                 drone_az=drone_az_;
00115         }
00116 
00117 int main(int argc, char** argv)
00118 {
00119         ros::init(argc, argv,"ARDrone_fly_from_joy");
00120     ros::NodeHandle node;
00121     ros::Rate loop_rate(50);
00122         ros::Publisher pub_twist;
00123         ros::Publisher pub_empty_reset;
00124         ros::Publisher pub_empty_land;
00125         ros::Publisher pub_empty_takeoff;
00126         ros::Publisher pub_v3;
00127         ros::Subscriber joy_sub;
00128         ros::Subscriber nav_sub;
00129 
00130     pub_twist = node.advertise<geometry_msgs::Twist>("/cmd_vel", 1); 
00131     pub_v3 = node.advertise<geometry_msgs::Vector3>("/joy_vel", 1); 
00132         joy_sub = node.subscribe("/joy", 1, joy_callback);
00133         nav_sub = node.subscribe("/ardrone/navdata", 1, nav_callback);  
00134         pub_empty_reset = node.advertise<std_msgs::Empty>("/ardrone/reset", 1);
00135         pub_empty_takeoff = node.advertise<std_msgs::Empty>("/ardrone/takeoff", 1);
00136         pub_empty_land = node.advertise<std_msgs::Empty>("/ardrone/land", 1);
00137         
00138     ROS_INFO("Starting Test Node, /cmd_vel = f(joy,quad velocity)");
00139         while (ros::ok()) {
00140         merge_new_mgs();
00141 
00142                 //commands to change state of drone
00143                 if (joy_a){
00144                         while (drone_state ==2){
00145                                 ROS_INFO("Launching drone");
00146                                 pub_empty_takeoff.publish(emp_msg); //launches the drone
00147                                 ros::spinOnce();
00148                                 loop_rate.sleep();
00149                         }//drone take off
00150                 }       
00151                 if (joy_b){
00152                         while (drone_state ==3 || drone_state ==4){
00153                                 ROS_INFO("landing drone");
00154                                 pub_empty_land.publish(emp_msg); //launches the drone
00155                                 ros::spinOnce();
00156                                 loop_rate.sleep();
00157                         }//drone land
00158                 }
00159                 if (joy_xbox){
00160                         double time_start=(double)ros::Time::now().toSec();
00161                         while (drone_state ==0 ){
00162                                 ROS_INFO("landing drone");
00163                                 pub_empty_reset.publish(emp_msg); //launches the drone
00164                                 ros::spinOnce();
00165                                 loop_rate.sleep();
00166                                 if((double)ros::Time::now().toSec()> time_start+3.0){                                   
00167                                         ROS_ERROR("Time limit reached, unable reset ardrone");
00168                                         break; //exit loop
00169                                 }
00170                         }//drone take off       
00171                 }
00172 
00173                 if (fabs(joy_x)<0.01) {joy_x =0;}
00174                 //else {joy_x=joy_x*forget+joy_x_old*(1-forget);} //smoothing via forget
00175 
00176                 if (fabs(joy_y)<0.01) {joy_y =0;}
00177                 //else {joy_y=joy_y*forget+joy_y_old*(1-forget);}
00178 
00179                 if (fabs(joy_z)<0.01) {joy_z =0;}
00180                 //else {joy_z=joy_z*forget+joy_z_old*(1-forget);} 
00181 
00182                 cmd_x= joy_x*max_speed;
00183                 cmd_y= joy_y*max_speed;
00184                 cmd_z= joy_z*max_speed;
00185         
00186                 test_controller(cmd_x,cmd_x,cmd_x,Kp,Kd); //modifies cmd_x,cmd_y,cmd_z proportinal to quad_speed
00187                 
00188                 twist_msg.linear.x=cmd_x;
00189                 twist_msg.linear.y=cmd_y;       
00190                 twist_msg.linear.z=0.0;//THRUST AND YAW ARE DISABLED
00191                 twist_msg.angular.z=0.0;        
00192 
00193                 v3_msg.x=cmd_x;
00194                 v3_msg.y=cmd_y;
00195                 v3_msg.z=cmd_z;
00196 
00197                 new_msg=0;
00198                 pub_v3.publish(v3_msg);
00199                 pub_twist.publish(twist_msg);
00200 
00201                 ros::spinOnce();
00202                 loop_rate.sleep();
00203 
00204                 }//ros::ok
00205 ROS_ERROR("ROS::ok failed- Node Closing");
00206 }//main


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