Ask Your Question

Help understanding a piece of TurtlebotTeleop code

asked 2012-08-20 08:24:28 -0600

Sardonyx gravatar image

updated 2014-01-28 17:13:24 -0600

ngrennan gravatar image

Hi all! I'm trying to modify a teleop program called turtlebot_key.cpp that came with the turtlebot_apps stack. There's this piece of code that I don't understand at all, and I've tried looking it up, and still can't make sense of it. I'm pretty sure this is where my problem is with my code right now, because i need a certain case where the terminal's handling of input is different from how the program normally works. If someone could explain this to me simply and clearly, that would be fantastic. The code is this:

int kfd;
struct termios cooked, raw;

and later:

// 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);
edit retag flag offensive close merge delete

1 Answer

Sort by ยป oldest newest most voted

answered 2012-08-20 10:26:10 -0600

Lorenz gravatar image

updated 2012-08-20 10:28:12 -0600

The purpose of the code is to directly get keys without requiring an EOL after each key sequence.

First the properties of the current terminal are read. Then the flags ICANON and ECHO are removed. Canonical terminals are line oriented, non-canonical terminals allow for getting single key presses. See '' for more info. Disabling echo means that characters are not printed when a key is pressed.

According to the documentation of termios '', c_cc is an array for specifying which characters are associated with various control functions. The documentation '' explicitly states that VEOL and VEOF are only used in canonical mode, so I guess setting c_cc as above doesn't really have any effect.

The last line just sets the new terminal settings.

Sorry for the bad links but seems to have problems with URLs that contain closing parenthesis.

edit flag offensive delete link more


I used this: and this: when I was learning how the teleop code worked

weiin gravatar image weiin  ( 2012-08-20 15:00:58 -0600 )edit

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account.

Add Answer

Question Tools


Asked: 2012-08-20 08:24:28 -0600

Seen: 683 times

Last updated: Aug 20 '12