Ask Your Question
0

the terminal response global 'name ' is not defined

asked 2020-07-16 20:33:24 -0500

bebeto gravatar image

updated 2020-07-16 23:29:10 -0500

jayess gravatar image

the terminal response global 'name ' is not defined

import rospy
import cv2
import numpy as np
from cv_bridge import CvBridge
from sensor_msgs.msg import LaserScan
from geometry_msgs.msg import Twist
from geometry_msgs.msg import Point
from std_msgs.msg import String
from sensor_msgs.msg import CompressedImage

def savoir(msg):
    global  obstacle

    if msg.ranges[320] >0.5:
        obstacle=False
    elif msg.ranges[320]<0.5:
        obstacle=True

def drive_callback(data):

    global vel ,msg
    ball_x  = data.x
    ball_y  = data.y
    width   = data.z

    # Create Twist() instance
    vel = Twist()


    if ball_x < 0 and ball_y < 0:
        vel.angular.z = 0
    else:
        # Determine center-x, normalized deviation from center
        mid_x   = int(width/2)
        delta_x = ball_x - mid_x
        norm_x  = delta_x/width

        if norm_x > 0.15:
            print ("delX: {:.3f}. Tournons a droite!".format(norm_x))
            vel.angular.z = -0.2 #tourner vers la droite avec une vitesse de 0.2 m/s
        elif norm_x < -0.15:
            print ("delX: {:.3f}. Tournons a gauche!".format(norm_x))
            vel.angular.z = 0.2 
        elif abs(norm_x<=0.15):
            print ("delX: {:.3f}. regardons devant!".format(norm_x))
            if obstacle==False:
                vel.linear.x = 0.3 
                print("en avant!")
            else:   
                vel.linear.x=0


    # publish vel on the publisher
    pub_vel.publish(vel)

if __name__ == '__main__':
    global vel, pub_vel

    rospy.init_node('drive_wheel', anonymous=True)


    img_sub = rospy.Subscriber("/ball_location",Point, drive_callback)
    pub_vel = rospy.Publisher('cmd_vel_mux/input/teleop', Twist, queue_size=5)
    infra_sub= rospy.Subscriber("/scan",LaserScan,savoir)
    rospy.spin()


#error
    if obstacle==False: NameError: global name 'obstacle' is not defined
edit retag flag offensive close merge delete

2 Answers

Sort by ยป oldest newest most voted
0

answered 2020-07-16 23:37:34 -0500

jayess gravatar image

Your drive_callback function is probably getting called before the obstacle variable is set. You should set it (and your other globals too) to a default value at the start of the program.

edit flag offensive delete link more
1

answered 2020-07-17 11:28:56 -0500

deepaktalwardt gravatar image

updated 2020-07-17 14:47:50 -0500

It is generally better to wrap these two functions into a Python class, and you can then set your global variables to that class' class variables. Then the order in which your callback/savior functions are run doesn't matter. Sample untested code:

class Driver:
    def __init__(self):
        self.img_sub = rospy.Subscriber("/ball_location",Point, self.drive_callback)
        self.pub_vel = rospy.Publisher('cmd_vel_mux/input/teleop', Twist, queue_size=5)
        self.infra_sub = rospy.Subscriber("/scan", LaserScan, self.savoir)

        self.obstacle = False
        self.vel = None

    def drive_callback(self, data):
        ball_x  = data.x
        ball_y  = data.y
        width   = data.z

        # Create Twist() instance
        self.vel = Twist()

        if ball_x < 0 and ball_y < 0:
            self.vel.angular.z = 0
        else:
            # Determine center-x, normalized deviation from center
            mid_x   = int(width/2)
            delta_x = ball_x - mid_x
            norm_x  = delta_x/width

            if norm_x > 0.15:
                print ("delX: {:.3f}. Tournons a droite!".format(norm_x))
                self.vel.angular.z = -0.2 #tourner vers la droite avec une vitesse de 0.2 m/s
            elif norm_x < -0.15:
                print ("delX: {:.3f}. Tournons a gauche!".format(norm_x))
                self.vel.angular.z = 0.2 
            elif abs(norm_x<=0.15):
                print ("delX: {:.3f}. regardons devant!".format(norm_x))
                if self.obstacle==False:
                    self.vel.linear.x = 0.3 
                    print("en avant!")
                else:   
                    self.vel.linear.x=0

        # publish vel on the publisher
        self.pub_vel.publish(vel)

    def savoir(self, msg):
        if msg.ranges[320] >0.5:
            self.obstacle=False
        elif msg.ranges[320]<0.5:
            self.obstacle=True

if __name__ == '__main__':
    driver_obj = Driver()
    rospy.spin()
edit flag offensive delete link more

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-07-16 20:33:24 -0500

Seen: 483 times

Last updated: Jul 17 '20