quadrotor_teleop.cpp
Go to the documentation of this file.
00001 //=================================================================================================
00002 // Copyright (c) 2012, Johannes Meyer, TU Darmstadt
00003 //               2018, Wolfgang Hoenig, USC
00004 // All rights reserved.
00005 
00006 // Redistribution and use in source and binary forms, with or without
00007 // modification, are permitted provided that the following conditions are met:
00008 //     * Redistributions of source code must retain the above copyright
00009 //       notice, this list of conditions and the following disclaimer.
00010 //     * Redistributions in binary form must reproduce the above copyright
00011 //       notice, this list of conditions and the following disclaimer in the
00012 //       documentation and/or other materials provided with the distribution.
00013 //     * Neither the name of the Flight Systems and Automatic Control group,
00014 //       TU Darmstadt, nor the names of its contributors may be used to
00015 //       endorse or promote products derived from this software without
00016 //       specific prior written permission.
00017 
00018 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
00019 // ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
00020 // WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
00021 // DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER BE LIABLE FOR ANY
00022 // DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
00023 // (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
00024 // LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
00025 // ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
00026 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
00027 // SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
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 }


crazyflie_demo
Author(s): Wolfgang Hoenig
autogenerated on Wed Jun 12 2019 19:20:46