00001
00002
00003
00004
00005
00006
00007
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;
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
00034 float forget =0.99;
00035
00036 geometry_msgs::Twist twist_msg;
00037 std_msgs::Empty emp_msg;
00038 geometry_msgs::Vector3 v3_msg;
00039 sensor_msgs::Joy joy_msg_in;
00040
00041
00042 void joy_callback(const sensor_msgs::Joy& joy_msg_in)
00043 {
00044
00045 joy_x_=joy_msg_in.axes[1];
00046 joy_y_=joy_msg_in.axes[0];
00047 joy_a_=joy_msg_in.buttons[0];
00048 joy_b_=joy_msg_in.buttons[1];
00049 joy_xbox_=joy_msg_in.buttons[8];
00050
00051
00052
00053 new_msg=1;
00054 }
00055
00056 void nav_callback(const ardrone_autonomy::Navdata& msg_in)
00057 {
00058
00059 drone_vx_=msg_in.vx*0.001;
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;
00064 drone_ay_=msg_in.ay*9.8;
00065 drone_az_=msg_in.az*9.8;
00066
00067 drone_state=msg_in.state;
00068
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_);
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
00085
00086
00087
00088
00089
00090
00091
00092
00093
00094
00095
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
00143 if (joy_a){
00144 while (drone_state ==2){
00145 ROS_INFO("Launching drone");
00146 pub_empty_takeoff.publish(emp_msg);
00147 ros::spinOnce();
00148 loop_rate.sleep();
00149 }
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);
00155 ros::spinOnce();
00156 loop_rate.sleep();
00157 }
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);
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;
00169 }
00170 }
00171 }
00172
00173 if (fabs(joy_x)<0.01) {joy_x =0;}
00174
00175
00176 if (fabs(joy_y)<0.01) {joy_y =0;}
00177
00178
00179 if (fabs(joy_z)<0.01) {joy_z =0;}
00180
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);
00187
00188 twist_msg.linear.x=cmd_x;
00189 twist_msg.linear.y=cmd_y;
00190 twist_msg.linear.z=0.0;
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 }
00205 ROS_ERROR("ROS::ok failed- Node Closing");
00206 }