Nav2 Navigation Stack - jazzy  jazzy
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 sys
16 from typing import Dict, Text
17 
18 import yaml
19 
20 
22  """Parsing argument using sys module."""
23 
24  def __init__(self, target_argument: Text):
25  """
26  Parse arguments for multi-robot's pose.
27 
28  for example,
29  `ros2 launch nav2_bringup bringup_multirobot_launch.py
30  robots:="robot1={x: 1.0, y: 1.0, yaw: 0.0};
31  robot2={x: 1.0, y: 1.0, z: 1.0, roll: 0.0, pitch: 1.5707, yaw: 1.5707}"`
32 
33  `target_argument` shall be 'robots'.
34  Then, this will parse a string value for `robots` argument.
35 
36  Each robot name which is corresponding to namespace and pose of it will be separted by `;`.
37  The pose consists of x, y and yaw with YAML format.
38 
39  :param: target argument name to parse
40  """
41  self.__args: Text = self.__parse_argument__parse_argument(target_argument)
42 
43  def __parse_argument(self, target_argument: Text) -> Text:
44  """Get value of target argument."""
45  if len(sys.argv) > 4:
46  argv = sys.argv[4:]
47  for arg in argv:
48  if arg.startswith(target_argument + ':='):
49  return arg.replace(target_argument + ':=', '')
50  return ''
51 
52  def value(self) -> Dict:
53  """Get value of target argument."""
54  args = self.__args
55  parsed_args = [] if len(args) == 0 else args.split(';')
56  multirobots = {}
57  for arg in parsed_args:
58  key_val = arg.strip().split('=')
59  if len(key_val) != 2:
60  continue
61  key = key_val[0].strip()
62  val = key_val[1].strip()
63  robot_pose = yaml.safe_load(val)
64  if 'x' not in robot_pose:
65  robot_pose['x'] = 0.0
66  if 'y' not in robot_pose:
67  robot_pose['y'] = 0.0
68  if 'z' not in robot_pose:
69  robot_pose['z'] = 0.0
70  if 'roll' not in robot_pose:
71  robot_pose['roll'] = 0.0
72  if 'pitch' not in robot_pose:
73  robot_pose['pitch'] = 0.0
74  if 'yaw' not in robot_pose:
75  robot_pose['yaw'] = 0.0
76  multirobots[key] = robot_pose
77  return multirobots