Robotics StackExchange | Archived questions

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

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-t-kill-qprocess-in-overriden-closeevent/9

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/Initialization%20and%20Shutdown) 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??

Asked by nikko on 2021-01-12 04:48:23 UTC

Comments

Answers

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/Initialization%20and%20Shutdown).

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!

Asked by nikko on 2021-01-20 02:32:03 UTC

Comments

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

Asked by nikko on 2021-01-20 02:33:11 UTC