Skip to content

Commit

Permalink
Initial version
Browse files Browse the repository at this point in the history
Signed-off-by: Francisco Martín Rico <[email protected]>
  • Loading branch information
fmrico committed Oct 28, 2024
1 parent a1d7ffe commit fe89017
Show file tree
Hide file tree
Showing 32 changed files with 2,045 additions and 0 deletions.
45 changes: 45 additions & 0 deletions .github/workflows/rolling.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
name: rolling

on:
pull_request:
branches:
- rolling
push:
branches:
- rolling
schedule:
- cron: '0 0 * * 6'
jobs:
build-and-test:
runs-on: ${{ matrix.os }}
strategy:
matrix:
os: [ubuntu-22.04]
fail-fast: false
steps:
- uses: actions/checkout@v2
- name: Setup ROS 2
uses: ros-tooling/[email protected]
with:
required-ros-distributions: rolling
- name: build and test
uses: ros-tooling/[email protected]
with:
package-name: cs4home_core
target-ros2-distro: rolling
colcon-defaults: |
{
"test": {
"parallel-workers" : 1
}
}
colcon-mixin-name: coverage-gcc
colcon-mixin-repository: https://raw.githubusercontent.com/colcon/colcon-mixin-repository/master/index.yaml
- name: Codecov
uses: codecov/[email protected]
with:
file: ros_ws/lcov/total_coverage.info
flags: unittests
name: codecov-umbrella
# yml: ./codecov.yml
fail_ci_if_error: false
60 changes: 60 additions & 0 deletions cs4home_core/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
cmake_minimum_required(VERSION 3.8)
project(cs4home_core)

set(CMAKE_BUILD_TYPE debug)

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)
find_package(rclcpp_lifecycle REQUIRED)
find_package(rclcpp_components REQUIRED)

set(dependencies
rclcpp
rclcpp_lifecycle
rclcpp_components
)

include_directories(include)

add_library(${PROJECT_NAME} SHARED
src/cs4home_core/Master.cpp
src/cs4home_core/Flow.cpp
src/cs4home_core/CognitiveModule.cpp
src/cs4home_core/Afferent.cpp
src/cs4home_core/Efferent.cpp
src/cs4home_core/Core.cpp
src/cs4home_core/Meta.cpp
src/cs4home_core/Coupling.cpp
)
ament_target_dependencies(${PROJECT_NAME} ${dependencies})

install(DIRECTORY include/
DESTINATION include/
)

install(TARGETS
${PROJECT_NAME}
ARCHIVE DESTINATION lib
LIBRARY DESTINATION lib
RUNTIME DESTINATION lib/${PROJECT_NAME}
)

if(BUILD_TESTING)
find_package(ament_lint_auto REQUIRED)
set(ament_cmake_copyright_FOUND TRUE)
ament_lint_auto_find_test_dependencies()

find_package(ament_cmake_gtest REQUIRED)
add_subdirectory(test)
endif()

ament_export_include_directories(include)
ament_export_libraries(${PROJECT_NAME})
ament_export_dependencies(${dependencies})

ament_package()
90 changes: 90 additions & 0 deletions cs4home_core/include/cs4home_core/Afferent.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
// Copyright 2024 Intelligent Robotics Lab
//
// 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.


#ifndef CS4HOME_CORE__AFFERENT_HPP_
#define CS4HOME_CORE__AFFERENT_HPP_

#include <memory>
#include <utility>
#include <queue>
#include <vector>
#include <string>

#include "rclcpp_lifecycle/lifecycle_node.hpp"
#include "rclcpp/rclcpp.hpp"
#include "rclcpp/macros.hpp"

namespace cs4home_core
{

class Afferent
{
public:
RCLCPP_SMART_PTR_DEFINITIONS(Afferent)

enum EfferentProcessMode {CALLBACK, ONDEMAND};

explicit Afferent(rclcpp_lifecycle::LifecycleNode::SharedPtr parent);

virtual bool configure() = 0;

void set_mode(
EfferentProcessMode mode,
std::function<void(std::unique_ptr<rclcpp::SerializedMessage>)> cb = nullptr);

EfferentProcessMode get_mode() {return mode_;}
void set_max_queue_size(size_t size) {max_queue_size_ = size;}
size_t get_max_queue_size() {return max_queue_size_;}

template<class MessageT> std::unique_ptr<MessageT> get_msg(
std::unique_ptr<rclcpp::SerializedMessage> msg)
{
auto typed_msg = std::make_unique<MessageT>();
rclcpp::Serialization<MessageT> serializer;
serializer.deserialize_message(msg.get(), typed_msg.get());

return std::move(typed_msg);
}

template<class MessageT> std::unique_ptr<MessageT> get_msg()
{
if (msg_queue_.empty()) {
return {};
}

std::unique_ptr<rclcpp::SerializedMessage> msg = std::move(msg_queue_.front());
msg_queue_.pop();

return get_msg<MessageT>(std::move(msg));
}

protected:
rclcpp_lifecycle::LifecycleNode::SharedPtr parent_;
std::vector<std::shared_ptr<rclcpp::GenericSubscription>> subs_;

EfferentProcessMode mode_ {ONDEMAND};

const size_t MAX_DEFAULT_QUEUE_SIZE = 100;
size_t max_queue_size_ {MAX_DEFAULT_QUEUE_SIZE};
std::queue<std::unique_ptr<rclcpp::SerializedMessage>> msg_queue_;

std::function<void(std::unique_ptr<rclcpp::SerializedMessage>)> callback_;

bool create_subscriber(const std::string & topic, const std::string & type);
};

} // namespace cs4home_core

#endif // CS4HOME_CORE__AFFERENT_HPP_
80 changes: 80 additions & 0 deletions cs4home_core/include/cs4home_core/CognitiveModule.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
// Copyright 2024 Intelligent Robotics Lab
//
// 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.


#ifndef CS4HOME_CORE__COGNITIVEMODULE_HPP_
#define CS4HOME_CORE__COGNITIVEMODULE_HPP_

#include <dlfcn.h>

#include <tuple>
#include <string>

#include "cs4home_core/Afferent.hpp"
#include "cs4home_core/Core.hpp"
#include "cs4home_core/Coupling.hpp"
#include "cs4home_core/Efferent.hpp"
#include "cs4home_core/Meta.hpp"

#include "rclcpp/macros.hpp"
#include "rclcpp/rclcpp.hpp"
#include "rclcpp_lifecycle/lifecycle_node.hpp"

namespace cs4home_core
{

class CognitiveModule : public rclcpp_lifecycle::LifecycleNode
{
public:
RCLCPP_SMART_PTR_DEFINITIONS(CognitiveModule)
using CallbackReturnT =
rclcpp_lifecycle::node_interfaces::LifecycleNodeInterface::CallbackReturn;

explicit CognitiveModule(const rclcpp::NodeOptions & options = rclcpp::NodeOptions());

CallbackReturnT on_configure(const rclcpp_lifecycle::State & state);
CallbackReturnT on_activate(const rclcpp_lifecycle::State & state);
CallbackReturnT on_deactivate(const rclcpp_lifecycle::State & state);
CallbackReturnT on_cleanup(const rclcpp_lifecycle::State & state);
CallbackReturnT on_shutdown(const rclcpp_lifecycle::State & state);
CallbackReturnT on_error(const rclcpp_lifecycle::State & state);

protected:
Afferent::SharedPtr afferent_;
Efferent::SharedPtr efferent_;
Core::SharedPtr core_;
Meta::SharedPtr meta_;
Coupling::SharedPtr coupling_;

std::string core_name_;
std::string afferent_name_;
std::string efferent_name_;
std::string meta_name_;
std::string coupling_name_;

template<class T>
std::tuple<typename T::SharedPtr, std::string> load_component(
const std::string & name, rclcpp_lifecycle::LifecycleNode::SharedPtr parent);
};

} // namespace cs4home_core

#endif // CS4HOME_CORE__COGNITIVEMODULE_HPP_

// #include "rclcpp_components/register_node_macro.hpp"
//
// // Register the component with class_loader.
// // This acts as a sort of entry point, allowing the component to be discoverable when its library
// // is being loaded into a running process.
// RCLCPP_COMPONENTS_REGISTER_NODE(cs4home_core::CognitiveModule)
53 changes: 53 additions & 0 deletions cs4home_core/include/cs4home_core/Core.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
// Copyright 2024 Intelligent Robotics Lab
//
// 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.


#ifndef CS4HOME_CORE__CORE_HPP_
#define CS4HOME_CORE__CORE_HPP_

#include <memory>


#include "cs4home_core/Afferent.hpp"
#include "cs4home_core/Efferent.hpp"

#include "rclcpp_lifecycle/lifecycle_node.hpp"
#include "rclcpp/macros.hpp"

namespace cs4home_core
{

class Core
{
public:
RCLCPP_SMART_PTR_DEFINITIONS(Core)

explicit Core(rclcpp_lifecycle::LifecycleNode::SharedPtr parent);

virtual bool configure() = 0;
virtual bool activate() = 0;
virtual bool deactivate() = 0;

void set_afferent(cs4home_core::Afferent::SharedPtr afferent) {afferent_ = afferent;}
void set_efferent(cs4home_core::Efferent::SharedPtr efferent) {efferent_ = efferent;}

protected:
rclcpp_lifecycle::LifecycleNode::SharedPtr parent_;
cs4home_core::Afferent::SharedPtr afferent_;
cs4home_core::Efferent::SharedPtr efferent_;
};

} // namespace cs4home_core

#endif // CS4HOME_CORE__CORE_HPP_
40 changes: 40 additions & 0 deletions cs4home_core/include/cs4home_core/Coupling.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
// Copyright 2024 Intelligent Robotics Lab
//
// 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.


#ifndef CS4HOME_CORE__COUPLING_HPP_
#define CS4HOME_CORE__COUPLING_HPP_

#include "rclcpp_lifecycle/lifecycle_node.hpp"
#include "rclcpp/macros.hpp"

namespace cs4home_core
{

class Coupling
{
public:
RCLCPP_SMART_PTR_DEFINITIONS(Coupling)

explicit Coupling(rclcpp_lifecycle::LifecycleNode::SharedPtr parent);

bool configure();

protected:
rclcpp_lifecycle::LifecycleNode::SharedPtr parent_;
};

} // namespace cs4home_core

#endif // CS4HOME_CORE__COUPLING_HPP_
Loading

0 comments on commit fe89017

Please sign in to comment.