joystick_handler.cpp
Go to the documentation of this file.
2 
3 
4 
6 {
8  : has_joy_msg_(false)
9 {
10  XmlRpc::XmlRpcValue params;
11 
12  if (nh.getParam("joystick", params))
13  {
14  initJoystickInput(params, "x", x_axis_);
15  initJoystickInput(params, "y", y_axis_);
16  initJoystickInput(params, "yaw", yaw_axis_);
17  initJoystickInput(params, "enable", enable_);
18  }
19  else
20  ROS_ERROR("[JoystickHandler] Input parameters missing in config!");
21 
22  joy_sub_ = nh.subscribe("joy", 1,& JoystickHandler::joyCallback, this);
23 }
24 
26 {
27 }
28 
29 void JoystickHandler::joyCallback(const sensor_msgs::Joy::ConstPtr& joy)
30 {
31  if (x_axis_)
32  x_axis_->joyCallback(joy);
33 
34  if (y_axis_)
35  y_axis_->joyCallback(joy);
36 
37  if (yaw_axis_)
38  yaw_axis_->joyCallback(joy);
39 
40  if (enable_)
41  enable_->joyCallback(joy);
42 
43  has_joy_msg_ = true;
44 }
45 
46 void JoystickHandler::getJoystickCommand(geometry_msgs::Twist& twist, bool& enable) const
47 {
48  twist = geometry_msgs::Twist();
49 
50  if (!has_joy_msg_)
51  {
52  enable = false;
53  return;
54  }
55 
56  if (x_axis_ && x_axis_->isPressed())
57  twist.linear.x = x_axis_->getValue();
58 
59  if (y_axis_ && y_axis_->isPressed())
60  twist.linear.y = y_axis_->getValue();
61 
62  if (yaw_axis_ && yaw_axis_->isPressed())
63  twist.angular.z = yaw_axis_->getValue();
64 
65  if (enable_)
66  enable = enable_->isPressed();
67 }
68 
70 {
71  if (params.hasMember(name))
72  {
73  XmlRpc::XmlRpcValue& input_params = params[name];
74 
75  if (input_params.hasMember("button"))
76  handle.reset(new JoystickButton(input_params));
77  else if (input_params.hasMember("axis"))
78  handle.reset(new JoystickAxis(input_params));
79  else
80  ROS_ERROR("[JoystickHandler] Couldn't handle '%s' input type!", name.c_str());
81  }
82  else
83  ROS_ERROR("[JoystickHandler] '%s' input parameter missing in config!", name.c_str());
84 }
85 
86 void JoystickHandler::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
87 {
88  // determine acceleration
89  double acc = convertJoyAxisToAcc(elapsed_time_sec, joy_val, val, min_vel, max_vel) * sensivity;
90  acc = std::min(max_acc, std::max(min_acc, acc));
91 
92  // update value
93  val = std::min(max_vel, std::max(min_vel, val+acc));
94 }
95 
96 double JoystickHandler::convertJoyAxisToAcc(double elapsed_time_sec, double joy_val, double val, double min_vel, double max_vel) const
97 {
98  // using current val to determine joy_val scale
99  if (val >= 0.0)
100  return (joy_val*std::abs(max_vel) - val)*elapsed_time_sec;
101  else
102  return (joy_val*std::abs(min_vel) - val)*elapsed_time_sec;
103 }
104 }
Subscriber subscribe(const std::string &topic, uint32_t queue_size, void(T::*fp)(M), T *obj, const TransportHints &transport_hints=TransportHints())
void joyCallback(const sensor_msgs::Joy::ConstPtr &last_joy_msg)
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
void initJoystickInput(XmlRpc::XmlRpcValue &params, std::string name, JoystickInputHandle::Ptr &handle) const
void getJoystickCommand(geometry_msgs::Twist &twist, bool &enable) const
bool hasMember(const std::string &name) const
bool getParam(const std::string &key, std::string &s) const
double convertJoyAxisToAcc(double elapsed_time_sec, double joy_val, double val, double min_vel, double max_vel) const
#define ROS_ERROR(...)


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