How to use PyCharm / Clion to debug on ROS
Abstract: First of all, this is a working setup tutorial. And that's all of it.
After years of dealing with ROS, I found the PyCharm or Clion from Jetbrain being my favourate IDE beside Sublime, and yes, I use Sublime most of the time.
What you will get from this
- An IDE and detects all function block codes so you can navigate your project easily.
- An debugger that let you set breakpoints in a python file. More specific, a python ROS node, such that you will be able to see the:
- online variable values
- function call orders
- etc.
How To Setup?
- Download Pycharm CE with any version.
- Install the PyCharm by:
- Unpack the PyCharm, I usually create a folder
~/App/
and dump the program there. - Open a console and cd into "{installation home}/bin" and type:
./pycharm.sh
- Unpack the PyCharm, I usually create a folder
- Create a Desktop Entry, this option is only shown in the program when you run the
./pucharm.sh
- Import bash environment to the PyCharm launcher by: edit the launcher file in either
/usr/share/applications
or~/.local/share/applications
(depending on whether or not you installed PyCharm for all users).- find this line
Exec="/usr/lib/pycharm-community/bin/pycharm.sh" %f
- replace it with
Exec=bash -i -c "/usr/lib/pycharm-community/bin/pycharm.sh" %f
, double check the thing, basically you are addingbash -i -c
after the=
.
- find this line
- Setup the ROS Python Interpreter, becareful, you need to setup the global(system) python 2.7 as the project interpreter, this is because the ROS uses it and all the message headers etc. are exported to this interpreter namespace. This PyCharm default one will not work also if you create a venv with python 2.7 will not work as well!
- It is very important that you launch the IDE from a terminal by doing:
$ pycharm-community
- Go to File->Settings->Project Interpreter -> Python Interpreters, and select the Python2.7
- It is very important that you launch the IDE from a terminal by doing:
- You are all set!
How to launch ?
You should be able to launch from the IDE shortcut icon, however I found it very confusing if you have a previous version's app icon also shown up in the launcher, so if you see two icons, try out them both and simply lock the one that works to your dashbar.
Alternatively, you can always launch the IDE from a terminal by doing:
$ pycharm-community
such that you has the bash env.
After that, you can do the regular making the run or debug config file on a python script or directly right click the file in project navigation to debug and script and setup break points.
Clion Version
Almost the same.
Set build paths to the catkin workspace
By default, CLion places build output in cmake-build-debug or cmake-build-release directory that is created automatically. For ROS development, it means that you will have two different builds in CLion and in the console where you run catkin_make.
To have a single build across the IDE and console, you need to set CLion build paths to the catkin workspace directories. For this, go to File | Settings (Ctrl+Alt+S) | Build, Execution, Deployment | CMake and change two fields:
In CMake options
-DCATKIN_DEVEL_PREFIX:PATH=/home/user/catkin_ws/devel
In Generation Path,
/home/user/catkin_ws/build
Register launch files as XML
Go to File | Settings (Ctrl+Alt+S) | Editor | File Types and select XML from the list of Recognized File Types.
Add *.launch
extension to the list of Registered Patterns.
To Make Clion work with ROS Python
You need to select the python interpreter as the system bin/2.7 one, if not existing, add a system type one to it.