ROS学习总结2-实时监听键盘输入(python)

在使用ROS开发移动机器人时,发现teleop_twist_keyboard这个包很好用,可以通过监听键盘输入来控制机器人的移动,具有很好的实时性。这个包用一个程序实现了'监听键盘输入'和'键盘输入转twist消息'两个部分的内容,实际使用中,可能第一部分的使用需求更大一些,因此根据github上的源码提取了其中'监听键盘输入'的部分。

#!/usr/bin/env python
import rospy
import sys, select, tty, termios
from std_msgs.msg import String
if __name__ == '__main__':
	rospy.init_node('keyboard')
	pub = rospy.Publisher('keys',String,queue_size = 1)
	rate = rospy.Rate(100)     #设置发布频率
	old_attr = termios.tcgetattr(sys.stdin)     #备份终端属性
	tty.setcbreak(sys.stdin.fileno())     #设置属性
	print('Please input keys, press Ctrl + C to quit')
	while not rospy.is_shutdown():
		if select.select([sys.stdin], [], [], 0)[0] == [sys.stdin]:     #设置超时参数为0,防止阻塞
			pub.publish(sys.stdin.read(1))
		rate.sleep()     #使用sleep()函数消耗剩余时间
	termios.tcsetattr(sys.stdin, termios.TCSADRAIN, old_attr)     #设置为原先的标准模式

这个程序用了一些没有学过的东西,本着探索学习的精神,下面对其中的一些模块进行分析。

termios模块中的函数采用文件描述符fd作为它们的第一个传入参数,可能是一个整数文件描述符,如sys.stdin.fileno(),或者是一个文件对象,如sys.stdin。这里我们主要用到两个函数:termios.tcgetattr()和termios.tcsetattr()。

termios.tcgetattr(fd)返回文件描述符的tty属性的列表。

termios.tcsetattr(fd,when,attributes)根据传入的attributes参数设置文件描述符的tty属性,attributes参数可以由tcgetattr()函数得到返回值,根据传入的when参数设置tty属性的生效时间:TCSANOW(立即生效),TCSADRAIN(传输完所有排队的输出后生效),TCSAFLUSH(传输完所有排队的输出并丢弃所有排队的输入)。

Set the tty attributes for file descriptor fd from the attributes, which is a list like the one returned by tcgetattr(). The when argument determines when the attributes are changed: TCSANOW to change immediately, TCSADRAIN to change after transmitting all queued output, or TCSAFLUSH to change after transmitting all queued output and discarding all queued input.

tty模块调用了tty.setcbreak(fd[,when])函数,这个函数将文件描述符的模式从fd变更为cbreak,when参数的缺省值为termios.TCSAFLUSH并传递给termios.tcsetattr()函数。调用cbreak函数后,除了"Del"和"Ctrl"键外,接受其他所有字符输入。

select.select(rlist,wlist,xlist[,timeout])函数的官方解释可以参考这个链接,使用可以参考这篇文章,这里我们不希望程序阻塞在标准输入上,因此设置超时参数为0,即进行无休止的轮询。在轮询的过程中,函数的返回值是rlist,wlist和xlist的子集,如果达到超时参数且没有可读的内容,将会返回三个空列表,此时if的条件不满足,将跳过if成立时执行的内容,进行下一次的轮询。如果存在可读的内容,则if条件满足,执行sys.stdin.read()函数。

采用rospy.Rate.sleep()消耗剩余的时间,防止程序占用过多的内存。

你可能感兴趣的:(python,ROS)