How can I pass a variable value from one python file to another on ROS ?

asked 2022-04-18 07:29:40 -0500

mgrallos gravatar image

Hi, I am trying to pass a global variable from one python file to another. The global variable acts as a stopper, it returns a boolean data type and is initialized as False. Inside my callback in my first file, I change the value of the global variable to True but when I try to import it to the other file, it returns only the initialized value which is False. Can someone help me with this one?

Here's my inside my file:

stop = False #global initialization

the function that calls the subscriber

def follow():
    ground_truth_state_subscriber = rospy.Subscriber('/ground_truth/state/', Odometry, get_rotation)
    r = rospy.Rate(10)
    rospy.spin()

the function that changes the value of the variable

def get_rotation( msg):

  #insert more codes 

    if (euclidean_distance(position.x, position.y, target_x, target_y) <= distance_tolerance):
        velocity_msg.linear.x = 0.0
        velocity_msg.angular.z = 0.0
        pub.publish(velocity_msg)
        print("already at target")
        rospy.sleep(5)
        print("moving to next target at target")
        counter = counter + 1
        if(counter == flag):
            global stop
            stop = True    #THIS IS WHERE IT IS CHANGED 
            # check_stop()
            print("Already at last waypoint")
            rospy.signal_shutdown("Already at last waypoint!")

  #insert more codes

And the function that throws the value of the global variable

def check_stop():
   global stop
   return stop

Now on my second file I just imported the first file like this

from file1 import check_stop

print(check_stop())

Is there a way we can pass the value of variables between python files within ROS. I tried this using only python and it works but when I already integrate it with some ROS. It doesn't seem to work. I can pass a value from one Python file to another.

edit retag flag offensive close merge delete

Comments

Not the solution but maybe a workaround but did you tried to publish that variable as topic and listen that topic from your other code? A topic for global variables that your system will listen could work what you are looking for maybe?

FurkanEdizkan gravatar image FurkanEdizkan  ( 2022-04-19 01:23:26 -0500 )edit

Hello, I already tried that and tried to publish the msg right before the rospy.signal_shutdown but the other file wasn't able to receive the msg but I used Bool for that msg, will try using String.

mgrallos gravatar image mgrallos  ( 2022-04-19 04:04:36 -0500 )edit

I don't know if that would solve it, could you try using ros service structure, since you wanted to see response of a message before shutdown, maybe with service structure when you request a response the system will wait for it before going for shutdown

FurkanEdizkan gravatar image FurkanEdizkan  ( 2022-04-19 04:47:11 -0500 )edit

Can I run multiple services in parallel? My structure right now is that I have 3 nodes running: one that follows a series of waypoints, one that avoids obstacles snd the other one is the one with flags that dictates which message to publish. And what I'm trying to do is to send a stopping criterion which came from the node that follows waypoints like once the node reach the last waypoint it shutdowns. And i want all the other two nodes to shutdown too. I thought it would work just by passing flags but it didn't. Huhu

mgrallos gravatar image mgrallos  ( 2022-04-19 05:47:52 -0500 )edit

This would be my implementation but free to change what ever your like. So you have 3 nodes, 1: follows way_points, 2:avoids obstacles 3:Publish flags. Node 1 is controls the system, 2 is just warning, idk about 3. So node 2 will start with node 1 but just publish a warning about obstacle, stop, move signal maybe.1 listens to 2, 2 will publish warning. Now we have 3, i dont understand what 3 does, but it seem 3 need to listen 1 and return a response. Make 3 a service, and call it from 1, depending on response of 3 which is maybe raised flag information. I think it should work.

or if you just want to kill a node use kill command python subprocess or any other way

  rosnode kill --all
FurkanEdizkan gravatar image FurkanEdizkan  ( 2022-04-21 01:48:26 -0500 )edit