Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031 #include <ros/ros.h>
00032 #include <sensor_msgs/Joy.h>
00033 #include <geometry_msgs/Twist.h>
00034
00035 class Teleop
00036 {
00037 private:
00038 ros::NodeHandle node_handle_;
00039 ros::Subscriber joy_subscriber_;
00040
00041 ros::Publisher velocity_publisher_;
00042 geometry_msgs::Twist velocity_;
00043
00044 struct Axis
00045 {
00046 int axis;
00047 double max;
00048 };
00049
00050 struct Button
00051 {
00052 int button;
00053 };
00054
00055 struct
00056 {
00057 Axis x;
00058 Axis y;
00059 Axis z;
00060 Axis yaw;
00061 } axes_;
00062
00063 double frequency_;
00064
00065 public:
00066 Teleop()
00067 {
00068 ros::NodeHandle params("~");
00069
00070 params.param<int>("x_axis", axes_.x.axis, 4);
00071 params.param<int>("y_axis", axes_.y.axis, 3);
00072 params.param<int>("z_axis", axes_.z.axis, 2);
00073 params.param<int>("yaw_axis", axes_.yaw.axis, 1);
00074
00075 params.param<double>("yaw_velocity_max", axes_.yaw.max, 90.0 * M_PI / 180.0);
00076
00077 params.param<double>("x_velocity_max", axes_.x.max, 2.0);
00078 params.param<double>("y_velocity_max", axes_.y.max, 2.0);
00079 params.param<double>("z_velocity_max", axes_.z.max, 2.0);
00080
00081 params.param<double>("frequency", frequency_, 100);
00082
00083 joy_subscriber_ = node_handle_.subscribe<sensor_msgs::Joy>("joy", 1, boost::bind(&Teleop::joyTwistCallback, this, _1));
00084 velocity_publisher_ = node_handle_.advertise<geometry_msgs::Twist>("cmd_vel", 10);
00085 }
00086
00087 ~Teleop()
00088 {
00089 stop();
00090 }
00091
00092 void execute()
00093 {
00094 ros::Rate loop_rate(frequency_);
00095 while (ros::ok()) {
00096 velocity_publisher_.publish(velocity_);
00097
00098 ros::spinOnce();
00099 loop_rate.sleep();
00100 }
00101 }
00102
00103 void joyTwistCallback(const sensor_msgs::JoyConstPtr &joy)
00104 {
00105 velocity_.linear.x = getAxis(joy, axes_.x);
00106 velocity_.linear.y = getAxis(joy, axes_.y);
00107 velocity_.linear.z = getAxis(joy, axes_.z);
00108 velocity_.angular.z = getAxis(joy, axes_.yaw);
00109 }
00110
00111 sensor_msgs::Joy::_axes_type::value_type getAxis(const sensor_msgs::JoyConstPtr &joy, Axis axis)
00112 {
00113 if (axis.axis == 0) {
00114 return 0;
00115 }
00116 sensor_msgs::Joy::_axes_type::value_type sign = 1.0;
00117 if (axis.axis < 0) {
00118 sign = -1.0;
00119 axis.axis = -axis.axis;
00120 }
00121 if ((size_t) axis.axis > joy->axes.size()) {
00122 return 0;
00123 }
00124 return sign * joy->axes[axis.axis - 1] * axis.max;
00125 }
00126
00127 sensor_msgs::Joy::_buttons_type::value_type getButton(const sensor_msgs::JoyConstPtr &joy, int button)
00128 {
00129 if (button <= 0) {
00130 return 0;
00131 }
00132 if ((size_t) button > joy->buttons.size()) {
00133 return 0;
00134 }
00135 return joy->buttons[button - 1];
00136 }
00137
00138 void stop()
00139 {
00140 if(velocity_publisher_.getNumSubscribers() > 0) {
00141 velocity_ = geometry_msgs::Twist();
00142 velocity_publisher_.publish(velocity_);
00143 }
00144 }
00145 };
00146
00147 int main(int argc, char **argv)
00148 {
00149 ros::init(argc, argv, "quadrotor_teleop");
00150
00151 Teleop teleop;
00152 teleop.execute();
00153
00154 return 0;
00155 }