-
Notifications
You must be signed in to change notification settings - Fork 314
/
turtlebot_joy.cpp
115 lines (99 loc) · 3.61 KB
/
turtlebot_joy.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
/*
* Copyright (c) 2010, Willow Garage, Inc.
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* * Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* * Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* * Neither the name of the Willow Garage, Inc. nor the names of its
* contributors may be used to endorse or promote products derived from
* this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*/
#include <ros/ros.h>
#include <geometry_msgs/Twist.h>
#include <sensor_msgs/Joy.h>
#include "boost/thread/mutex.hpp"
#include "boost/thread/thread.hpp"
#include "ros/console.h"
class TurtlebotTeleop
{
public:
TurtlebotTeleop();
private:
void joyCallback(const sensor_msgs::Joy::ConstPtr& joy);
void publish();
ros::NodeHandle ph_, nh_;
int linear_, angular_, deadman_axis_;
double l_scale_, a_scale_;
ros::Publisher vel_pub_;
ros::Subscriber joy_sub_;
geometry_msgs::Twist last_published_;
boost::mutex publish_mutex_;
bool deadman_pressed_;
bool zero_twist_published_;
ros::Timer timer_;
};
TurtlebotTeleop::TurtlebotTeleop():
ph_("~"),
linear_(1),
angular_(0),
deadman_axis_(4),
l_scale_(0.3),
a_scale_(0.9)
{
ph_.param("axis_linear", linear_, linear_);
ph_.param("axis_angular", angular_, angular_);
ph_.param("axis_deadman", deadman_axis_, deadman_axis_);
ph_.param("scale_angular", a_scale_, a_scale_);
ph_.param("scale_linear", l_scale_, l_scale_);
deadman_pressed_ = false;
zero_twist_published_ = false;
vel_pub_ = ph_.advertise<geometry_msgs::Twist>("cmd_vel", 1, true);
joy_sub_ = nh_.subscribe<sensor_msgs::Joy>("joy", 10, &TurtlebotTeleop::joyCallback, this);
timer_ = nh_.createTimer(ros::Duration(0.1), boost::bind(&TurtlebotTeleop::publish, this));
}
void TurtlebotTeleop::joyCallback(const sensor_msgs::Joy::ConstPtr& joy)
{
geometry_msgs::Twist vel;
vel.angular.z = a_scale_*joy->axes[angular_];
vel.linear.x = l_scale_*joy->axes[linear_];
last_published_ = vel;
deadman_pressed_ = joy->buttons[deadman_axis_];
}
void TurtlebotTeleop::publish()
{
boost::mutex::scoped_lock lock(publish_mutex_);
if (deadman_pressed_)
{
vel_pub_.publish(last_published_);
zero_twist_published_=false;
}
else if(!deadman_pressed_ && !zero_twist_published_)
{
vel_pub_.publish(*new geometry_msgs::Twist());
zero_twist_published_=true;
}
}
int main(int argc, char** argv)
{
ros::init(argc, argv, "turtlebot_teleop");
TurtlebotTeleop turtlebot_teleop;
ros::spin();
}