0
$\begingroup$

I am following the tutorial Setting up a robot simulation (Webots) ROS2 Humble.

Here is my_robot.urdf file:

<?xml version="1.0" ?>
<robot name="My robot">
    <webots>
        <plugin type="my_package.my_robot_driver.MyRobotDriver" />
    </webots>
</robot>

Here is the part of robot_launch.py file:

my_robot_driver = WebotsController(
    robot_name='my_robot',
    parameters=[
        {'robot_description': robot_description_path},
    ]
)

Here is the part of my_robot_driver.py file:

class MyRobotDriver:
    def init(self, webots_node, properties):
        self.__robot = webots_node.robot

        self.__left_motor = self.__robot.getDevice('left wheel motor')
        self.__right_motor = self.__robot.getDevice('right wheel motor')

        self.__left_motor.setPosition(float('inf'))
        self.__left_motor.setVelocity(0)

        self.__right_motor.setPosition(float('inf'))
        self.__right_motor.setVelocity(0)

        self.__target_twist = Twist()

        rclpy.init(args=None)
        self.__node = rclpy.create_node('my_robot_driver')
        self.__node.create_subscription(Twist, 'cmd_vel', self.__cmd_vel_callback, 1)

    def __cmd_vel_callback(self, twist):
        self.__target_twist = twist

I want to take the robot_name from robot_launch.py file and use it inside the MyRobotDriver class of my_robot_driver.py file.

Following is the little part of my desired MyRobotDriver class code:

self.__target_twist = Twist()
name = robot_name   # here robot_name comes from launch file

rclpy.init(args=None)
self.__node = rclpy.create_node('my_robot_driver')
self.__node.create_subscription(Twist, f'{name}/cmd_vel', self.__cmd_vel_callback, 1)

Is there any way to implement name = robot_name # here robot_name comes from launch file this line correctly? Here my_robot.urdf, robot_launch.py, my_robot_driver.py are from the tutorial.

$\endgroup$

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Browse other questions tagged or ask your own question.