fly_from_joy.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 
00015 double joy_x_,joy_y_,joy_z_;
00016 double joy_x,joy_y,joy_z;
00017 int new_msg=0;
00018 float forget =0.99;
00019 double joy_x_old,joy_y_old,joy_z_old;
00020 geometry_msgs::Twist twist_msg;
00021 std_msgs::Empty emp_msg;
00022 geometry_msgs::Vector3 v3_msg; //[x, y,z]
00023 sensor_msgs::Joy joy_msg_in;
00024 
00025         
00026 void joy_callback(const sensor_msgs::Joy& joy_msg_in)
00027 {
00028         //Take in joystick
00029         joy_x_=joy_msg_in.axes[0];
00030         joy_y_=joy_msg_in.axes[1];
00031         joy_z_=joy_msg_in.axes[2];
00032         
00033         //Take in time
00034         //msg_time=(double)ros::Time::now().toNSec();
00035     new_msg=1;
00036 }
00037         
00038 float map(float value, float in_min, float in_max, float out_min, float out_max) {
00039   return (float)((value - in_min) * (out_max - out_min) / (in_max - in_min) + out_min);
00040 }       
00041 
00042 int main(int argc, char** argv)
00043 {
00044         ros::init(argc, argv,"ARDrone_fly_from_joy");
00045     ros::NodeHandle node;
00046     ros::Rate loop_rate(50);
00047         ros::Publisher pub_twist;
00048         ros::Publisher pub_empty;
00049         ros::Publisher pub_v3;
00050         ros::Subscriber joy_sub;
00051 
00052     pub_twist = node.advertise<geometry_msgs::Twist>("cmd_vel", 1); //send robot input on /cmd_vel topic
00053     pub_v3 = node.advertise<geometry_msgs::Vector3>("joy_vel", 1);  //send velocity for graphing on /joy_vel topic
00054         joy_sub = node.subscribe("/joy", 1, joy_callback); //suscribe to the joystick message
00055         
00056     
00057     ROS_INFO("Waiting for joystick message");
00058     while (!new_msg){ 
00059     ros::spinOnce();
00060         loop_rate.sleep();
00061     }
00062     
00063     ROS_INFO("Starting Joy --> cmd_vel Node");
00064         while (ros::ok() && new_msg) { //start the node when a messge comes in
00065                 //pub_empty.publish(emp_msg); //launches the drone
00066 
00067                 joy_x=map(joy_x_,-1024,1024,-1,1); //map the joy input (generally 10 bit to what the AR.Drone drivers expect)
00068                 joy_y=map(joy_y_,-1024,1024,-1,1);
00069                 joy_z=map(joy_z_,-1024,1024,-1,1);
00070 
00071                 if (fabs(joy_x)<0.01) {joy_x =0;}
00072                 //else {joy_x=joy_x*forget+joy_x_old*(1-forget);} //This line can smoothing the input signal via the float forget. 
00073 
00074                 if (fabs(joy_y)<0.01) {joy_y =0;}
00075                 //else {joy_y=joy_y*forget+joy_y_old*(1-forget);}
00076 
00077                 if (fabs(joy_z)<0.01) {joy_z =0;}
00078                 //else {joy_z=joy_z*forget+joy_z_old*(1-forget);} 
00079 
00080                 ROS_INFO("new message");
00081 
00082                 twist_msg.linear.x=joy_x;
00083                 twist_msg.linear.y=joy_y;       
00084                 twist_msg.linear.z=joy_z;
00085                 twist_msg.angular.z=0.0; //YAW IS DISABLED
00086 
00087                 v3_msg.x=joy_x;
00088                 v3_msg.y=joy_y;
00089                 v3_msg.z=joy_z;
00090 
00091                 new_msg=0;
00092                 pub_v3.publish(v3_msg); //message is posted for easy graphing
00093                 pub_twist.publish(twist_msg); //send message to the robot
00094 
00095                 ros::spinOnce();
00096                 loop_rate.sleep();
00097 
00098                 }//ros::ok
00099 ROS_ERROR("ROS::ok failed- Node Closing");
00100 }//main


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