ros
src
cob_teleop_keyboard.cpp
Go to the documentation of this file.
1
/*
2
* Copyright 2017 Fraunhofer Institute for Manufacturing Engineering and Automation (IPA)
3
*
4
* Licensed under the Apache License, Version 2.0 (the "License");
5
* you may not use this file except in compliance with the License.
6
* You may obtain a copy of the License at
7
*
8
* http://www.apache.org/licenses/LICENSE-2.0
9
10
* Unless required by applicable law or agreed to in writing, software
11
* distributed under the License is distributed on an "AS IS" BASIS,
12
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
* See the License for the specific language governing permissions and
14
* limitations under the License.
15
*/
16
17
18
#include <termios.h>
19
#include <signal.h>
20
#include <math.h>
21
#include <stdio.h>
22
#include <stdlib.h>
23
24
#include <
ros/ros.h
>
25
#include <geometry_msgs/Twist.h>
26
27
#define KEYCODE_A 0x61
28
#define KEYCODE_D 0x64
29
#define KEYCODE_S 0x73
30
#define KEYCODE_W 0x77
31
#define KEYCODE_Q 0x71
32
#define KEYCODE_E 0x65
33
34
#define KEYCODE_A_CAP 0x41
35
#define KEYCODE_D_CAP 0x44
36
#define KEYCODE_S_CAP 0x53
37
#define KEYCODE_W_CAP 0x57
38
#define KEYCODE_Q_CAP 0x51
39
#define KEYCODE_E_CAP 0x45
40
41
class
TeleopKeyboard
42
{
43
private
:
44
double
walk_vel
,
run_vel
,
yaw_rate
,
yaw_rate_run
;
45
geometry_msgs::Twist
cmd
;
46
47
ros::NodeHandle
n_
;
48
ros::Publisher
vel_pub_
;
49
50
public
:
51
void
init
()
52
{
53
cmd
.linear.x =
cmd
.linear.y =
cmd
.angular.z = 0;
54
55
vel_pub_
=
n_
.
advertise
<geometry_msgs::Twist>(
"cmd_vel"
, 1);
56
57
ros::NodeHandle
n_private(
"~"
);
58
n_private.param(
"walk_vel"
,
walk_vel
, 0.5);
59
n_private.param(
"run_vel"
,
run_vel
, 1.0);
60
n_private.param(
"yaw_rate"
,
yaw_rate
, 1.0);
61
n_private.param(
"yaw_run_rate"
,
yaw_rate_run
, 1.5);
62
63
}
64
65
TeleopKeyboard
()
66
{
67
walk_vel
= 0;
68
run_vel
= 0;
69
yaw_rate
= 0;
70
yaw_rate_run
= 0;
71
}
72
~TeleopKeyboard
() { }
73
void
keyboardLoop
();
74
75
};
76
77
int
kfd
= 0;
78
struct
termios cooked,
raw
;
79
80
void
quit
(
int
sig)
81
{
82
tcsetattr(
kfd
, TCSANOW, &cooked);
83
exit(0);
84
}
85
86
int
main
(
int
argc,
char
** argv)
87
{
88
ros::init
(argc, argv,
"teleop_keyboard"
);
89
90
TeleopKeyboard
tpk;
91
tpk.
init
();
92
93
signal(SIGINT,
quit
);
94
95
tpk.
keyboardLoop
();
96
97
return
(0);
98
}
99
100
void
TeleopKeyboard::keyboardLoop
()
101
{
102
char
c;
103
bool
dirty=
false
;
104
105
// get the console in raw mode
106
tcgetattr(
kfd
, &cooked);
107
memcpy(&
raw
, &cooked,
sizeof
(
struct
termios));
108
raw
.c_lflag &=~ (ICANON | ECHO);
109
// Setting a new line, then end of file
110
raw
.c_cc[VEOL] = 1;
111
raw
.c_cc[VEOF] = 2;
112
tcsetattr(
kfd
, TCSANOW, &
raw
);
113
114
puts(
"Reading from keyboard"
);
115
puts(
"---------------------------"
);
116
puts(
"Use 'WASD' to translate"
);
117
puts(
"Use 'QE' to yaw"
);
118
puts(
"Press 'Shift' to run"
);
119
120
121
for
(;;)
122
{
123
// get the next event from the keyboard
124
if
(read(
kfd
, &c, 1) < 0)
125
{
126
perror(
"read():"
);
127
exit(-1);
128
}
129
130
cmd
.linear.x =
cmd
.linear.y =
cmd
.angular.z = 0;
131
132
switch
(c)
133
{
134
// Walking
135
case
KEYCODE_W
:
136
cmd
.linear.x =
walk_vel
;
137
dirty =
true
;
138
break
;
139
case
KEYCODE_S
:
140
cmd
.linear.x = -
walk_vel
;
141
dirty =
true
;
142
break
;
143
case
KEYCODE_A
:
144
cmd
.linear.y =
walk_vel
;
145
dirty =
true
;
146
break
;
147
case
KEYCODE_D
:
148
cmd
.linear.y = -
walk_vel
;
149
dirty =
true
;
150
break
;
151
case
KEYCODE_Q
:
152
cmd
.angular.z =
yaw_rate
;
153
dirty =
true
;
154
break
;
155
case
KEYCODE_E
:
156
cmd
.angular.z = -
yaw_rate
;
157
dirty =
true
;
158
break
;
159
160
// Running
161
case
KEYCODE_W_CAP
:
162
cmd
.linear.x =
run_vel
;
163
dirty =
true
;
164
break
;
165
case
KEYCODE_S_CAP
:
166
cmd
.linear.x = -
run_vel
;
167
dirty =
true
;
168
break
;
169
case
KEYCODE_A_CAP
:
170
cmd
.linear.y =
run_vel
;
171
dirty =
true
;
172
break
;
173
case
KEYCODE_D_CAP
:
174
cmd
.linear.y = -
run_vel
;
175
dirty =
true
;
176
break
;
177
case
KEYCODE_Q_CAP
:
178
cmd
.angular.z =
yaw_rate_run
;
179
dirty =
true
;
180
break
;
181
case
KEYCODE_E_CAP
:
182
cmd
.angular.z = -
yaw_rate_run
;
183
dirty =
true
;
184
break
;
185
}
186
187
188
if
(dirty ==
true
)
189
{
190
vel_pub_
.
publish
(
cmd
);
191
}
192
193
194
}
195
}
TeleopKeyboard::~TeleopKeyboard
~TeleopKeyboard()
Definition:
cob_teleop_keyboard.cpp:72
ros::Publisher
TeleopKeyboard::walk_vel
double walk_vel
Definition:
cob_teleop_keyboard.cpp:44
ros::init
ROSCPP_DECL void init(const M_string &remappings, const std::string &name, uint32_t options=0)
KEYCODE_Q
#define KEYCODE_Q
Definition:
cob_teleop_keyboard.cpp:31
TeleopKeyboard::run_vel
double run_vel
Definition:
cob_teleop_keyboard.cpp:44
TeleopKeyboard::init
void init()
Definition:
cob_teleop_keyboard.cpp:51
KEYCODE_A_CAP
#define KEYCODE_A_CAP
Definition:
cob_teleop_keyboard.cpp:34
KEYCODE_A
#define KEYCODE_A
Definition:
cob_teleop_keyboard.cpp:27
KEYCODE_S_CAP
#define KEYCODE_S_CAP
Definition:
cob_teleop_keyboard.cpp:36
ros.h
KEYCODE_W_CAP
#define KEYCODE_W_CAP
Definition:
cob_teleop_keyboard.cpp:37
main
int main(int argc, char **argv)
Definition:
cob_teleop_keyboard.cpp:86
TeleopKeyboard::yaw_rate
double yaw_rate
Definition:
cob_teleop_keyboard.cpp:44
KEYCODE_W
#define KEYCODE_W
Definition:
cob_teleop_keyboard.cpp:30
ros::Publisher::publish
void publish(const boost::shared_ptr< M > &message) const
ros::NodeHandle::advertise
Publisher advertise(AdvertiseOptions &ops)
TeleopKeyboard::n_
ros::NodeHandle n_
Definition:
cob_teleop_keyboard.cpp:47
quit
void quit(int sig)
Definition:
cob_teleop_keyboard.cpp:80
KEYCODE_D_CAP
#define KEYCODE_D_CAP
Definition:
cob_teleop_keyboard.cpp:35
TeleopKeyboard::cmd
geometry_msgs::Twist cmd
Definition:
cob_teleop_keyboard.cpp:45
KEYCODE_Q_CAP
#define KEYCODE_Q_CAP
Definition:
cob_teleop_keyboard.cpp:38
KEYCODE_E_CAP
#define KEYCODE_E_CAP
Definition:
cob_teleop_keyboard.cpp:39
raw
struct termios cooked raw
Definition:
cob_teleop_keyboard.cpp:78
kfd
int kfd
Definition:
cob_teleop_keyboard.cpp:77
KEYCODE_S
#define KEYCODE_S
Definition:
cob_teleop_keyboard.cpp:29
TeleopKeyboard::yaw_rate_run
double yaw_rate_run
Definition:
cob_teleop_keyboard.cpp:44
TeleopKeyboard::vel_pub_
ros::Publisher vel_pub_
Definition:
cob_teleop_keyboard.cpp:48
KEYCODE_D
#define KEYCODE_D
Definition:
cob_teleop_keyboard.cpp:28
TeleopKeyboard::keyboardLoop
void keyboardLoop()
Definition:
cob_teleop_keyboard.cpp:100
TeleopKeyboard
Definition:
cob_teleop_keyboard.cpp:41
ros::NodeHandle
TeleopKeyboard::TeleopKeyboard
TeleopKeyboard()
Definition:
cob_teleop_keyboard.cpp:65
KEYCODE_E
#define KEYCODE_E
Definition:
cob_teleop_keyboard.cpp:32
cob_teleop
Author(s): Florian Weisshardt, Maximilian Sieber
autogenerated on Fri Aug 2 2024 09:45:55