- Axis: 新增运动学/功率/错误管理属性(_position 等 10+ 项) - Axis: 新增 doPower()/readState()/cycle() 接口骨架 - MC_Power: 实现功率使能功能块(边沿检测 + 状态输出) - GlobalResource: 新增 CreateAxis/GetAxisRefById 全局轴管理 - App/CMakeLists.txt: 添加 GlobalResource.cpp 源文件 - docs: 更新 Axis 属性清单与 MC_Power 设计文档
61 lines
1.4 KiB
C++
61 lines
1.4 KiB
C++
/**
|
|
* @file GlobalResource.cpp
|
|
* @author
|
|
* @brief 全局轴资源管理 — 持有并管理所有轴实例
|
|
* @version 0.1
|
|
* @date 2026-07-03
|
|
*
|
|
* @copyright Copyright (c) 2026
|
|
*
|
|
*/
|
|
|
|
#include "GlobalResource.h"
|
|
#include <unordered_map>
|
|
#include <vector>
|
|
#include <mutex>
|
|
|
|
namespace {
|
|
|
|
// 轴实例存储 — 拥有所有轴的内存
|
|
std::vector<plcopen::Axis> g_axes;
|
|
|
|
// 快速查找表:id → AXIS_REF& (指向 g_axes 中的元素)
|
|
std::unordered_map<plcopen::UINT, std::size_t> g_indexMap;
|
|
|
|
// 线程安全
|
|
std::mutex g_mutex;
|
|
|
|
// id 不存在时返回的哨兵
|
|
plcopen::AXIS_REF g_invalidRef{0, "", nullptr};
|
|
|
|
} // namespace
|
|
|
|
plcopen::AXIS_REF& CreateAxis(plcopen::UINT id, const std::string& name) {
|
|
std::lock_guard<std::mutex> lock(g_mutex);
|
|
|
|
// id 已存在 → 返回已有轴的引用
|
|
auto it = g_indexMap.find(id);
|
|
if (it != g_indexMap.end()) {
|
|
return g_axes[it->second].ref();
|
|
}
|
|
|
|
// 新建轴
|
|
g_axes.emplace_back(id, name);
|
|
std::size_t idx = g_axes.size() - 1;
|
|
g_indexMap[id] = idx;
|
|
return g_axes[idx].ref();
|
|
}
|
|
|
|
plcopen::AXIS_REF& GetAxisRefById(plcopen::UINT id) {
|
|
std::lock_guard<std::mutex> lock(g_mutex);
|
|
|
|
auto it = g_indexMap.find(id);
|
|
if (it != g_indexMap.end()) {
|
|
return g_axes[it->second].ref();
|
|
}
|
|
|
|
g_invalidRef.axisNo = id;
|
|
g_invalidRef.axis = nullptr;
|
|
return g_invalidRef;
|
|
}
|