sick_scan_xd_simu_cfg.py
Go to the documentation of this file.
1 import argparse
2 import json
3 import os
4 import shutil
5 from datetime import datetime
6 from pathlib import Path
7 
9  """
10  Configuration and commandline parameter for a sick_scan_xd simulation
11  """
12 
13  def __init__(self, simu_start_time):
14  """
15  Initializing constructor
16  """
17 
18  # Parse command line arguments
19  os_name = "linux"
20  if os.name == "nt":
21  os_name = "windows"
22  self.config_file = "./src/sick_scan_xd/test/docker/data/multiscan_compact_test01_cfg.json"
23  self.log_folder = f"./log/sick_scan_xd_simu/{simu_start_time.strftime('%Y%m%d_%H%M%S')}"
24  parser = argparse.ArgumentParser(description="Run a sick_scan_xd simulation")
25  parser.add_argument('--os', type=str, help="OS (linux or windows)", default=os_name, choices=["linux", "windows"])
26  parser.add_argument('--ros', type=str, help="ROS version (none, noetic, foxy or humble)", default="noetic", choices=["none", "noetic", "foxy", "humble"])
27  parser.add_argument('--cfg', type=str, help="configuration file", default=self.config_file)
28  parser.add_argument('--log_folder', type=str, help="log folder", default=self.log_folder)
29  parser.add_argument('--save_messages_jsonfile', type=str, help="optional jsonfile to save all received messages", default="")
30  parser.add_argument('--run_seconds', type=int, help="run simulation in seconds, delay before shutdown", default=-1)
31 
32  args = parser.parse_args()
33  self.os_name = args.os # "linux" or "windows"
34  self.ros_version = args.ros # "none", "noetic", "foxy" or "humble"
35  self.config_file = args.cfg
36  self.log_folder = args.log_folder
37  self.report_md_filename = "sick_scan_xd_testreport.md"
38  self.error_messages = []
39  self.sick_scan_xd_pointcloud_topics = [] # list of sick_scan_xd PointCloud2 topics to be monitored
40  self.sick_scan_xd_laserscan_topics = [] # list of sick_scan_xd LaserScan topics to be monitored
41  self.sick_scan_xd_imu_topics = [] # list of sick_scan_xd IMU topics to be monitored
42  self.reference_messages_jsonfile = "" # jsonfile with reference pointcloud and laserscan messages, which are verified against sick_scan_xd messages
43  self.save_messages_jsonfile = "" # optionally save all received messages in a json-file, e.g. "sick_scan_xd_simu_received_messages.json"
44  self.run_simu_seconds_before_shutdown = 10 # delay before simulation shutdown
45 
46  # Read configuration from json file
47  try:
48  with open(self.config_file, "r") as file_stream:
49  json_config = json.load(file_stream)
50  reference_messages_jsonfile = json_config["reference_messages_jsonfile"]
51  self.save_messages_jsonfile = json_config["save_messages_jsonfile"]
52  self.report_md_filename = json_config["report_md_filename"]
53  sick_scan_xd_pointcloud_topics = json_config["sick_scan_xd_pointcloud_topics"]
54  sick_scan_xd_laserscan_topics = json_config["sick_scan_xd_laserscan_topics"]
55  sick_scan_xd_imu_topics = json_config["sick_scan_xd_imu_topics"]
56  args_sick_scan_xd_launch = json_config["args_sick_scan_xd_launch"]
57  args_udp_scandata_sender = json_config["args_udp_scandata_sender"]
58  args_sopas_server = json_config["args_sopas_server"]
59  self.run_simu_seconds_before_shutdown = json_config["run_simu_seconds_before_shutdown"]
60  args_rviz = json_config["args_rviz_{}_{}".format(self.os_name, self.ros_version)]
61  args_shutdown_nodes = json_config["args_shutdown_nodes"]
62  args_shutdown_server = json_config["args_shutdown_server"]
63  # overwrite parameter with commandline options
64  if len(args.save_messages_jsonfile) > 0:
65  self.save_messages_jsonfile = args.save_messages_jsonfile
66  if len(self.save_messages_jsonfile) > 0:
67  self.save_messages_jsonfile = os.path.basename(self.save_messages_jsonfile)
68  else:
69  self.save_messages_jsonfile = "sick_scan_xd_simu_received_messages.json"
70  if args.run_seconds >= 0:
71  self.run_simu_seconds_before_shutdown = args.run_seconds
72  # with open("/tmp/sick_scan_xd_simu_cfg.json", "w") as file_stream:
73  # json.dump(json_config, file_stream, indent=2)
74  except Exception as exc:
75  self.error_messages.append(f"## ERROR SickScanXdSimuConfig: exception {exc}, check configuration file \"{self.config_file}\"")
76 
77  # Create log and output folder
78  Path(self.log_folder).mkdir(parents=True, exist_ok=True)
79  if not os.path.exists(self.log_folder):
80  self.error_messages.append(f"## ERROR SickScanXdSimuConfig: could not create output folder \"{self.log_folder}\"")
81 
82  # Prepare toolchain commands
83  self.cmd_init = []
84  self.cmd_sopas_server = []
85  self.cmd_rviz = []
86  self.cmd_sick_scan_xd = []
88  self.cmd_shutdown = []
89 
90  if self.ros_version == "none":
91  if self.os_name == "linux":
92  for arg_sick_scan_xd_launch in args_sick_scan_xd_launch:
93  self.cmd_sick_scan_xd.append(f"./src/sick_scan_xd/build/sick_scan_xd_api_dockertest ./src/sick_scan_xd/launch/{arg_sick_scan_xd_launch} _jsonfile:={self.log_folder}/{self.save_messages_jsonfile}")
94  if len(self.cmd_sick_scan_xd) < 1:
95  self.error_messages.append(f"## ERROR SickScanXdSimuConfig: sick_scan_xd launch parameter not configured, check configuration file \"{self.config_file}\"")
96 
97  self.cmd_shutdown.append("pkill -f sick_scan_xd_api_dockertest")
98  for arg_shutdown_server in args_shutdown_server:
99  self.cmd_shutdown.append(f"pkill -f {arg_shutdown_server}")
100 
101  elif self.ros_version == "noetic":
102  if self.os_name == "linux":
103 
104  self.cmd_init.append("source /opt/ros/noetic/setup.bash")
105  self.cmd_init.append("source ./devel_isolated/setup.bash")
106 
107  for arg_rviz in args_rviz:
108  self.cmd_rviz.append(f"(rosrun rviz rviz -d {arg_rviz} &)")
109  self.cmd_rviz.append("sleep 1")
110 
111  for arg_sick_scan_xd_launch in args_sick_scan_xd_launch:
112  self.cmd_sick_scan_xd.append(f"roslaunch sick_scan_xd {arg_sick_scan_xd_launch}")
113  if len(self.cmd_sick_scan_xd) < 1:
114  self.error_messages.append(f"## ERROR SickScanXdSimuConfig: sick_scan_xd launch parameter not configured, check configuration file \"{self.config_file}\"")
115 
116  for arg_shutdown_nodes in args_shutdown_nodes:
117  self.cmd_shutdown.append(f"rosnode kill {arg_shutdown_nodes}")
118  self.cmd_shutdown.append("sleep 3")
119  self.cmd_shutdown.append("rosnode kill -a")
120  for arg_shutdown_server in args_shutdown_server:
121  self.cmd_shutdown.append(f"pkill -f {arg_shutdown_server}")
122 
123  for sick_scan_xd_pointcloud_topic in sick_scan_xd_pointcloud_topics:
124  self.sick_scan_xd_pointcloud_topics.append(sick_scan_xd_pointcloud_topic)
125  for sick_scan_xd_laserscan_topic in sick_scan_xd_laserscan_topics:
126  self.sick_scan_xd_laserscan_topics.append(sick_scan_xd_laserscan_topic)
127  for sick_scan_xd_imu_topic in sick_scan_xd_imu_topics:
128  self.sick_scan_xd_imu_topics.append(sick_scan_xd_imu_topic)
129 
130  elif self.ros_version == "foxy" or self.ros_version == "humble":
131  if self.os_name == "linux":
132 
133  self.cmd_init.append("source /opt/ros/{}/setup.bash".format(self.ros_version))
134  self.cmd_init.append("source ./install/setup.bash")
135 
136  for arg_rviz in args_rviz:
137  self.cmd_rviz.append(f"(ros2 run rviz2 rviz2 -d {arg_rviz} &)")
138  self.cmd_rviz.append("sleep 1")
139 
140  for arg_sick_scan_xd_launch in args_sick_scan_xd_launch:
141  arg_sick_scan_xd_launch = arg_sick_scan_xd_launch.replace(".launch", ".launch.py")
142  self.cmd_sick_scan_xd.append(f"ros2 launch sick_scan_xd {arg_sick_scan_xd_launch}")
143  if len(self.cmd_sick_scan_xd) < 1:
144  self.error_messages.append(f"## ERROR SickScanXdSimuConfig: sick_scan_xd launch parameter not configured, check configuration file \"{self.config_file}\"")
145 
146  self.cmd_shutdown.append(f"pkill -f sick_generic_caller")
147  self.cmd_shutdown.append("sleep 3")
148  for arg_shutdown_server in args_shutdown_server:
149  self.cmd_shutdown.append(f"pkill -f {arg_shutdown_server}")
150 
151  for sick_scan_xd_pointcloud_topic in sick_scan_xd_pointcloud_topics:
152  self.sick_scan_xd_pointcloud_topics.append(sick_scan_xd_pointcloud_topic)
153  for sick_scan_xd_laserscan_topic in sick_scan_xd_laserscan_topics:
154  self.sick_scan_xd_laserscan_topics.append(sick_scan_xd_laserscan_topic)
155  for sick_scan_xd_imu_topic in sick_scan_xd_imu_topics:
156  self.sick_scan_xd_imu_topics.append(sick_scan_xd_imu_topic)
157 
158  elif self.os_name == "windows":
159  self.cmd_sick_scan_xd.append("echo %COMPUTERNAME% && dir /b/on") # TODO ...
160 
161  # Sopas server configuration for all dockertest (Linux, Windows, ROS1, ROS1, API)
162  for arg_sopas_server in args_sopas_server:
163  self.cmd_sopas_server.append(f"python3 {arg_sopas_server}")
164  if len(self.cmd_sopas_server) < 1:
165  print(f"SickScanXdSimuConfig: sopas server parameter not configured, sopas server not started. This is a valid option for error testcases, but check configuration file \"{self.config_file}\" if this is not intended.")
166 
167  # Udp sender configuration for all dockertest (Linux, Windows, ROS1, ROS1, API)
168  for arg_udp_scandata_sender in args_udp_scandata_sender:
169  self.cmd_udp_scandata_sender.append(f"python3 {arg_udp_scandata_sender}")
170 
171  # Copy config and reference files to logfolder for all dockertest (Linux, Windows, ROS1, ROS1, API)
172  if len(reference_messages_jsonfile) > 0 and os.path.exists(reference_messages_jsonfile):
173  shutil.copy(reference_messages_jsonfile, self.log_folder)
174  self.reference_messages_jsonfile = os.path.basename(reference_messages_jsonfile)
175  if len(self.config_file) > 0 and os.path.exists(self.config_file):
176  shutil.copy(self.config_file, self.log_folder)
177  self.config_file = os.path.basename(self.config_file)
178 
sick_scan_xd_simu_cfg.SickScanXdSimuConfig.reference_messages_jsonfile
reference_messages_jsonfile
Definition: sick_scan_xd_simu_cfg.py:42
sick_scan_xd_simu_cfg.SickScanXdSimuConfig.cmd_sopas_server
cmd_sopas_server
Definition: sick_scan_xd_simu_cfg.py:84
sick_scan_xd_simu_cfg.SickScanXdSimuConfig.cmd_init
cmd_init
Definition: sick_scan_xd_simu_cfg.py:83
sick_scan_xd_simu_cfg.SickScanXdSimuConfig.sick_scan_xd_imu_topics
sick_scan_xd_imu_topics
Definition: sick_scan_xd_simu_cfg.py:41
sick_scan_xd_simu_cfg.SickScanXdSimuConfig.__init__
def __init__(self, simu_start_time)
Definition: sick_scan_xd_simu_cfg.py:13
roswrap::console::print
ROSCONSOLE_DECL void print(FilterBase *filter, void *logger, Level level, const char *file, int line, const char *function, const char *fmt,...) ROSCONSOLE_PRINTF_ATTRIBUTE(7
Don't call this directly. Use the ROS_LOG() macro instead.
sick_scan_xd_simu_cfg.SickScanXdSimuConfig.cmd_rviz
cmd_rviz
Definition: sick_scan_xd_simu_cfg.py:85
sick_scan_xd_simu_cfg.SickScanXdSimuConfig.os_name
os_name
Definition: sick_scan_xd_simu_cfg.py:33
sick_scan_xd_simu_cfg.SickScanXdSimuConfig.sick_scan_xd_laserscan_topics
sick_scan_xd_laserscan_topics
Definition: sick_scan_xd_simu_cfg.py:40
sick_scan_xd_simu_cfg.SickScanXdSimuConfig.save_messages_jsonfile
save_messages_jsonfile
Definition: sick_scan_xd_simu_cfg.py:43
nav_msgs::Path
::nav_msgs::Path_< std::allocator< void > > Path
Definition: Path.h:56
sick_scan_xd_simu_cfg.SickScanXdSimuConfig.config_file
config_file
Definition: sick_scan_xd_simu_cfg.py:22
sick_scan_xd_simu_cfg.SickScanXdSimuConfig.cmd_udp_scandata_sender
cmd_udp_scandata_sender
Definition: sick_scan_xd_simu_cfg.py:87
sick_scan_xd_simu_cfg.SickScanXdSimuConfig.cmd_sick_scan_xd
cmd_sick_scan_xd
Definition: sick_scan_xd_simu_cfg.py:86
sick_scan_xd_simu_cfg.SickScanXdSimuConfig.cmd_shutdown
cmd_shutdown
Definition: sick_scan_xd_simu_cfg.py:88
sick_scan_xd_simu_cfg.SickScanXdSimuConfig.log_folder
log_folder
Definition: sick_scan_xd_simu_cfg.py:23
sick_scan_xd_simu_cfg.SickScanXdSimuConfig.report_md_filename
report_md_filename
Definition: sick_scan_xd_simu_cfg.py:37
sick_scan_xd_simu_cfg.SickScanXdSimuConfig.error_messages
error_messages
Definition: sick_scan_xd_simu_cfg.py:38
roswrap::names::append
ROSCPP_DECL std::string append(const std::string &left, const std::string &right)
Append one name to another.
sick_scan_xd_simu_cfg.SickScanXdSimuConfig.ros_version
ros_version
Definition: sick_scan_xd_simu_cfg.py:34
sick_scan_xd_simu_cfg.SickScanXdSimuConfig.sick_scan_xd_pointcloud_topics
sick_scan_xd_pointcloud_topics
Definition: sick_scan_xd_simu_cfg.py:39
sick_scan_xd_simu_cfg.SickScanXdSimuConfig.run_simu_seconds_before_shutdown
run_simu_seconds_before_shutdown
Definition: sick_scan_xd_simu_cfg.py:44
sick_scan_xd_simu_cfg.SickScanXdSimuConfig
Definition: sick_scan_xd_simu_cfg.py:8


sick_scan_xd
Author(s): Michael Lehning , Jochen Sprickerhof , Martin Günther
autogenerated on Fri Oct 25 2024 02:47:11