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
15 changes: 9 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,12 @@ English | [中文](README.zh_CN.md)

## Sample List

| Sample | Description | Level |
| :----------------------------------- | :------------------------------------------------------------------------------------------------------------- | :----- |
| [helloworld](./helloworld) | A simple example demonstrating how to use AimRT framework | :star: |
| [my_pb_channel](./my_pb_channel) | A simple example demonstrating publisher-subscriber communication using the Channel API with Protobuf messages | :star: |
| [my_ros2_channel](./my_ros2_channel) | A simple example demonstrating publisher-subscriber communication using the Channel API with ROS2 messages | :star: |
| [my_pb_rpc](./my_pb_rpc) | A simple example demonstrating publisher-subscriber communication using the Rpc API with Protobuf messages | :star: |
| Example | Description | Level |
| :----------------------------------- | :--------------------------------------------------------------------------------------------------------- | :----------- |
| [helloworld](./helloworld) | A simple example demonstrating how to use AimRT framework in app mode | :star: |
| [helloworld_pkg](./helloworld_pkg) | A simple example demonstrating how to use AimRT framework in pkg mode | :star: |
| [my_pb_channel](./my_pb_channel) | A simple example demonstrating publisher-subscriber communication using Channel API with Protobuf messages | :star: |
| [my_ros2_channel](./my_ros2_channel) | A simple example demonstrating publisher-subscriber communication using Channel API with ROS2 messages | :star: |
| [my_pb_rpc](./my_pb_rpc) | A simple example demonstrating request-response communication using Rpc API with Protobuf messages | :star: |
| [my_ros2_rpc](./my_ros2_rpc) | A simple example demonstrating request-response communication using Rpc API with ROS2 messages | :star: |
| [human_detector](./human_detector) | Publishes camera data via Channel API and performs face detection using OpenCV | :star::star: |
19 changes: 9 additions & 10 deletions README.zh_CN.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,12 @@

## 示例列表

| 示例 | 描述 | 级别 |
| :----------------------------------- | :----------------------------------------------------------------------- | :----- |
| [helloworld](./helloworld) | 演示如何使用 AimRT 框架的 app 模式的简单示例 | :star: |
| [helloworld_pkg](./helloworld_pkg) | 演示如何使用 AimRT 框架的 pkg 模式的简单示例 | :star: |
| [my_pb_channel](./my_pb_channel) | 演示使用 Channel API 进行发布者-订阅者通信的简单示例,使用 Protobuf 消息 | :star: |
| [my_ros2_channel](./my_ros2_channel) | 演示使用 Channel API 进行发布者-订阅者通信的简单示例,使用 ROS2 消息 | :star: |
| [my_pb_rpc](./my_pb_rpc) | 演示使用 Rpc API 进行发布者-订阅者通信的简单示例,使用 Protobuf 消息 | :star: |
| [my_ros2_rpc](./my_ros2_rpc) | 演示使用 Rpc API 进行发布者-订阅者通信的简单示例,使用 ROS2 消息 | :star: |

## 贡献
| 示例 | 描述 | 级别 |
| :----------------------------------- | :------------------------------------------------------------------------------------ | :------------ |
| [helloworld](./helloworld) | 演示如何使用 AimRT 框架的 app 模式的简单示例 | :star: |
| [helloworld_pkg](./helloworld_pkg) | 演示如何使用 AimRT 框架的 pkg 模式的简单示例 | :star: |
| [my_pb_channel](./my_pb_channel) | 演示使用 Channel API 进行发布者-订阅者通信的简单示例,使用 Protobuf 消息 | :star: |
| [my_ros2_channel](./my_ros2_channel) | 演示使用 Channel API 进行发布者-订阅者通信的简单示例,使用 ROS2 消息 | :star: |
| [my_pb_rpc](./my_pb_rpc) | 演示使用 Rpc API 进行发布者-订阅者通信的简单示例,使用 Protobuf 消息 | :star: |
| [my_ros2_rpc](./my_ros2_rpc) | 演示使用 Rpc API 进行发布者-订阅者通信的简单示例,使用 ROS2 消息 | :star: |
| [human_detector](./human_detector) | 演示使用 Channel API 通过发布订阅传输系统摄像头数据,并使用 OpenCV 进行人脸识别的示例 | :star: :star: |
3 changes: 2 additions & 1 deletion helloworld/cmake/GetAimRT.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ include(FetchContent)
message(STATUS "get aimrt ...")

set(aimrt_DOWNLOAD_URL
"https://github.com/AimRT/AimRT/archive/refs/tags/v1.0.0.tar.gz"
"https://github.com/AimRT/AimRT/archive/refs/tags/v1.4.0.tar.gz"
CACHE STRING "")

if(aimrt_LOCAL_SOURCE)
Expand All @@ -27,6 +27,7 @@ function(get_aimrt)
FetchContent_GetProperties(aimrt)
if(NOT aimrt_POPULATED)
set(AIMRT_BUILD_RUNTIME ON)
set(AIMRT_BUILD_WITH_PROTOBUF ON)

FetchContent_MakeAvailable(aimrt)
endif()
Expand Down
5 changes: 3 additions & 2 deletions helloworld/src/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,13 @@

add_subdirectory(module/helloworld_module)

add_subdirectory(app/helloworld_app)
add_subdirectory(pkg/helloworld_pkg)

add_custom_target(
${PROJECT_NAME}_build_all ALL
COMMAND ${CMAKE_COMMAND} -E copy_directory ${CMAKE_CURRENT_SOURCE_DIR}/install/bin ${CMAKE_BINARY_DIR}
DEPENDS helloworld_app)
DEPENDS aimrt::runtime::main
helloworld_pkg)

# Install
if(AIMRT_SAMPLES_INSTALL)
Expand Down
26 changes: 0 additions & 26 deletions helloworld/src/app/helloworld_app/CMakeLists.txt

This file was deleted.

59 changes: 0 additions & 59 deletions helloworld/src/app/helloworld_app/main.cc

This file was deleted.

35 changes: 20 additions & 15 deletions helloworld/src/install/bin/cfg/helloworld_cfg.yaml
Original file line number Diff line number Diff line change
@@ -1,21 +1,26 @@
# AimRT core configuration
aimrt:
log: # log configuration
core_lvl: INFO # core log level, options: Trace/Debug/Info/Warn/Error/Fatal/Off, case-insensitive
backends: # log backends
- type: console # print to console
log:
core_lvl: INFO # Trace/Debug/Info/Warn/Error/Fatal/Off
backends:
- type: console
- type: rotate_file
options:
color: true # colored print
- type: rotate_file # print to file and rotate
path: ./log
filename: helloworld.log
max_file_size_m: 4
max_file_num: 10
executor:
executors:
- name: work_executor
type: asio_thread
options:
path: ./log # log file path
filename: helloworld.log # log file name
max_file_size_m: 4 # log file max size, unit: MB
max_file_num: 10 # max log file num, 0 means unlimited
thread_num: 4
module:
pkgs:
- path: ./libhelloworld_pkg.so
enable_modules: [HelloWorldModule]

# Custom module configuration
# the framework will generate a temporary configuration file for each module,
# and the developer can get the configuration file path through the Configurator interface.
HelloWorldModule:
key1: val1
key2: val2
param1: val1
param2: 2
2 changes: 1 addition & 1 deletion helloworld/src/install/bin/start_helloworld.sh
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
#!/bin/sh

./helloworld_app ./cfg/helloworld_cfg.yaml
./aimrt_main --cfg_file_path=./cfg/helloworld_cfg.yaml
1 change: 0 additions & 1 deletion helloworld/src/module/helloworld_module/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ file(GLOB_RECURSE src ${CMAKE_CURRENT_SOURCE_DIR}/*.cc)

# Add target
add_library(helloworld_module STATIC)
add_library(helloworld::helloworld_module ALIAS helloworld_module)

# Set source file of target
target_sources(helloworld_module PRIVATE ${src})
Expand Down
62 changes: 49 additions & 13 deletions helloworld/src/module/helloworld_module/helloworld_module.cc
Original file line number Diff line number Diff line change
Expand Up @@ -5,38 +5,74 @@
#include "yaml-cpp/yaml.h" // IWYU pragma: keep

bool HelloWorldModule::Initialize(aimrt::CoreRef core) {
core_ = core;
ctx_ptr_ = std::make_shared<aimrt::context::Context>(core);
ctx_ptr_->LetMe();

try {
// Write your other initialization logic here ...
// Read custom module configuration
auto file_path = core_.GetConfigurator().GetConfigFilePath();
auto file_path = ctx_ptr_->GetConfigFilePath();
if (!file_path.empty()) {
YAML::Node cfg_node = YAML::LoadFile(file_path.data());
for (const auto& itr : cfg_node) {
auto k = itr.first.as<std::string>();
auto v = itr.second.as<std::string>();
AIMRT_INFO("cfg [{} : {}]", k, v);
}
param1_ = cfg_node["param1"].as<std::string>();
param2_ = cfg_node["param2"].as<int>();
}
// Write your other initialization logic here ...

// Get executor handle
executor_ = ctx_ptr_->CreateExecutor("work_executor");

timer_ = aimrt::executor::CreateTimer(
executor_,
std::chrono::milliseconds(1000),
[this]() {
ctx_ptr_->LetMe();
Task(); },
false);

} catch (const std::exception& e) {
AIMRT_ERROR("Init failed, {}", e.what());
return false;
}

AIMRT_INFO("Init succeeded.");

return true;
}

bool HelloWorldModule::Start() {
// Write your runtime logic here
AIMRT_INFO("Start succeeded.");
// use timer
timer_->Reset();

// use executor
executor_.Execute([this]() { ctx_ptr_->LetMe(); LoopTask(); });

// use co_task
scope_.spawn(aimrt::co::On(aimrt::co::InlineScheduler(), CoLoopTask()));
return true;
}

void HelloWorldModule::Shutdown() {
// Write your resource release logic here
AIMRT_INFO("Shutdown succeeded.");
ctx_ptr_->StopRunning();
timer_->Cancel();
aimrt::co::SyncWait(scope_.complete());
}

void HelloWorldModule::Task() {
AIMRT_INFO("Single Task param1: {}, param2: {}", param1_, param2_);
}

void HelloWorldModule::LoopTask() {
while (ctx_ptr_->Running()) {
AIMRT_WARN("Loop Task param1: {}, param2: {}", param1_, param2_);
std::this_thread::sleep_for(std::chrono::milliseconds(1000));
}
}

aimrt::co::Task<void> HelloWorldModule::CoLoopTask() {
auto scheduler = aimrt::co::AimRTScheduler(executor_);
while (ctx_ptr_->Running()) {
ctx_ptr_->LetMe();
AIMRT_ERROR("CoLoop Task param1: {}, param2: {}", param1_, param2_);
co_await aimrt::co::ScheduleAfter(scheduler, std::chrono::milliseconds(1000));
}
co_return;
}
20 changes: 17 additions & 3 deletions helloworld/src/module/helloworld_module/helloworld_module.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

#pragma once

#include "aimrt_module_cpp_interface/module_base.h"
#include "aimrt_module_cpp_interface/aimrt_module_cpp_interface.h"

class HelloWorldModule : public aimrt::ModuleBase {
public:
Expand All @@ -19,8 +19,22 @@ class HelloWorldModule : public aimrt::ModuleBase {
void Shutdown() override;

private:
auto GetLogger() { return core_.GetLogger(); }
void Task();
void LoopTask();
aimrt::co::Task<void> CoLoopTask();

private:
aimrt::CoreRef core_;
std::shared_ptr<aimrt::context::Context> ctx_ptr_;

std::string param1_;
int param2_;

// 通用执行器
aimrt::executor::ExecutorRef executor_;

// 定时器
std::shared_ptr<aimrt::executor::TimerBase> timer_;

// 协程作用域
aimrt::co::AsyncScope scope_;
};
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ file(GLOB_RECURSE src ${CMAKE_CURRENT_SOURCE_DIR}/*.cc)

# Add target
add_library(${CUR_TARGET_NAME} SHARED)
add_library(helloworld::${CUR_TARGET_NAME} ALIAS ${CUR_TARGET_NAME})

# Set source file of target
target_sources(${CUR_TARGET_NAME} PRIVATE ${src})
Expand All @@ -15,4 +14,4 @@ target_sources(${CUR_TARGET_NAME} PRIVATE ${src})
target_link_libraries(
${CUR_TARGET_NAME}
PRIVATE aimrt::interface::aimrt_pkg_c_interface
helloworld::helloworld_module)
helloworld_module)
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

static std::tuple<std::string_view, std::function<aimrt::ModuleBase*()>> aimrt_module_register_array[]{
// Here register all modules in the package
{"HelloWorldModule", []() -> aimrt::ModuleBase* { return new HelloWorldModule(); }}};
{"HelloWorldModule", []() -> aimrt::ModuleBase* { return new HelloWorldModule(); }},
};

AIMRT_PKG_MAIN(aimrt_module_register_array)
Loading