uos_diffdrive_teleop.cpp
Go to the documentation of this file.
1 /*
2  *
3  * Copyright (C) 2015 University of Osnabrück, Germany
4  *
5  * This program is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU General Public License
7  * as published by the Free Software Foundation; either version 2
8  * of the License, or (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with this program; if not, write to the Free Software
17  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18  *
19  * uos_diffdrive_teleop.cpp
20  *
21  * Created on: 16.02.2015
22  * Author: Sebastian Pütz <spuetz@uos.de>
23  */
24 
25 #include <uos_diffdrive_teleop.h>
26 
28  ros::NodeHandle n_private("~");
29 
30  n_private.param("max_vel", max_vel, 1.0); // max velocity
31  n_private.param("max_rot_vel", max_rot_vel, 2.0); // max rot velocity
32 
33  // acc_ahead_* resp. acc_y.* describes the forwards/backwards part
34  // acc_rot_* resp. acc_x.* describes the rotational part
35 
36  // *_pos describes the positive acceleration
37 
38  // *_neg describes the negative acceleration
39  // and is used to decelerate, while not accelerating
40 
41  // *_stop describes the active deceleration and
42  // works as a break and it is used when pressing the opposite
43  // direction button
44 
45  //forward acceleration parameters
46  n_private.param("acc_ahead_pos", acc_y.pos, 0.4);
47  n_private.param("acc_ahead_neg", acc_y.neg, 0.8);
48  n_private.param("acc_ahead_stop", acc_y.stop, 2.0);
49 
50  //rotational acceleration parameters
51  n_private.param("acc_rot_pos", acc_x.pos, 1.4);
52  n_private.param("acc_rot_neg", acc_x.neg, 1.8);
53  n_private.param("acc_rot_stop", acc_x.stop, 2.6);
54 
55  //update rates
56  n_private.param("update_velocity_rate", update_velocity_rate, 0.01);
57  n_private.param("update_inputs_rate", update_inputs_rate, 0.05);
58 
59  vel_pub = n_.advertise<geometry_msgs::Twist>("cmd_vel", 1);
60 
63 
64  velo.x = 0;
65  velo.y = 0;
66 }
67 
69  ros::Duration delta = t_event.current_real - t_event.last_real;
70 
71  double left = in.left;
72  double forwards = in.forwards;
73 
74  left = std::min(1.0, left);
75  left = std::max(-1.0, left);
76  forwards = std::min(1.0, forwards);
77  forwards = std::max(-1.0, forwards);
78 
79  velo.y = adaptVelocity(delta.toSec(), velo.y, forwards, acc_y.stop, acc_y.neg, acc_y.pos);
80  velo.x = adaptVelocity(delta.toSec(), velo.x, left, acc_x.stop, acc_x.neg, acc_x.pos);
81 
82  // velocity limits by intensity
83  velo.dyn_limit_y = max_vel * forwards;
84  velo.dyn_limit_x = max_rot_vel * left;
85 
86  // observe the limits for the forward part
87  if((forwards < 0 && velo.y < velo.dyn_limit_y)
88  || (forwards > 0 && velo.y > velo.dyn_limit_y))
90 
91  // observe the limits for the rotational part
92  if((left < 0 && velo.x < velo.dyn_limit_x)
93  || (left > 0 && velo.x > velo.dyn_limit_x))
95 
96  // set command
97  vel_cmd.linear.x = velo.y;
98  vel_cmd.angular.z = velo.x;
99 
100  //publish command
101  if(std::abs(vel_cmd.linear.x) > EPSILON_VELO || std::abs(vel_cmd.angular.z) > EPSILON_VELO) {
103  }
104  // TODO apply velocity by service call
105 }
106 
108  if(!in.updated){
109  in.forwards = 0;
110  in.left = 0;
111  }
112  in.updated = false;
113 }
114 
116  double delta_time, // delta time
117  double velocity, // current velocity
118  double factor, // intensity factor
119  double acc_stop, // active deceleration
120  double acc_neg, // deceleartion, if no button is pressed
121  double acc_pos) // positive acceleration
122 {
123 
124  double new_velocity = velocity;
125 
126  if(factor != 0) {
127  if(factor > 0) {
128  // active decelerate
129  if(velocity < 0)
130  return (new_velocity += acc_stop * factor * delta_time) > -EPSILON_VELO ? 0 : new_velocity;
131  // accelerate
132  else
133  return new_velocity + acc_pos * factor * delta_time;
134  } else {
135  // active decelerate
136  if(velocity > 0)
137  return (new_velocity += acc_stop * factor * delta_time) < EPSILON_VELO ? 0 : new_velocity;
138  // accelerate
139  else
140  return new_velocity + acc_pos * factor * delta_time;
141  }
142  }
143  else {
144  // nothing pressed -> decelerate
145  if(velocity > 0)
146  return (new_velocity += -acc_neg * delta_time) < EPSILON_VELO ? 0 : new_velocity;
147  else if(velocity < 0)
148  return (new_velocity += acc_neg * delta_time) > -EPSILON_VELO ? 0 : new_velocity;
149  return 0;
150  }
151 }
double adaptVelocity(double time_delta, double velocity, double factor, double acc_stop, double acc_neg, double acc_pos)
#define EPSILON_VELO
double update_velocity_rate
void publish(const boost::shared_ptr< M > &message) const
void updateInputs(const ros::TimerEvent &t_event)
ros::Timer vel_timer
ros::NodeHandle n_
bool param(const std::string &param_name, T &param_val, const T &default_val) const
Timer createTimer(Rate r, Handler h, Obj o, bool oneshot=false, bool autostart=true) const
ros::Publisher vel_pub
Publisher advertise(const std::string &topic, uint32_t queue_size, bool latch=false)
ros::Timer key_timer
acceleration acc_x
double update_inputs_rate
geometry_msgs::Twist vel_cmd
velocity velo
void updateVelocity(const ros::TimerEvent &t_event)
acceleration acc_y
double max_rot_vel


uos_diffdrive_teleop
Author(s): Jochen Sprickerhof, Sebastian Pütz
autogenerated on Mon Jun 10 2019 15:49:27