feat: 实现 Axis 核心属性与 MC_Power 功能块

- Axis: 新增运动学/功率/错误管理属性(_position 等 10+ 项)
- Axis: 新增 doPower()/readState()/cycle() 接口骨架
- MC_Power: 实现功率使能功能块(边沿检测 + 状态输出)
- GlobalResource: 新增 CreateAxis/GetAxisRefById 全局轴管理
- App/CMakeLists.txt: 添加 GlobalResource.cpp 源文件
- docs: 更新 Axis 属性清单与 MC_Power 设计文档
This commit is contained in:
chen
2026-07-03 10:01:06 +00:00
parent 7f73fb48ef
commit 0587f7ac6c
13 changed files with 685 additions and 94 deletions
+28 -2
View File
@@ -59,12 +59,38 @@ public:
explicit Axis(UINT axisNo = 1, std::string name = "Axis1");
void cycle();
MC_AXIS_STATE readState();
void doPower(bool en);
/** \brief 获取轴编号 */
UINT axisNo() const { return _ref.axisNo; }
/** \brief 获取内部 AXIS_REF(供全局注册表使用) */
AXIS_REF& ref() { return _ref; }
private:
AXIS_REF ref_;
MC_AXIS_STATE state_ = MC_AXIS_STATE::Disabled;
AXIS_REF _ref;
MC_AXIS_STATE _state = MC_AXIS_STATE::Disabled;
MC_AXIS_STATE _resultingSstate = MC_AXIS_STATE::Disabled;
MotionCommand _activeCommand;
// 运动学属性
REAL _position;
REAL _actualPosition;
REAL _velocity;
REAL _actualVelocity;
REAL _acceleration;
REAL _actualAcceleration;
// 功率与使能
BOOL _powerEnabled;
BOOL _homed;
// 错误管理
MC_ERROR_ID _errorId;
BOOL _errorStop;
};
}
+13
View File
@@ -0,0 +1,13 @@
/**
* @file FunctionBlocks.h
* @author
* @brief
* @version 0.1
* @date 2026-07-03
*
* @copyright Copyright (c) 2026
*
*/
#include "MC_Power.h"
+44
View File
@@ -0,0 +1,44 @@
/**
* @file MC_Power.h
* @author
* @brief
* @version 0.1
* @date 2026-07-03
*
* @copyright Copyright (c) 2026
*
*/
#pragma once
#include "Axis.h"
#include "Types.h"
namespace plcopen {
class MC_Power{
public:
explicit MC_Power(AXIS_REF& axisRef);
void cycle();
// --- 输入参数 (VAR_INPUT) ---
BOOL Enable = false;
BOOL Enable_Positive = true;
BOOL Enable_Negative = true;
// --- 输出参数 (VAR_OUTPUT) ---
BOOL Status = false;
BOOL Busy = false;
BOOL Error = false;
MC_ERROR_ID ErrorID = MC_ERROR_ID::mcNoError;
private:
AXIS_REF& _axisRef; // 轴引用,不持有所有权
bool _prevEnable = false; // 上一周期的 Enable 值(边沿检测)
};
}
+73 -4
View File
@@ -14,14 +14,83 @@
namespace plcopen {
Axis::Axis(UINT axisNo, std::string name) {
ref_.axisNo = axisNo;
ref_.axisName = std::move(name);
ref_.axis = this;
_ref.axisNo = axisNo;
_ref.axisName = std::move(name);
_ref.axis = this;
}
void Axis::cycle()
{
if (_errorStop) return;
if (!_powerEnabled) return;
switch (_state) {
case MC_AXIS_STATE::Disabled :
{
break;
}
case MC_AXIS_STATE::Standstill :
{
break;
}
case MC_AXIS_STATE::Stopping :
{
break;
}
// 独立运动
case MC_AXIS_STATE::DiscreteMotion :
{
break;
}
// 持续运动
case MC_AXIS_STATE::ContinuousMotion :
{
break;
}
// 同步运动
case MC_AXIS_STATE::SynchronizedMotion :
{
break;
}
// 回零
case MC_AXIS_STATE::Homing :
{
break;
}
default:
{
break;
}
}
}
void Axis::doPower(bool en)
{
if(en)
{
_powerEnabled = true;
}
else {
_powerEnabled = false;
_activeCommand.type = MotionCommandType::None;
_velocity = 0.0;
_acceleration = 0.0;
_errorStop = false;
_state = MC_AXIS_STATE::Disabled;
}
}
MC_AXIS_STATE Axis::readState()
{
return _state;
}
}
+84
View File
@@ -0,0 +1,84 @@
/**
* @file MC_Power.cpp
* @author
* @brief
* @version 0.1
* @date 2026-07-03
*
* @copyright Copyright (c) 2026
*
*/
#include "MC_Power.h"
#include "Types.h"
namespace plcopen {
MC_Power::MC_Power(AXIS_REF& axisRef)
: _axisRef(axisRef) { }
void MC_Power::cycle()
{
if(_axisRef.axis == nullptr)
{
Error = true;
ErrorID = MC_ERROR_ID::mcInvalidParameter;
Status = false;
Busy = false;
return;
}
Axis* axis = _axisRef.axis;
Error = false;
ErrorID = MC_ERROR_ID::mcNoError;
// 上升沿
if(Enable && !_prevEnable)
{
axis->doPower(true);
}
// 下降沿
if(!Enable && _prevEnable)
{
axis->doPower(false);
}
// 在使能状态下持续更新轴状态
if(Enable)
{
auto state = axis->readState();
if(state == MC_AXIS_STATE::Standstill)
{
Busy = false;
Status = true;
}
else if(state == MC_AXIS_STATE::ErrorStop)
{
Busy = false;
Status = false;
Error = true;
ErrorID = MC_ERROR_ID::mcPowerFailure;
}
else
{
Status = false;
Busy = false;
}
}
// 保存前置状态
_prevEnable = Enable;
}
}