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

set realtime prio before executing launchfile

asked 2020-10-07 09:30:02 -0600

crnewton gravatar image

I’m using the Elfin robot arm and to launch it you need to execute the following command:

sudo chrt 10 bash

then in the same terminal you can launch the controller by running:

roslaunch elfin_robot_bringup elfin_ros_control_v2.launch

Now I’m automating the launch of all the nodes, but I have no idea how I can execute ‘sudo chrt 10 bash’ using my launch file. I'm looking for a 'clean' solution, and hope it doesn't include calling a bash script.

What is a good /safe way to execute both commands after each other in the same shell? hope someone can help me further.

edit retag flag offensive close merge delete

Comments

Does #q246090 cover this?

And actually I would suggest having the driver binary itself request the RT priority. That would be a lot easier and have better UX.

gvdhoorn gravatar image gvdhoorn  ( 2020-10-07 10:28:41 -0600 )edit

I'll look into it. Having the driver request RT is exactly what I want. I'll update when It worked :).

crnewton gravatar image crnewton  ( 2020-10-09 01:19:33 -0600 )edit

Not sure you understood what I meant.

I'm suggesting to add something like this to the driver binary itself: ur_robot_driver/src/ros/hardware_interface_node.cpp; lines 59 to 106.

Not in a .launch file.

gvdhoorn gravatar image gvdhoorn  ( 2020-10-09 03:07:03 -0600 )edit

Thanks for the example code! doing it in the driver itself is what I want, i like to keep launch files only for launching ( as simple as possible)

crnewton gravatar image crnewton  ( 2020-10-13 06:07:39 -0600 )edit

doing it in the driver itself is what I want, i like to keep launch files only for launching

technically .launch files encode the configuration of the application (together with .yaml and some other files). The priority at which processes started by .launch files are run could be considered configuration.

Hiding scheduling configuration in node binaries essentially makes it impossible to introspect or change that configuration, reducing the control a .launch file can exert over that aspect of the application.

I'm not saying you shouldn't do it, just highlighting some disadvantages.

gvdhoorn gravatar image gvdhoorn  ( 2020-10-13 07:36:40 -0600 )edit

1 Answer

Sort by » oldest newest most voted
0

answered 2020-10-23 07:09:28 -0600

crnewton gravatar image

updated 2020-10-23 07:11:48 -0600

For now the propopsed solution(s) won't be inplemented. @gvdhoorn gave the answer to my question, I'm positive this solves my problem. he refered to this example:

 std::ifstream realtime_file("/sys/kernel/realtime", std::ios::in);
  bool has_realtime;
  realtime_file >> has_realtime;
  if (has_realtime)
  {
    const int max_thread_priority = sched_get_priority_max(SCHED_FIFO);
    if (max_thread_priority != -1)
    {
      // We'll operate on the currently running thread.
      pthread_t this_thread = pthread_self();

      // struct sched_param is used to store the scheduling priority
      struct sched_param params;

      // We'll set the priority to the maximum.
      params.sched_priority = max_thread_priority;

      int ret = pthread_setschedparam(this_thread, SCHED_FIFO, &params);
      if (ret != 0)
      {
        ROS_ERROR_STREAM("Unsuccessful in setting main thread realtime priority. Error code: " << ret);
      }
      // Now verify the change in thread priority
      int policy = 0;
      ret = pthread_getschedparam(this_thread, &policy, &params);
      if (ret != 0)
      {
        std::cout << "Couldn't retrieve real-time scheduling paramers" << std::endl;
      }

      // Check the correct policy was applied
      if (policy != SCHED_FIFO)
      {
        ROS_ERROR("Main thread: Scheduling is NOT SCHED_FIFO!");
      }
      else
      {
        ROS_INFO("Main thread: SCHED_FIFO OK");
      }

      // Print thread scheduling priority
      ROS_INFO_STREAM("Main thread priority is " << params.sched_priority);
    }
    else
    {
      ROS_ERROR("Could not get maximum thread priority for main thread");
    }
  }

here are some other examples with a python implementation of the solution: https://python.hotexamples.com/exampl...

edit flag offensive delete link more

Question Tools

2 followers

Stats

Asked: 2020-10-07 09:30:02 -0600

Seen: 317 times

Last updated: Oct 23 '20