mirror of
https://gitee.com/mirrors_PX4/PX4-Autopilot.git
synced 2026-10-21 05:33:35 +08:00
rtps: add uorb_rtps_classifier.py iot classify RTPS topics as "send", "receive" or "ignore"
This commit is contained in:
@@ -44,6 +44,7 @@ import argparse
|
||||
import shutil
|
||||
import px_generate_uorb_topic_files
|
||||
import px_generate_uorb_topic_helper
|
||||
import uorb_rtps_classifier
|
||||
import subprocess
|
||||
import glob
|
||||
import errno
|
||||
@@ -54,48 +55,17 @@ except ImportError:
|
||||
"Failed to import yaml. You may need to install it with 'sudo pip install pyyaml'")
|
||||
|
||||
|
||||
def get_absolute_path(arg_parse_dir):
|
||||
root_path = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
|
||||
|
||||
if isinstance(arg_parse_dir, list):
|
||||
dir = arg_parse_dir[0]
|
||||
else:
|
||||
dir = arg_parse_dir
|
||||
|
||||
if dir[0] != '/':
|
||||
dir = root_path + "/" + dir
|
||||
|
||||
return dir
|
||||
|
||||
|
||||
def parse_yaml_msg_id_file(yaml_file):
|
||||
def check_rtps_id_uniqueness(classifier):
|
||||
"""
|
||||
Parses a yaml file into a dict
|
||||
Checks if:
|
||||
1. there are no ID's for different msgs repeated on the map
|
||||
2. the same msg is set to be sent/received and unclassified at the same time
|
||||
"""
|
||||
try:
|
||||
with open(yaml_file, 'r') as f:
|
||||
return yaml.load(f)
|
||||
except OSError as e:
|
||||
if e.errno == errno.ENOENT:
|
||||
raise IOError(errno.ENOENT, os.strerror(errno.ENOENT), yaml_file)
|
||||
else:
|
||||
raise
|
||||
msgs_to_use = classifier.msgs_to_send
|
||||
msgs_to_use.update(classifier.msgs_to_receive)
|
||||
msgs_to_ignore = classifier.msgs_to_ignore
|
||||
|
||||
|
||||
def get_used_rtps_ids(msg_id_map):
|
||||
msg_ids = {}
|
||||
for dict in msg_id_map['rtps']:
|
||||
msg_ids.update({dict['msg']: dict['id']})
|
||||
return msg_ids
|
||||
|
||||
|
||||
def check_rtps_id_uniqueness(msg_id_map):
|
||||
"""
|
||||
Checks if there are no ID's repeated on the map
|
||||
"""
|
||||
msg_ids = get_used_rtps_ids(msg_id_map)
|
||||
|
||||
used_ids = msg_ids.values()
|
||||
used_ids = msgs_to_use.values()
|
||||
used_ids.sort()
|
||||
|
||||
repeated_keys = dict()
|
||||
@@ -111,9 +81,9 @@ def check_rtps_id_uniqueness(msg_id_map):
|
||||
", ".join('%d' % id for id in px_generate_uorb_topic_helper.check_available_ids(used_ids)))
|
||||
|
||||
|
||||
default_client_out = get_absolute_path(
|
||||
default_client_out = px_generate_uorb_topic_helper.get_absolute_path(
|
||||
"src/modules/micrortps_bridge/micrortps_client")
|
||||
default_agent_out = get_absolute_path(
|
||||
default_agent_out = px_generate_uorb_topic_helper.get_absolute_path(
|
||||
"src/modules/micrortps_bridge/micrortps_agent")
|
||||
default_uorb_templates_dir = "templates/uorb_microcdr"
|
||||
default_urtps_templates_dir = "templates/urtps"
|
||||
@@ -122,10 +92,6 @@ default_package_name = px_generate_uorb_topic_files.PACKAGE
|
||||
|
||||
parser = argparse.ArgumentParser()
|
||||
|
||||
parser.add_argument("-s", "--send", dest='send', metavar='*.msg',
|
||||
type=str, nargs='+', help="Topics to be sended")
|
||||
parser.add_argument("-r", "--receive", dest='receive', metavar='*.msg',
|
||||
type=str, nargs='+', help="Topics to be received")
|
||||
parser.add_argument("-a", "--agent", dest='agent', action="store_true",
|
||||
help="Flag for generate the agent, by default is true if -c is not specified")
|
||||
parser.add_argument("-c", "--client", dest='client', action="store_true",
|
||||
@@ -141,11 +107,11 @@ parser.add_argument("-l", "--generate-cmakelists", dest='cmakelists',
|
||||
parser.add_argument("-t", "--topic-msg-dir", dest='msgdir', type=str,
|
||||
help="Topics message dir, by default msg/", default="msg")
|
||||
parser.add_argument("-b", "--uorb-templates-dir", dest='uorb_templates', type=str,
|
||||
help="uORB templates dir, by default msg_dir/templates/uorb_microcdr", default=default_uorb_templates_dir)
|
||||
help="uORB templates dir, by default msgdir/templates/uorb_microcdr", default=default_uorb_templates_dir)
|
||||
parser.add_argument("-q", "--urtps-templates-dir", dest='urtps_templates', type=str,
|
||||
help="uRTPS templates dir, by default msg_dir/templates/urtps", default=default_urtps_templates_dir)
|
||||
help="uRTPS templates dir, by default msgdir/templates/urtps", default=default_urtps_templates_dir)
|
||||
parser.add_argument("-y", "--rtps-ids-file", dest='yaml_file', type=str,
|
||||
help="RTPS msg IDs definition file, relative to the msg_dir, by default tools/uorb_rtps_message_ids.yaml", default=default_rtps_id_file)
|
||||
help="RTPS msg IDs definition file, relative to the msgdir, by default tools/uorb_rtps_message_ids.yaml", default=default_rtps_id_file)
|
||||
parser.add_argument("-p", "--package", dest='package', type=str,
|
||||
help="Msg package naming, by default px4", default=default_package_name)
|
||||
parser.add_argument("-o", "--agent-outdir", dest='agentdir', type=str, nargs=1,
|
||||
@@ -166,13 +132,7 @@ if len(sys.argv) <= 1:
|
||||
|
||||
# Parse arguments
|
||||
args = parser.parse_args()
|
||||
msg_folder = get_absolute_path(args.msgdir)
|
||||
msg_files_send = []
|
||||
msg_files_receive = []
|
||||
if args.send:
|
||||
msg_files_send = [get_absolute_path(msg) for msg in args.send]
|
||||
if args.receive:
|
||||
msg_files_receive = [get_absolute_path(msg) for msg in args.receive]
|
||||
msg_folder = px_generate_uorb_topic_helper.get_absolute_path(args.msgdir)
|
||||
package = args.package
|
||||
agent = args.agent
|
||||
client = args.client
|
||||
@@ -181,12 +141,13 @@ cmakelists = args.cmakelists
|
||||
del_tree = args.del_tree
|
||||
px_generate_uorb_topic_files.append_to_include_path(
|
||||
{msg_folder}, px_generate_uorb_topic_files.INCL_DEFAULT, package)
|
||||
agent_out_dir = get_absolute_path(args.agentdir)
|
||||
client_out_dir = get_absolute_path(args.clientdir)
|
||||
agent_out_dir = px_generate_uorb_topic_helper.get_absolute_path(args.agentdir)
|
||||
client_out_dir = px_generate_uorb_topic_helper.get_absolute_path(
|
||||
args.clientdir)
|
||||
gen_idl = args.gen_idl
|
||||
idl_dir = args.idl_dir
|
||||
if idl_dir != '':
|
||||
idl_dir = get_absolute_path(args.idl_dir)
|
||||
idl_dir = px_generate_uorb_topic_helper.get_absolute_path(args.idl_dir)
|
||||
else:
|
||||
idl_dir = os.path.join(agent_out_dir, "idl")
|
||||
|
||||
@@ -196,11 +157,12 @@ if args.fastrtpsgen is None or args.fastrtpsgen == "":
|
||||
else:
|
||||
# Path to fastrtpsgen is explicitly specified
|
||||
fastrtpsgen_path = os.path.join(
|
||||
get_absolute_path(args.fastrtpsgen), "/fastrtpsgen")
|
||||
px_generate_uorb_topic_helper.get_absolute_path(args.fastrtpsgen), "/fastrtpsgen")
|
||||
fastrtpsgen_include = args.fastrtpsgen_include
|
||||
if fastrtpsgen_include is not None and fastrtpsgen_include != '':
|
||||
fastrtpsgen_include = "-I " + \
|
||||
get_absolute_path(args.fastrtpsgen_include) + " "
|
||||
px_generate_uorb_topic_helper.get_absolute_path(
|
||||
args.fastrtpsgen_include) + " "
|
||||
|
||||
# If nothing specified it's generated both
|
||||
if agent == False and client == False:
|
||||
@@ -234,70 +196,70 @@ if agent and os.path.isdir(os.path.join(agent_out_dir, "idl")):
|
||||
uorb_templates_dir = os.path.join(msg_folder, args.uorb_templates)
|
||||
urtps_templates_dir = os.path.join(msg_folder, args.urtps_templates)
|
||||
# parse yaml file into a map of ids
|
||||
rtps_ids = parse_yaml_msg_id_file(os.path.join(msg_folder, args.yaml_file))
|
||||
classifier = Classifier(px_generate_uorb_topic_helper.parse_yaml_msg_id_file(os.path.join(msg_folder, args.yaml_file), msg_folder)
|
||||
# check if there are no ID's repeated
|
||||
check_rtps_id_uniqueness(rtps_ids)
|
||||
check_rtps_id_uniqueness(classifier)
|
||||
|
||||
|
||||
uRTPS_CLIENT_TEMPL_FILE = 'microRTPS_client.cpp.template'
|
||||
uRTPS_AGENT_TOPICS_H_TEMPL_FILE = 'RtpsTopics.h.template'
|
||||
uRTPS_AGENT_TOPICS_SRC_TEMPL_FILE = 'RtpsTopics.cpp.template'
|
||||
uRTPS_AGENT_TEMPL_FILE = 'microRTPS_agent.cpp.template'
|
||||
uRTPS_AGENT_CMAKELISTS_TEMPL_FILE = 'microRTPS_agent_CMakeLists.txt.template'
|
||||
uRTPS_PUBLISHER_SRC_TEMPL_FILE = 'Publisher.cpp.template'
|
||||
uRTPS_PUBLISHER_H_TEMPL_FILE = 'Publisher.h.template'
|
||||
uRTPS_SUBSCRIBER_SRC_TEMPL_FILE = 'Subscriber.cpp.template'
|
||||
uRTPS_SUBSCRIBER_H_TEMPL_FILE = 'Subscriber.h.template'
|
||||
uRTPS_CLIENT_TEMPL_FILE='microRTPS_client.cpp.template'
|
||||
uRTPS_AGENT_TOPICS_H_TEMPL_FILE='RtpsTopics.h.template'
|
||||
uRTPS_AGENT_TOPICS_SRC_TEMPL_FILE='RtpsTopics.cpp.template'
|
||||
uRTPS_AGENT_TEMPL_FILE='microRTPS_agent.cpp.template'
|
||||
uRTPS_AGENT_CMAKELISTS_TEMPL_FILE='microRTPS_agent_CMakeLists.txt.template'
|
||||
uRTPS_PUBLISHER_SRC_TEMPL_FILE='Publisher.cpp.template'
|
||||
uRTPS_PUBLISHER_H_TEMPL_FILE='Publisher.h.template'
|
||||
uRTPS_SUBSCRIBER_SRC_TEMPL_FILE='Subscriber.cpp.template'
|
||||
uRTPS_SUBSCRIBER_H_TEMPL_FILE='Subscriber.h.template'
|
||||
|
||||
|
||||
def generate_agent(out_dir):
|
||||
|
||||
if msg_files_send:
|
||||
for msg_file in msg_files_send:
|
||||
if classifier.msg_files_send:
|
||||
for msg_file in classifier.msg_files_send:
|
||||
if gen_idl:
|
||||
if out_dir != agent_out_dir:
|
||||
px_generate_uorb_topic_files.generate_idl_file(msg_file, os.path.join(out_dir, "/idl"), urtps_templates_dir,
|
||||
package, px_generate_uorb_topic_files.INCL_DEFAULT, rtps_ids)
|
||||
package, px_generate_uorb_topic_files.INCL_DEFAULT, classifier.msg_id_map)
|
||||
else:
|
||||
px_generate_uorb_topic_files.generate_idl_file(msg_file, idl_dir, urtps_templates_dir,
|
||||
package, px_generate_uorb_topic_files.INCL_DEFAULT, rtps_ids)
|
||||
package, px_generate_uorb_topic_files.INCL_DEFAULT, classifier.msg_id_map)
|
||||
px_generate_uorb_topic_files.generate_topic_file(msg_file, out_dir, urtps_templates_dir,
|
||||
package, px_generate_uorb_topic_files.INCL_DEFAULT, rtps_ids, uRTPS_PUBLISHER_SRC_TEMPL_FILE)
|
||||
package, px_generate_uorb_topic_files.INCL_DEFAULT, classifier.msg_id_map, uRTPS_PUBLISHER_SRC_TEMPL_FILE)
|
||||
px_generate_uorb_topic_files.generate_topic_file(msg_file, out_dir, urtps_templates_dir,
|
||||
package, px_generate_uorb_topic_files.INCL_DEFAULT, rtps_ids, uRTPS_PUBLISHER_H_TEMPL_FILE)
|
||||
package, px_generate_uorb_topic_files.INCL_DEFAULT, classifier.msg_id_map, uRTPS_PUBLISHER_H_TEMPL_FILE)
|
||||
|
||||
if msg_files_receive:
|
||||
for msg_file in msg_files_receive:
|
||||
if classifier.msg_files_receive:
|
||||
for msg_file in classifier.msg_files_receive:
|
||||
if gen_idl:
|
||||
if out_dir != agent_out_dir:
|
||||
px_generate_uorb_topic_files.generate_idl_file(msg_file, os.path.join(out_dir, "/idl"), urtps_templates_dir,
|
||||
package, px_generate_uorb_topic_files.INCL_DEFAULT, rtps_ids)
|
||||
package, px_generate_uorb_topic_files.INCL_DEFAULT, classifier.msg_id_map)
|
||||
else:
|
||||
px_generate_uorb_topic_files.generate_idl_file(msg_file, idl_dir, urtps_templates_dir,
|
||||
package, px_generate_uorb_topic_files.INCL_DEFAULT, rtps_ids)
|
||||
package, px_generate_uorb_topic_files.INCL_DEFAULT, classifier.msg_id_map)
|
||||
px_generate_uorb_topic_files.generate_topic_file(msg_file, out_dir, urtps_templates_dir,
|
||||
package, px_generate_uorb_topic_files.INCL_DEFAULT, rtps_ids, uRTPS_SUBSCRIBER_SRC_TEMPL_FILE)
|
||||
package, px_generate_uorb_topic_files.INCL_DEFAULT, classifier.msg_id_map, uRTPS_SUBSCRIBER_SRC_TEMPL_FILE)
|
||||
px_generate_uorb_topic_files.generate_topic_file(msg_file, out_dir, urtps_templates_dir,
|
||||
package, px_generate_uorb_topic_files.INCL_DEFAULT, rtps_ids, uRTPS_SUBSCRIBER_H_TEMPL_FILE)
|
||||
package, px_generate_uorb_topic_files.INCL_DEFAULT, classifier.msg_id_map, uRTPS_SUBSCRIBER_H_TEMPL_FILE)
|
||||
|
||||
px_generate_uorb_topic_files.generate_uRTPS_general(msg_files_send, msg_files_receive, out_dir, urtps_templates_dir,
|
||||
package, px_generate_uorb_topic_files.INCL_DEFAULT, rtps_ids, uRTPS_AGENT_TEMPL_FILE)
|
||||
px_generate_uorb_topic_files.generate_uRTPS_general(msg_files_send, msg_files_receive, out_dir, urtps_templates_dir,
|
||||
package, px_generate_uorb_topic_files.INCL_DEFAULT, rtps_ids, uRTPS_AGENT_TOPICS_H_TEMPL_FILE)
|
||||
px_generate_uorb_topic_files.generate_uRTPS_general(msg_files_send, msg_files_receive, out_dir, urtps_templates_dir,
|
||||
package, px_generate_uorb_topic_files.INCL_DEFAULT, rtps_ids, uRTPS_AGENT_TOPICS_SRC_TEMPL_FILE)
|
||||
px_generate_uorb_topic_files.generate_uRTPS_general(classifier.msg_files_send, classifier.msg_files_receive, out_dir, urtps_templates_dir,
|
||||
package, px_generate_uorb_topic_files.INCL_DEFAULT, classifier.msg_id_map, uRTPS_AGENT_TEMPL_FILE)
|
||||
px_generate_uorb_topic_files.generate_uRTPS_general(classifier.msg_files_send, classifier.msg_files_receive, out_dir, urtps_templates_dir,
|
||||
package, px_generate_uorb_topic_files.INCL_DEFAULT, classifier.msg_id_map, uRTPS_AGENT_TOPICS_H_TEMPL_FILE)
|
||||
px_generate_uorb_topic_files.generate_uRTPS_general(classifier.msg_files_send, classifier.msg_files_receive, out_dir, urtps_templates_dir,
|
||||
package, px_generate_uorb_topic_files.INCL_DEFAULT, classifier.msg_id_map, uRTPS_AGENT_TOPICS_SRC_TEMPL_FILE)
|
||||
if cmakelists:
|
||||
px_generate_uorb_topic_files.generate_uRTPS_general(msg_files_send, msg_files_receive, out_dir, urtps_templates_dir,
|
||||
package, px_generate_uorb_topic_files.INCL_DEFAULT, rtps_ids, uRTPS_AGENT_CMAKELISTS_TEMPL_FILE)
|
||||
px_generate_uorb_topic_files.generate_uRTPS_general(classifier.msg_files_send, classifier.msg_files_receive, out_dir, urtps_templates_dir,
|
||||
package, px_generate_uorb_topic_files.INCL_DEFAULT, classifier.msg_id_map, uRTPS_AGENT_CMAKELISTS_TEMPL_FILE)
|
||||
|
||||
# Final steps to install agent
|
||||
mkdir_p(os.path.join(out_dir, "fastrtpsgen"))
|
||||
prev_cwd_path = os.getcwd()
|
||||
prev_cwd_path=os.getcwd()
|
||||
os.chdir(os.path.join(out_dir, "fastrtpsgen"))
|
||||
if not glob.glob(os.path.join(idl_dir, "*.idl")):
|
||||
raise Exception("No IDL files found in %s" % idl_dir)
|
||||
for idl_file in glob.glob(os.path.join(idl_dir, "*.idl")):
|
||||
ret = subprocess.call(fastrtpsgen_path + " -d " + out_dir +
|
||||
ret=subprocess.call(fastrtpsgen_path + " -d " + out_dir +
|
||||
"/fastrtpsgen -example x64Linux2.6gcc " + fastrtpsgen_include + idl_file, shell=True)
|
||||
if ret:
|
||||
raise Exception(
|
||||
@@ -346,18 +308,18 @@ def generate_client(out_dir):
|
||||
|
||||
# Rename work in the default path
|
||||
if default_client_out != out_dir:
|
||||
def_file = os.path.join(default_client_out, "microRTPS_client.cpp")
|
||||
def_file=os.path.join(default_client_out, "microRTPS_client.cpp")
|
||||
if os.path.isfile(def_file):
|
||||
os.rename(def_file, def_file.replace(".cpp", ".cpp_"))
|
||||
def_file = os.path.join(default_client_out, "microRTPS_transport.cpp")
|
||||
def_file=os.path.join(default_client_out, "microRTPS_transport.cpp")
|
||||
if os.path.isfile(def_file):
|
||||
os.rename(def_file, def_file.replace(".cpp", ".cpp_"))
|
||||
def_file = os.path.join(default_client_out, "microRTPS_transport.h")
|
||||
def_file=os.path.join(default_client_out, "microRTPS_transport.h")
|
||||
if os.path.isfile(def_file):
|
||||
os.rename(def_file, def_file.replace(".h", ".h_"))
|
||||
|
||||
px_generate_uorb_topic_files.generate_uRTPS_general(msg_files_send, msg_files_receive, out_dir, uorb_templates_dir,
|
||||
package, px_generate_uorb_topic_files.INCL_DEFAULT, rtps_ids, uRTPS_CLIENT_TEMPL_FILE)
|
||||
px_generate_uorb_topic_files.generate_uRTPS_general(classifier.msg_files_send, classifier.msg_files_receive, out_dir, uorb_templates_dir,
|
||||
package, px_generate_uorb_topic_files.INCL_DEFAULT, classifier.msg_id_map, uRTPS_CLIENT_TEMPL_FILE)
|
||||
|
||||
# Final steps to install client
|
||||
cp_wildcard(os.path.join(urtps_templates_dir,
|
||||
|
||||
Reference in New Issue
Block a user