0

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:

  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)

What is failing here?

KansaiRobot
  • 7,564
  • 11
  • 71
  • 150
  • Which operating system are you running? `select` only works that way on the Unix-like systems. – Tim Roberts Mar 20 '21 at 02:05
  • ubuntu 20 (linux) – KansaiRobot Mar 20 '21 at 02:06
  • I don't know `rospy`, but the code looks fundamentally correct. Are you running this from an xterm, from a console, or are you trying it from inside an IDE? – Tim Roberts Mar 20 '21 at 02:23
  • from a terminal. I run this https://stackoverflow.com/a/2409034/4451521 and it worked but with ROS rate.sleep something is failing... – KansaiRobot Mar 20 '21 at 02:24
  • 1
    I solved the problem. Apparently it was a ROS problem. There was a rosmaster running in the background, so I did `killall -9 rosmaster` and then run all from the beginning. Now it works – KansaiRobot Mar 20 '21 at 07:11

0 Answers0