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 #include <ros/ros.h>
00031 #include <sensor_msgs/Joy.h>
00032 #include <geometry_msgs/Twist.h>
00033
00034 namespace hector_quadrotor {
00035
00036 class Teleop
00037 {
00038 private:
00039 ros::NodeHandle node_handle_;
00040 ros::Subscriber joy_subscriber_;
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 unsigned int button;
00053 };
00054
00055 struct {
00056 Axis x;
00057 Axis y;
00058 Axis z;
00059 Axis yaw;
00060 } axes_;
00061
00062 struct
00063 {
00064 } buttons_;
00065
00066 public:
00067 Teleop()
00068 {
00069 ros::NodeHandle params("~");
00070 axes_.x.axis = 0;
00071 axes_.x.max = 2.0;
00072 axes_.y.axis = 0;
00073 axes_.y.max = 2.0;
00074 axes_.z.axis = 0;
00075 axes_.z.max = 2.0;
00076 axes_.yaw.axis = 0;
00077 axes_.yaw.max = 90.0*M_PI/180.0;
00078 params.getParam("x_axis", axes_.x.axis);
00079 params.getParam("y_axis", axes_.y.axis);
00080 params.getParam("z_axis", axes_.z.axis);
00081 params.getParam("yaw_axis", axes_.yaw.axis);
00082 params.getParam("x_velocity_max", axes_.x.max);
00083 params.getParam("y_velocity_max", axes_.y.max);
00084 params.getParam("z_velocity_max", axes_.z.max);
00085 params.getParam("yaw_velocity_max", axes_.yaw.max);
00086
00087 joy_subscriber_ = node_handle_.subscribe<sensor_msgs::Joy>("joy", 1, boost::bind(&Teleop::joyCallback, this, _1));
00088 velocity_publisher_ = node_handle_.advertise<geometry_msgs::Twist>("cmd_vel", 10);
00089 }
00090
00091 ~Teleop()
00092 {
00093 stop();
00094 }
00095
00096 void joyCallback(const sensor_msgs::JoyConstPtr& joy)
00097 {
00098 velocity_.linear.x = getAxis(joy, axes_.x.axis) * axes_.x.max;
00099 velocity_.linear.y = getAxis(joy, axes_.y.axis) * axes_.y.max;
00100 velocity_.linear.z = getAxis(joy, axes_.z.axis) * axes_.z.max;
00101 velocity_.angular.z = getAxis(joy, axes_.yaw.axis) * axes_.yaw.max;
00102 velocity_publisher_.publish(velocity_);
00103 }
00104
00105 sensor_msgs::Joy::_axes_type::value_type getAxis(const sensor_msgs::JoyConstPtr& joy, int axis)
00106 {
00107 if (axis == 0) return 0;
00108 sensor_msgs::Joy::_axes_type::value_type sign = 1.0;
00109 if (axis < 0) { sign = -1.0; axis = -axis; }
00110 if ((size_t)axis > joy->axes.size()) return 0;
00111 return sign * joy->axes[axis - 1];
00112 }
00113
00114 sensor_msgs::Joy::_buttons_type::value_type getButton(const sensor_msgs::JoyConstPtr& joy, int button)
00115 {
00116 if (button <= 0) return 0;
00117 if ((size_t)button > joy->axes.size()) return 0;
00118 return joy->buttons[button - 1];
00119 }
00120
00121 void stop()
00122 {
00123 velocity_ = geometry_msgs::Twist();
00124 velocity_publisher_.publish(velocity_);
00125 }
00126 };
00127
00128 }
00129
00130 int main(int argc, char **argv)
00131 {
00132 ros::init(argc, argv, "quadrotor_teleop");
00133
00134 hector_quadrotor::Teleop teleop;
00135 ros::spin();
00136
00137 return 0;
00138 }