Error with a code containing server and subscriber [closed]

asked 2020-01-12 07:48:16 -0600

hrushi19 gravatar image

updated 2020-01-12 07:49:44 -0600

I have a code which has a server and subscriber. When I run the code using rosrun, I get the following error: NameError: global name 'my_object' is not defined Kindly ignore the indentation here, it is correct in my original code. I would be grateful for any help or advice

#!/usr/bin/env python
import rospy
from geometry_msgs.msg import Twist
from std_srvs.srv import Trigger,TriggerResponse,TriggerRequest
from sensor_msgs.msg import LaserScan
class check_collision():
    def __init__(self,data):
         self.direction=''
         self.collide=False
    def callback(self,TriggerRequest):
         Response=TriggerResponse()
         if self.data.ranges[0]<0.3:
               rospy.WARN("You will collide on rightside")
               self.direction="RIGHT"
               self.collide=True
         elif self.data.ranges[719]<0.3:
               rospy.WARN("Turn right,quick")
               self.direction="LEFT"
               self.collide=True
         elif self.data.ranges[360]<0.3:
               rospy.WARN("Danger ahead")
               self.direction="AHEAD"
               self.collide=True
         Response.success=self.collide
         Response.message=self.direction
         return Response
if __name__=="__main__":
    rospy.init_node('check_collision')
    rospy.loginfo("here!")
    def my_fun(data):
        global my_object
        rospy.loginfo("my_fun called")
        my_object=check_collision(data)
    sub=rospy.Subscriber('/kobuki/laser/scan',LaserScan,my_fun)
    rospy.loginfo("subscriber created")
    my_service=rospy.Service('collision_checker',Trigger, my_object.callback())
    rospy.spin()
edit retag flag offensive reopen merge delete

Closed for the following reason duplicate question by gvdhoorn
close date 2020-01-12 08:04:31.611828

Comments

This seems like a duplicate of #q341523.

Please do not post duplicates unnecessarily.

If you have updates or new information, edit your question instead of posting a new one. You can use the edit button/link for it.

gvdhoorn gravatar imagegvdhoorn ( 2020-01-12 08:05:33 -0600 )edit