ROS Resources: Documentation | Support | Discussion Forum | Index | Service Status | Q&A
Ask Your Question

set realtime prio before executing launchfile

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

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


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 -0500 )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 -0500 )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 -0500 )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 -0500 )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 -0500 )edit

1 Answer

Sort by » oldest newest most voted

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

crnewton gravatar image

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

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!");
        ROS_INFO("Main thread: SCHED_FIFO OK");

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

here are some other examples with a python implementation of the solution:

edit flag offensive delete link more

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account.

Add Answer

Question Tools



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

Seen: 172 times

Last updated: Oct 23 '20