Skip to content
Merged
133 changes: 8 additions & 125 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
@@ -1,130 +1,13 @@
name: XRobot Module Build Test

name: Module CI
on:
push:
pull_request:
schedule:
- cron: '0 3 1 * *' # 每月1日凌晨3点(UTC)自动触发

workflow_dispatch:
jobs:
build:
runs-on: ubuntu-latest
container:
image: ghcr.io/xrobot-org/docker-image-linux:main
options: --user 0

env:
XR_MODULE_NAME: ${{ github.event.repository.name }}

steps:
- name: Checkout current module repo to ./Modules/${{ env.XR_MODULE_NAME }}
uses: actions/checkout@v4
with:
path: Modules/${{ env.XR_MODULE_NAME }}

- name: Create main.cpp
run: |
cat > main.cpp <<'EOF'
#include <iostream>
#include "xrobot_main.hpp"
#include "libxr.hpp"

int main() {
LibXR::PlatformInit();
LibXR::STDIO::Printf<"This is XRobot Module Template Test\n">();
LibXR::HardwareContainer hw;
XRobotMain(hw);
return 0;
}
EOF

- name: Create minimal CMakeLists.txt
run: |
cat > CMakeLists.txt <<'EOF'
project(xrobot_mod_test CXX)
set(CMAKE_CXX_STANDARD 20)
add_executable(xr_test main.cpp)
set(XROBOT_MODULES_DIR ${CMAKE_CURRENT_SOURCE_DIR}/Modules/)
add_subdirectory(libxr)
target_include_directories(xr_test PUBLIC $<TARGET_PROPERTY:xr,INTERFACE_INCLUDE_DIRECTORIES> ${CMAKE_SOURCE_DIR}/User)
target_link_libraries(xr_test PUBLIC xr)
EOF

- name: Pull libxr to ./libxr
run: git clone --depth=1 https://github.com/Jiu-xiao/libxr ./libxr

- name: Checkout CMD module
uses: actions/checkout@v4
with:
repository: QDU-Robomaster/CMD
path: Modules/CMD

- name: Setup Python & Install deps
run: |
python3 -m pip install --upgrade pip
pip3 install pyyaml requests

- name: Add XRobot tools to PATH
run: |
echo "$HOME/.local/bin" >> $GITHUB_PATH

- name: Install xrobot toolchain
run: |
pip3 install xrobot libxr

- name: Run xrobot_setup
run: |
xrobot_setup || true

- name: Run xrobot_init_mod
run: |
xrobot_init_mod

- name: Add BlinkLED module
run: |
xrobot_add_mod BlinkLED --instance-id BlinkLED_0
xrobot_add_mod CMD --instance-id cmd

- name: Add this repo module
run: |
xrobot_add_mod ${{ env.XR_MODULE_NAME }} && cat User/xrobot.yaml

- name: Generate main again
run: |
xrobot_setup

- name: Build
run: |
mkdir -p build
cd build
cmake ..
make

- name: Create Tag via GitHub API
if: ${{ success() && github.event_name != 'pull_request' }}
uses: actions/github-script@v7
with:
github-token: ${{ secrets.GITHUB_TOKEN }}
script: |
const moduleTag = process.env.XR_MODULE_NAME;
const date = new Date();
const yyyy = date.getUTCFullYear();
const mm = String(date.getUTCMonth() + 1).padStart(2, '0');
const dd = String(date.getUTCDate()).padStart(2, '0');
const HH = String(date.getUTCHours()).padStart(2, '0');
const MM = String(date.getUTCMinutes()).padStart(2, '0');
const SS = String(date.getUTCSeconds()).padStart(2, '0');
const tagName = `${moduleTag}-${yyyy}${mm}${dd}-${HH}${MM}${SS}`;

// 获取当前 commit sha
const sha = process.env.GITHUB_SHA;

// 创建 tag reference
await github.rest.git.createRef({
owner: context.repo.owner,
repo: context.repo.repo,
ref: `refs/tags/${tagName}`,
sha: sha
});
console.log(`Created tag: ${tagName} on sha: ${sha}`);

uses: xrobot-org/XRobot/.github/workflows/module-ci.yml@dev
with:
xrobot-ref: dev
libxr-ref: dev
dependency-ref: refs/heads/dev
template-args: '[]'
180 changes: 90 additions & 90 deletions HostData.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,21 +2,14 @@

// clang-format off
/* === MODULE MANIFEST V2 ===
module_description: No description provided
constructor_args:
- cmd: '@nullptr'
- host_euler_topic_name: "target_eulr"
- host_chassis_data_topic_name: "host_chassis_data"
- host_fire_topic_name: "host_fire_notify"
template_args: []
required_hardware: []
module_description: 上位机数据接入模块:把上位机发来的云台目标、底盘速度和发射命令汇总为 CMD 的 AI 控制数据 / Host data Module that combines the gimbal target, chassis speed and fire command from the host into AI control data for CMD
depends:
- qdu-future/CMD
- id: QDU-Robomaster/CMD
ref: same-or-dev
=== END MANIFEST === */
// clang-format on

#include "CMD.hpp"
#include "app_framework.hpp"
#include "libxr_cb.hpp"
#include "libxr_def.hpp"
#include "libxr_time.hpp"
Expand All @@ -30,62 +23,80 @@ required_hardware: []
#include "transform.hpp"

/**
* @brief 上位机数据接入模块
* @details 将上位机发送的云台、底盘、发射命令转换为 CMD::Data 并喂给 CMD 模块。
* @brief 上位机数据接入模块:把上位机发来的云台目标、底盘速度和发射命令汇总为
* `CMD::Data`,通过 CMD 的 AI 控制入口提交。
* Host data Module that combines the gimbal target, chassis speed and fire command
* from the host into `CMD::Data` and submits it through the AI control entry of
* CMD.
*/
class HostData : public LibXR::Application {
class HostData
{
public:
struct HostChassisTarget {
float vx;
float vy;
float w;
/**
* @brief 上位机给出的底盘目标速度。
* Chassis target speed from the host.
*/
struct HostChassisTarget
{
float vx; ///< x 方向速度 Velocity along x
float vy; ///< y 方向速度 Velocity along y
float w; ///< 旋转角速度 Rotation angular velocity
};

struct LauncherCMD {
bool isfire;
/**
* @brief 上位机给出的发射命令。
* Fire command from the host.
*/
struct LauncherCMD
{
bool isfire; ///< 是否开火 Whether to fire
};

struct HostGimbalTarget {
float rol, pit, yaw;
float rol_dot, pit_dot, yaw_dot;
float rol_ddot, pit_ddot, yaw_ddot;
/**
* @brief 上位机给出的云台目标欧拉角及其一阶、二阶导数。
* Gimbal target Euler angles from the host with their first and second
* derivatives.
*/
struct HostGimbalTarget
{
float rol; ///< 目标 roll Target roll
float pit; ///< 目标 pitch Target pitch
float yaw; ///< 目标 yaw Target yaw
float rol_dot; ///< roll 一阶导数 First derivative of roll
float pit_dot; ///< pitch 一阶导数 First derivative of pitch
float yaw_dot; ///< yaw 一阶导数 First derivative of yaw
float rol_ddot; ///< roll 二阶导数 Second derivative of roll
float pit_ddot; ///< pitch 二阶导数 Second derivative of pitch
float yaw_ddot; ///< yaw 二阶导数 Second derivative of yaw
};

/**
* @brief 构造 HostData 模块
* @param hw 硬件容器
* @param app 应用管理器
* @param cmd CMD 模块引用
* @param host_euler_topic_name 云台目标欧拉角 Topic
* @param host_chassis_data_topic_name 底盘目标速度 Topic
* @param host_fire_topic_name 发射控制 Topic
* @brief 构造 HostData,创建三个 Topic 并注册回调。
* Construct HostData, create the three Topics and register the callbacks.
*
* @param cmd CMD 实例,接收 AI 控制数据。
* CMD instance that receives the AI control data.
* @param host_gimbal_topic_name 云台目标 Topic 名称。
* Name of the gimbal target Topic.
* @param host_chassis_data_topic_name 底盘目标速度 Topic 名称。
* Name of the chassis target speed Topic.
* @param host_fire_topic_name 发射命令 Topic 名称。
* Name of the fire command Topic.
*/
HostData(LibXR::HardwareContainer& hw, LibXR::ApplicationManager& app,
CMD* cmd, const char* host_gimbal_topic_name,
const char* host_chassis_data_topic_name,
const char* host_fire_topic_name)
: cmd_(cmd) {
UNUSED(hw);

ASSERT(cmd_ != nullptr);

host_gimbal_data_tp_ = LibXR::Topic::CreateTopic<HostGimbalTarget>(
host_gimbal_topic_name);
host_chassis_data_tp_ = LibXR::Topic::CreateTopic<HostChassisTarget>(
host_chassis_data_topic_name);
host_fire_notify_tp_ =
LibXR::Topic::CreateTopic<LauncherCMD>(host_fire_topic_name);

HostData(CMD& cmd, const char* host_gimbal_topic_name = "target_euler",
const char* host_chassis_data_topic_name = "host_chassis_data",
const char* host_fire_topic_name = "host_fire_notify")
: cmd_(&cmd),
host_gimbal_data_tp_(
LibXR::Topic::CreateTopic<HostGimbalTarget>(host_gimbal_topic_name)),
host_chassis_data_tp_(
LibXR::Topic::CreateTopic<HostChassisTarget>(host_chassis_data_topic_name)),
host_fire_notify_tp_(LibXR::Topic::CreateTopic<LauncherCMD>(host_fire_topic_name))
{
auto euler_callback = LibXR::Topic::Callback::Create(
[](bool in_isr, HostData* host_data, LibXR::RawData& raw_data) {
if (host_data == nullptr || raw_data.addr_ == nullptr ||
raw_data.size_ < sizeof(HostGimbalTarget)) {
return;
}
HostGimbalTarget t;
LibXR::Memory::FastCopy(&t, raw_data.addr_, sizeof(t));
host_data->host_euler_ =
LibXR::EulerAngle<float>(t.rol, t.pit, t.yaw);
[](bool in_isr, HostData* host_data, const HostGimbalTarget& t)
{
host_data->host_euler_ = LibXR::EulerAngle<float>(t.rol, t.pit, t.yaw);
host_data->host_gyro_ =
Eigen::Matrix<float, 3, 1>(t.rol_dot, t.pit_dot, t.yaw_dot);
host_data->host_accl_ =
Expand All @@ -96,26 +107,18 @@ class HostData : public LibXR::Application {
this);

auto chassis_callback = LibXR::Topic::Callback::Create(
[](bool in_isr, HostData* host_data, LibXR::RawData& raw_data) {
if (host_data == nullptr || raw_data.addr_ == nullptr ||
raw_data.size_ < sizeof(HostChassisTarget)) {
return;
}
LibXR::Memory::FastCopy(&host_data->host_chassis_data_,
raw_data.addr_, sizeof(HostChassisTarget));
[](bool in_isr, HostData* host_data, const HostChassisTarget& chassis)
{
host_data->host_chassis_data_ = chassis;
host_data->last_chassis_time_ = LibXR::Timebase::GetMilliseconds();
host_data->HostCMD(in_isr);
},
this);

auto fire_callback = LibXR::Topic::Callback::Create(
[](bool in_isr, HostData* host_data, LibXR::RawData& raw_data) {
if (host_data == nullptr || raw_data.addr_ == nullptr ||
raw_data.size_ < sizeof(LauncherCMD)) {
return;
}
LibXR::Memory::FastCopy(&host_data->host_fire_notify_, raw_data.addr_,
sizeof(LauncherCMD));
[](bool in_isr, HostData* host_data, const LauncherCMD& fire)
{
host_data->host_fire_notify_ = fire;
host_data->last_fire_time_ = LibXR::Timebase::GetMilliseconds();
host_data->HostCMD(in_isr);
},
Expand All @@ -124,40 +127,42 @@ class HostData : public LibXR::Application {
host_gimbal_data_tp_.RegisterCallback(euler_callback);
host_chassis_data_tp_.RegisterCallback(chassis_callback);
host_fire_notify_tp_.RegisterCallback(fire_callback);

app.Register(*this);
}

HostData(LibXR::HardwareContainer& hw, LibXR::ApplicationManager& app,
CMD& cmd, const char* host_gimbal_topic_name,
const char* host_chassis_data_topic_name,
const char* host_fire_topic_name)
: HostData(hw, app, &cmd, host_gimbal_topic_name,
host_chassis_data_topic_name, host_fire_topic_name) {}

/**
* @brief 汇总并下发 Host 命令
* @param in_isr 是否在中断上下文(当前未使用)
* @brief 用三路最新数据组成一帧 `CMD::Data` 并调用 `cmd.FeedAI()`。
* Compose one `CMD::Data` frame from the latest data of the three Topics and
* call `cmd.FeedAI()`.
*
* @param in_isr 是否在中断上下文。
* Whether called from interrupt context.
*/
void HostCMD(bool in_isr) {
void HostCMD(bool in_isr)
{
UNUSED(in_isr);
CMD::Data host_cmd;

if (host_chassis_data_.vx == 0.0f && host_chassis_data_.vy == 0.0f &&
host_chassis_data_.w == 0.0f) {
host_chassis_data_.w == 0.0f)
{
host_cmd.chassis = {0, 0, 0};
host_cmd.chassis_online = false;
} else {
}
else
{
host_cmd.chassis.x = host_chassis_data_.vx;
host_cmd.chassis.y = host_chassis_data_.vy;
host_cmd.chassis.z = host_chassis_data_.w;
host_cmd.chassis_online = true;
}

if (host_euler_.Pitch() == 0.0f && host_euler_.Yaw() == 0.0f) {
if (host_euler_.Pitch() == 0.0f && host_euler_.Yaw() == 0.0f)
{
host_cmd.gimbal = {0, 0, 0, 0, 0, 0, 0, 0, 0};
host_cmd.gimbal_online = false;
} else {
}
else
{
host_cmd.gimbal.pit = host_euler_.Pitch();
host_cmd.gimbal.yaw = host_euler_.Yaw();
host_cmd.gimbal.pit_dot = host_gyro_.y();
Expand All @@ -173,11 +178,6 @@ class HostData : public LibXR::Application {
cmd_->FeedAI(host_cmd);
}

/**
* @brief 监控回调
*/
void OnMonitor() override {}

private:
CMD* cmd_;
HostChassisTarget host_chassis_data_;
Expand Down
Loading
Loading