Python回调参数问题

时间:2018-12-14 21:45:06

标签: python-2.7 ros

我是python的新手。我有以下代码:

#! /usr/bin/env python

import rospy
from sensor_msgs.msg import LaserScan
from geometry_msgs.msg import Twist

#defines a callback
def callback(msg):
    rospy.init_node('obstacle_avoidance')
    pub = rospy.Publisher('/cmd_vel', Twist, queue_size=10)
    rate = rospy.Rate(1)

    while not rospy.is_shutdown():
        print('==================================')

        print('CHECKING .....')
        print msg.ranges


if __name__ == '__main__':
    try:
        callback()
    except rospy.ROSInterruptException:
        pass

具有以下响应:

  

TypeError:callback()恰好接受1个参数(给定0个参数)

我理解错误的意思,但是msg变量不是我定义的变量,所以我不确定要传递什么。

1 个答案:

答案 0 :(得分:0)

您似乎在以无效的方式组合了2个教程。您致电callback(),但不提供参数,因为callback()需要参数。

然后在函数中使用msg.ranges。 (这是Laser消息的想法)。

您可能应该做这样的事情

#! /usr/bin/env python

import rospy
from sensor_msgs.msg import LaserScan
from geometry_msgs.msg import Twist

#defines a callback
def callback(msg):
    while not rospy.is_shutdown():
        print('==================================')
        print('CHECKING .....')
        print msg.ranges


if __name__ == '__main__':
    try:
        rospy.init_node('obstacle_avoidance')
        pub = rospy.Publisher('/cmd_vel', Twist, queue_size=10)
        rospy.Subscriber('/base_scan', LaserScan, callback)
    except rospy.ROSInterruptException:
        pass