choose random items from python file and send to launch file
Hi all, I want to randomly choose 2 items from a list[banana,apple,pear,mango,orange] in a python file (rand_choose.py). Then I need to pass these 2 items to a launch file (choose_fruit.launch.xml) and assign them to two strings "first" and "last". My operation system is ROS2 eloquent in Ubuntu18.04. May I know how to fulfill it? Thank you!
Can you elaborate on your intentions? normally you do something like this programmatically in your node. you can also launch a launchfile with arguments, maybe that's what you're looking for.
from random import seed
from random import choice
import rclpy
from rclpy.node import Node
class GenParams(Node):
seed(1)
array = [banana,apple,pear,mango,orange]
for _ in range(2):
selection = choice(array)
print(selection)
def __init__(self):
super().__init__('gen_params_rclpy')
self.declare_parameter('rand_fruit')
rand_fruit = selection
print(rand_fruit)
def main(args=Node):
rclpy.init(args=args)
node = TestParams()
rclpy.spin(node)
node.destroy_node()
rclpy.shutdown()
if __name__==”__main__”:
main()
<launch>
< param name="rand_fruit" command="$(find fruit_pkg)/scripts/rand_choose.py"/>
</launch>
When I launch the choose_fruit file, i got error messages:
Caught exception when trying to load file of format [xml]:Caught exception when trying to load file of format [xml]: not well-formed (invalid token): line 4, column 1
May I know how to improve it to be workable? Thanks!
Related files: rand_choose.py:
choose_fruit.launch.xml:
Please advise what's the problem in above files and how to make them workable in ros2. Thanks!