/
robot_4wd_key.cpp
122 lines (101 loc) · 2.99 KB
/
robot_4wd_key.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
116
117
118
119
120
121
//
// ROS node for robot 4wd keyboard teleoperation
//
//
// robocraft.ru
//
#include "ros/ros.h"
#include "geometry_msgs/Twist.h" // cmd_vel
#include "orcp2/console.h"
#define KEYCODE_ESC 0x1B
#define KEYCODE_SPACE 0x20
#define KEYCODE_R 0x43
#define KEYCODE_L 0x44
#define KEYCODE_U 0x41
#define KEYCODE_D 0x42
#define KEYCODE_Q 0x71
double linear=0, angular=0;
ros::Time first_publish;
ros::Time last_publish;
double l_scale=1, a_scale=1;
ros::Publisher vel_pub;
void publish(double angular_, double linear_)
{
geometry_msgs::Twist vel;
vel.angular.z = a_scale*angular_;
vel.linear.x = l_scale*linear_;
vel_pub.publish(vel);
}
int main(int argc, char **argv)
{
ros::init(argc, argv, "robot_4wd_teleop");
ros::NodeHandle n;
vel_pub = n.advertise<geometry_msgs::Twist>("cmd_vel", 1);
ROS_INFO("Start");
ROS_INFO("Load params");
ros::NodeHandle ph("~");
ph.param("scale_angular", a_scale, a_scale);
ph.param("scale_linear", l_scale, l_scale);
ROS_INFO("scale_angular: %0.2f", a_scale);
ROS_INFO("scale_linear: %0.2f", l_scale);
#if defined(LINUX)
// Use termios to turn off line buffering
termios term, cooked;
tcgetattr(STDIN_FILENO, &term);
memcpy(&cooked, &term, sizeof(term));
term.c_lflag &= ~(ICANON | ECHO);
term.c_cc[VEOL] = 1;
term.c_cc[VEOF] = 2;
tcsetattr(STDIN_FILENO, TCSANOW, &term);
setbuf(stdin, NULL);
#endif
printf("[i] Reading from keyboard\n");
printf("[i] ---------------------------\n");
printf("[i] Use WASD keys to move the robot, SPACE for stop and ESC for quit.\n");
while (ros::ok()) {
int key = console::waitKey(30);
if(key != 0 ) {
ROS_DEBUG( "[i] Key: %c (0x%X)\n", key, key);
}
if(key == KEYCODE_ESC) { //ESC
ROS_DEBUG("Exit");
linear = angular = 0;
publish(angular, linear);
break;
}
else if(key == KEYCODE_SPACE) { // SPACE
linear = angular = 0;
ROS_DEBUG("[i] stop\n");
publish(angular, linear);
}
else if(key == 'w' || key == 'W') {
linear = 1.0;
angular = 0;
ROS_DEBUG("[i] forward %.2f %.2f", linear, angular);
publish(angular, linear);
}
else if(key == 's' || key == 'S') {
linear = -1.0;
angular = 0;
ROS_DEBUG("[i] backward %.2f %.2f", linear, angular);
publish(angular, linear);
}
else if(key == 'a' || key == 'A') {
linear = 0;
angular = 1.0;
ROS_DEBUG("[i] left %.2f %.2f", linear, angular);
publish(angular, linear);
}
else if(key == 'd' || key == 'D') {
linear = 0;
angular = -1.0;
ROS_DEBUG("[i] right %.2f %.2f", linear, angular);
publish(angular, linear);
}
}
ROS_INFO("End");
#if defined(LINUX)
tcsetattr(STDIN_FILENO, TCSANOW, &cooked);
#endif
return 0;
}