#################################################################
Create Ned's digital twin with Webots and ROS
#################################################################
.. image:: /images/digital-twin-webots/digital_twin_webots.gif
:alt: Create Ned's digital twin with Webots and ROS
:width: 100%
:align: center
.. note::
This tutorial is working from:
| The version v3.0.0 of the ned_ros_stack
| The version v3.0.0 of Niryo Studio
Introduction
^^^^^^^^^^^^^^^^^^^^^^^^^^^
- On July 2021, Ned was added on Webots. You can have more information on `Cyberbotics' website `_.
- By downloading Webots and running Ned's sample World, you will be able to control its joints and its gripper with your keyboard, and also run a demo. It is highly recommanded to follow :doc:`our previous tutorial about Webots simulation ` before reading this one.
What is Webots from Cyberbotics ?
+++++++++++++++++++++++++++++++++
"Webots is an open source and multi-platform desktop application used to simulate robots. It provides a complete development environment to model, program and simulate robots.
It has been designed for a professional use, and it is widely used in industry, education and research. Cyberbotics Ltd. maintains Webots as its main product continuously since 1998."
You can download Webots for free on their `website `_.
.. note::
To do this tutorial we used "Ubuntu 18.04" and "Webots R2021a". You will need it for this tutorial.
You can find a sample of Ned on `their website `_.
Goal of the tutorial
^^^^^^^^^^^^^^^^^^^^^^^^^^^
In the first part of the tutorial you will learn how to use the digital twin we created to control a real Ned with a simulated one on the robotics simulator Webots.
By controlling Ned on Webots, you will be able to send the same commands on the real robot, in realtime.
In the second part of the tutorial, you will learn from scratch **how to implement a ROS controller for Ned on Webots** using the `webots_ros `_ package
and **how to develop a digital twin between Webots and Ned**.
By finishing this tutorial, you will be able to modify the ROS package and do your own projects with Ned, Webots and ROS.
You can find the ROS package `on our github `_.
How to use the digital twin
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
Installing ROS and "webots_ros" Package
++++++++++++++++++++++++++++++++++++++++++
The first part of this tutorial consists in installing ROS and the `webots_ros `_ package on your computer. To do that you will have to open a new terminal
and write the following commands:
.. code-block:: bash
cd
sudo sh -c 'echo "deb http://packages.ros.org/ros/ubuntu $(lsb_release -sc) main" > /etc/apt/sources.list.d/ros-latest.list'
sudo apt-key adv --keyserver 'hkp://keyserver.ubuntu.com:80' --recv-key C1CF6E31E6BADE8868B172B4F42ED6FBAB17C654
sudo apt-get update
sudo apt-get install ros-melodic-desktop-full (if not already installed)
sudo apt-get install python-rosdep
sudo apt-get install python-rospkg
pip3 install rospkg==1.3.0
cd /etc/ros/rosdep//sources.list.d/
sudo rm 20-default.list
sudo rosdep init
rosdep update
sudo apt-get install ros-melodic-webots-ros
Ned is working with Ubuntu 18.04 and ROS melodic. Following these commands you will install ROS melodic (if it is not already done), update your rosdep
and install ros-webots dependencies on your computer.
.. note::
If you want more information about the installation of ROS and the `webots_ros `_ package, you can take a look at `Webots' documentation `_.
Setting the appropriate environment
++++++++++++++++++++++++++++++++++++++
Now that you have the ros-webots dependencies installed on your computer, you will have to set the appropriate environment.
One thing you can already do is to source the ROS environment properly. To do that, open a new terminal:
.. code-block:: bash
cd
source /opt/ros/melodic/setup.bash
Create your ROS workspace
---------------------------
Now, we need to create a new catkin workspace. It is on this workspace that we will put a new ROS package to communicate between Webots and Ned via ROS.
To create your catkin workspace, you just have to add the following lines:
.. code-block:: bash
mkdir -p ~/catkin_ned_webots/src
cd ~/catkin_ned_webots/
catkin_make
source devel/setup.bash
Your new catkin workspace is now ready!
Check if the ROS_PACKAGE_PATH environment is properly set up
-------------------------------------------------------------
Before creating a new webots ROS package, you have to make sure that the ROS_PACKAGE_PATH environment is properly set up.
To check that, on the same terminal you created the workspace, you can add this command line:
.. code-block:: bash
printenv | grep ROS
With this command you can see all the environment starting with "ROS". In our case we need to set the ROS_PACKAGE_PATH environment
so we can use the following command:
.. code-block:: bash
echo $ROS_PACKAGE_PATH
It should return "/home/youruser/catkin_ws/src:/opt/ros/melodic/share".
If not, use:
.. code-block:: bash
export ROS_PACKAGE_PATH=/home/youruser/catkin_ws/src:/opt/ros/melodic/share
Set the WEBOTS_HOME environment
--------------------------------
If you have installed Webots in your root repository, your WEBOTS_HOME path should be "/usr/local/webots" and you don't have to do anything else about it.
If your WEBOTS_HOME path is different, you need to write this command in the terminal:
.. code-block:: bash
export WEBOTS_HOME=/usr/local/webots
Fill your .bashrc
------------------
If you don't want to have to repeat all the exports and settings each time, you can fill the details in your .bashrc by using the following command
in the terminal:
.. code-block:: bash
nano ~/.bashrc
Now you can put all the exports at the end of the .bashrc and you won't need to do it again when you open another terminal.
Run a first node from Webots
++++++++++++++++++++++++++++++++++++++++++++
Now that your workspace is created and you have set the appropriate environment, **you are ready to launch your first webots node**.
Indeed, when you installed the ros-webots dependencies, you downloaded at the same time the webots nodes examples.
To launch it, go in your workspace, source and launch it. To do that, write the following commands:
.. code-block:: bash
cd catkin_ned_webots
source devel.setup.bash
roslaunch webots_ros e_puck_line.launch
Here, webots_ros is the package and e_puck_line.launch is the launcher file that calls the node e_puck_line.cpp.
Webots should now open and you should have an e_puck robot following a line like this screenshot:
.. image:: /images/digital-twin-webots/e_puck_webots.png
:alt: Webots e_puck robot following a line
:width: 1000px
:align: center
You can see the different topics of e_puck if you open a new terminal and run this command:
.. code-block:: bash
rostopic list
You should have this in your terminal:
.. image:: /images/digital-twin-webots/e_puck_topics.png
:alt: Ned's topics list
:width: 500px
:align: center
Put the ned_webots_package from Niryo in your workspace
++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
It is now time to add the niryo_robot_webots package made by Niryo in your package to be able to run the digital twin.
To do that, you will have to "git clone" the package from our Github, put it in your /src file of the workspace you created before and catkin_make again.
You can use the following commands:
.. code-block:: bash
cd catkin_ned_webots/src
git clone https://github.com/NiryoRobotics/ned_applications.git ned_applications
mv ned_applications/tutorials/ROS_Webots_Digital_Twin .
rm -rf ned_applications
cd ..
catkin_make
source devel/setup.bash
Connect via multi-machines to Ned and run the digital twin
++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Before being able to control Ned on Webots and have a digital twin on the real robot, there are still few steps to follow.
To run your project, you will have to connect to Ned from your computer via multi-machines (Ned's Raspberry Pi cannot launch Webots).
.. note::
You can follow our tutorial about :doc:`the connection via multi-machines from your computer to Ned <./setup_ned_ros_multi_machines>` if you need help for this setup.
To connect via multi-machines to Ned from your computer, you can open a first terminal, connect via ssh and write the next commands:
.. note::
If you need help for the SSH connection, please follow our tutorial: :doc:`./setup_connect_ned_ssh`.
.. code-block:: bash
ssh niryo@IP_OF_NED (password: robotics)
cd catkin_ws
sudo service niryo_robot_ros stop (wait a bit)
export ROS_IP=IP_OF_NED
export ROS_MASTER_URI=http://IP_OF_NED::11311
roslaunch niryo_robot_bringup niryo_ned_robot.launch
Then open a second terminal and write the following commands:
.. code-block:: bash
cd catkin_ned_webots
export ROS_IP=IP_OF_YOUR_COMPUTER
export ROS_MASTER_URI=http://IP_OF_NED::11311
.. warning::
It is important to connect Ned to Niryo Studio and launch a calibration. After that, click on the button "BACK TO HOME".
By doing that you make sure that both real Ned and simulated Ned have the same position at the beggining.
Launch the digital-twin
++++++++++++++++++++++++++++++++++++++++++++
**It's now time to launch the digital-twin.**
Select the second terminal (the one connected to your computer) and write the following commands to launch the launcher ned_webots.launch:
.. code-block:: bash
cd catkin_ned_webots
source devel/setup.bash
roslaunch niryo_robot_webots ned_webots.launch
By doing that, Webots will be launched with Ned's environment and it will be connected via ROS to the real robot.
**If you move the robot on Webots via your keyboard, the real robot should reproduce the exact same movement.**
How to create the digital twin step by step
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
If you want to go further and learn more about **how to implement your own digital twin between Webots and Ned**, we highly recommand you to follow this section of the tutorial.
.. note::
On our Github, you can find all the required files to directly launch the digital twin without creating the code
(just follow the :ref:`previous section of this tutorial