Python vs C++ – How to run Python and C++ code in ROS

how to run Python and C++ code in ROS

Written by Bayode Aderinola

17/07/2018

Python vs C++ – see the major difference between running code in the two major languages used by ROS. Let’s go!

Step 1: Create an account and Login to Robot Ignite Academy

On Robot Ignite Academy, you get access to the best online ROS courses and environment. No need to install and set up ROS locally – the only thing you need is a browser!

  • Create an account or log in here.
  • Launch the ROS Basics in 5 Days Python course.
  • You’ll now have access to the simulation screen with a Notebook, an Editor, four Shells, and a Simulation Window. We are only using the Editor and Shells for this demo.

Step 2: Create a ROS package and some Python and C++ code

2. 1  Create a package by typing the following command in the shell

cd ~/catkin_ws/src
catkin_create_pkg obiwan rospy roscpp

2.2  Then in the src folder under the obiwan package. Let’s put the two source file obiwancpp.cpp and obiwanpy.py.

#! /usr/bin/env python
import rospy

rospy.init_node("Obiwan")
rate = rospy.Rate(2)
while not rospy.is_shutdown():
    print "Help me Obi-Wan Kenobi, you're my only hope"
    rate.sleep()
#include <ros/ros.h>

int main(int argc, char** argv) {
  ros::init(argc,argv,"ObiWan");
  ros::NodeHandle nh;
  ros::Rate loop_rate(2);

  while(ros::ok())
  {
    ROS_INFO("Help me Obi-Wan Kenobi, you'are my only hope");
  }

  return 0;
}

2.3  Type the following command to compile the package

cd ~/catkin_ws
catkin_make
source devel/setup.bash

2.4  After compile, let’s try to run the python script first

rosrun obiwan obiwanpy.py

You should see the output. The script executed without any problem.

2.5  Now try to run the C++ file in the same way. I bet it didn’t run! It turns out, to use the C++ file, you have to uncomment the following part in CMakeLists.txt.

add_executable(obiwancpp src/obiwan.cpp)
add_dependencies(obiwancpp ${${PROJECT_NAME}_EXPORTED_TARGETS} ${catkin_EXPORTED_TARGETS})

target_link_libraries(obiwancpp
  ${catkin_LIBRARIES}
)

Then compile and run again. You’ll see the output now.

Step 3: Master the Concept – Python vs C++ in ROS

The main difference between using C++ and Python in ROS is that you have to write down comments every time to include all the source files in CMakeLists.txt to compile and generate the executable.

QED 🙂

Extras: Video

Here is a sights and sounds version of the post, in case you prefer to see the action that way!

Further learning

If you are a ROS beginner and would like to learn more about programming ROS, please check one of the courses below, depending on which programming language you’re more familiar with:

Feedback

Did you like this post? Please leave a comment on the comments section below, so we can interact and learn from each other. Thank you!

Masterclass 2023 batch2 blog banner

Check Out These Related Posts

129. ros2ai

129. ros2ai

I would like to dedicate this episode to all the ROS Developers who believe that ChatGPT or...

read more

6 Comments

  1. Roberto

    It is a nice infographic, but in nr.2 you show c++ code where it says python, and the same the other way arround! Also the code formatting is terrible (non existent)!
    Otherwise as always great content The Construct!

    Reply
    • Ricardo Tellez

      Completely agree with your comments! Let us correct those errors in the next one 😉

      Reply
  2. Nagendra Prasad

    how to run a normal python code which doesn’t consist of any ros related things and only pure python(which can be executed in google colab)

    Reply
    • Anonymous

      use python .py

      Reply
      • Anonymous

        use python abc.py

        Reply
  3. Akita

    I think there might be a typo in the CMake include portion above. it’s currently:

    add_executable(obiwancpp src/obiwan.cpp)

    I think instead, it should read:
    add_executable(obiwancpp src/obiwancpp.cpp)

    This built successfully without errors.

    Reply

Submit a Comment

Your email address will not be published.

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Pin It on Pinterest

Share This