Variable namespaces in classes (Python3)
I am having difficulties using variable namespaces with python3 classes
Here's a snippet of code that I would think would work, but doesn't.
In the BasicNavigator class:
self.namespace = ''
self.localization_pose_sub = self.create_subscription(PoseWithCovarianceStamped,
self.namespace + '/amcl_pose',
self._amclPoseCallback,
amcl_pose_qos)
In main:
navigator = BasicNavigator()
navigator.namespace = "robot1"
This does not work, it does not recognize the "robot1" in the class.
Instead, if I remove the self.namespace
and just write the topic name as such: 'robot1/amcl_pose'
it works, but I need to be able to have a variable amount of robots...
What am I doing wrong?
Very likely the subscription is getting created before
self.namespace
is being set torobot1
. What if you made the constructor ofBasicNavigator
take in a namespace, so that you can do something likeself.namespace = namespace
?As the previous comment said, you need to rearrange when you're setting the namespace and when the subscription is created.