I am trying to run the following example script 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)
and I am finding the following problems:
- select does not do what is supposed. After I run this script, it never enters the if portion (so
print(key)
is never executed). - 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)
What is failing here?