Skip to content

Commit

Permalink
Initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
andyblarblar authored Mar 23, 2024
0 parents commit 18aaccf
Show file tree
Hide file tree
Showing 10 changed files with 285 additions and 0 deletions.
27 changes: 27 additions & 0 deletions .clang-format
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
---
Language: Cpp
BasedOnStyle: Google

TabWidth: 2
IndentWidth: 4
AccessModifierOffset: -4
BraceWrapping:
AfterCaseLabel: false
AfterClass: false
AfterControlStatement: "false"
AfterEnum: false
AfterFunction: false
AfterNamespace: false
AfterObjCDeclaration: false
AfterStruct: false
AfterUnion: false
AfterExternBlock: false
BeforeCatch: false
BeforeElse: false
IndentBraces: false
BreakBeforeBraces: Custom
ColumnLimit: 120
DerivePointerAlignment: false
PointerAlignment: Left
ReflowComments: false
...
18 changes: 18 additions & 0 deletions .github/workflows/main.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
name: CI

on: [ push, pull_request ]

jobs:
industrial_ci:
runs-on: ubuntu-latest
strategy:
matrix:
env:
- { ROS_DISTRO: humble, ROS_REPO: testing }
- { ROS_DISTRO: humble, ROS_REPO: main }
name: ROS ${{ matrix.ROS_DISTRO }} (${{ matrix.ROS_REPO }})
steps:
- uses: actions/checkout@v3
- uses: 'ros-industrial/industrial_ci@master'
env: ${{ matrix.env }}

6 changes: 6 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
log
build
install
.vscode
.cache
compile_commands.json
73 changes: 73 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
cmake_minimum_required(VERSION 3.8)
project(TODO_PACKAGE_NAME)
include(FetchContent)

if (CMAKE_COMPILER_IS_GNUCXX OR CMAKE_CXX_COMPILER_ID MATCHES "Clang")
add_compile_options(-Wall -Wextra -Wpedantic)
endif ()

# find dependencies
find_package(ament_cmake REQUIRED)
find_package(rclcpp REQUIRED)
# Messages TODO_EXTRA
find_package(ackermann_msgs REQUIRED)
find_package(sensor_msgs REQUIRED)
find_package(std_msgs REQUIRED)
# OpenCV TODO_EXTRA
find_package(cv_bridge REQUIRED)
find_package(OpenCV 4.2.0 REQUIRED)

# Add source for node executable (link non-ros dependencies here)
add_executable(TODO_PACKAGE_NAME src/TODO_NODE_NAME.cpp src/TODO_NODE_NAME_node.cpp)
target_include_directories(TODO_PACKAGE_NAME PUBLIC
$<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/include>
$<INSTALL_INTERFACE:include>)
target_compile_features(TODO_PACKAGE_NAME PUBLIC c_std_99 cxx_std_17) # Require C99 and C++17

# Make ros deps a variable so they get linked to tests as well
set(dependencies
rclcpp
# Messages TODO_EXTRA
ackermann_msgs
sensor_msgs
std_msgs
# OpenCv TODO_EXTRA
cv_bridge
OpenCV
)

# Link ros dependencies
ament_target_dependencies(
TODO_PACKAGE_NAME
${dependencies}
)

install(TARGETS TODO_PACKAGE_NAME
DESTINATION lib/${PROJECT_NAME})

# Uncomment below to make launch files available if created
#install(
# DIRECTORY launch config
# DESTINATION share/${PROJECT_NAME}/
#)

if (BUILD_TESTING)
# Manually invoke clang format so it actually uses our file
find_package(ament_cmake_clang_format REQUIRED)
ament_clang_format(CONFIG_FILE ${CMAKE_CURRENT_SOURCE_DIR}/.clang-format)

find_package(ament_cmake_gtest REQUIRED)

# Add unit tests
ament_add_gtest(${PROJECT_NAME}-test
tests/unit.cpp
# Remember to add node source files
src/TODO_NODE_NAME_node.cpp
)
ament_target_dependencies(${PROJECT_NAME}-test ${dependencies})
target_include_directories(${PROJECT_NAME}-test PUBLIC
$<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/include>
$<INSTALL_INTERFACE:include>)
endif ()

ament_package()
47 changes: 47 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
An opinionated ROS2 C++ node template, optimised for ISC.

# Instructions

1. Clone repo inside your workspaces src directory (Ex. phnx_ws/src)
2. `rosdep install --from-paths . --ignore-src -r -y` to install deps
3. `colcon build` to make sure the repo builds before you mess with it
4. Replace the following in both file names and code exactly and consistently.
1. TODO_PACKAGE_NAME: Replace with the package name. Use snake case. Ex. `data_logger`
2. TODO_NODE_NAME: Replace with the node name. Use Pascal case. Ex. `DataLogger`
5. `colcon build` again. If it builds, you are done
6. Rename outer folder
7. Review the optional dependencies, and remove what you do not need

# Dependencies
Some common extra dependencies are included. Review them and remove what you don't need.
These are marked with TODO_EXTRA.

# Features

- Unit tests
- ROS-Industrial github CI (will test units and lints)
- C++ formatting via clangformat
- A selection of sane lints
- A single node setup in a multithreaded executor

# File structure

```
.
├── include
│   └── TODO_PACKAGE_NAME
│   └── TODO_NODE_NAME_node.hpp
├── package.xml
├── README.md
├── src
│   ├── TODO_NODE_NAME.cpp
│   └── TODO_NODE_NAME_node.cpp
└── tests
└── unit.cpp
```

TODO_NODE_NAME_node: Source files for the ROS2 node object itself, and only itself

TODO_NODE_NAME.cpp: Source for the main function of the node, and only the main function

tests/unit.cpp: Example file for unit tests. This is linked to the node and ros, so both can be used
17 changes: 17 additions & 0 deletions include/TODO_PACKAGE_NAME/TODO_NODE_NAME_node.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
#pragma once

#include "rclcpp/rclcpp.hpp"
#include "std_msgs/msg/string.hpp"

class TODO_NODE_NAME : public rclcpp::Node {
private:
rclcpp::Publisher<std_msgs::msg::String>::SharedPtr pub;

rclcpp::Subscription<std_msgs::msg::String>::SharedPtr sub;

public:
TODO_NODE_NAME(const rclcpp::NodeOptions& options);

/// subscriber callback
void sub_cb(std_msgs::msg::String::SharedPtr msg);
};
36 changes: 36 additions & 0 deletions package.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
<?xml version="1.0"?>
<?xml-model href="http://download.ros.org/schema/package_format3.xsd" schematypens="http://www.w3.org/2001/XMLSchema"?>
<package format="3">
<name>TODO_PACKAGE_NAME</name>
<version>0.1.0</version>
<description>A node template</description>
<maintainer email="avealov@umich.edu">Andrew Ealovega</maintainer>
<license>MIT</license>

<buildtool_depend>ament_cmake</buildtool_depend>

<depend>rclcpp</depend>

<!--Messages TODO_EXTRA-->
<depend>ackermann_msgs</depend>
<depend>sensor_msgs</depend>
<depend>std_msgs</depend>

<!--OpenCv Things TODO_EXTRA-->
<depend>cv_bridge</depend>
<depend>libopencv-dev</depend>

<test_depend>ament_lint_auto</test_depend>
<test_depend>ament_cmake_flake8</test_depend>
<test_depend>ament_cmake_xmllint</test_depend>
<test_depend>ament_cmake_clang_format</test_depend>
<test_depend>ament_cmake_cppcheck</test_depend>
<test_depend>ament_cmake_pep257</test_depend>
<test_depend>ament_clang_tidy</test_depend>
<test_depend>ament_cmake_gtest</test_depend>
<test_depend>ament_cmake_nose</test_depend>

<export>
<build_type>ament_cmake</build_type>
</export>
</package>
17 changes: 17 additions & 0 deletions src/TODO_NODE_NAME.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
#include "TODO_PACKAGE_NAME/TODO_NODE_NAME_node.hpp"

int main(int argc, char** argv) {
// Setup runtime
rclcpp::init(argc, argv);
rclcpp::executors::MultiThreadedExecutor exec;
rclcpp::NodeOptions options;

// Add nodes to executor
auto node = std::make_shared<TODO_NODE_NAME>(options);
exec.add_node(node);

// Run
exec.spin();
rclcpp::shutdown();
return 0;
}
27 changes: 27 additions & 0 deletions src/TODO_NODE_NAME_node.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
#include "TODO_PACKAGE_NAME/TODO_NODE_NAME_node.hpp"

// For _1
using namespace std::placeholders;

TODO_NODE_NAME::TODO_NODE_NAME(const rclcpp::NodeOptions& options) : Node("TODO_NODE_NAME", options) {
// Parameters
float x = this->declare_parameter<float>("foo", -10.0);

// Pub Sub
this->sub =
this->create_subscription<std_msgs::msg::String>("/str", 1, std::bind(&TODO_NODE_NAME::sub_cb, this, _1));
this->pub = this->create_publisher<std_msgs::msg::String>("/run_folder", 1);

// Log a sample log
RCLCPP_INFO(this->get_logger(), "You passed %f", x);

// Send a sample message
std_msgs::msg::String msg{};
msg.data = std::string{"Hello World!"};
pub->publish(msg);
}

void TODO_NODE_NAME::sub_cb(const std_msgs::msg::String::SharedPtr msg) {
// Echo message
this->pub->publish(*msg);
}
17 changes: 17 additions & 0 deletions tests/unit.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
#include <gtest/gtest.h>

#include <rclcpp/rclcpp.hpp>

#include "TODO_PACKAGE_NAME/TODO_NODE_NAME_node.hpp"

TEST(TODO_NODE_NAME, Test1) {}

int main(int argc, char** argv) {
rclcpp::init(0, nullptr);

::testing::InitGoogleTest(&argc, argv);
auto res = RUN_ALL_TESTS();

rclcpp::shutdown();
return res;
}

0 comments on commit 18aaccf

Please sign in to comment.