ROS Resources: Documentation | Support | Discussion Forum | Index | Service Status | ros @ Robotics Stack Exchange
Ask Your Question
2

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
4

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 'http://info2html.sourceforge.net/cgi-bin/info2html-demo/info2html?(libc.info.gz)Canonical%2520or%2520Not' for more info. Disabling echo means that characters are not printed when a key is pressed.

According to the documentation of termios 'http://info2html.sourceforge.net/cgi-bin/info2html-demo/info2html?(libc.info.gz)Mode%2520Data%2520Types', c_cc is an array for specifying which characters are associated with various control functions. The documentation 'http://info2html.sourceforge.net/cgi-bin/info2html-demo/info2html?(libc.info.gz)Editing%2520Characters' 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 answers.ros.org seems to have problems with URLs that contain closing parenthesis.

edit flag offensive delete link more

Comments

I used this: http://en.wikibooks.org/wiki/Serial_Programming/termios and this: http://linux.die.net/man/3/termios when I was learning how the teleop code worked

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

Question Tools

Stats

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

Seen: 863 times

Last updated: Aug 20 '12