Skip to content

Commit

Permalink
Add Zenoh pico support
Browse files Browse the repository at this point in the history
  • Loading branch information
PetervdPerk-NXP authored and dagar committed Oct 18, 2023
1 parent 5137ca1 commit 019d232
Show file tree
Hide file tree
Showing 38 changed files with 3,479 additions and 3 deletions.
10 changes: 10 additions & 0 deletions .gitmodules
Original file line number Diff line number Diff line change
Expand Up @@ -62,3 +62,13 @@
path = src/modules/uxrce_dds_client/Micro-XRCE-DDS-Client
url = https://github.com/PX4/Micro-XRCE-DDS-Client.git
branch = px4
[submodule "src/lib/cdrstream/cyclonedds"]
path = src/lib/cdrstream/cyclonedds
url = https://github.com/px4/cyclonedds
[submodule "src/lib/cdrstream/rosidl"]
path = src/lib/cdrstream/rosidl
url = https://github.com/px4/rosidl
[submodule "src/modules/zenoh/zenoh-pico"]
path = src/modules/zenoh/zenoh-pico
url = https://github.com/px4/zenoh-pico
branch = pr-zubf-werror-fix
2 changes: 2 additions & 0 deletions Kconfig
Original file line number Diff line number Diff line change
Expand Up @@ -205,3 +205,5 @@ menu "platforms"
depends on PLATFORM_QURT || PLATFORM_POSIX
source "platforms/common/Kconfig"
endmenu

source "src/lib/*/Kconfig"
4 changes: 4 additions & 0 deletions ROMFS/px4fmu_common/init.d/rcS
Original file line number Diff line number Diff line change
Expand Up @@ -537,6 +537,10 @@ else
cyphal start
fi
fi
if param greater -s ZENOH_ENABLE 0
then
zenoh start
fi

#
# End of autostart.
Expand Down
3 changes: 3 additions & 0 deletions Tools/astyle/files_to_check_code_style.sh
Original file line number Diff line number Diff line change
Expand Up @@ -26,4 +26,7 @@ exec find boards msg src platforms test \
-path src/lib/crypto/libtomcrypt -prune -o \
-path src/lib/crypto/libtommath -prune -o \
-path src/modules/uxrce_dds_client/Micro-XRCE-DDS-Client -prune -o \
-path src/lib/cdrstream/cyclonedds -prune -o \
-path src/lib/cdrstream/rosidl -prune -o \
-path src/drivers/zenoh/zenoh-pico -prune -o \
-type f \( -name "*.c" -o -name "*.h" -o -name "*.cpp" -o -name "*.hpp" \) | grep $PATTERN
16 changes: 13 additions & 3 deletions Tools/msg/px_generate_uorb_topic_files.py
Original file line number Diff line number Diff line change
Expand Up @@ -70,9 +70,9 @@
__email__ = "[email protected]"


TEMPLATE_FILE = ['msg.h.em', 'msg.cpp.em']
TEMPLATE_FILE = ['msg.h.em', 'msg.cpp.em', 'uorb_idl_header.h.em']
TOPICS_LIST_TEMPLATE_FILE = ['uORBTopics.hpp.em', 'uORBTopics.cpp.em']
OUTPUT_FILE_EXT = ['.h', '.cpp']
OUTPUT_FILE_EXT = ['.h', '.cpp', '.h']
INCL_DEFAULT = ['std_msgs:./msg/std_msgs']
PACKAGE = 'px4'
TOPICS_TOKEN = '# TOPICS '
Expand Down Expand Up @@ -150,6 +150,7 @@ def generate_output_from_file(format_idx, filename, outputdir, package, template
em_globals = {
"name_snake_case": full_type_name_snake,
"file_name_in": filename,
"file_base_name": file_base_name,
"search_path": search_path,
"msg_context": msg_context,
"spec": spec,
Expand All @@ -161,7 +162,10 @@ def generate_output_from_file(format_idx, filename, outputdir, package, template
os.makedirs(outputdir)

template_file = os.path.join(templatedir, TEMPLATE_FILE[format_idx])
output_file = os.path.join(outputdir, full_type_name_snake + OUTPUT_FILE_EXT[format_idx])
if format_idx == 2:
output_file = os.path.join(outputdir, file_base_name + OUTPUT_FILE_EXT[format_idx])
else:
output_file = os.path.join(outputdir, full_type_name_snake + OUTPUT_FILE_EXT[format_idx])

return generate_by_template(output_file, template_file, em_globals)

Expand Down Expand Up @@ -217,6 +221,7 @@ def append_to_include_path(path_to_append, curr_include, package):
parser = argparse.ArgumentParser(description='Convert msg files to uorb headers/sources')
parser.add_argument('--headers', help='Generate header files', action='store_true')
parser.add_argument('--sources', help='Generate source files', action='store_true')
parser.add_argument('--uorb-idl-header', help='Generate uORB compatible idl header', action='store_true')
parser.add_argument('-f', dest='file',
help="files to convert (use only without -d)",
nargs="+")
Expand All @@ -241,6 +246,11 @@ def append_to_include_path(path_to_append, curr_include, package):
generate_idx = 0
elif args.sources:
generate_idx = 1
elif args.uorb_idl_header:
for f in args.file:
print(f)
generate_output_from_file(2, f, args.outputdir, args.package, args.templatedir, INCL_DEFAULT)
exit(0)
else:
print('Error: either --headers or --sources must be specified')
exit(-1)
Expand Down
65 changes: 65 additions & 0 deletions Tools/msg/templates/cdrstream/uorb_idl_header.h.em
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
@{
import genmsg.msgs
import re

from px_generate_uorb_topic_helper import * # this is in Tools/

uorb_struct = '%s_s'%name_snake_case
uorb_struct_upper = name_snake_case.upper()
}@

/****************************************************************

PX4 Cyclone DDS IDL to C Translator compatible idl struct
Source: @file_name_in
Compatible with Cyclone DDS: V0.11.0

*****************************************************************/
#ifndef DDSC_IDL_UORB_@(uorb_struct_upper)_H
#define DDSC_IDL_UORB_@(uorb_struct_upper)_H

#include "dds/ddsc/dds_public_impl.h"
#include "dds/cdr/dds_cdrstream.h"
#include <uORB/topics/@(name_snake_case).h>

@##############################
@# Includes for dependencies
@##############################
@{
for field in spec.parsed_fields():
if (not field.is_builtin):
if (not field.is_header):
(package, name) = genmsg.names.package_resource_name(field.base_type)
package = package or spec.package # convert '' to package

print('#include "%s.h"'%(name))
name = re.sub(r'(?<!^)(?=[A-Z])', '_', name).lower()
print('#include <uORB/topics/%s.h>'%(name))
}@


#ifdef __cplusplus
extern "C" {
#endif

@{
for field in spec.parsed_fields():
if (not field.is_builtin):
if (not field.is_header):
(package, name) = genmsg.names.package_resource_name(field.base_type)
package = package or spec.package # convert '' to package

print('typedef px4_msg_%s px4_msg_px4__msg__%s;' % (name,name))
}@



typedef struct @uorb_struct px4_msg_@(file_base_name);

extern const struct dds_cdrstream_desc px4_msg_@(file_base_name)_cdrstream_desc;

#ifdef __cplusplus
}
#endif

#endif /* DDSC_IDL_UORB_@(uorb_struct_upper)_H */
175 changes: 175 additions & 0 deletions Tools/zenoh/px_generate_zenoh_topic_files.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,175 @@
#!/usr/bin/env python3
#############################################################################
#
# Copyright (C) 2013-2022 PX4 Pro Development Team. All rights reserved.
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions
# are met:
#
# 1. Redistributions of source code must retain the above copyright
# notice, this list of conditions and the following disclaimer.
# 2. Redistributions in binary form must reproduce the above copyright
# notice, this list of conditions and the following disclaimer in
# the documentation and/or other materials provided with the
# distribution.
# 3. Neither the name PX4 nor the names of its contributors may be
# used to endorse or promote products derived from this software
# without specific prior written permission.
#
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
# FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
# COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
# INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
# BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
# OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
# AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
# ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
# POSSIBILITY OF SUCH DAMAGE.
#
#############################################################################

"""
px_generate_zenoh_topic_files.py
Generates c/cpp header/source files for use with zenoh
message files
"""

import os
import argparse
import re
import sys

try:
import em
except ImportError as e:
print("Failed to import em: " + str(e))
print("")
print("You may need to install it using:")
print(" pip3 install --user empy")
print("")
sys.exit(1)

try:
import genmsg.template_tools
except ImportError as e:
print("Failed to import genmsg: " + str(e))
print("")
print("You may need to install it using:")
print(" pip3 install --user pyros-genmsg")
print("")
sys.exit(1)


__author__ = "Sergey Belash, Thomas Gubler, Beat Kueng"
__copyright__ = "Copyright (C) 2013-2022 PX4 Development Team."
__license__ = "BSD"
__email__ = "[email protected]"

ZENOH_TEMPLATE_FILE = ['Kconfig.topics.em', 'uorb_pubsub_factory.hpp.em']
TOPICS_TOKEN = '# TOPICS '


def get_topics(filename):
"""
Get TOPICS names from a "# TOPICS" line
"""
ofile = open(filename, 'r')
text = ofile.read()
result = []
for each_line in text.split('\n'):
if each_line.startswith(TOPICS_TOKEN):
topic_names_str = each_line.strip()
topic_names_str = topic_names_str.replace(TOPICS_TOKEN, "")
topic_names_list = topic_names_str.split(" ")
for topic in topic_names_list:
# topic name PascalCase (file name) to snake_case (topic name)
topic_name = re.sub(r'(?<!^)(?=[A-Z])', '_', topic).lower()
result.append(topic_name)
ofile.close()

if len(result) == 0:
# topic name PascalCase (file name) to snake_case (topic name)
file_base_name = os.path.basename(filename).replace(".msg", "")
topic_name = re.sub(r'(?<!^)(?=[A-Z])', '_', file_base_name).lower()
result.append(topic_name)

return result

def generate_by_template(output_file, template_file, em_globals):
"""
Invokes empy intepreter to geneate output_file by the
given template_file and predefined em_globals dict
"""
# check if folder exists:
folder_name = os.path.dirname(output_file)
if not os.path.exists(folder_name):
os.makedirs(folder_name)

ofile = open(output_file, 'w')
# todo, reuse interpreter
interpreter = em.Interpreter(output=ofile, globals=em_globals, options={
em.RAW_OPT: True, em.BUFFERED_OPT: True})
try:
interpreter.file(open(template_file))
except OSError as e:
ofile.close()
os.remove(output_file)
raise
interpreter.shutdown()
ofile.close()
return True


def generate_topics_list_file_from_files(files, outputdir, template_filename, templatedir):
# generate cpp file with topics list
filenames = []
for filename in [os.path.basename(p) for p in files if os.path.basename(p).endswith(".msg")]:
filenames.append(re.sub(r'(?<!^)(?=[A-Z])', '_', filename).lower())

datatypes = []
for filename in [os.path.basename(p) for p in files if os.path.basename(p).endswith(".msg")]:
datatypes.append(re.sub(r'(?<!^)(?=[A-Z])', '_', filename).lower().replace(".msg",""))

full_base_names = []
for filename in [os.path.basename(p) for p in files if os.path.basename(p).endswith(".msg")]:
full_base_names.append(filename.replace(".msg",""))

topics = []
for msg_filename in files:
topics.extend(get_topics(msg_filename))

tl_globals = {"msgs": filenames, "topics": topics, "datatypes": datatypes, "full_base_names": full_base_names}
tl_template_file = os.path.join(templatedir, template_filename)
tl_out_file = os.path.join(outputdir, template_filename.replace(".em", ""))

generate_by_template(tl_out_file, tl_template_file, tl_globals)

if __name__ == "__main__":
parser = argparse.ArgumentParser(description='Convert msg files to uorb headers/sources')
parser.add_argument('--zenoh-config', help='Generate Zenoh Kconfig file', action='store_true')
parser.add_argument('--zenoh-pub-sub', help='Generate Zenoh Pubsub factory', action='store_true')
parser.add_argument('-f', dest='file',
help="files to convert (use only without -d)",
nargs="+")
parser.add_argument('-e', dest='templatedir',
help='directory with template files',)
parser.add_argument('-o', dest='outputdir',
help='output directory for header files')
parser.add_argument('-p', dest='prefix', default='',
help='string added as prefix to the output file '
' name when converting directories')
args = parser.parse_args()

if args.zenoh_config:
generate_topics_list_file_from_files(args.file, args.outputdir, ZENOH_TEMPLATE_FILE[0], args.templatedir)
exit(0)
elif args.zenoh_pub_sub:
generate_topics_list_file_from_files(args.file, args.outputdir, ZENOH_TEMPLATE_FILE[1], args.templatedir)
exit(0)
else:
print('Error: either --headers or --sources must be specified')
exit(-1)
25 changes: 25 additions & 0 deletions Tools/zenoh/templates/zenoh/Kconfig.topics.em
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
@{
topics_count = len(topics)
topic_names_all = list(set(topics)) # set() filters duplicates
topic_names_all.sort()

datatypes = list(set(datatypes)) # set() filters duplicates
datatypes.sort()
}@

menu "Zenoh publishers/subscribers"
depends on MODULES_ZENOH
@[for idx, topic_name in enumerate(datatypes)]@
config ZENOH_PUBSUB_@(topic_name.upper())
bool "@(topic_name)"
default n

@[end for]

config ZENOH_PUBSUB_ALL_SELECTION
bool
default y if ZENOH_PUBSUB_ALL
@[for idx, topic_name in enumerate(datatypes)]@
select ZENOH_PUBSUB_@(topic_name.upper())
@[end for]
endmenu
Loading

0 comments on commit 019d232

Please sign in to comment.