First of all you should make sure that you have gone through the official ROS tutorials, at least the beginner section, and that you have installed the Catkin tools. Then, we assume that you have the latest Ubuntu running, that ROS is installed, and that the workspace folders are set. Here also refer to the official documentation regarding the ROS installation.
The general ROS functionality in CoppeliaSim is supported via the ROS Interface (libsimExtROS.so). The Linux distribution should include that file already compiled in CoppeliaSim/compiledROSPlugins, but it first needs to be copied to CoppeliaSim/, otherwise it won't be loaded. You might however experience plugin load problems, depending on your system specificities: make sure to always inspect the terminal window of CoppeliaSim for details about plugin load operations. Plugins are loaded when CoppeliaSim is launched. The ROS plugin will only successfully load and initialize if roscore is running at that time (roscore is the ROS master). Also make sure to source the ROS environment prior to running CoppeliaSim.
If the plugin cannot be loaded, then you should recompile it by yourself. It is open source and can be modified as much as needed in order to support a specific feature or to extend its functionality. If specific messages/services/etc. need to be supported, make sure to edit files located in simExtROS/meta/, prior to recompilation. There are 2 packages:
Above packages should be copied to your catkin_ws/src folder. Make sure that ROS is aware of those packages, i.e. that you can switch to above package folders with:
In order to build the packages, navigate to the catkin_ws folder and type:
Note for Ubuntu 20.04: due to a bug in python-catkin-tools you need to use catkin_make instead of catkin build.
That's it! The packages should have been generated and compiled to a library. Copy the devel/lib/libsimExtROS.so file to the CoppeliaSim installation folder. The plugin is now ready to be used!
Now open a terminal and start the ROS master with:
Open another terminal, move to the CoppeliaSim installation folder and start CoppeliaSim. This is what you should have (or similar):
Upon succesful ROS Interface load, checking the available nodes gives this:
In an empty CoppeliaSim scene, select an object, then attach a non-threaded child script to it with [Menu bar --> Add --> Associated child script --> Non threaded]. Open the script editor for that script and replace the content with following:
Above script will publish the simulation time, and subscribe to it at the same time. It will also publish the transform of the object the script is attached to. You should be able to see the simulation time topic with:
To see the message content, you can type:
Now load the demo scene rosInterfaceTopicPublisherAndSubscriber.ttt, and run the simulation. The code in the child script attached to Vision_sensor will enable a publisher to stream the vision sensor's image, and also enable a subscriber to listen to that same stream. The subscriber applies the read data to the passive vision sensor, that is only used as a data container. So CoppeliaSim is streaming data, while listening to the same data! This is what is happening:
[Image publisher and image subscriber demo]
Try experimenting a little bit with the code. You can also visualize the image that CoppeliaSim streams with following command:
Had you been streaming simpler data, then you could also have visualized it with:
Now stop the simulation and load the demo scene controlTypeExamples/controlledViaRos.ttt, and run the simulation. The robot is simplistic, and also behaving in a simplistic way for simplification purposes. It is controlled via the ROS Interface:
[External client application controlling the robot via ROS]
The child script attached to the robot, and running in a non-threaded fashion, is in charge of following:
While simulation is running, copy and paste a few times the robot. Notice that every copy is directly operational and independent. This is one of the many strengths of CoppeliaSim.
Now stop the simulation and open a new scene, then drag following model into it: Models/tools/rosInterface helper tool.ttm. This model is constituted by a single customization script that offers following topic publishers and subscribers:
Have a look at the content of the customization script, that can be fully customized for various purposes. Try generating topic messages from the command line, for instance:
In order to display the current simulation time, you could type:
Finally, make sure to have a look at the remote API functionality in CoppeliaSim: it allows for remote function execution, fast data streaming back and forth, is quite simple to use, leightweight and cross-platform. The remote API functionality is available for 7 different languages and can be an interesting alternative to ROS in some cases.