/** * @file Axis.h * @author your name (you@domain.com) * @brief * @version 0.1 * @date 2026-07-02 * * @copyright Copyright (c) 2026 * */ #pragma once #include "Types.h" namespace plcopen { /** * \enum MotionCommandType * \brief 内部运动指令类型(功能块 → 轴模型映射) */ enum class MotionCommandType { None, /*!< 无活动指令 */ PointToPoint, /*!< MC_MoveAbsolute / MC_MoveRelative */ Velocity, /*!< MC_MoveVelocity */ Stop, /*!< MC_Stop */ Halt, /*!< MC_Halt */ Home, /*!< MC_Home */ }; /** * \struct MotionCommand * \brief 当前活动运动指令的参数快照 */ struct MotionCommand { MotionCommandType type = MotionCommandType::None; REAL targetPosition = 0.0; REAL targetVelocity = 0.0; REAL maxVelocity = 0.0; REAL acceleration = 0.0; REAL deceleration = 0.0; REAL homePosition = 0.0; MC_AXIS_STATE resultingState = MC_AXIS_STATE::DiscreteMotion; }; /** * \class Axis * \brief 单轴仿真对象,实现 PLCopen 轴状态机与运动学 * \note 不要在功能块 cycle() 之外直接修改 Axis 内部状态。 */ class Axis { public: /** * \brief 构造轴 * \param axisNo 轴编号 * \param name 轴名称 */ 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; 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; }; }