具體從PLC讀取函數查看官方手冊,在此主要寫一下獲取數據後存儲到對應結構體
結構體頭文件
#ifndef MSTC_H
#define MSTC_H
//! 與beckhoff交互信息結構體
//! 與beckhoff端結構體定義一致
/*******************************************************************************************
* 文件名:mSTc.h
* 編制日期:2018.11.13
* 作者:Kare
* 功能:
* bk_ctlStatus 定義與BECKHOFF控制器通信的結構體,與在BECKHOFF PLC內的結構體一致
********************************************************************************************/
#include "mMacro.h"
#include <vector>
using namespace std;
//! 按照單字節對齊,需要與PLC端定義一致。不需要優化。
#pragma pack(push)
#pragma pack(1)
//! beckhoff結構體 NCTOPLC_AXLESTRUCT
struct Nc2Plc
{
BK_DWORD nStateDWord; //Status double word
BK_DWORD nErrorCode; // Axis error code
BK_DWORD nAxisState; // Axis moving status
BK_DWORD nAxisModeCon; // Axis mode confirmation (feedback from NC)
BK_DWORD nCalibrationState; // State of axis calibration (homing)
BK_DWORD nCoupleState; // Axis coupling state
BK_DWORD nSvbEntries; // SVB entries/orders (SVB = Set preparation task)
BK_DWORD nSafEntries; // SAF entries/orders (SAF = Set execution task
BK_DWORD nAxisId ; // Axis ID
BK_DWORD nOpModeDWord; // Current operation mode
BK_DWORD nReserved2_HIDDEN; // reserved
BK_LREAL fPosIst; // Actual position (absolut value from NC)
BK_LREAL fModuloPosIst; // Actual position as modulo value (e.g. in degrees)
BK_DINT nModuloTurns; // Actual modulo turns
BK_LREAL fVeloIst; // Actual velocity (optional)
BK_LREAL fPosDiff; // Position difference (lag distance)
BK_LREAL fPosSoll; // Setpoint position
BK_LREAL fVeloSoll; // Setpoint velocity
BK_LREAL fAccSoll; // Setpoint acceleration, OLD: "fReserve1_HIDDEN"
BK_LREAL fReserve2_HIDDEN; // reserved
BK_LREAL fReserve3_HIDDEN; // reserved
BK_LREAL fReserve4_HIDDEN; // reserved
};
//! NCTOPLC_AXLESTRUCT nStateDWord變量
struct NCTOPLC_AXLESTRUCT_nStateDWord
{
/* NCTOPLC_AXLESTRUCT
Byte Bit Def. range Variable Name Description
0 0/1 Operational Axis is ready for operation
1 0/1 Homed Axis has been referenced/ homed ("Axis calibrated")
2 0/1 NotMoving Axis is logically stationary ("Axis not moving")
3 0/1 InPositionArea Axis is in position window (physical feedback)
4 0/1 InTargetPosition Axis is at target position (PEH) (physical feedback)
5 0/1 Protected Axis is in a protected operating mode (e.g. as a slave axis, or under axis interpolation)
6 0/1 ErrorPrewarning NEW: Axis signals an error pre warning (from TC 2.11)
7 0/1 HasBeenStopped Axis has been stopped or is presently executing a stop
8 0/1 HasJob Axis has instructions, is carrying instructions out
9 0/1 PositiveDirection Axis moving to logically larger values
10 0/1 NegativeDirection Axis moving to logically smaller values
11 0/1 HomingBusy Axis referenced ("Axis being calibrated")
12 0/1 ConstantVelocity Axis has reached its constant velocity or rotary speed
13 0/1 Compensating Section compensation passive[0]/active[1] (s. "MC_MoveSuperImposed")
14 0/1 ExtSetPointGenEnabled External setpoint generator enabled
15 0/1 Operating mode not yet executed (Busy). Not implemented yet!
16 0/1 ExternalLatchValid External latch value or sensing switch has become valid
17 0/1 NewTargetPos Axis has a new target position or a new velocity
18 0/1 Axis is not at target position or cannot reach the target position (e.g. stop). Not implemented yet!
19 0/1 ContinuousMotion Axis has target position (±) endless
20 0/1 ControlLoopClosed Axis is ready for operation and axis control loop is closed (e.g. position control)
21 0/1 CamTableQueued CAM table is queued for "Online Change" and waiting for activation
22 0/1 CamDataQueued CAM data (only MF) are queued for "Online Change" and waiting for activation
23 0/1 CamScalingPending CAM scaling are queued for "Online Change" and waiting for activation
24 0/1 CmdBuffered NEW: Following command is queued in then command buffer (s. Buffer Mode)
(from TwinCAT V2.10 Build 1311)
25 0/1 PTPmode NEW: Axis in PTP mode (no slave, no NCI axis) (from TC 2.10 Build 1326)
26 0/1 SoftLimitMinExceeded NEW: Position software limit switch minimum is exceeded (from TC 2.10 Build 1327)
27 0/1 SoftLimitMaxExceeded NEW: Position software limit switch maximum is exceeded (from TC 2.10 Build 1327)
28 0/1 DriveDeviceError NEW: Hardware drive device error (no warning), interpretation only possible when drive is data exchanging, e.g. EtherCAT "OP"-state (from TC 2.10 Build 1326)
29 0/1 MotionCommandsLocked NEW: Axis is locked for motion commands (TcMc2)
30 0/1 IoDataInvalid NEW: IO data invalid (e.g. 'WcState' or 'CdlState')
31 0/1 Error Axis is in a fault state
*/
bool Operational; // Axis is ready for operation
bool Homed; // Axis has been referenced/ homed ("Axis calibrated")
bool NotMoving; // Axis is logically stationary ("Axis not moving")
bool InPositionArea; // Axis is in position window (physical feedback)
bool InTargetPosition; // Axis is at target position (PEH) (physical feedback)
bool Protected; // Axis is in a protected operating mode (e.g. as a slave axis, or under axis interpolation)
bool ErrorPrewarning; // NEW: Axis signals an error pre warning (from TC 2.11)
bool HasBeenStopped; // Axis has been stopped or is presently executing a stop
bool HasJob; // Axis has instructions, is carrying instructions out
bool PositiveDirection; // Axis moving to logically larger values
bool NegativeDirection; // Axis moving to logically smaller values
bool HomingBusy; // Axis referenced ("Axis being calibrated")
bool ConstantVelocity; // Axis has reached its constant velocity or rotary speed
bool Compensating; // Section compensation passive[0]/active[1] (s. "MC_MoveSuperImposed")
bool ExtSetPointGenEnabled; // External setpoint generator enabled
// Operating mode not yet executed (Busy). Not implemented yet!
bool ExternalLatchValid; // External latch value or sensing switch has become valid
bool NewTargetPos; // Axis has a new target position or a new velocity
// 18 0/1 Axis is not at target position or cannot reach the target position (e.g. stop). Not implemented yet!
bool ContinuousMotion; // Axis has target position (±) endless
bool ControlLoopClosed; // Axis is ready for operation and axis control loop is closed (e.g. position control)
bool CamTableQueued; // CAM table is queued for "Online Change" and waiting for activation
bool CamDataQueued; // CAM data (only MF) are queued for "Online Change" and waiting for activation
bool CamScalingPending; // CAM scaling are queued for "Online Change" and waiting for activation
bool CmdBuffered; // NEW: Following command is queued in then command buffer (s. Buffer Mode)
//(from TwinCAT V2.10 Build 1311)
bool PTPmode; // NEW: Axis in PTP mode (no slave, no NCI axis) (from TC 2.10 Build 1326)
bool SoftLimitMinExceeded; // NEW: Position software limit switch minimum is exceeded (from TC 2.10 Build 1327)
bool SoftLimitMaxExceeded; // NEW: Position software limit switch maximum is exceeded (from TC 2.10 Build 1327)
bool DriveDeviceError; // NEW: Hardware drive device error (no warning), interpretation only possible when drive is data exchanging, e.g. EtherCAT "OP"-state (from TC 2.10 Build 1326)
bool MotionCommandsLocked; // NEW: Axis is locked for motion commands (TcMc2)
bool IoDataInvalid; // NEW: IO data invalid (e.g. 'WcState' or 'CdlState')
bool Error; // Axis is in a fault state
};
#pragma pack(pop) //恢復默認對齊
//#pragma pack()
#endif
結構體轉換函數
//相關宏定義
//定義對應beckhoff數據類型
#define BK_LREAL double //8 bytes
#define BK_USINT unsigned char //1 byte
#define BK_UINT unsigned short int //2 byte
#define BK_INT short int //2byte
#define BK_BOOL unsigned char //1 bit
#define BK_UDINT unsigned int//4 bytes
#define BK_DINT int //4 bytes
#define BK_DWORD unsigned int //4 bytes
//處理函數,將BK_DWORD轉換爲對應的結構體
//c++的bool是一個字節(我一直以爲只佔一個內存bit)
NCTOPLC_AXLESTRUCT_nStateDWord mg_uint2NCTOPLC_AXLESTRUCT_nStateDWord( unsigned int fml_val )
{
NCTOPLC_AXLESTRUCT_nStateDWord nState;
int siz = sizeof(NCTOPLC_AXLESTRUCT_nStateDWord);
// memcpy(&nState, &fml_val, sizeof(nState));
nState.Operational = (fml_val & (0x01)) != 0;
nState.Homed = (fml_val & (0x01<<1)) != 0;
nState.NotMoving = (fml_val & (0x01<<2)) != 0;
nState.InTargetPosition = (fml_val & (0x01<<4)) != 0;
nState.HasJob = (fml_val & (0x01<<8)) != 0;
nState.Error = (fml_val & (0x01>>31)) != 0;
return nState;
}