termios and select failng with ROS

asked 2021-03-19 21:29:04 -0500

Kansai gravatar image

I am truing to run the following example from the book "Programming Robots with ROS"

#!/usr/bin/env python
import rospy
import sys, select, tty, termios
from std_msgs.msg import String

if __name__=='__main__':
    key_pub = rospy.Publisher('keys',String,queue_size=1)
    rospy.init_node("keyboard_driver")
    rate= rospy.Rate(100)
    old_attr = termios.tcgetattr(sys.stdin)
    tty.setcbreak(sys.stdin.fileno())
    print "Publishing keystrokes. Press Ctrl-C to exit..."
    while not rospy.is_shutdown():
        if select.select([sys.stdin],[],[],0)[0] == [sys.stdin]:
            key= sys.stdin.read(1)
            print(key)
            key_pub.publish(key)
            #key_pub.publish(sys.stdin.read(1))
        rate.sleep()
    termios.tcsetattr(sys.stdin,termios.TCSADRAIN,old_attr)

However I am running with the following problems;

  1. select does not do what is supposed. After I run this script, it never enters the if portion (so print(key) is never executed).
  2. When I press Ctrl-C, the program finishes but it seems that termios.tcsetattr(sys.stdin,termios.TCSADRAIN,old_attr) is never executed because the terminal becomes unusable. (the keystroke capture is not restored to default)

I have tried to run the non-ROS program in here and it works without problems, so it seems the problem is when applied with ROS and rate.sleep

What am I doing wrong?

edit retag flag offensive close merge delete