Regularly, it is necessary to subscribe different topic messages and publish one or more messages in one ROS node. Therefore,
we need to design a node which owns several publishers and/or subscribers. Sometimes we will even use multi-thread methods in C++.
Python
The following codes shows how to subscribe a topic message and publish another topic message in one node. This method utilizes reponsion mechanism, it will publish a message after it receive a topic message and finish dealing with it.
if __name__ == '__main__': try: main() except rospy.ROSInterruptException: pass
If we have a while circulaiton and would like to do somthing periodically in it, meanwhile we have to subscribe different types of messages. We can use the following codes.
if __name__ == "__main__": try: main() except rospy.ROSInterruptException: pass
C++
In C++, I think it is a good habit to use a class to represent a Ros node. Various publishers and subscribers are differnt class members in it.
In its constuction funciton, all these publishers and subscribers will be initialized. The following codes show the whole procedure of work flow.
(Similar to the first example above.)