본문으로 바로가기

블럭킹없이 키보드 입력 받는 코드..

#include <ros/ros.h>
#include <termios.h>
 
char getch()
{
    fd_set set;
    struct timeval timeout;
    int rv;
    char buff = 0;
    int len = 1;
    int filedesc = 0;
    FD_ZERO(&set);
    FD_SET(filedesc, &set);
     
    timeout.tv_sec = 0;
    timeout.tv_usec = 1000;
 
    rv = select(filedesc + 1, &set, NULL, NULL, &timeout);
 
    struct termios old = {0};
    if (tcgetattr(filedesc, &old) < 0)
        ROS_ERROR("tcsetattr()");
    old.c_lflag &= ~ICANON;
    old.c_lflag &= ~ECHO;
    old.c_cc[VMIN] = 1;
    old.c_cc[VTIME] = 0;
    if (tcsetattr(filedesc, TCSANOW, &old) < 0)
        ROS_ERROR("tcsetattr ICANON");
 
    if(rv == -1)
        ROS_ERROR("select");
    else if(rv == 0)
        ROS_INFO("no_key_pressed");
    else
        read(filedesc, &buff, len );
 
    old.c_lflag |= ICANON;
    old.c_lflag |= ECHO;
    if (tcsetattr(filedesc, TCSADRAIN, &old) < 0)
        ROS_ERROR ("tcsetattr ~ICANON");
    return (buff);
}
 
 
int main(int argc, char **argv)
{
    ros::init(argc, argv, "key_input_node");
    ros::NodeHandle n;
 
    ros::Rate loop_rate(10);
    while (ros::ok())
    {
        int c = 0;
        c=getch();
        ROS_INFO("%c", c);
 
        ros::spinOnce();
 
        loop_rate.sleep();
    }
 
    return 0;
}