【发布时间】:2021-08-24 19:47:33
【问题描述】:
我正在使用以下代码读取传感器数据,对其进行处理并将一些反馈发布到不同的主题,/scans_freespace。
#!/usr/bin/env python
import rospy
from rospy.core import is_shutdown
from sensor_msgs.msg import LaserScan
from sweep_bot.msg import Space
from sweep_bot.msg import SpaceArray
class FreeSpace :
def __init__(self) :
rospy.loginfo('starting')
self.scans = ()
self.regions = {}
self.publisher = self.subscriber = ''
def callbackScans(self, data) :
self.scans = data.ranges
self.regions = {
'starboard_aft' : self.scans[0:135],
'starboard_abeam_aft' : self.scans[136:271],
'starboard_abeam_bow' : self.scans[272:407],
'starboard_bow' : self.scans[408:543],
'port_aft' : self.scans[949:1084],
'port_abeam_aft' : self.scans[814:949],
'port_abeam_bow' : self.scans[679:814],
'port_bow' : self.scans[544:679],
}
def publish(self) :
self.getSomeData('port_bow')
def getSomeData(self, region) :
rospy.loginfo("Checking region: %s", region)
rospy.loginfo("Checking scans: %s", self.scans)
return self.scans[region]
if __name__ == '__main__' :
rospy.init_node('space_identifier')
rate = rospy.Rate(10)
spacer = FreeSpace()
subscriber = rospy.Subscriber("scans", LaserScan, spacer.callbackScans)
publisher = rospy.Publisher("scans_freespace", SpaceArray, queue_size=10)
while not rospy.is_shutdown() :
spacer.publish()
rate.sleep()
正如代码所示,我从 publish() 函数中调用 getSomeData() 函数并传递一个字符串参数。 getSomeData() 按预期接收参数但从 callbackScan() 函数填充的全局变量为空。
脚本的输出如下:
[INFO] [1629830370.246796, 0.000000]: starting
[INFO] [1629830370.253719, 0.000000]: Checking region: port_bow
[INFO] [1629830370.256151, 0.000000]: Checking scans: ()
Traceback (most recent call last):
File "/home/sisko/catkin_ws/src/sweepbot/Sweeper/sweep_bot/src/freespace.py", line 49, in <module>
spacer.publish()
File "/home/sisko/catkin_ws/src/sweepbot/Sweeper/sweep_bot/src/freespace.py", line 31, in publish
self.getSomeData('port_bow')
File "/home/sisko/catkin_ws/src/sweepbot/Sweeper/sweep_bot/src/freespace.py", line 37, in getSomeData
return self.scans[region]
TypeError: tuple indices must be integers, not str
具有讽刺意味的是,在我尝试更新输出之前,代码确实有效。
我错过了什么?
【问题讨论】:
-
什么全局变量?
data.ranges显然是一个元组(一个空的),你正试图用字符串port_bow来索引它。是谁打电话给callbackScans或以什么论据并不是很明显。
标签: python-2.7 ros publisher subscriber