0

I have a custom ros node, that subscribes data from robot-joints. in order to access the joint data, I have a None variable to which the joint positions are being assigned during function callback. Now, the problem is , i can only access that variable when the subscriber is running, once its stopped, the variable is being reset to None. If the ros node keeps running, my program can not skip to the next line unless this loop is stopped. So i am either struck with this loop where I get all this data printed through loginfo or if i remove the line rospy.spin(), in the next lines where I am using that position stored data, its returning None as the node is stopped. Can anyone please suggest me a work around for this ?

   def callback(data):
       Position = data.position

I am using this callback in the subscriber I want to access the position values of the robot when I call position, as I have assigned data.position to position. but its returning None, I believe its because, since position is None type, it only gets the data.position values assigned to it as long as the node is spinning or running, once its stopped it might be getting reset to None. but if i let it keep spinning using rospy.spin(), my program does not move forward to the next step as it gets stuck in the infinite loop of rospy.spin

Sai Raghava
  • 147
  • 1
  • 4
  • 13
  • 1
    To begin with, since you're using Python as the language, Python _does not_ "reset" variables. If the initial global value is None, and you always change it to a non-None value, it will never be None except when you access it before it is first changed. If you assigned to it in a local function without declaring it global, the global value will never have changed. – JWCS Aug 13 '19 at 18:15
  • The larger issue is that the _entire_ ros paradigm of using callbacks, sending and receiving data, requires the spin() function, and requires the infinite loop. The function that calls the infinite loop, your main function, should never compute data: it should only initialize data. The only code that runs after spin() is to destroy or close resources. – JWCS Aug 13 '19 at 18:18
  • 1
    If you want a better answer, post a relevant, short, demonstrative example of your code. Or see the [original code](https://stackoverflow.com/a/57277796/6069586) responding to your first question, this is how you would implement this style. – JWCS Aug 13 '19 at 18:21
  • Post some code examples so we could help you – W0RT4 Aug 19 '19 at 07:36
  • Thank you for offering help, as @JWCS has pointed in the related question, I totally had a wrong approach to developing a ros node in python, maybe its because i directly started developing it in python without understanding the nuances of it. I should have first worked with c++ to get a good hold. – Sai Raghava Aug 19 '19 at 18:56

0 Answers0