ROS Resources: Documentation | Support | Discussion Forum | Index | Service Status | ros @ Robotics Stack Exchange
Ask Your Question
0

NameError: global name 'lscaled_pub' is not defined

asked 2019-05-16 10:36:06 -0500

achraf gravatar image

updated 2019-05-16 12:13:10 -0500

gvdhoorn gravatar image

Hi, i'm new with ros, i'd to use the differential-drive package but i have some probleme with wheel_scaler.py node :

#!/usr/bin/env python

"""
   wheel_scaler
   scales the wheel readings (and inverts the sign)

     Copyright (C) 2012 Jon Stephan. 

    This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.
    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.
    You should have received a copy of the GNU General Public License
    along with this program.  If not, see <http://www.gnu.org/licenses/>.

"""

import rospy
import roslib

from std_msgs.msg import Int16

def scaleWheel():
    rospy.init_node("wheel_scaler")
    rospy.loginfo("wheel_scaler started")

    scale = rospy.get_param('distance_scale', 1)
    rospy.loginfo("wheel_scaler scale: %0.2f", scale)

    rospy.Subscriber("lwheel", Int16, lwheelCallback)
    rospy.Subscriber("rwheel", Int16, rwheelCallback)

    lscaled_pub = rospy.Publisher("lwheel_scaled", Int16, queue_size=10)
    rscaled_pub = rospy.Publisher("rwheel_scaled", Int16, queue_size=10) 

    ### testing sleep CPU usage
    r = rospy.Rate(50) 
    while not rospy.is_shutdown:
        r.sleep()

    rospy.spin()


def lwheelCallback(msg):
            lscaled_pub.publish( msg.data * -1 * scale)

def rwheelCallback(msg):
            rscaled_pub.publish( msg.data * -1 * scale)


if __name__ == '__main__':
    """main"""
    try:
        scaleWheel()
    except rospy.ROSInterruptException:
    pass

when i run it i got this error :

[ERROR] [1558020685.558100]: bad callback: <function rwheelCallback at 0x7f5b77526848>
Traceback (most recent call last):
  File "/opt/ros/kinetic/lib/python2.7/dist-packages/rospy/topics.py", line 750, in _invoke_callback
    cb(msg)
  File "/home/ubuntu/catkin_ws/src/odomtest/src/wheel_scaler.py", line 52, in rwheelCallback
    rscaled_pub.publish( msg.data * -1 * scale)
NameError: global name 'rscaled_pub' is not defined

[ERROR] [1558020685.559276]: bad callback: <function lwheelCallback at 0x7f5b775267d0>
Traceback (most recent call last):
  File "/opt/ros/kinetic/lib/python2.7/dist-packages/rospy/topics.py", line 750, in _invoke_callback
    cb(msg)
  File "/home/ubuntu/catkin_ws/src/odomtest/src/wheel_scaler.py", line 49, in lwheelCallback
    lscaled_pub.publish( msg.data * -1 * scale)
NameError: global name 'lscaled_pub' is not defined

thank you.

edit retag flag offensive close merge delete

1 Answer

Sort by ยป oldest newest most voted
1

answered 2019-05-16 12:16:49 -0500

gvdhoorn gravatar image

This is a Python problem, not a ROS one.

def scaleWheel():
    [..]

    lscaled_pub = rospy.Publisher("lwheel_scaled", Int16, queue_size=10)
    rscaled_pub = rospy.Publisher("rwheel_scaled", Int16, queue_size=10) 

    [..]

def lwheelCallback(msg):
    lscaled_pub.publish( msg.data * -1 * scale)

def rwheelCallback(msg):
    rscaled_pub.publish( msg.data * -1 * scale)

The cause here is the fact that lscaled_pub and rscaled_pub are variables local to scaleWheel.

You cannot refer to them in other methods as those variables are not defined in a scope that contains both scaleWheel and lscaled_pub and rscaled_pub.

edit flag offensive delete link more

Question Tools

1 follower

Stats

Asked: 2019-05-16 10:36:06 -0500

Seen: 1,289 times

Last updated: May 16 '19