## Publisher and Subscriber The Publisher and Subscriber communication model is a common variant in message queue implementations. The [ROS documentation](http://wiki.ros.org/Topics) provides a more detailed description, so please refer to the wiki or documentation page to get a deeper insight. In short, the ROS Publisher publishes data on a topic that a ROS Subscriber can subscribe to. Since every topic has a specific *type*, the publisher can only publish data of that exact type and it will fail to do so otherwise. The subscriber must behave likewise and only subscribe to a *topic* that fits it's datatype. Thus, to connect Publisher and Subscriber they need to have the same type, namespace, and topic while they are connected to the same roscore. The `General usage of ROS` section introduced the naming scheme inside VEROSIM. It will help you to identify all the Subscribers and Publishers that are available in VEROSIM. For completeness, the following image shows all the Publisher/Subscriber extensions that you can add to your model. ![](ros_pubsub.png "") We will now introduce a practical example that you can refer to if you need information on how to access ROS Topics as a Publisher or Subscriber. We will pick up on the prior example of the robot that should be mirroring the motion and state of a physical robot. ### Subscribing to the JointState topic As you can see, you should add a `VSLibROS::SubSensorMsgsJointState` extension to your model. That will allow you to subscribe to a topic and receive data from the environment. ![](ros_robotmotion1.png "") The second step to go is assigning the topic name you want to subscribe to. Select the previously added `VSLibROS::SubSensorMsgsJointState` and switch to the `Properties Widget`. You will find the `ROS Topic` property that you assign the value `/ur10_1/joint_states`. That in general is the topic name for the joint position of the first robot. ![](ros_robotmotion2.png "") The next step, is to assign a kinematic the received joint values should be applied to. Expand the `VSLibROS::SubSensorMsgsJointState` extension and open the context menu on the kinematicRoot property by right clicking on it. ![](ros_robotmotion3.png "") A dialog will open that you can use to select a kinematic root. This model only contains one robot, so select the `UR10 mapping root` and click `OK`. ![](ros_robotmotion4.png "") Afterwards the kinematic root will be referenced by the property and configuration of the Subscriber extension is finished. The next step is to enable the extension and run the simulation to start receiving data. ![](ros_robotmotion5.png "") To enable the Subscriber you need to switch to the IO Editor. Navigate to the menu band and click `Modeling -> IO editor`. You will find the Subscriber's IO Board there that contains a single input `enabled` and a number of outputs that represent the current joint state. You could use the outputs for different applications, but in this case it is sufficient that you referenced the kinematic root. The `enabled` input will switch the Subscriber on or off. To enable it, right click on the input and click `Toggle input`. ![](ros_robotmotion6.png "") If a Subscriber is enabled, it will start receiving data with the simulation start. Click on the respective action in the toolbar and you will see the simulated robot mirror the physical robots' state. ![](ros_robotmotion7.png "") ### Publishing to a JointState topic Using Publishers is very similar to using the Subscribers. We will use the same robot model that we used earlier to reverse the roles. This time the simulated robot will publish its joint state so that another ROS node can subscribe to a topic and receive the state of the simulation. Firstly, you need to add the `VSLibROS::PubSensorMsgsJointState` extension to the model. ![](ros_robotmotion8.png "") In a second step, you modify the property ROS Topic and set an appropriate name that you will also need to use for a potential Subscriber. ![](ros_robotmotion9.png "") Then you have two choices. You can set the kinematicRoot reference like you already did in the previous section. ![](ros_robotmotion10.png "") Alternatively, you can connect the `Joint values` output of the kinematic root with the `position` input of the Publisher in the IO Editor. ![](ros_robotmotion11.png "") In any case you need to enable the Publisher by toggling the input and run the simulation afterwards. ![](ros_robotmotion7.png "")