Solution to Exercise 6.2 solution missing global

The proposed solution should mean:

def my_callback(request):
    global move_circle
    global my_pub
    rospy.loginfo("The Service move_bb8_in_circle has been called")
    move_circle.linear.x = 0.2
    move_circle.angular.z = 0.2
    my_pub.publish(move_circle)
    rospy.loginfo("Finished service move_bb8_in_circle")
    return EmptyResponse()

without the global reference, it doesn’t work.

1 Like

Hi, if you are referring to the file bb8_move_in_circle_service_server.py, then it works as it should. I’ve used it and the bb8 moves as expected:

2 Likes

@ruediger

It actually works, as the global keyword is not needed for those variables:

  1. There’s no need to use the global keyword to access a global variable (it’s required when you are modifying it). Regarding my_pub, we are merely accessing one of it’s methods.
  2. We also don’t need to use the global keyword to modify properties of a global object. In this case, move_circle. This is a special case for objects, vs “plain” variables, I understand that this is quite surprising and interesting!
2 Likes

Hi,
could u check why my code doesn’t work.
I think it’s basically the same, but the robot doesn’t move at all, and only the loginfo outseide the fcun is printed
thank u!

Hi,

You are showing in your screenshot that you have launched the service server, now you have to call that service from a different terminal in order for the robot to move.

1 Like

yes, i forgot to call it.
thank u so much!

1 Like