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

Passing parameter for launch testing in ros2

asked 2022-06-24 17:46:09 -0500

Flash gravatar image

updated 2022-06-26 20:17:44 -0500

Hi,
I am running launch testing where I have a test node that's used for testing if messages are been transmitted. When I run individual nodes like pub_demo or sub_demo it works, it takes parameters from the config file. But when I run the demo_pkg_test.py using launch_test these two nodes don't take the parameters. Even the test node doesn't take the parameters.

src
├── demo_pkg
│   ├── CMakeLists.txt
│   ├── config
│   │   └── params.yaml
│   ├── include
│   │   └── demo_pkg
│   │       └── qos_custom_profile.hpp
│   ├── package.xml
│   ├── src
│   │   ├── pub_demo.cpp
│   │   └── sub_demo.cpp
│   └── test
│       ├── demo_pkg_test.py

Below is my code

Streaming = QoSProfile(durability=qos.QoSDurabilityPolicy.VOLATILE,
                   reliability=qos.QoSReliabilityPolicy.BEST_EFFORT, history=qos.QoSHistoryPolicy.KEEP_LAST, depth=1)
State = QoSProfile(durability=qos.QoSDurabilityPolicy.TRANSIENT_LOCAL,
               reliability=qos.QoSReliabilityPolicy.RELIABLE, history=qos.QoSHistoryPolicy.KEEP_LAST, depth=20)


@pytest.mark.launch_test
@launch_testing.markers.keep_alive
def generate_test_description():

config = os.path.join('/home/'+$USER+'/dev_ws/src/demo_pkg/config/params.yaml')


print(config)
return launch.LaunchDescription([
    launch_ros.actions.Node(
        executable='pub_ex',
        package='demo_pkg',
        name='pub_node',
        parameters=[config]
    ),
    launch_ros.actions.Node(
        executable='sub_ex',
        package='demo_pkg',
        name='sub_node',
        parameters=[config]
    ),
    launch_testing.actions.ReadyToTest()
])


class TestFixture(unittest.TestCase):

def test_pin_robot_enable(self):
    rclpy.init()
    node = Node('test_node')
    self.declare_parameters(
        namespace = '',
        parameters = [
        ('robot_name',None)])
    robot_name = self.get_parameter('robot_name').get_parameter_value().string_value
    print("-------1----",robot_name)
    self.robot_enable_data = []
    sub = node.create_subscription(
        Bool,
        '/sub_topic',
        self.topicMsgData,
        Streaming
    )
    self.pub = node.create_publisher(
        Bool,
        '/pub_topic' ,
        Streaming
    )
    timer_period = 0.5  # seconds
    self.timer = node.create_timer(timer_period, self.pub_callback)
    self.value = Bool()

    try:
        end_time = time.time() + 10
        self.test_data_recv = []
        while time.time() < end_time:
            rclpy.spin_once(node, timeout_sec=0.1)

        if self.test_data_recv == []:
            test_data = ""
        else:
            test_data = self.test_data[-1]


        self.assertEqual(test_data, True,
                         "Error")

    finally:
        node.destroy_subscription(sub)
    rclpy.shutdown()

def pub_callback(self):
    data= Bool()
    data.data = True
    self.pub.publish(data)

def topicMsgData(self, msg):
    self.test_data_recv.append(msg.data)

Below is my params.yaml file data.

pub_node:
  ros__parameters:
    robot_name: "BOT02/"

sub_node:
  ros__parameters:
    robot_name: "BOT02/"

test_node:
  ros__parameters:
    robot_name: "BOT02/"

Thanks in advance, any help is much appreciated.

edit retag flag offensive close merge delete

Comments

can you also post contents of params.yaml, it could be something with name mismatch of the node

Fetullah Atas gravatar image Fetullah Atas  ( 2022-06-24 17:54:26 -0500 )edit

@Fetullah Atas Thanks for the quick response. I have updated my question.

Flash gravatar image Flash  ( 2022-06-24 18:07:34 -0500 )edit

2 Answers

Sort by » oldest newest most voted
0

answered 2022-06-29 08:10:22 -0500

Flash gravatar image

updated 2022-06-29 08:12:18 -0500

If anyone comes across the same problem this is how I was able to fix my problem.
There was a mistake in my def generate_test_description(): function after fixing that my sub_node and pub_node are able to receive or read the parameters from the param.yaml file but still, my test_node is unable to read the parameters. To fix that part I used the python read file function, just in case anyone wondering how I solved it check this post.
And the solution to transmit parameters to sub_node and pub_node nodes that are initiated on test_node is as below

def generate_test_description():
    config = os.path.join('/home/'+$USER+'/dev_ws/src/demo_pkg/config/params.yaml')
    nodePub = launch_ros.actions.Node(
        executable='pub_ex',
        package='demo_pkg',
        name='pub_node',
        parameters=[config]
        )
    nodeSub = launch_ros.actions.Node(
        executable='sub_ex',
        package='demo_pkg',
        name='sub_node',
        parameters=[config]
        )
    context = {'nodePub': nodePub, 'nodeSub': nodeSub}
    return launch.LaunchDescription([nodePub , nodeSub, launch_testing.actions.ReadyToTest()]), context

the missing part was the context.

edit flag offensive delete link more
1

answered 2022-06-25 01:18:56 -0500

updated 2022-06-25 15:44:30 -0500

After you post params.yaml, I think there is a possibility this is caused by your putting the params in wrong namespace. For example in the launch file, you specify a node name “sub_node” but in yaml file, your specify as “sub_demo”. You need to match the names of nodes in yaml and launch files.

Edit as reply to your comment;

sub_demo:
  ros__parameters:
    robot_name: "BOT02/"

here your telling that robot_name parameter belongs to node with name sub_demo , but in the launch file your saying

   launch_ros.actions.Node(
        executable='sub_ex',
        package='demo_pkg',
        name='sub_node',
        parameters=[config]
    ),

the name of the node is sub_node, they are not matching.

edit flag offensive delete link more

Comments

No all nodes i checked its exactly same

Flash gravatar image Flash  ( 2022-06-25 11:02:29 -0500 )edit

please see the edit

Fetullah Atas gravatar image Fetullah Atas  ( 2022-06-25 15:44:38 -0500 )edit

sry for the error in the printing. Above code, I modified it for this question during which I made a mistake. But the Node name on my actual code does match.
I did run my test fine and successfully when I don't use and provide the parameter file.
I corrected my question.

Flash gravatar image Flash  ( 2022-06-25 17:30:07 -0500 )edit

Question Tools

1 follower

Stats

Asked: 2022-06-24 17:46:09 -0500

Seen: 629 times

Last updated: Jun 29 '22