joystick_mux.cpp
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2015-2020, the neonavigation authors
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  *
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 copyright holder nor the names of its
14  * contributors may be used to endorse or promote products derived from
15  * this software without specific prior written permission.
16  *
17  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
18  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
21  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
22  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
23  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
24  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
25  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
26  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
27  * POSSIBILITY OF SUCH DAMAGE.
28  */
29 
30 #include <ros/ros.h>
31 #include <sensor_msgs/Joy.h>
32 
34 
36 
38 {
39 private:
46  double timeout_;
50  int selected_;
51 
52  void cbJoy(const sensor_msgs::Joy::Ptr msg)
53  {
54  if (static_cast<size_t>(interrupt_button_) >= msg->buttons.size())
55  {
56  ROS_ERROR(
57  "Out of range: number of buttons (%lu) must be greater than interrupt_button (%d).",
58  msg->buttons.size(), interrupt_button_);
59  return;
60  }
61 
63  if (msg->buttons[interrupt_button_])
64  {
65  selected_ = 1;
66  }
67  else
68  {
69  selected_ = 0;
70  }
71  };
73  {
74  if (selected_ == id)
75  {
76  if (!advertised_)
77  {
78  advertised_ = true;
81  {
82  ROS_ERROR(
83  "Use %s (%s%s) topic instead of %s (%s%s)",
84  nh_.resolveName("mux_output", false).c_str(),
86  "mux_output",
87  pnh_.resolveName("output", false).c_str(),
89  "output");
90  pub_topic_ = msg->advertise(pnh_, "output", 1, false);
91  }
92  else
93  {
94  pub_topic_ = msg->advertise(nh_, "mux_output", 1, false);
95  }
96  }
97  pub_topic_.publish(*msg);
98  }
99  };
100  void cbTimer(const ros::TimerEvent& e)
101  {
103  {
104  selected_ = 0;
105  }
106  }
107 
108 public:
110  : nh_("")
111  , pnh_("~")
112  {
114  sub_joy_ = nh_.subscribe("joy", 1, &JoystickMux::cbJoy, this);
115  sub_topics_[0] = neonavigation_common::compat::subscribe<topic_tools::ShapeShifter>(
116  nh_, "mux_input0",
117  pnh_, "input0", 1, boost::bind(&JoystickMux::cbTopic, this, _1, 0));
118  sub_topics_[1] = neonavigation_common::compat::subscribe<topic_tools::ShapeShifter>(
119  nh_, "mux_input1",
120  pnh_, "input1", 1, boost::bind(&JoystickMux::cbTopic, this, _1, 1));
121 
122  pnh_.param("interrupt_button", interrupt_button_, 5);
123  pnh_.param("timeout", timeout_, 0.5);
125 
127 
128  advertised_ = false;
129  selected_ = 0;
130  }
131 };
132 
133 int main(int argc, char* argv[])
134 {
135  ros::init(argc, argv, "joystick_mux");
136 
137  JoystickMux jy;
138  ros::spin();
139 
140  return 0;
141 }
neonavigation_common::compat::getCompat
int getCompat()
JoystickMux
Definition: joystick_mux.cpp:37
ros::Publisher
boost::shared_ptr
ros::init
ROSCPP_DECL void init(const M_string &remappings, const std::string &name, uint32_t options=0)
ros.h
JoystickMux::sub_joy_
ros::Subscriber sub_joy_
Definition: joystick_mux.cpp:43
JoystickMux::selected_
int selected_
Definition: joystick_mux.cpp:50
JoystickMux::cbTopic
void cbTopic(const boost::shared_ptr< topic_tools::ShapeShifter const > &msg, int id)
Definition: joystick_mux.cpp:72
JoystickMux::cbTimer
void cbTimer(const ros::TimerEvent &e)
Definition: joystick_mux.cpp:100
compatibility.h
JoystickMux::pnh_
ros::NodeHandle pnh_
Definition: joystick_mux.cpp:41
ros::Publisher::publish
void publish(const boost::shared_ptr< M > &message) const
main
int main(int argc, char *argv[])
Definition: joystick_mux.cpp:133
JoystickMux::interrupt_button_
int interrupt_button_
Definition: joystick_mux.cpp:47
ros::NodeHandle::resolveName
std::string resolveName(const std::string &name, bool remap=true) const
JoystickMux::JoystickMux
JoystickMux()
Definition: joystick_mux.cpp:109
JoystickMux::pub_topic_
ros::Publisher pub_topic_
Definition: joystick_mux.cpp:44
JoystickMux::sub_topics_
ros::Subscriber sub_topics_[2]
Definition: joystick_mux.cpp:42
neonavigation_common::compat::current_level
const int current_level
ros::NodeHandle::subscribe
Subscriber subscribe(const std::string &topic, uint32_t queue_size, const boost::function< void(C)> &callback, const VoidConstPtr &tracked_object=VoidConstPtr(), const TransportHints &transport_hints=TransportHints())
ros::TimerEvent
JoystickMux::last_joy_msg_
ros::Time last_joy_msg_
Definition: joystick_mux.cpp:48
neonavigation_common::compat::checkCompatMode
void checkCompatMode()
shape_shifter.h
ros::Time
ROS_ERROR
#define ROS_ERROR(...)
JoystickMux::nh_
ros::NodeHandle nh_
Definition: joystick_mux.cpp:40
ros::NodeHandle::param
T param(const std::string &param_name, const T &default_val) const
JoystickMux::cbJoy
void cbJoy(const sensor_msgs::Joy::Ptr msg)
Definition: joystick_mux.cpp:52
JoystickMux::timeout_
double timeout_
Definition: joystick_mux.cpp:46
ros::spin
ROSCPP_DECL void spin()
JoystickMux::advertised_
bool advertised_
Definition: joystick_mux.cpp:49
ros::NodeHandle::createTimer
Timer createTimer(Duration period, const TimerCallback &callback, bool oneshot=false, bool autostart=true) const
ros::Duration
ros::Timer
ros::NodeHandle
ros::Subscriber
neonavigation_common::compat::getSimplifiedNamespace
std::string getSimplifiedNamespace(ros::NodeHandle &nh)
ros::Time::now
static Time now()
JoystickMux::timer_
ros::Timer timer_
Definition: joystick_mux.cpp:45


joystick_interrupt
Author(s): Atsushi Watanabe
autogenerated on Wed Mar 19 2025 02:14:07