p2os_teleop.cc
Go to the documentation of this file.
1 /*
2  * teleop_base
3  * Copyright (c) 2008, Willow Garage, Inc.
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  *
9  * * Redistributions of source code must retain the above copyright
10  * notice, this list of conditions and the following disclaimer.
11  * * Redistributions in binary form must reproduce the above copyright
12  * notice, this list of conditions and the following disclaimer in the
13  * documentation and/or other materials provided with the distribution.
14  * * Neither the name of the <ORGANIZATION> nor the names of its
15  * contributors may be used to endorse or promote products derived from
16  * this software without specific prior written permission.
17  *
18  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
19  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
22  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
23  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
24  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
25  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
26  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
27  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
28  * POSSIBILITY OF SUCH DAMAGE.
29  */
30 
31 /* modifications to teleop_base to work with p2os
32  * Copyright (C) 2010 Hunter Allen [allen286@purdue.edu], David Feil-Seifer [dfseifer@usc.edu], Edward T. Kaszubski [kaszubsk@usc.edu]
33  *
34  * This program is free software; you can redistribute it and/or modify
35  * it under the terms of the GNU General Public License as published by
36  * the Free Software Foundation; either version 2 of the License, or
37  * (at your option) any later version.
38  *
39  * This program is distributed in the hope that it will be useful,
40  * but WITHOUT ANY WARRANTY; without even the implied warranty of
41  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
42  * GNU General Public License for more details.
43  *
44  * You should have received a copy of the GNU General Public License
45  * along with this program; if not, write to the Free Software
46  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
47  * MA 02110-1301, USA.
48  */
49 
50 
51 #include <cstdlib>
52 #include <unistd.h>
53 #include <math.h>
54 #include <ros/ros.h>
55 #include <sensor_msgs/Joy.h>
56 #include <geometry_msgs/Twist.h>
57 
58 class TeleopBase
59 {
60 public:
61  geometry_msgs::Twist cmd, passthrough_cmd;
62  double req_vx, req_vy, req_vw;
67  bool deadman_;
68  bool running_;
69 
72 
77 
78  TeleopBase(bool deadman_no_publish = false)
79  : max_vx(0.6),
80  max_vy(0.6),
81  max_vw(0.8),
82  max_vx_run(0.6),
83  max_vy_run(0.6),
84  max_vw_run(0.8),
85  deadman_no_publish_(deadman_no_publish),
86  running_(false)
87  {}
88 
89  void init()
90  {
91  cmd.linear.x = cmd.linear.y = cmd.angular.z = 0;
92  n_.param("max_vx", max_vx, max_vx);
93  n_.param("max_vy", max_vy, max_vy);
94  n_.param("max_vw", max_vw, max_vw);
95 
96  // Set max speed while running
97  n_.param("max_vx_run", max_vx_run, max_vx_run);
98  n_.param("max_vy_run", max_vy_run, max_vy_run);
99  n_.param("max_vw_run", max_vw_run, max_vw_run);
100 
101  n_.param("axis_vx", axis_vx, 3);
102  n_.param("axis_vw", axis_vw, 0);
103  n_.param("axis_vy", axis_vy, 2);
104 
105  n_.param("deadman_button", deadman_button, 0);
106  n_.param("run_button", run_button, 0);
107 
108  double joy_msg_timeout;
109  n_.param("joy_msg_timeout", joy_msg_timeout, -1.0); //default to no timeout
110  if (joy_msg_timeout <= 0)
111  {
112  joy_msg_timeout_ = ros::Duration().fromSec(9999999);//DURATION_MAX;
113  ROS_DEBUG("joy_msg_timeout <= 0 -> no timeout");
114  }
115  else
116  {
117  joy_msg_timeout_.fromSec(joy_msg_timeout);
118  ROS_DEBUG("joy_msg_timeout: %.3f", joy_msg_timeout_.toSec());
119  }
120 
121  ROS_DEBUG("max_vx: %.3f m/s\n", max_vx);
122  ROS_DEBUG("max_vy: %.3f m/s\n", max_vy);
123  ROS_DEBUG("max_vw: %.3f deg/s\n", max_vw*180.0/M_PI);
124 
125  ROS_DEBUG("max_vx_run: %.3f m/s\n", max_vx_run);
126  ROS_DEBUG("max_vy_run: %.3f m/s\n", max_vy_run);
127  ROS_DEBUG("max_vw_run: %.3f deg/s\n", max_vw_run*180.0/M_PI);
128 
129  ROS_DEBUG("axis_vx: %d\n", axis_vx);
130  ROS_DEBUG("axis_vy: %d\n", axis_vy);
131  ROS_DEBUG("axis_vw: %d\n", axis_vw);
132 
133 
134  ROS_INFO("deadman_button: %d", deadman_button);
135  ROS_INFO("run_button: %d", run_button);
136  ROS_DEBUG("joy_msg_timeout: %f\n", joy_msg_timeout);
137 
138  vel_pub_ = n_.advertise<geometry_msgs::Twist>("cmd_vel", 1);
139  passthrough_sub_ = n_.subscribe( "des_vel", 10, &TeleopBase::passthrough_cb, this );
140  joy_sub_ = n_.subscribe("joy", 10, &TeleopBase::joy_cb, this);
141 
142 
143  }
144 
146 
147  void passthrough_cb( const geometry_msgs::TwistConstPtr& pass_msg )
148  {
149  ROS_DEBUG( "passthrough_cmd: [%f,%f]", passthrough_cmd.linear.x, passthrough_cmd.angular.z );
150  passthrough_cmd = *pass_msg;
151  }
152 
153  void joy_cb(const sensor_msgs::Joy::ConstPtr& joy_msg)
154  {
155 
156  deadman_ = (((unsigned int)deadman_button < joy_msg->buttons.size()) && joy_msg->buttons[deadman_button]);
157 
158  if (!deadman_)
159  return;
160 
161  //Record this message reciept
162  last_recieved_joy_message_time_ = ros::Time::now();
163 
164  // Base
165  running_ = (((unsigned int)run_button < joy_msg->buttons.size()) && joy_msg->buttons[run_button]);
166  double vx = running_ ? max_vx_run : max_vx;
167  double vy = running_ ? max_vy_run : max_vy;
168  double vw = running_ ? max_vw_run : max_vw;
169 
170  if((axis_vx >= 0) && (((unsigned int)axis_vx) < joy_msg->axes.size()))
171  req_vx = joy_msg->axes[axis_vx] * vx;
172  else
173  req_vx = 0.0;
174  if((axis_vy >= 0) && (((unsigned int)axis_vy) < joy_msg->axes.size()))
175  req_vy = joy_msg->axes[axis_vy] * vy;
176  else
177  req_vy = 0.0;
178  if((axis_vw >= 0) && (((unsigned int)axis_vw) < joy_msg->axes.size()))
179  req_vw = joy_msg->axes[axis_vw] * vw;
180  else
181  req_vw = 0.0;
182 
183  }
184 
186  {
187  if (deadman_ && last_recieved_joy_message_time_ + joy_msg_timeout_ > ros::Time::now() && running_) {
188  cmd.linear.x = req_vx;
189  cmd.linear.y = req_vy;
190  cmd.angular.z = req_vw;
191  vel_pub_.publish(cmd);
192 
193  fprintf(stdout,"teleop_base:: %f, %f, %f\n",cmd.linear.x,cmd.linear.y,cmd.angular.z);
194  } else {
195  cmd = passthrough_cmd;
196  if (!deadman_no_publish_)
197  vel_pub_.publish(cmd);//Only publish if deadman_no_publish is enabled
198  }
199  }
200 };
201 
202 int main(int argc, char ** argv)
203 {
204  ros::init(argc, argv, "teleop_base");
205  ros::NodeHandle nh;
206 
207  bool no_publish = false;
208  for(unsigned int x = 1; x < argc; ++x)
209  {
210  if(!strncmp(argv[x], "--deadman_no_publish", strlen("--deadman_no_publish")))
211  no_publish = true;
212  }
213 
214  //There's no reason for this to be 20 Hz pub speed...
215  //Reducing to 10Hz @allen286
216  ros::Rate pub_rate(10);
217 
218  TeleopBase teleop_base(no_publish);
219  teleop_base.init();
220 
221  while (ros::ok())
222  {
223  ros::spinOnce();
224  teleop_base.send_cmd_vel();
225  pub_rate.sleep();
226  }
227 
228  return 0;
229 }
void joy_cb(const sensor_msgs::Joy::ConstPtr &joy_msg)
Definition: p2os_teleop.cc:153
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())
double max_vw
Definition: p2os_teleop.cc:63
ros::Time last_recieved_joy_message_time_
Definition: p2os_teleop.cc:70
double max_vy
Definition: p2os_teleop.cc:63
ROSCPP_DECL void init(int &argc, char **argv, const std::string &name, uint32_t options=0)
void init()
Definition: p2os_teleop.cc:89
ros::Subscriber passthrough_sub_
Definition: p2os_teleop.cc:76
int run_button
Definition: p2os_teleop.cc:65
int main(int argc, char **argv)
Definition: p2os_teleop.cc:202
bool deadman_no_publish_
Definition: p2os_teleop.cc:66
bool deadman_
Definition: p2os_teleop.cc:67
int deadman_button
Definition: p2os_teleop.cc:65
ros::Duration joy_msg_timeout_
Definition: p2os_teleop.cc:71
double req_vw
Definition: p2os_teleop.cc:62
geometry_msgs::Twist passthrough_cmd
Definition: p2os_teleop.cc:61
Duration & fromSec(double t)
#define ROS_INFO(...)
bool param(const std::string &param_name, T &param_val, const T &default_val) const
ROSCPP_DECL bool ok()
geometry_msgs::Twist cmd
Definition: p2os_teleop.cc:61
double max_vy_run
Definition: p2os_teleop.cc:63
double max_vx_run
Definition: p2os_teleop.cc:63
TFSIMD_FORCE_INLINE const tfScalar & x() const
Publisher advertise(const std::string &topic, uint32_t queue_size, bool latch=false)
bool running_
Definition: p2os_teleop.cc:68
void send_cmd_vel()
Definition: p2os_teleop.cc:185
bool sleep()
ros::Publisher vel_pub_
Definition: p2os_teleop.cc:74
double req_vy
Definition: p2os_teleop.cc:62
double max_vx
Definition: p2os_teleop.cc:63
void passthrough_cb(const geometry_msgs::TwistConstPtr &pass_msg)
Definition: p2os_teleop.cc:147
double max_vw_run
Definition: p2os_teleop.cc:63
static Time now()
ros::NodeHandle n_
Definition: p2os_teleop.cc:73
ROSCPP_DECL void spinOnce()
ros::Subscriber joy_sub_
Definition: p2os_teleop.cc:75
double req_vx
Definition: p2os_teleop.cc:62
TeleopBase(bool deadman_no_publish=false)
Definition: p2os_teleop.cc:78
#define ROS_DEBUG(...)


p2os_teleop
Author(s): Hunter L. Allen , David Feil-Seifer , Brian Gerkey, Kasper Stoy, Richard Vaughan, Andrew Howard, Tucker Hermans, ActivMedia Robotics LLC, MobileRobots Inc.
autogenerated on Sat Jun 20 2020 03:29:45