Basic ROS2 Test

This example demonstrates fundamental ROS2 operations using the ROS2ClientLibrary.

Overview

The basic ROS2 test covers:

  • Discovery Operations: Listing topics, services, and nodes

  • Node Management: Starting, monitoring, and terminating nodes

  • Topic Operations: Publishing, subscribing, and echoing messages

  • Launch Operations: Starting and managing launch files

  • Error Handling: Testing with non-existent resources

Test Structure

*** Settings ***
Documentation    Basic ROS2 CLI Library Test
Library          ros2_client.ROS2ClientLibrary
Library          Process
Library          Collections

*** Variables ***
${TEST_TIMEOUT}    10.0
${TEST_MESSAGE}    Hello from new structure!

Key Test Cases

Discovery Operations

Verify ROS2 Discovery Operations
    [Documentation]    Test basic ROS2 CLI operations
    [Tags]    basic    ros2

    # Test listing topics
    ${topics}=    List Topics    timeout=${TEST_TIMEOUT}
    Should Not Be Empty    ${topics}
    Log    Found topics: ${topics}

    # Test listing services
    ${services}=    List Services    timeout=${TEST_TIMEOUT}
    Log    Found services: ${services}

    # Test listing nodes
    ${nodes}=    List Nodes    timeout=${TEST_TIMEOUT}
    Log    Found nodes: ${nodes}

Node and Topic Operations

Test ROS2 Node And Topic Operations With Demo
    [Documentation]    Test ROS2 CLI operations with demo nodes
    [Tags]    demo    ros2
    [Timeout]    30s

    # Start demo talker node
    ${talker_process}=    Start Process    ros2    run    demo_nodes_cpp    talker
    Set Test Variable    ${talker_process}

    # Wait for the node to start
    Sleep    3s

    # Test node operations
    ${nodes}=    List Nodes    timeout=${TEST_TIMEOUT}
    Should Contain    ${nodes}    /talker

    # Test topic operations
    ${topics}=    List Topics    timeout=${TEST_TIMEOUT}
    Should Contain    ${topics}    /chatter

    # Test topic info
    ${topic_info}=    Get Topic Info    /chatter    timeout=${TEST_TIMEOUT}
    Should Be Equal    ${topic_info}[type]    std_msgs/msg/String

    # Test node info
    ${node_info}=    Get Node Info    /talker    timeout=${TEST_TIMEOUT}
    Log    Node info: ${node_info}

    # Clean up
    Terminate Process    ${talker_process}

Launch Operations

Test ROS2 Launch File Operations
    [Documentation]    Test launch operations with demo nodes
    [Tags]    launch    ros2
    [Timeout]    30s

    # Launch a simple demo launch file
    ${launch_success}=    Run Keyword And Return Status    Launch Package    demo_nodes_cpp    talker_listener.launch.py
    IF    ${launch_success}
        Log    Launch started successfully
        ${process}=    Launch Package    demo_nodes_cpp    talker_listener.launch.py

        # Wait for system to initialize
        Sleep    3s

        # Verify topics are available
        ${topics}=    List Topics    timeout=${TEST_TIMEOUT}
        Should Contain    ${topics}    /chatter

        # Stop the launch
        ${terminated}=    Terminate Launch Process    ${process}
        Should Be True    ${terminated}
        Log    Launch terminated successfully
    ELSE
        Log    Demo launch file not available, skipping launch test
    END

Native Operations

Test ROS2 Native Publisher And Subscriber Operations
    [Documentation]    Test native ROS2 operations with subscribers and publishers
    [Tags]    native    ros2
    [Timeout]    30s

    # Get client information
    ${info}=    Get Client Info
    Log    Using client with native support: ${info}[native_available]

    # Start a demo talker node
    ${talker_process}=    Run Node    demo_nodes_cpp    talker
    Set Test Variable    ${talker_process}

    # Wait for the node to start
    Sleep    3s

    # Create a native subscriber for the chatter topic
    ${subscriber}=    Create Subscriber    /chatter    std_msgs/msg/String
    Log    Created native subscriber: ${subscriber}

    # Wait for messages
    Sleep    5s

    # Get the latest message using native operations
    ${message}=    Get Latest Message    /chatter
    IF    ${message} is not None
        Log    Received message: ${message}[data]
        Should Contain    ${message}[data]    Hello World
    ELSE
        Log    No message received yet
    END

    # Clean up
    Shutdown Process    ${talker_process}
    Cleanup

Running the Example

To run this example:

# Run the basic ROS2 test
robot examples/basics/ros2_basic_test.robot

# Run with verbose output
robot -v examples/basics/ros2_basic_test.robot

# Run specific test cases
robot -t "Verify ROS2 Discovery Operations" examples/basics/ros2_basic_test.robot

Expected Output

The test will:

  1. Verify ROS2 Discovery: List and verify topics, services, and nodes

  2. Test Node Operations: Start a demo talker node and verify it’s running

  3. Test Topic Communication: Verify topic information and message flow

  4. Test Launch Operations: Start and stop launch files

  5. Test Native Operations: Use native ROS2 Python API for real-time communication

  6. Test Error Handling: Verify proper handling of non-existent resources

Key Learning Points

Troubleshooting

Common Issues

  1. Demo nodes not available: Ensure demo_nodes_cpp package is installed

  2. Permission errors: Check file permissions and user access

  3. Timeout errors: Increase timeout values for slow systems

  4. Process not found: Ensure demo nodes are available

Debug Tips

  1. Enable debug logging in Robot Framework

  2. Check ROS2 environment variables

  3. Verify ROS2 installation with ros2 –help

  4. Test individual commands manually

Next Steps

After running this example, you can: