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 
42 {
43  private:
45  geometry_msgs::Twist cmd;
46 
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 
66  {
67  walk_vel = 0;
68  run_vel = 0;
69  yaw_rate = 0;
70  yaw_rate_run = 0;
71  }
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 
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  {
191  }
192 
193 
194  }
195 }
ros::NodeHandle n_
#define KEYCODE_A
#define KEYCODE_W_CAP
struct termios cooked raw
void publish(const boost::shared_ptr< M > &message) const
#define KEYCODE_W
ROSCPP_DECL void init(int &argc, char **argv, const std::string &name, uint32_t options=0)
#define KEYCODE_E_CAP
#define KEYCODE_S
#define KEYCODE_Q_CAP
#define KEYCODE_D_CAP
#define KEYCODE_Q
#define KEYCODE_S_CAP
Publisher advertise(const std::string &topic, uint32_t queue_size, bool latch=false)
#define KEYCODE_A_CAP
#define KEYCODE_D
geometry_msgs::Twist cmd
int main(int argc, char **argv)
void quit(int sig)
#define KEYCODE_E
ros::Publisher vel_pub_


cob_teleop
Author(s): Florian Weisshardt, Maximilian Sieber
autogenerated on Wed Apr 7 2021 03:03:13