Go to the documentation of this file.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
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;
00023 sensor_msgs::Joy joy_msg_in;
00024
00025
00026 void joy_callback(const sensor_msgs::Joy& joy_msg_in)
00027 {
00028
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
00034
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);
00053 pub_v3 = node.advertise<geometry_msgs::Vector3>("joy_vel", 1);
00054 joy_sub = node.subscribe("/joy", 1, joy_callback);
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) {
00065
00066
00067 joy_x=map(joy_x_,-1024,1024,-1,1);
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
00073
00074 if (fabs(joy_y)<0.01) {joy_y =0;}
00075
00076
00077 if (fabs(joy_z)<0.01) {joy_z =0;}
00078
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;
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);
00093 pub_twist.publish(twist_msg);
00094
00095 ros::spinOnce();
00096 loop_rate.sleep();
00097
00098 }
00099 ROS_ERROR("ROS::ok failed- Node Closing");
00100 }