How to debug ROS2 Code with an IDE?

asked 2018-08-30 03:45:15 -0600

jborja1 gravatar image

updated 2018-08-30 05:04:34 -0600

Hi everyone,

I am having some trouble trying to debug a ROS2 Project with Visual Studio 2017.I was working before with Qt but then I converted the project to VS, so you will find some Qt Code. I am working on Windows 10 with ROS2 Bouncy installed according to the steps of building from source. My code compiles and works fine, without any errors, but I would like to debug it properly.

Here is the code:

Class Publisher

  #include "ros2publisher.h"

  #include <QDebug>

  ROS2Publisher::ROS2Publisher(rclcpp::Node::SharedPtr ros2node, QString topic)
  {
      createPublisher(ros2node, topic);
  }

  void ROS2Publisher::createPublisher(rclcpp::Node::SharedPtr ros2node, QString topic)
  {
      if(ros2Publisher)
          ros2Publisher.reset();

   ros2Publisher = ros2node->create_publisher<std_msgs::msg::Float64>(topic.toStdString(), rmw_qos_profile_default);
   ros2Publisher_status = ros2node->create_publisher<std_msgs::msg::Bool>("status_rw_" + topic.toStdString(), 
   rmw_qos_profile_default);
  }

  void ROS2Publisher::publishMessage(double vec)
  {
      std_msgs::msg::Float64 msg;

      msg.data = vec;
      qDebug()<<"-------------------------- \nPublishing Message: "<<vec;
      ros2Publisher->publish(msg);
  }


   void ROS2Publisher::publishStatus(bool b)
  {
      std_msgs::msg::Bool msg;
      msg.data = b;

      qDebug() << "Publish Status: " << b;
      ros2Publisher_status->publish(msg);
   }

Class Subscriber

 #include "ros2subscriber.h"
 #include <QDebug>

 ROS2Subscriber::ROS2Subscriber(rclcpp::Node::SharedPtr ros2node, QString topic)
 {
     createSubscriber(ros2node, topic);
 }

 void ROS2Subscriber::createSubscriber(rclcpp::Node::SharedPtr ros2node, QString topic)
{
   if(ros2Subscriber)
      ros2Subscriber.reset();

ros2Subscriber = ros2node->create_subscription<std_msgs::msg::Float64>(topic.toStdString(), 
std::bind(&ROS2Subscriber::msgCallback, this, std::placeholders::_1));

 qDebug() << QString::fromStdString(topic.toStdString() + " SetPoint");
 }

  void ROS2Subscriber::msgCallback(const std_msgs::msg::Float64::SharedPtr msg)
 {
     double data = msg->data;
     data++;
     qDebug()<< "Read data: "<<data;
     emit newROS2Msg(data);
 }

"Main"

void MainWindow::on_publishButton_clicked()
{
rclcpp::init(0, 0);

bool ok;
value = (ui->setPoint_lineEdit->text()).toDouble(&ok);

QString topic = ui->orientation_label->text();

ros2NodeP = rclcpp::Node::make_shared("StandAlonePub");
ros2NodeS = rclcpp::Node::make_shared("StandAloneSub");



  if (rclcpp::ok())
  {
        qDebug() << "ROS2 started successfully!";
        ui->status_lineEdit->setText("ROS2 is running...");

            ros2Publisher = new ROS2Publisher(ros2NodeP, topic);
    qDebug() << "ROS2 Publisher created successfully!";

            ros2Subscriber = new ROS2Subscriber(ros2NodeS, topic);
    qDebug() << "ROS2 Subscriber created successfully!";

     }
 else
    ui->status_lineEdit->setText("ROS2 NOT running");

         connect(ros2Subscriber, SIGNAL(newROS2Msg(double)), this, SLOT(readValue(double)));
         exec = rclcpp::executors::SingleThreadedExecutor::make_shared();
         exec->add_node(ros2NodeP);
         exec->add_node(ros2NodeS);
     int i=0;

     for (i=0;i<50;i++) 
     {
             ros2Publisher->publishMessage(value);
             Sleeper::msleep(10); //In order to wait some time that messages arrive
     exec->spin_once();
      }

rclcpp::shutdown();
}

Doing some tests, I could identify that the debugger "crashes" or doesn't work properly when it reaches the line code "ros2NodeP = rclcpp::Node::make_shared("StandAlonePub");" with the following error at this Macro definition "RCLCPP_SMART_PTR_DEFINITIONS(Node)" in the node.hpp file.

  Unhandled exception at 0x00007FFBE3BE50D8 in TestNode.exe: Microsoft C ++ exception: 
  rclcpp::exceptions::InvalidNodeNameError at location 0x0000007A069F9030.

My question is this one: How can a ROS2 Project/Code be correctly debugged with an IDE like Visual Studio or QtCreator? Maybe I've been doing it the wrong way. Are there some other steps required to do so?

I would really appreciate your help.

Note: The rclcpp repository is correctly linked. An easier way to test that the debugger doesn't work is just writing a simple code with the line that causes problem. "rclcpp::Node::make_shared("StandAlonePub");"

edit retag flag offensive close merge delete

Comments

Just something to check: in order to successfully debug on Windows, you'll have to build everything with the Debug build type. Did you do that?

Edit: and with "everything" I mean: every ROS2 library that gets linked against your node/program.

gvdhoorn gravatar imagegvdhoorn ( 2018-08-30 05:34:36 -0600 )edit

Do you mean the debug build type in Visual Studio? Because yes, it was built with the Debug build type, but I don't know what you mean by build every ROS2 library in Debug build type. Can you explain maybe it a bit better?

jborja1 gravatar imagejborja1 ( 2018-08-30 07:03:52 -0600 )edit

Linking debug and non-debug code together on Windows is not advisable. So if your node is compiled in Debug mode, but the rest of ROS2 isn't, that could lead to problems.

See this older thread about it (but it's not ROS specific).

gvdhoorn gravatar imagegvdhoorn ( 2018-08-30 08:06:57 -0600 )edit

As @gvdhoorn suggested it seems like this is an issue with mixing Debug and Release. Our binaries are Release binaries, you should try building and debugging a release build of your project and see if you get the same issue. If so, then to use debug you'll need to build ROS 2 as debug too.

William gravatar imageWilliam ( 2018-10-05 10:30:21 -0600 )edit