In server code: error processing request: global name ‘sensor_msgs’ is not defined
For the following code, I am getting the following error:
ERROR: service [/collision_checker] responded with an error: error processing request: global name ‘sensor_msgs’ is not defined
It is a server code and when I run the server, I don’t get error but when I call the server,with no arguments(there is no request argument for that service message type): rosservice call /collision_checker “{}”
, I get the error. Any help,advice would be appreciated. I haven’t indented properly here, but I did indent it in my real code.
#!/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):
self.direction=''
self.collide=False
self.sub=rospy.Subscriber('/kobuki/laser/scan',sensor_msgs/LaserScan,callback)
def callback(self,data):
Response=TriggerResponse()
if data.ranges[0]<0.3:
rospy.WARN("You will collide on rightside")
self.direction="RIGHT"
self.collide=True
elif data.ranges[719]<0.3:
rospy.WARN("Turn right,quick")
self.direction="LEFT"
self.collide=True
elif 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(Request):
my_object=check_collision()
my_service=rospy.Service('collision_checker',Trigger,my_fun)
rospy.spin()
Edit: Thanks, it worked. Based on your suggestion I tried to change my code, now I am getting another error, could you please look into it.
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()
sensor_msgs/LaserScan
should probably besensor_msgs.msg.LaserScan
(although you've alreadyimport
ed it earlier, so the module name should not be needed).And creating
Subsciber
instances in service callbacks is not going to work most likely, and if it does, it won't work as you expect it to. You should really not do that.You appear to have some issues with Python scoping of variables.
my_object
simply doesn't exist when you register the service server, as it's created in the callback.In general your code is not how I would have structured things. It would perhaps help if you could get some more experience with asynchronus and callback based programming.
Finally, I would suggest, if you haven't already, to first find and complete some Python tutorials. That should make this all much less confusing, and by ignoring ROS for the moment, would allow you to focus on one thing at a time.
Thanks for the suggestion. I have created another code with different structure and it worked. But was just wondering why this particular code didn't work.