Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
93 changes: 93 additions & 0 deletions sygnal_dbc/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
# Copyright (c) 2025-present Polymath Robotics, Inc. All rights reserved
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
cmake_minimum_required(VERSION 3.5)

project(sygnal_dbc VERSION 0.1.0 LANGUAGES C)

find_package(ament_cmake REQUIRED)
find_package(Python3 COMPONENTS Interpreter REQUIRED)
include(GNUInstallDirs)

# Install the DBC database directory
install(
DIRECTORY database/
DESTINATION share/${PROJECT_NAME}/database
)


# Generate C sources/headers from all .dbc files under database/
file(GLOB_RECURSE DBC_FILES CONFIGURE_DEPENDS
"${CMAKE_CURRENT_SOURCE_DIR}/database/*.dbc")

set(GENERATED_C_SOURCES)
set(GENERATED_HEADERS)

foreach(DBC_FILE IN LISTS DBC_FILES)
# Compute path pieces to mirror the database/ layout under build dir
file(RELATIVE_PATH REL_PATH "${CMAKE_CURRENT_SOURCE_DIR}/database" "${DBC_FILE}")
get_filename_component(REL_DIR "${REL_PATH}" DIRECTORY)
get_filename_component(DBC_BASENAME_WE "${DBC_FILE}" NAME_WE)

# cantools generates lowercase file basenames; mirror that here
string(TOLOWER "${DBC_BASENAME_WE}" MOD_NAME)

# Generate directly into the build include tree so dependents
# can include headers during the same colcon invocation.
set(OUT_DIR "${CMAKE_CURRENT_BINARY_DIR}/include/${PROJECT_NAME}/${REL_DIR}")
set(OUT_C "${OUT_DIR}/${MOD_NAME}.c")
set(OUT_H "${OUT_DIR}/${MOD_NAME}.h")

add_custom_command(
OUTPUT ${OUT_C} ${OUT_H}
COMMAND ${CMAKE_COMMAND} -E make_directory ${OUT_DIR}
COMMAND ${Python3_EXECUTABLE} -m cantools generate_c_source ${DBC_FILE} -o ${OUT_DIR} --database-name ${MOD_NAME}
DEPENDS ${DBC_FILE}
COMMENT "Generating C source from DBC '${REL_PATH}' with cantools"
VERBATIM
)

list(APPEND GENERATED_C_SOURCES ${OUT_C})
list(APPEND GENERATED_HEADERS ${OUT_H})
endforeach()

add_library(${PROJECT_NAME} STATIC ${GENERATED_C_SOURCES})
target_include_directories(${PROJECT_NAME}
PUBLIC
$<BUILD_INTERFACE:${CMAKE_CURRENT_BINARY_DIR}/include>
$<INSTALL_INTERFACE:include>
)

# Ensure generation runs as part of normal build (sources already bind it)
add_custom_target(generate_can_sources ALL
DEPENDS ${GENERATED_C_SOURCES} ${GENERATED_HEADERS})

# Install generated headers so dependents can include them
install(
DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}/include/${PROJECT_NAME}/
DESTINATION include/${PROJECT_NAME}
FILES_MATCHING PATTERN "*.h"
)

install(
TARGETS ${PROJECT_NAME}
EXPORT export_${PROJECT_NAME}
ARCHIVE DESTINATION lib
LIBRARY DESTINATION lib
RUNTIME DESTINATION bin
)

# Export only modern CMake targets; include dirs and libs are already
# encoded on the exported target usage requirements above.
ament_export_targets(export_${PROJECT_NAME} HAS_LIBRARY_TARGET)
ament_package(CONFIG_EXTRAS "cmake/${PROJECT_NAME}-extras.cmake")
59 changes: 59 additions & 0 deletions sygnal_dbc/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
# sygnal_dbc

Provides Sygnal DBC files and a C library produced by cantools. The package
installs the raw DBC database and, at build time, generates `.c/.h` files for
each `.dbc` and exposes them via a static library for consumers.

## Contents
- Installed DBCs: `share/sygnal_dbc/database/...`
- Installed headers: `include/sygnal_dbc/<subdir>/<name>.h`
- Library: `sygnal_dbc`
- CMake var: `SYGNAL_DBC_DIR` → points to installed `database` directory

## Build
Prereqs:
- ROS 2 ament environment sourced
- Python 3 with cantools: `python3 -m pip install cantools`

Build only this package:

```
colcon build --packages-select sygnal_dbc
```

## Using in C++ (ament_cmake)
In your package CMakeLists.txt:

```
find_package(ament_cmake REQUIRED)
find_package(sygnal_dbc REQUIRED)

add_executable(my_node src/my_node.cpp)
ament_target_dependencies(my_node )
# Link the library (headers are exported)
target_link_libraries(my_node sygnal_dbc)
```

Include headers (lowercase file basenames, folder layout mirrors `database/`):

```
#include <sygnal_dbc/cb/configuration.h>
#include <sygnal_dbc/mcm/heartbeat.h>
```

The exact header names come from each DBC file name, lowercased.

## Accessing raw DBCs
After `find_package(sygnal_dbc)`, the CMake variable `SYGNAL_DBC_DIR` is set to
`<install-prefix>/share/sygnal_dbc/database`. Use it to locate raw `.dbc` files:

```
# Example: install a copy of a specific dbc next to your target
install(FILES "${SYGNAL_DBC_DIR}/cb/Configuration.dbc" DESTINATION share/${PROJECT_NAME}/dbc)
```

## Notes
- The installed headers mirror the folder structure under `database/`
(`cb/`, `io/`, `mcm/`, `vehicles/`).
- If you add or rename `.dbc` files, just rebuild; sources regenerate
automatically.
24 changes: 24 additions & 0 deletions sygnal_dbc/cmake/sygnal_dbc-extras.cmake
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
# Copyright (c) 2025-present Polymath Robotics, Inc. All rights reserved
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
# sygnal_dbc extras included by ament's find_package

# Determine the installed share directory from the location of this config
get_filename_component(_pkg_share_dir "${sygnal_dbc_DIR}/.." ABSOLUTE)

# Public variable for consumers to locate the DBC database
set(SYGNAL_DBC_DIR "${_pkg_share_dir}/database")

if(NOT sygnal_dbc_FIND_QUIETLY)
message(STATUS "sygnal_dbc: SYGNAL_DBC_DIR='${SYGNAL_DBC_DIR}'")
endif()
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
16 changes: 16 additions & 0 deletions sygnal_dbc/package.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<?xml version="1.0"?>
<package format="3">
<name>sygnal_dbc</name>
<version>0.0.0</version>
<description>DBC database package for Sygnal. Installs DBC files and exposes SYGNAL_DBC_DIR for consumers.</description>
<maintainer email="engineering@polymathrobotics.com">Polymath Engineering</maintainer>
<license>Apache-2.0</license>
<author email="zeerekahmad@hotmail.com">Zeerek Ahmad</author>

<buildtool_depend>ament_cmake</buildtool_depend>
<buildtool_depend>python3-cantools-pip</buildtool_depend>

<export>
<build_type>ament_cmake</build_type>
</export>
</package>