-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathteleop.cpp
More file actions
125 lines (123 loc) · 2.69 KB
/
teleop.cpp
File metadata and controls
125 lines (123 loc) · 2.69 KB
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
122
123
124
125
#include <ros/ros.h>
#include <geometry_msgs/Twist.h>
#include <signal.h>
#include <termios.h>
#include <stdio.h>
#define KEYCODE_R 0x43
#define KEYCODE_L 0x44
#define KEYCODE_U 0x41
#define KEYCODE_D 0x42
#define KEYCODE_Q 0x71
#define KEYCODE_SPACE 0x20
class TeleopRosAria
{
public:
TeleopRosAria();
void keyLoop();
private:
ros::NodeHandle nh_;
double linear_, angular_, l_scale_, a_scale_;
ros::Publisher twist_pub_;
};
TeleopRosAria::TeleopRosAria():
linear_(0),
angular_(0),
l_scale_(2.0),
a_scale_(2.0)
{
nh_.param("scale_angular", a_scale_, a_scale_);
nh_.param("scale_linear", l_scale_, l_scale_);
twist_pub_ = nh_.advertise<geometry_msgs::Twist>("RosAria/cmd_vel", 1);
}
int kfd = 0;
struct termios cooked, raw;
void quit(int sig)
{
tcsetattr(kfd, TCSANOW, &cooked);
ros::shutdown();
exit(0);
}
int main(int argc, char** argv)
{
ros::init(argc, argv, "teleop_RosAria");
TeleopRosAria teleop_RosAria;
signal(SIGINT,quit);
teleop_RosAria.keyLoop();
return(0);
}
void TeleopRosAria::keyLoop()
{
char c;
bool dirty=false;
// get the console in raw mode
tcgetattr(kfd, &cooked);
memcpy(&raw, &cooked, sizeof(struct termios));
raw.c_lflag &=~ (ICANON | ECHO);
// Setting a new line, then end of file
raw.c_cc[VEOL] = 1;
raw.c_cc[VEOF] = 2;
tcsetattr(kfd, TCSANOW, &raw);
puts("Reading from keyboard");
puts("---------------------------");
puts("Use arrow keys to move the robot.");
puts("Press the space bar to stop the robot.");
puts("Press q to stop the program");
for(;;)
{
// get the next event from the keyboard
if(read(kfd, &c, 1) < 0)
{
perror("read():");
exit(-1);
}
linear_=angular_=0;
ROS_DEBUG("value: 0x%02X\n", c);
switch(c)
{
case KEYCODE_L:
ROS_DEBUG("LEFT");
angular_ = 0.1;
linear_ = 0;
dirty = true;
break;
case KEYCODE_R:
ROS_DEBUG("RIGHT");
angular_ = -0.1;
linear_ = 0;
dirty = true;
break;
case KEYCODE_U:
ROS_DEBUG("UP");
linear_ = 0.1;
angular_ = 0;
dirty = true;
break;
case KEYCODE_D:
ROS_DEBUG("DOWN");
linear_ = -0.1;
angular_ = 0;
dirty = true;
break;
case KEYCODE_SPACE:
ROS_DEBUG("STOP");
linear_ = 0;
angular_ = 0;
dirty = true;
break;
case KEYCODE_Q:
ROS_DEBUG("QUIT");
ROS_INFO_STREAM("You quit the teleop successfully");
return;
break;
}
geometry_msgs::Twist twist;
twist.angular.z = a_scale_*angular_;
twist.linear.x = l_scale_*linear_;
if(dirty ==true)
{
twist_pub_.publish(twist);
dirty=false;
}
}
return;
}