Robotics StackExchange | Archived questions

Subscriber class data getting wiped

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 = ClassName(), it gets instantiated properly, my subscriber runs which calls my callback function called "searchmsg" 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 = textdata within my searchmsg 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.

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
            p.publish(t)
            rospy.sleep(0.5)
        else:
            t.angular.z = 0
            p.publish(t)
            self.text = msg_data
            # print(self.text)
            rospy.sleep(1)

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) 
    client.wait_for_server()

    for pose in waypoints: 
        goal = goal_pose(pose)
        client.send_goal(goal)
        client.wait_for_result()
        rospy.sleep(3)
        pls = Class_Name()
        print(pls.text)

        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]
            continue
        else:
            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 = ClassName().msgsearch() 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!

Asked by Zaqi on 2022-11-20 09:08:04 UTC

Comments

Answers