ROS Resources: Documentation | Support | Discussion Forum | Index | Service Status | ros @ Robotics Stack Exchange
Ask Your Question
0

How to kill rosnode via Qt gui? (QProcess). Node hungs.

asked 2021-01-12 06:31:12 -0500

nikko gravatar image

updated 2021-01-19 01:15:34 -0500

Hello,

I'm facing difficulties shutting down my rosnode that I run through my self made Qt (5) GUI utilizing its QProcess class. I have tried multiple ideas found from the internet, but for some reason none of them are working for me. My ROS is Melodic.

EDIT Since I did not get much attention here I also tried to find answers from Qt forums. I got some good tips, but the answer hasn't been found yet. This problem seems to be more about ROS than Qt, I think. I tested QProcess running and killing with gnome-calculator and it worked well. Here is the link for my post in Qt forums: https://forum.qt.io/topic/122703/can-...

Here is a snippet from my code where I'm trying to kill my node after I have pushed a exit button. The node started inside QProcess is named as "point_collector". You can see some of my other tryings as comments in the code.

    if (resBtn != QMessageBox::Yes) {
    event->ignore();
} else {
    if (scanProcess->atEnd()){

        std::cout << "Scan process running. will be killed" << std::endl;
        //scanProcess->terminate();
        //scanProcess->kill();
       //scanProcess->close();
        QProcess killer;
        killer.start("kill -SIGINT " + QString::number(scanProcess->processId()));
        //killer.terminate();
        bool started = killer.waitForStarted();

        //std::cout <<started<<std::endl;
        //std::cout <<scanProcess->atEnd()<<std::endl;
        //killer.start("rosnode kill point_collector");
    }

Here the code how I start the Qprocess:

 QString str = "rosrun point_collector collector "+ scantime->text()+" "+savepath->text()+" "+"livox/lidar"+" "+"livox_frame"+" "+"worksite";
    scanProcess->setProcessChannelMode(QProcess::MergedChannels);
    scanProcess->start(str);
    bool started = scanProcess->waitForStarted(-1);

For my point_collector I also tried to utilize the Custom SIGINT Handler like in the tutorial (http://wiki.ros.org/roscpp/Overview/I...) it did not make any difference.

The node shuts down normally in terminal though when I rosrun it ans "rosnode kill node" works also if I have started the node via QProcess. BUT If I have tried to kill() the node via QProcess, "rosnode kill node" in terminal does not work. Somehow the node gets jammed. "rosnode cleanup" seem to clean the node from nodelist.

For ROS launch file "kill -SIGINT processPID" worked fine but for a single node (that I run as node also) it didn't.

Any ideas how to solve this??

edit retag flag offensive close merge delete

1 Answer

Sort by ยป oldest newest most voted
0

answered 2021-01-20 01:32:03 -0500

nikko gravatar image

One solution found!

I found out that by shutting ( with SIGINT)

I found solution for this by creating a SIGINT handler in my node WITHOUT ros::shutdown() call but with a global variable set to True that is checked by my while loop. More about SIGINT handler in ROS: (http://wiki.ros.org/roscpp/Overview/I...).

In my Qt app I kill the node with kill(scanProces->processId(), SIGINT) command. Now QProcess.start(""kill -SIGINT " + QString::number(scanProcess->processId())) should also work.

I don't know if this is good way to handle this, but it seem to work!

So what I have in my node is this:

#include <ros/ros.h>
#include <signal.h>

bool exit = false
void mySigintHandler(int sig)
{
  // Do some custom action.
  // For example, publish a stop message to some other nodes.

  exit = true

}

int main(int argc, char** argv)
{
  ros::init(argc, argv, "my_node_name", ros::init_options::NoSigintHandler);
  ros::NodeHandle nh;

  // Override the default ros sigint handler.
  // This must be set after the first NodeHandle is created.
  signal(SIGINT, mySigintHandler);


  while (ros::ok & !exit)
   {
       ros::spinOnce() 
   }
  return 0;
}

If someone more knowledgeable can tell the root problem of this and if my solution is ok, please enlighten me!

edit flag offensive delete link more

Comments

ok. I cant set this as solution by myself..

nikko gravatar image nikko  ( 2021-01-20 01:33:11 -0500 )edit

Question Tools

Stats

Asked: 2021-01-12 03:48:23 -0500

Seen: 709 times

Last updated: Jan 20 '21