Subscriber class data getting wiped

asked 2022-11-20 08:08:04 -0600

Zaqi gravatar image

I'm working with a simulated robot in Gazebo that goes to a waypoint and then spins around until it sees a text message. I want to save this message to self.text so that I can access it outside of the class, namely in main as shown. What seems to happen is that when I call the class in main as pls = Class_Name(), it gets instantiated properly, my subscriber runs which calls my callback function called "search_msg" which in turn is supposed to change self.text if the robot sees a message.

The trouble is that it doesn't seem to keep the value when I try to access it in main. I have tried making print statements in various locations to see when it changes and when I try to print right after self.text = text_data within my search_msg function, it will properly display the message, but when I attempt to print in my for loop it returns as an empty string again. A note here is that the robot isn't constantly moving, so the message is registered constantly, which I have also confirmed by echoing the topic to see the message.

  • My system: Ubuntu 18.04 Bionic on a WSL on Windows 10
  • ROS: ROS1 Melodic
  • Python: 2.7.17

The below code will output an empty string on the first line and "Isn't working" from the else statement in the next. I have also tried putting the for loop into a while loop, but then it just repeats.

class Class_Name:
    def __init__(self):
        self.text = ''
        self.sub = rospy.Subscriber('/Topic_Name', String, self.search_msg)

    def search_msg(self, msg_data):
        p = rospy.Publisher('cmd_vel', Twist, queue_size=1000)
        r = rospy.Rate(2)
        t = Twist()

        if msg_data == '':
            t.angular.z = 0.5
            t.angular.z = 0
            self.text = msg_data
            # print(self.text)

waypoints = [  [(-3.95, 0.125, 0.0), (0.0, 0.0, -0.16547, -0.986213798314)] ]

if __name__ == '__main__':
    rospy.init_node('Node_Name', anonymous=True)
    client = actionlib.SimpleActionClient('move_base', MoveBaseAction) 

    for pose in waypoints: 
        goal = goal_pose(pose)
        pls = Class_Name()

        if pls.text != '':
            print(pls.text, "IF STATEMENT")
            Numb = pls.text[8]
            #I haven't defined the word list in this code, can disregard.
            word[Numb] = pls.text[-1]
            print("Isn't working")

I hope it's alright that I ask this too, but a general question I have about the subscriber is about whether or not I can call pls = Class_Name().msg_search() instead of instantiating the class only. My problem here is that I need to supply msg_search() with the callback data and I don't think I can simply input the subscriber.

I have obviously searched for fixes but there are none, or I simply don't understand them. Please let me know if you need any information about my system or any clarifications. Any help is greatly appreciated!

edit retag flag offensive close merge delete