Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

I've found a workaround, but I don't think this is the idiomatic way to do it.

Use the launch file to accept an argument:

def generate_launch_description():
    return LaunchDescription([

        DeclareLaunchArgument("simulation_mode", description = "launch a simulation node that will spoof the rti gateway messages, and configure the rpc server to interact with this simulation node"),

        Node(
            package='my_package',
            node_executable='my_node',
            emulate_tty=True,
            arguments = [launch.substitutions.LaunchConfiguration('simulation_mode')]
        )
])

Then launch the launch file with:

ros2 launch -a launch/my_launch.py simulation_mode:=False

And then this argument can be used in the node like this:

import rclpy
from rclpy.node import Node
import sys
import typing

class MyNode(Node):
    def __init__(self, simMode : Typing):
        super().__init__("my_node")
        self.simMode = simMode

def main():

    rclpy.init()

    print(str(sys.argv[1]))

    node = MyNode(sys.argv[1])
    node.get_logger().info("Hello from my_node")

    try:
        rclpy.spin(node) 
    except:
        pass

    node.destroy_node()
    rclpy.shutdown()

if __name__ == '__main__':
    main()

argv[1] becomes the right-hand side of the assignment simulation_mode:=False. It's a positional argument instead of a named argument, so it could be more prone to errors, but it will do until I find a better solution.

I've found a workaround, but I don't think this is the idiomatic way to do it.

Use the launch file to accept an argument:

def generate_launch_description():
    return LaunchDescription([

        DeclareLaunchArgument("simulation_mode", description = "launch a simulation node that will spoof the rti gateway messages, and configure the rpc server to interact with this simulation node"),

        Node(
            package='my_package',
            node_executable='my_node',
            emulate_tty=True,
            arguments = [launch.substitutions.LaunchConfiguration('simulation_mode')]
        )
])

Then launch the launch file with:

ros2 launch -a launch/my_launch.py simulation_mode:=False

And then this argument can be used in the node like this:

import rclpy
from rclpy.node import Node
import sys
import typing

class MyNode(Node):
    def __init__(self, simMode : Typing):
        super().__init__("my_node")
        self.simMode = simMode

def main():

    rclpy.init()

    print(str(sys.argv[1]))

    node = MyNode(sys.argv[1])
    node.get_logger().info("Hello from my_node")

    try:
        rclpy.spin(node) 
    except:
        pass

    node.destroy_node()
    rclpy.shutdown()

if __name__ == '__main__':
    main()

argv[1] becomes the right-hand side of the assignment simulation_mode:=False. It's a positional argument instead of a named argument, so it could be more prone to errors, but it will do until I find a better solution.

I've found a workaround, but I don't think this is the idiomatic way to do it.

Use the launch file to accept an argument:

def generate_launch_description():
    return LaunchDescription([

        DeclareLaunchArgument("simulation_mode", description = "launch a simulation node that will spoof the gateway messages, and configure the rpc server to interact with this simulation node"),
DeclareLaunchArgument("simulation_mode"),

        Node(
            package='my_package',
            node_executable='my_node',
            emulate_tty=True,
            arguments = [launch.substitutions.LaunchConfiguration('simulation_mode')]
        )
])

Then launch the launch file with:

ros2 launch -a launch/my_launch.py simulation_mode:=False

And then this argument can be used in the node like this:

import rclpy
from rclpy.node import Node
import sys
import typing

class MyNode(Node):
    def __init__(self, simMode : Typing):
        super().__init__("my_node")
        self.simMode = simMode

def main():

    rclpy.init()

    print(str(sys.argv[1]))

    node = MyNode(sys.argv[1])
    node.get_logger().info("Hello from my_node")

    try:
        rclpy.spin(node) 
    except:
        pass

    node.destroy_node()
    rclpy.shutdown()

if __name__ == '__main__':
    main()

argv[1] becomes the right-hand side of the assignment simulation_mode:=False. It's a positional argument instead of a named argument, so it could be more prone to errors, but it will do until I find a better solution.

I've found a workaround, but I don't think this is the idiomatic way to do it.

Use the launch file to accept an argument:

def generate_launch_description():
    return LaunchDescription([

        DeclareLaunchArgument("simulation_mode"),

        Node(
            package='my_package',
            node_executable='my_node',
            emulate_tty=True,
            arguments = [launch.substitutions.LaunchConfiguration('simulation_mode')]
        )
])

Then launch the launch file with:

ros2 launch -a launch/my_launch.py simulation_mode:=False

And then this argument can be used in the node like this:

import rclpy
from rclpy.node import Node
import sys
import typing

class MyNode(Node):
    def __init__(self, simMode : Typing):
bool):
        super().__init__("my_node")
        self.simMode = simMode

def main():

    rclpy.init()

    print(str(sys.argv[1]))

    node = MyNode(sys.argv[1])
    node.get_logger().info("Hello from my_node")

    try:
        rclpy.spin(node) 
    except:
        pass

    node.destroy_node()
    rclpy.shutdown()

if __name__ == '__main__':
    main()

argv[1] becomes the right-hand side of the assignment simulation_mode:=False. It's a positional argument instead of a named argument, so it could be more prone to errors, but it will do until I find a better solution.