ROS Resources: Documentation | Support | Discussion Forum | Index | Service Status | ros @ Robotics Stack Exchange |
1 | initial version |
As far as I know, there's no ROS (or Boost) built-in function for this.
You can look at the method used in the turtlesim teleop_turtle_key. This node is used in the Understanding Topics tutorial.
All the default built-in functions (cin >>
, getchar
, etc.) block until the user presses "enter". Which is probably not the behavior you want. So, you'll need to use some sort of OS-specific hack to read the keys as they are pressed.
See this thread for suggestions on how to implement a non-blocking getchar()
in linux. It boils down to modifying the terminal settings to disable input buffering. You'll want to make sure and restore the original settings when your program exits, or else the terminal will behave oddly. Basically, you need something like:
int getch()
{
static struct termios oldt, newt;
tcgetattr( STDIN_FILENO, &oldt); // save old settings
newt = oldt;
newt.c_lflag &= ~(ICANON); // disable buffering
tcsetattr( STDIN_FILENO, TCSANOW, &newt); // apply new settings
int c = getchar(); // read character (non-blocking)
tcsetattr( STDIN_FILENO, TCSANOW, &oldt); // restore old settings
return c;
}
Once you've got that function written, you can do something like:
while (ros::ok())
{
int c = getch(); // call your non-blocking input function
if (c == 'a')
send message 'A'
else if (c == 'b')
send message 'B'
<< do other processing >>
}