ROS Resources: Documentation | Support | Discussion Forum | Index | Service Status | Q&A answers.ros.org
Ask Your Question
0

TypeError: las_callback() takes exactly 2 arguments (1 given)

asked 2020-12-12 21:58:27 -0600

zzdmw gravatar image

I try to make spatial median filtering that uses in sensor_msgs/LaserScan.msg. this is my python code:

#!/usr/bin/env python
"""
"""

import rospy

from sensor_msgs.msg import LaserScan

def las_callback(self, msg):

    data_list=list(msg.ranges)

    self.median_ranges=[]

    self.median_filter_size=3

    for i in range(0, len(data_list)):

        if (i>= self.median_filter_size and i<(len(data_list)-self.median_filter_size)):

            media_call=median(data_list[i-self.median_filter_size:i+self.median_filter_size+1])

            self.median_ranges.append(median_call)

    print(media_call)


rospy.init_node('laser_readings')
sub = rospy.Subscriber('/base_scan', LaserScan, las_callback)
rospy.spin()

when I try to rosrun this code, it shows me a worrying message is:

[ERROR] [1607831425.437657, 1678.699000]: bad callback: <function las_callback at 0x7fb50adbddd0>
Traceback (most recent call last):
  File "/opt/ros/melodic/lib/python2.7/dist-packages/rospy/topics.py", line 750, in _invoke_callback
    cb(msg)
TypeError: las_callback() takes exactly 2 arguments (1 given)

How can I solve this problem? thanks for your help!

edit retag flag offensive close merge delete

1 Answer

Sort by ยป oldest newest most voted
0

answered 2020-12-13 01:48:40 -0600

gvdhoorn gravatar image

updated 2020-12-13 02:24:20 -0600

This is a Python problem more than a ROS one.

def las_callback(self, msg):
    [..]

You've defined a method here (the first argument is self), but it is not part of any class.

Remove the self argument, so las_callback(..) becomes a function.

Alternatively: create a Python class and make las_callback(..) a method of that class. You'll have to take care to create all the member variables as well in that case, or the callback will not run successfully.

edit flag offensive delete link more

Comments

when I make"las_callback(self, msg):" become "def las_callback(msg):". It show a new error:

[ERROR] [1607846695.003072, 183.190000]: bad callback: <function las_callback at 0x7f8cda22ed50>
Traceback (most recent call last):
  File "/opt/ros/melodic/lib/python2.7/dist-packages/rospy/topics.py", line 750, in _invoke_callback
    cb(msg)
  File "/home/wg/catkin_ws/src/rob599_hw3/src/median_filter.py", line 13, in las_callback
    self.median_ranges=[]
NameError: global name 'self' is not defined
zzdmw gravatar image zzdmw  ( 2020-12-13 02:06:07 -0600 )edit

That's because you are still referencing self in the body of your callback.

This is still a Python problem: did you copy the callback code from somewhere?

If you haven't yet, you might want to lookup some Python tutorials to get some more experience with Python before taking on ROS. Learning all these new things at the same time can be quite a challenge.

gvdhoorn gravatar image gvdhoorn  ( 2020-12-13 02:23:24 -0600 )edit

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account.

Add Answer

Question Tools

1 follower

Stats

Asked: 2020-12-12 21:58:27 -0600

Seen: 929 times

Last updated: Dec 13 '20