joystick_handler.h
Go to the documentation of this file.
1 //=================================================================================================
2 // Copyright (c) 2018, Alexander Stumpf, TU Darmstadt
3 // All rights reserved.
4 
5 // Redistribution and use in source and binary forms, with or without
6 // modification, are permitted provided that the following conditions are met:
7 // * Redistributions of source code must retain the above copyright
8 // notice, this list of conditions and the following disclaimer.
9 // * Redistributions in binary form must reproduce the above copyright
10 // notice, this list of conditions and the following disclaimer in the
11 // documentation and/or other materials provided with the distribution.
12 // * Neither the name of the Simulation, Systems Optimization and Robotics
13 // group, TU Darmstadt nor the names of its contributors may be used to
14 // endorse or promote products derived from this software without
15 // specific prior written permission.
16 
17 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
18 // ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
19 // WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
20 // DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER BE LIABLE FOR ANY
21 // DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
22 // (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
23 // LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
24 // ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
26 // SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 //=================================================================================================
28 
29 #ifndef JOYSTICK_HANDLER_H__
30 #define JOYSTICK_HANDLER_H__
31 
32 #include <ros/ros.h>
33 
34 #include <geometry_msgs/Point.h>
35 #include <geometry_msgs/Twist.h>
36 
37 #include <sensor_msgs/Joy.h>
38 
39 
40 
42 {
44 {
45 public:
46  JoystickInputHandle(double thresh = 1.0)
47  : val_(0.0)
48  , thresh_(thresh)
49  {}
50 
51  virtual void joyCallback(const sensor_msgs::Joy::ConstPtr& joy_msg) = 0;
52 
53  virtual double getValue() const { return val_; }
54  virtual bool isPressed() const { return std::abs(val_) >= thresh_; }
55 
58 
59 protected:
60  double val_;
61  double thresh_;
62 };
63 
65  : public JoystickInputHandle
66 {
67 public:
68  JoystickButton(int button, double thresh = 1.0)
69  : JoystickInputHandle(thresh)
70  , button_id_(button)
71  {
72  ROS_INFO("[Button %i]", button_id_);
73  }
74 
76  : JoystickButton(params["button"], params["thresh"])
77  {}
78 
79  void joyCallback(const sensor_msgs::Joy::ConstPtr& joy_msg) override
80  {
81  if (joy_msg->buttons.size() <= button_id_)
82  ROS_ERROR_THROTTLE(1.0, "Couldn't read button %i as only %lu buttons exist. Maybe wrong controller connected?", button_id_, joy_msg->buttons.size());
83  else
84  val_ = joy_msg->buttons[button_id_];
85  }
86 
87 protected:
89 };
90 
92  : public JoystickInputHandle
93 {
94 public:
95  JoystickAxis(int axis, double thresh = 1.0, double min_val = -1.0, double max_val = 1.0, double zero_val = 0.0, double calib_offset = 0.0, bool invert = false)
96  : JoystickInputHandle(thresh)
97  , axis_id_(axis)
98  , min_(min_val)
99  , max_(max_val)
100  , zero_val_(zero_val)
101  , calib_offset_(calib_offset)
102  , invert_(invert)
103  , scale_factor_(2.0/(max_-min_))
104  , scale_offset_(0.5*(max_+min_))
105  {
106  ROS_INFO("[Axis %i] %f - (%f, %f) - %f", axis, thresh, min_val, max_val, calib_offset);
107  }
108 
110  : JoystickAxis(params["axis"], params["thresh"], params["min"], params["max"], params["zero"], params["calib"], params["invert"])
111  {}
112 
113  void joyCallback(const sensor_msgs::Joy::ConstPtr& joy_msg) override
114  {
115  if (joy_msg->axes.size() <= axis_id_)
116  ROS_ERROR_THROTTLE(1.0, "Couldn't read axis %i as only %lu axes exist. Maybe wrong controller connected?", axis_id_, joy_msg->axes.size());
117  else
118  {
119  // read value und remove offset
120  val_ = joy_msg->axes[axis_id_] - calib_offset_;
121 
122  // invert if set
123  if (invert_)
124  val_ = -val_;
125 
126  // rescale to [-1; 1] based on min max borders
127  val_ = std::min(std::max(val_, min_), max_); // clamping in [min; max]
128  val_ = val_ * scale_factor_ + scale_offset_; // rescale
129  }
130  }
131 
132  bool isPressed() const override { return std::abs(val_-zero_val_) >= thresh_; }
133 
134 protected:
135  int axis_id_;
136 
137  double min_;
138  double max_;
139  double zero_val_;
140 
142 
143  bool invert_;
144 
147 };
148 
150 {
151 public:
153  virtual ~JoystickHandler();
154 
155  // joystick input
156  void joyCallback(const sensor_msgs::Joy::ConstPtr& last_joy_msg);
157 
158  void getJoystickCommand(geometry_msgs::Twist& twist, bool& enable) const;
159 
162 
163 protected:
164  void initJoystickInput(XmlRpc::XmlRpcValue& params, std::string name, JoystickInputHandle::Ptr& handle) const;
165 
166  void updateJoystickCommand(double elapsed_time_sec, double joy_val, double& val, double min_vel, double max_vel, double min_acc, double max_acc, double sensivity) const;
167  double convertJoyAxisToAcc(double elapsed_time_sec, double joy_val, double val, double min_vel, double max_vel) const;
168 
169  // joystick settings
173 
175 
177 
178  // subscriber
180 };
181 }
182 
183 #endif
virtual void joyCallback(const sensor_msgs::Joy::ConstPtr &joy_msg)=0
JoystickButton(XmlRpc::XmlRpcValue &params)
void joyCallback(const sensor_msgs::Joy::ConstPtr &joy_msg) override
void joyCallback(const sensor_msgs::Joy::ConstPtr &joy_msg) override
boost::shared_ptr< JoystickHandler > ConstPtr
boost::shared_ptr< JoystickInputHandle > ConstPtr
boost::shared_ptr< JoystickHandler > Ptr
#define ROS_ERROR_THROTTLE(rate,...)
JoystickButton(int button, double thresh=1.0)
JoystickAxis(int axis, double thresh=1.0, double min_val=-1.0, double max_val=1.0, double zero_val=0.0, double calib_offset=0.0, bool invert=false)
#define ROS_INFO(...)
boost::shared_ptr< JoystickInputHandle > Ptr
JoystickAxis(XmlRpc::XmlRpcValue &params)


vigir_pattern_generator
Author(s): Alexander Stumpf
autogenerated on Sun Nov 17 2019 03:30:06