quadrotor_teleop.cpp
Go to the documentation of this file.
1 //=================================================================================================
2 // Copyright (c) 2012, Johannes Meyer, TU Darmstadt
3 // 2018, Wolfgang Hoenig, USC
4 // All rights reserved.
5 
6 // Redistribution and use in source and binary forms, with or without
7 // modification, are permitted provided that the following conditions are met:
8 // * Redistributions of source code must retain the above copyright
9 // notice, this list of conditions and the following disclaimer.
10 // * Redistributions in binary form must reproduce the above copyright
11 // notice, this list of conditions and the following disclaimer in the
12 // documentation and/or other materials provided with the distribution.
13 // * Neither the name of the Flight Systems and Automatic Control group,
14 // TU Darmstadt, nor the names of its contributors may be used to
15 // endorse or promote products derived from this software without
16 // specific prior written permission.
17 
18 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
19 // ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
20 // WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
21 // DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER BE LIABLE FOR ANY
22 // DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
23 // (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
24 // LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
25 // ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
27 // SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28 //=================================================================================================
29 
30 
31 #include <ros/ros.h>
32 #include <sensor_msgs/Joy.h>
33 #include <geometry_msgs/Twist.h>
34 
35 class Teleop
36 {
37 private:
40 
42  geometry_msgs::Twist velocity_;
43 
44  struct Axis
45  {
46  int axis;
47  double max;
48  };
49 
50  struct Button
51  {
52  int button;
53  };
54 
55  struct
56  {
61  } axes_;
62 
63  double frequency_;
64 
65 public:
67  {
68  ros::NodeHandle params("~");
69 
70  params.param<int>("x_axis", axes_.x.axis, 4);
71  params.param<int>("y_axis", axes_.y.axis, 3);
72  params.param<int>("z_axis", axes_.z.axis, 2);
73  params.param<int>("yaw_axis", axes_.yaw.axis, 1);
74 
75  params.param<double>("yaw_velocity_max", axes_.yaw.max, 90.0 * M_PI / 180.0);
76 
77  params.param<double>("x_velocity_max", axes_.x.max, 2.0);
78  params.param<double>("y_velocity_max", axes_.y.max, 2.0);
79  params.param<double>("z_velocity_max", axes_.z.max, 2.0);
80 
81  params.param<double>("frequency", frequency_, 100);
82 
83  joy_subscriber_ = node_handle_.subscribe<sensor_msgs::Joy>("joy", 1, boost::bind(&Teleop::joyTwistCallback, this, _1));
84  velocity_publisher_ = node_handle_.advertise<geometry_msgs::Twist>("cmd_vel", 10);
85  }
86 
88  {
89  stop();
90  }
91 
92  void execute()
93  {
94  ros::Rate loop_rate(frequency_);
95  while (ros::ok()) {
96  velocity_publisher_.publish(velocity_);
97 
98  ros::spinOnce();
99  loop_rate.sleep();
100  }
101  }
102 
103  void joyTwistCallback(const sensor_msgs::JoyConstPtr &joy)
104  {
105  velocity_.linear.x = getAxis(joy, axes_.x);
106  velocity_.linear.y = getAxis(joy, axes_.y);
107  velocity_.linear.z = getAxis(joy, axes_.z);
108  velocity_.angular.z = getAxis(joy, axes_.yaw);
109  }
110 
111  sensor_msgs::Joy::_axes_type::value_type getAxis(const sensor_msgs::JoyConstPtr &joy, Axis axis)
112  {
113  if (axis.axis == 0) {
114  return 0;
115  }
116  sensor_msgs::Joy::_axes_type::value_type sign = 1.0;
117  if (axis.axis < 0) {
118  sign = -1.0;
119  axis.axis = -axis.axis;
120  }
121  if ((size_t) axis.axis > joy->axes.size()) {
122  return 0;
123  }
124  return sign * joy->axes[axis.axis - 1] * axis.max;
125  }
126 
127  sensor_msgs::Joy::_buttons_type::value_type getButton(const sensor_msgs::JoyConstPtr &joy, int button)
128  {
129  if (button <= 0) {
130  return 0;
131  }
132  if ((size_t) button > joy->buttons.size()) {
133  return 0;
134  }
135  return joy->buttons[button - 1];
136  }
137 
138  void stop()
139  {
140  if(velocity_publisher_.getNumSubscribers() > 0) {
141  velocity_ = geometry_msgs::Twist();
142  velocity_publisher_.publish(velocity_);
143  }
144  }
145 };
146 
147 int main(int argc, char **argv)
148 {
149  ros::init(argc, argv, "quadrotor_teleop");
150 
151  Teleop teleop;
152  teleop.execute();
153 
154  return 0;
155 }
geometry_msgs::Twist velocity_
void publish(const boost::shared_ptr< M > &message) const
Subscriber subscribe(const std::string &topic, uint32_t queue_size, void(T::*fp)(M), T *obj, const TransportHints &transport_hints=TransportHints())
ROSCPP_DECL void init(int &argc, char **argv, const std::string &name, uint32_t options=0)
double frequency_
ros::Publisher velocity_publisher_
sensor_msgs::Joy::_buttons_type::value_type getButton(const sensor_msgs::JoyConstPtr &joy, int button)
ros::Subscriber joy_subscriber_
bool param(const std::string &param_name, T &param_val, const T &default_val) const
ROSCPP_DECL bool ok()
void execute()
Publisher advertise(const std::string &topic, uint32_t queue_size, bool latch=false)
struct Teleop::@0 axes_
bool sleep()
sensor_msgs::Joy::_axes_type::value_type getAxis(const sensor_msgs::JoyConstPtr &joy, Axis axis)
int main(int argc, char **argv)
uint32_t getNumSubscribers() const
ros::NodeHandle node_handle_
ROSCPP_DECL void spinOnce()
void joyTwistCallback(const sensor_msgs::JoyConstPtr &joy)


crazyflie_demo
Author(s): Wolfgang Hoenig
autogenerated on Mon Sep 28 2020 03:40:12