Ask Your Question
1

How to launch node if param equals true?

asked 2021-04-13 09:56:36 -0600

Py gravatar image

How would I start a node if a parameter equalled a given value? I'm thinking something like this pseudo code:

<launch>
    if my_param == true: // PSEUDO CODE - How to properly implement this logic?
         <node name="my_node" pkg="my_package" type="my_node" output="screen"/>
</launch>

Also, how would I do this using rosrun from the command line?

edit retag flag offensive close merge delete

2 Answers

Sort by » oldest newest most voted
0

answered 2021-05-07 06:31:53 -0600

Py gravatar image

I solved my problem using a bash script to keep checking until my_param changes to true to rosrun and kill the node when needed. This is as follows:

#!/bin/bash
rosparam set my_param false
while :
do
  node_name="/my_node"
  node_search=$(rosnode list | grep $node_name)
  if [ $(rosparam get my_param) == false ]
  then
    if [ "$node_search" == $node_name ]
    then
      rosnode kill $node_name
      fi
  else
    if ! [ "$node_search" == $node_name ]
    then
      rosrun my_package my_node
    fi
  fi
done
edit flag offensive delete link more
2

answered 2021-04-13 10:09:48 -0600

LR1997 gravatar image

updated 2021-04-13 10:14:32 -0600

Hi, I think the easiest way is by defining an arg. Something like:

<arg name="var"/>

and then grouping the nodes to start if the argument of var is true. Your launch file should look like:

<launch>
    <arg name="var"/>

    <group if="$(arg var)">
       ...nodes to start...
    </group>
</launch>

You can then start the nodes from the terminal with:

roslaunch you_package launch_file var:=true

Let me know if that helps

edit flag offensive delete link more

Comments

1

Note that any tag can use the if attribute. For example, if you only have one node to launch, you can just put it in the <node> tag rather than using a group. Here's the roslaunch XML wiki for further info.

tryan gravatar image tryan  ( 2021-04-13 10:25:03 -0600 )edit

That is very helpful. Two questions... How would I set var to a parameter from the parameter server in the launch file or command line? Also, if var is false, I'd like to keep checking until the parameter that defines it changes to true and then start the node. I guess this would be like while false do nothing. Is this possible?

Py gravatar image Py  ( 2021-04-13 14:14:22 -0600 )edit

I'm wondering if a bash script is needed to achieve what I want. Maybe it would look similar to the following, although I'm unsure of exact syntax.

#!/bin/bash

while [$(rosparam get my_param) == ‘false’ or ‘’]; do
if [my_node is running]
then
rosnode kill my_node
fi
done

while [$(rosparam get my_param) == ‘true’]; do
if [my_node is not running]
rosrun my_package my_node // in background so can keep listening for change in my_param value
fi
done

What do you think?

Py gravatar image Py  ( 2021-04-13 15:25:51 -0600 )edit

To answer your previous questions:

How would I set var to a parameter from the parameter server in the launch file or command line?

You can't use a parameter as a variable (AFAIK; there was some discussion, but IDK if it's implemented), but you could set the parameter from the same argument:

...
<arg name="my_arg" default="true" />
<param name="my_param" value="$(arg my_arg)" />
<group if="$(arg my_arg)">
  ...
</group>

I'd like to keep checking until the parameter that defines it changes ... Is this possible?

Launch files aren't meant for ongoing behavior, like loops--more just startup. To keep checking, you'd have to use an external script (as you have), or build the functionality into the node, e.g., using dynamic_reconfigure. You might also consider using something other than a parameter, like a topic or service depending on your use case. There are pros ...(more)

tryan gravatar image tryan  ( 2021-05-07 07:28:15 -0600 )edit

Your Answer

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

Add Answer

Question Tools

1 follower

Stats

Asked: 2021-04-13 09:56:36 -0600

Seen: 87 times

Last updated: May 07