Nav2 Navigation Stack - humble  humble
ROS 2 Navigation Stack
parse_multirobot_pose.py
1 # Copyright (c) 2023 LG Electronics.
2 #
3 # Licensed under the Apache License, Version 2.0 (the "License");
4 # you may not use this file except in compliance with the License.
5 # You may obtain a copy of the License at
6 #
7 # http://www.apache.org/licenses/LICENSE-2.0
8 #
9 # Unless required by applicable law or agreed to in writing, software
10 # distributed under the License is distributed on an "AS IS" BASIS,
11 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 # See the License for the specific language governing permissions and
13 # limitations under the License.
14 
15 import yaml
16 import sys
17 from typing import Text, Dict
18 
19 
21  """
22  Parsing argument using sys module
23  """
24 
25  def __init__(self, target_argument: Text):
26  """
27  Parse arguments for multi-robot's pose
28 
29  for example,
30  `ros2 launch nav2_bringup bringup_multirobot_launch.py
31  robots:="robot1={x: 1.0, y: 1.0, yaw: 0.0};
32  robot2={x: 1.0, y: 1.0, z: 1.0, roll: 0.0, pitch: 1.5707, yaw: 1.5707}"`
33 
34  `target_argument` shall be 'robots'.
35  Then, this will parse a string value for `robots` argument.
36 
37  Each robot name which is corresponding to namespace and pose of it will be separted by `;`.
38  The pose consists of x, y and yaw with YAML format.
39 
40  :param: target argument name to parse
41  """
42  self.__args: Text = self.__parse_argument__parse_argument(target_argument)
43 
44  def __parse_argument(self, target_argument: Text) -> Text:
45  """
46  get value of target argument
47  """
48  if len(sys.argv) > 4:
49  argv = sys.argv[4:]
50  for arg in argv:
51  if arg.startswith(target_argument + ":="):
52  return arg.replace(target_argument + ":=", "")
53  return ""
54 
55  def value(self) -> Dict:
56  """
57  get value of target argument
58  """
59  args = self.__args
60  parsed_args = list() if len(args) == 0 else args.split(';')
61  multirobots = dict()
62  for arg in parsed_args:
63  key_val = arg.strip().split('=')
64  if len(key_val) != 2:
65  continue
66  key = key_val[0].strip()
67  val = key_val[1].strip()
68  robot_pose = yaml.safe_load(val)
69  if 'x' not in robot_pose:
70  robot_pose['x'] = 0.0
71  if 'y' not in robot_pose:
72  robot_pose['y'] = 0.0
73  if 'z' not in robot_pose:
74  robot_pose['z'] = 0.0
75  if 'roll' not in robot_pose:
76  robot_pose['roll'] = 0.0
77  if 'pitch' not in robot_pose:
78  robot_pose['pitch'] = 0.0
79  if 'yaw' not in robot_pose:
80  robot_pose['yaw'] = 0.0
81  multirobots[key] = robot_pose
82  return multirobots