Project

General

Profile

Statistics
| Branch: | Revision:

robobuggy / buggyvis / tutorials / teleop_turtle_key.cpp @ f75a88be

History | View | Annotate | Download (2.42 KB)

1
/**
2
 * Simple C++ Publisher;
3
 *   Publish keystrokes to the
4
 *
5
 */
6
#include <ros/ros.h>
7
#include <turtlesim/Velocity.h>
8
#include <signal.h>
9
#include <termios.h>
10
#include <stdio.h>
11

    
12
#define KEYCODE_R 0x43
13
#define KEYCODE_L 0x44
14
#define KEYCODE_U 0x41
15
#define KEYCODE_D 0x42
16
#define KEYCODE_Q 0x71
17

    
18
class TeleopTurtle
19
{
20
public:
21
   TeleopTurtle();
22
   void keyLoop();
23

    
24
private:
25

    
26

    
27
   ros::NodeHandle nh_;
28
   double linear_, angular_, l_scale_, a_scale_;
29
   ros::Publisher vel_pub_;
30

    
31
};
32

    
33
TeleopTurtle::TeleopTurtle():
34
   linear_(0),
35
   angular_(0),
36
   l_scale_(2.0),
37
   a_scale_(2.0)
38
{
39
  assert(false);
40
   nh_.param("scale_angular", a_scale_, a_scale_);
41
   nh_.param("scale_linear", l_scale_, l_scale_);
42

    
43
   vel_pub_ = nh_.advertise<turtlesim::Velocity>("turtle1/command_velocity", 1);
44
}
45

    
46
int kfd = 0;
47
struct termios cooked, raw;
48

    
49
void quit(int sig)
50
{
51
   tcsetattr(kfd, TCSANOW, &cooked);
52
   ros::shutdown();
53
   exit(0);
54
}
55

    
56

    
57
int main(int argc, char** argv)
58
{
59
   ros::init(argc, argv, "teleop_turtle");
60
   TeleopTurtle teleop_turtle;
61

    
62
   signal(SIGINT,quit);
63

    
64
   teleop_turtle.keyLoop();
65

    
66
   return(0);
67
}
68

    
69

    
70
void TeleopTurtle::keyLoop()
71
{
72
   char c;
73
   bool dirty=false;
74

    
75

    
76
   // get the console in raw mode
77
   tcgetattr(kfd, &cooked);
78
   memcpy(&raw, &cooked, sizeof(struct termios));
79
   raw.c_lflag &=~ (ICANON | ECHO);
80
   // Setting a new line, then end of file
81
   raw.c_cc[VEOL] = 1;
82
   raw.c_cc[VEOF] = 2;
83
   tcsetattr(kfd, TCSANOW, &raw);
84

    
85
   puts("Reading from keyboard");
86
   puts("---------------------------");
87
   puts("Use arrow keys to move the turtle.");
88

    
89

    
90
   for(;;) {
91
      // get the next event from the keyboard
92
      if(read(kfd, &c, 1) < 0) {
93
         perror("read():");
94
         exit(-1);
95
      }
96

    
97
      linear_=angular_=0;
98
      ROS_DEBUG("value: 0x%02X\n", c);
99

    
100
      switch(c) {
101
      case KEYCODE_L:
102
         ROS_DEBUG("LEFT");
103
         angular_ = 1.0;
104
         dirty = true;
105
         break;
106
      case KEYCODE_R:
107
         ROS_DEBUG("RIGHT");
108
         angular_ = -1.0;
109
         dirty = true;
110
         break;
111
      case KEYCODE_U:
112
         ROS_DEBUG("UP");
113
         linear_ = 1.0;
114
         dirty = true;
115
         break;
116
      case KEYCODE_D:
117
         ROS_DEBUG("DOWN");
118
         linear_ = -1.0;
119
         dirty = true;
120
         break;
121
      }
122

    
123

    
124
      turtlesim::Velocity vel;
125
      vel.angular = a_scale_*angular_;
126
      vel.linear = l_scale_*linear_;
127
      if(dirty ==true) {
128
         vel_pub_.publish(vel);
129
         dirty=false;
130
      }
131
   }
132

    
133

    
134
   return;
135
}
136

    
137