remote_joy.cpp
Go to the documentation of this file.
1 #include <ros/ros.h>
2 #include <sensor_msgs/Joy.h>
3 #include <std_srvs/Trigger.h>
4 #include <nav2d_operator/cmd.h>
6 
7 
9 {
10 public:
11  Teleoperator();
12 
13 private:
14  void joyCB(const sensor_msgs::Joy::ConstPtr& msg);
15 
23 
31 
33 };
34 
36 {
37  // parameters for Button and Axis
38  ros::NodeHandle remoteNode("~/");
39  remoteNode.param("axis_velocity", mAxisVelocity, 4);
40  remoteNode.param("axis_direction", mAxisDirection, 0);
41  remoteNode.param("drive_mode", mButtonDriveMode, 5);
42  remoteNode.param("pause_nav", mButtonPauseNavigator, 6);
43  remoteNode.param("start_exploration", mButtonStartExploration, 0);
44  remoteNode.param("button_getmap", mButtonGetMap, 3);
45  remoteNode.param("stop_button", mButtonStop, 1);
46 
47  mCommandPublisher = mNode.advertise<nav2d_operator::cmd>("cmd", 1);
48  mJoySubscriber = mNode.subscribe<sensor_msgs::Joy>("joy", 10, &Teleoperator::joyCB, this);
49  mStopClient = mNode.serviceClient<std_srvs::Trigger>(NAV_STOP_SERVICE);
50  mPauseClient = mNode.serviceClient<std_srvs::Trigger>(NAV_PAUSE_SERVICE);
53 
54  mButtonPressed = false;
55 }
56 
57 void Teleoperator::joyCB(const sensor_msgs::Joy::ConstPtr& msg)
58 {
59  // Ignore Button-Release events
60  if(mButtonPressed)
61  {
62  mButtonPressed = false;
63  }else
64  {
65  nav2d_operator::cmd cmd;
66  cmd.Turn = msg->axes[mAxisDirection] * -1.0;
67  cmd.Velocity = msg->axes[mAxisVelocity];
68  cmd.Mode = 0;
69  if(msg->buttons[mButtonDriveMode]) cmd.Mode = 1;
71  }
72 
73  if(msg->buttons[mButtonStop])
74  {
75  std_srvs::Trigger srv;
76  if(!mStopClient.call(srv))
77  {
78  ROS_ERROR("Failed to send STOP_COMMAND to Navigator.");
79  }
80  return;
81  }
82 
83  if(msg->buttons[mButtonPauseNavigator])
84  {
85  std_srvs::Trigger srv;
86  if(!mPauseClient.call(srv))
87  {
88  ROS_ERROR("Failed to send PAUSE_COMMAND to Navigator.");
89  }
90  return;
91  }
92 
93  if(msg->buttons[mButtonGetMap])
94  {
95  std_srvs::Trigger srv;
96  if(!mGetMapClient.call(srv))
97  {
98  ROS_ERROR("Failed to send GETMAP_COMMAND to GetMap-Client.");
99  }
100  mButtonPressed = true;
101  return;
102  }
103 
104  if(msg->buttons[mButtonStartExploration])
105  {
106  std_srvs::Trigger srv;
107  if(!mExploreClient.call(srv))
108  {
109  ROS_ERROR("Failed to send EXPLORE_COMMAND to Explore-Client.");
110  }
111  mButtonPressed = true;
112  return;
113  }
114 }
115 
116 int main(int argc, char** argv)
117 {
118  ros::init(argc, argv, "teleoperator");
119  Teleoperator tele_op;
120 
121  ros::spin();
122 }
Teleoperator::mButtonDriveMode
int mButtonDriveMode
Definition: remote_joy.cpp:26
Teleoperator::joyCB
void joyCB(const sensor_msgs::Joy::ConstPtr &msg)
Definition: remote_joy.cpp:57
Teleoperator::mPauseClient
ros::ServiceClient mPauseClient
Definition: remote_joy.cpp:20
ros::Publisher
ros::init
ROSCPP_DECL void init(const M_string &remappings, const std::string &name, uint32_t options=0)
ros.h
NAV_EXPLORE_SERVICE
#define NAV_EXPLORE_SERVICE
Teleoperator::mButtonGetMap
int mButtonGetMap
Definition: remote_joy.cpp:29
NAV_GETMAP_SERVICE
#define NAV_GETMAP_SERVICE
ros::NodeHandle::serviceClient
ServiceClient serviceClient(const std::string &service_name, bool persistent=false, const M_string &header_values=M_string())
Teleoperator::mButtonStartExploration
int mButtonStartExploration
Definition: remote_joy.cpp:28
ros::Publisher::publish
void publish(const boost::shared_ptr< M > &message) const
ros::NodeHandle::advertise
Publisher advertise(AdvertiseOptions &ops)
Teleoperator
Definition: remote_joy.cpp:8
Teleoperator::mButtonPressed
bool mButtonPressed
Definition: remote_joy.cpp:32
Teleoperator::mButtonPauseNavigator
int mButtonPauseNavigator
Definition: remote_joy.cpp:27
Teleoperator::mGetMapClient
ros::ServiceClient mGetMapClient
Definition: remote_joy.cpp:22
Teleoperator::mExploreClient
ros::ServiceClient mExploreClient
Definition: remote_joy.cpp:21
NAV_STOP_SERVICE
#define NAV_STOP_SERVICE
commands.h
NAV_PAUSE_SERVICE
#define NAV_PAUSE_SERVICE
Teleoperator::mJoySubscriber
ros::Subscriber mJoySubscriber
Definition: remote_joy.cpp:18
Teleoperator::mStopClient
ros::ServiceClient mStopClient
Definition: remote_joy.cpp:19
Teleoperator::Teleoperator
Teleoperator()
Definition: remote_joy.cpp:35
ros::ServiceClient
Teleoperator::mAxisVelocity
int mAxisVelocity
Definition: remote_joy.cpp:24
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())
Teleoperator::mAxisDirection
int mAxisDirection
Definition: remote_joy.cpp:25
ROS_ERROR
#define ROS_ERROR(...)
ros::ServiceClient::call
bool call(const MReq &req, MRes &resp, const std::string &service_md5sum)
ros::NodeHandle::param
T param(const std::string &param_name, const T &default_val) const
Teleoperator::mNode
ros::NodeHandle mNode
Definition: remote_joy.cpp:16
main
int main(int argc, char **argv)
Definition: remote_joy.cpp:116
ros::spin
ROSCPP_DECL void spin()
cmd
string cmd
Teleoperator::mCommandPublisher
ros::Publisher mCommandPublisher
Definition: remote_joy.cpp:17
Teleoperator::mButtonStop
int mButtonStop
Definition: remote_joy.cpp:30
ros::NodeHandle
ros::Subscriber


nav2d_remote
Author(s): Sebastian Kasperski
autogenerated on Wed Mar 2 2022 00:37:43