Skip to content

Master Station Library

Introduction

The INEXBOT EtherCAT Master Station controller has a master station jitter time of less than 20us. It can be widely applied to the development of industrial automation control systems, especially in scenarios with high real-time requirements such as robotics and servo motor control.

Version Information

Secondary Development VersionCompany
1.0.0INEXBOT

Changelog

VersionDateAuthorDescription
1.0.020250310EAInitial version

Overview

About This Document

This document aims to help users use the INEXBOT EtherCAT Master Station control C++ library libNexIghEcm.

About the libNexIghEcm Library

This library can automatically generate ENI files. Users only need to obtain PDO operation addresses and register periodic tasks, greatly reducing the difficulty of using the EtherCAT Master Station.

Development Environment Requirements

Operating SystemUbuntu 20.04 LTS
System Architecturex86_64
CompilerGCC version 9.4.0/GLIBC 2.31-0ubuntu9.2
GCC version 4.8.2/EGLIBC 2.19-0ubuntu6.15
DependenciesLibpthread, librt, libdl, libm

| --- | --- |

Library API Reference

Usage Overview

  1. Copy the libNexIghEcm.a static library file to the project's lib directory

  2. Copy the EcMasterApi.h header file to the project's include directory

  3. Link the libNexIghEcm.a library during compilation

NexIghLib API Function List

Function NameFunction Description
startEcMasterStart the master station
enableRealtimeEnvironmentEnable the real-time environment
ECM_LogMsgMessage-level log output provided by the master station
ECM_LogErrorError-level log output provided by the master station
ecatGetConnectedSlavesNumNumber of slaves connected to the master station
ecatGetConfiguredSlavesNumNumber of slaves configured by the master station
ecatGetSlaveStateGet the state of a slave
ecatSetSDOSet SDO
ecatGetSDOGet SDO
setEcatLicenseKeySet the master station key (empty function, no effect)
isEcatLicenseCorrectGet whether the master station key is correct
setLogDirNameSet the directory for master station log output
setEcatLogSwitchSet whether master station log is enabled
getCycleTimeGet the real-time status of the master station
getEcLibVersionGet the master station library version
getEniFileNameGet the ENI file name
getPDOAddrVecGet the slave PDO data list
getSlaveIDVecGet the slave ID and code list
registerCustomeContrastIDRegister the callback function for whether the master station is complete
registerCustomeAppWorkpdRegister the callback function for comparing whether the slave ID found by the master station matches the preset slave ID
registerCustomeEcMasterStartFinishRegister the callback function called every cycle by the master station
registerCustomeAppLogRegister the callback function for outputting master station logs to the NEX log

Error Code Definitions

Error CodeDescription
0No error
1Bus busy state
2Bus is being used
3Bus error
4Device access timeout
5IO device inaccessible
6Invalid parameter
7File loading error
8Dynamic library loading error
9EtherCAT master request error
10Error getting slave count
11EtherCAT master failed to get slave
12XML file open failure
13XML information parsing failure
14Slave configuration data initialization failure
15Creating EtherCAT domain failure
16Error configuring slave
17Error configuring slave PDO
18Error configuring slave PDO
19Error selecting EtherCAT master reference clock
20Error configuring slave Sync0 signal
21Master activation failure
22Domain data error
23Domain size error
24Error getting EtherCAT PDO

Basic Data Types

typedef unsigned char EC_T_BYTE;
typedef unsigned short EC_T_WORD;
typedef unsigned int EC_T_DWORD;
typedef signed char EC_T_SBYTE;
typedef signed short EC_T_SWORD;
typedef signed int EC_T_SDWORD;
typedef double EC_T_REAL;

PDO Data Structure

Function Description

The PDOAddrData struct is the structure for PDO addresses. One struct represents one PDO address object.

Struct Prototype
typedef struct {
    EC_T_WORD index;
    EC_T_WORD subIndex;
    EC_T_BYTE* addrMap;
} PDOAddrData;
Member Variable Description
Variable NameDescription
IndexPDO index
subIndexPDO sub-index
addrMapPDO object address pointer

NexIghLib API Usage Flow

NexIghLib API usage flowchart showing the call sequence from initialization through master station startup to periodic callbacks

Usage Example

Demo.cpp

int CALLBACK()
{
    static int iState = -5000;
    if (iState < 3001)
    {
        ctrlwdSend(iState);
        iState++;
    }
    else
    {
        for (int i = 0; i < slave_num; i++)
        {
            if (target_position[i] != nullptr)
            {
                iPos[i] += 300;
                *target_position[i] = iPos[i];
            }
        }
    }
    return 0;
}




void myprintf(unsigned char c1, const char *s1, const char *s2, const char *s3, const long n, const char *format, ...)
{
    char dest[1024 * 16 * 16];
    va_list argptr;
    va_start(argptr, format);
    vsprintf(dest, format, argptr);
    va_end(argptr);
    printf(dest);
}


int main()
{
// Initialize parameters
    int nArgc = 1;
    char *argv[8];
    int CycleTime = 1000;
    argv[0]=(char *)(&CycleTime);
 
// Enable real-time environment
    enableRealtimeEnvironment();
// Register log callback
    EC_PF_EC_START_CustomeLog_CALLBACK p2 = myprintf;
    registerCustomeAppLog(p2);
// Start master station
    startEcMaster(nArgc,argv);
// Get slave count
    slave_num = ecatGetConnectedSlavesNum();
    printf("getSlaveIDVec\n");
// Get slave ID array
    std::vector<std::pair<unsigned int, unsigned int> > vectorID;
    getSlaveIDVec(vectorID);
 
// Get PDO address mapping
    printf("getPDOAddrVec\n");
    PDOAddrVec outPDOAddrVec, inPDOAddrVec;
    getPDOAddrVec(outPDOAddrVec, inPDOAddrVec);
// Access output PDO
    for (unsigned int i = 0; i < outPDOAddrVec.size(); i++) {
        for (unsigned int j = 0; j < outPDOAddrVec[i].size(); j++) {
            if ((outPDOAddrVec[i][j].index == 0x6040)
                    && (outPDOAddrVec[i][j].subIndex == 0x00)) {
                control_word[i] = (EC_T_WORD*) (outPDOAddrVec[i][j].addrMap);
                printf("get slave %d control_word addr = %x\n", i, control_word[i]);
            }
        }
    }
// Access input PDO
    for (unsigned int i = 0; i < inPDOAddrVec.size(); i++) {
        for (unsigned int j = 0; j < inPDOAddrVec[i].size(); j++) {
            if ((inPDOAddrVec[i][j].index == 0x6041)
                    && (inPDOAddrVec[i][j].subIndex == 0x00)) {
                status_word[i] = (EC_T_WORD*) (inPDOAddrVec[i][j].addrMap);
                printf("get slave %d status_word addr = %x\n", i, status_word[i]);
            }
        }
    }
// Register custom
    printf("registerCustomeAppWorkpd\n");
    EC_PF_EC_START_AppWorkpd_CALLBACK p = CALLBACK;
    registerCustomeAppWorkpd(p);
    while(1)
    {
      // Custom event loop
    }
    return 0;
}

MakeFile

# Specify compiler
CXX ?= g++
 
CFLAGS = -std=c++11
# Add libNexIghEcm.a dependency
LDFLAGS = -lm -ldl -lpthread -lrt
 
INCLUDE = -I.
# Link libNexIghEcm.a from the ../ directory
LIB = -L.. -lNexIghEcm 
 
OBJ += $(patsubst %.cpp, %.o, $(wildcard *.cpp))
 
target = test
 
all:$(OBJ)
    $(CXX) *.o -o $(target) $(LIB) $(LDFLAGS)
%.o:%.cpp
    $(CXX) $(CFLAGS) -c $< -o $@ $(INCLUDE)
clean:
    rm -rf *.o $(target)

NexIghLib API Detailed Reference

startEcMaster

Function Prototypeint startEcMaster();
Function DescriptionStart the EtherCAT Master Station.
Parameter DescriptionNone
Return ValueReturns an int value, 0 indicates success, non-zero indicates failure.
NotesNone

enableRealtimeEnvironment

Function Prototypeint enableRealtimeEnvironment();
Function DescriptionEnable the real-time environment.
Parameter DescriptionNone
Return ValueReturns an int value, 0 indicates success, non-zero indicates failure.
NotesThis function is used to enable the real-time environment so that the master station can run in the real-time system.

ECM_LogMsg

Function Prototypevoid ECM_LogMsg(const char* msg);
Function DescriptionLog a message.
Parameter DescriptionInput parameter:
msg: the log message to record, type const char*.
Return ValueNone
NotesThis function is used to record regular log messages. It can be used in the system for debugging and runtime tracking.

ECM_LogError

Function Prototypevoid ECM_LogError(const char* errorMsg);
Function DescriptionLog an error.
Parameter DescriptionInput parameter:
errorMsg: the error message to record, type const char*.
Return ValueNone
NotesThis function is used to record error logs. Suitable for recording error information that occurs during program execution.

ecatGetConnectedSlavesNum

Function Prototypeint ecatGetConnectedSlavesNum();
Function DescriptionGet the number of slaves connected to the master station.
Parameter DescriptionNone
Return ValueReturns an int value representing the number of connected slaves.
NotesThis function is used to get the number of slaves successfully connected to the master station.

ecatGetConfiguredSlavesNum

Function Prototypeint ecatGetConfiguredSlavesNum();
Function DescriptionGet the number of configured slaves.
Parameter DescriptionNone
Return ValueReturns an int value representing the number of configured slaves.
NotesNone

ecatGetSlaveState

Function PrototypeEC_T_WORD ecatGetSlaveState(EC_T_DWORD ecSlaveNum);
Function DescriptionGet the state of the specified slave.
Parameter DescriptionInput parameter:
ecSlaveNum: slave number, starting from 0.
Return ValueThe state of the slave, type EC_T_WORD.
NotesThis function is used to get the state information of the specified slave.

ecatSetSDO

Function Prototypeint ecatSetSDO(EC_T_DWORD slaveNum, EC_T_WORD index, EC_T_DWORD subindex, EC_T_BYTE* value, EC_T_DWORD size);
Function DescriptionSet the SDO value of a slave.
Parameter DescriptionInput parameters:
slaveNum: slave number.
index: SDO index.
subindex: SDO sub-index.
value: the SDO data value to set.
size: the size of the data value.
Return ValueReturns an int value, 0 indicates success, non-zero indicates failure.
NotesNone

ecatGetSDO

Function Prototypeint ecatGetSDO(EC_T_DWORD slaveNum, EC_T_WORD index, EC_T_DWORD subindex, EC_T_BYTE* value, EC_T_DWORD size, int num = 0);
Function DescriptionGet the SDO value of a slave.
Parameter DescriptionInput parameters:
slaveNum: slave number.
index: SDO index.
subindex: SDO sub-index.
value: buffer for storing the SDO value.
size: the size of the buffer.
num: optional parameter, default is 0, indicating the number of SDO data items to get.
Output parameter:
value: the received SDO data value.
Return ValueReturns an int value, 0 indicates success, non-zero indicates failure.
------
NotesThis function is used to get the SDO value of a slave.

setEcatLicenseKey

Function Prototypevoid setEcatLicenseKey(const char* licenseKey);
Function DescriptionSet the EtherCAT license key.
Parameter DescriptionInput parameter:
licenseKey: EtherCAT license key, type const char*
Return ValueNone
NotesEmpty function with no effect

isEcatLicenseCorrect

Function Prototypebool isEcatLicenseCorrect();
Function DescriptionCheck whether the EtherCAT license is valid.
Parameter DescriptionNone
Return ValueNone
NotesEmpty function with no effect

setLogDirName

Function Prototypevoid setLogDirName(const char* dir);
Function DescriptionSet the log output directory.
Parameter DescriptionInput parameter:
dir: log directory path, type const char*.
Return ValueNone
NotesThis function is used to set the output directory for log files.

setEcatLogSwitch

Function Prototypevoid setEcatLogSwitch(bool enable);
Function DescriptionSet the EtherCAT log switch.
Parameter DescriptionInput parameter:
enable: boolean value, true indicates enabling logging, false indicates disabling logging.
Return ValueNone
NotesThis function is used to enable or disable the EtherCAT logging function.

getCycleTime

Function PrototypeCycleTime getCycleTime();
Function DescriptionGet the real-time cycle time of the master station.
Parameter DescriptionNone
Return ValueCycleTime type, representing the cycle time.
NotesThis function is used to get the real-time cycle time of the master station.

getEcLibVersion

Function Prototypeconst char* getEcLibVersion();
Function DescriptionGet the version number of the EtherCAT library.
Parameter DescriptionNone
Return Valueconst char*, representing the version number of the EtherCAT library.
NotesNone

getEniFileName

Function Prototypeconst char* getEniFileName();
Function DescriptionGet the configured ENI file name.
Parameter DescriptionNone
Return Valueconst char*, representing the configured ENI file name.
NotesThis function is used to get the currently configured ENI file name.

getPDOAddrVec

Function Prototypeconst std::vector<EC_T_DWORD>& getPDOAddrVec();
Function DescriptionGet the PDO address vector.
Parameter DescriptionNone
Return ValueReturns a reference to the PDO address vector representing the PDO addresses for communication with the master station.
NotesThis function is used to get the PDO address information between the master station and slaves.

getSlaveIDVec

Function Prototypeconst std::vector<EC_T_DWORD>& getSlaveIDVec();
Function DescriptionGet the slave ID vector.
Parameter DescriptionNone
Return Valuestd::vector<EC_T_DWORD> type, representing the slave ID vector.
NotesThis function is used to get the IDs of all slaves in the current network.

registerCustomeContrastID

Function Prototypevoid registerCustomeContrastID(void (*callback)(EC_T_DWORD slaveNum, bool isMatched));
Function DescriptionRegister a custom callback function for comparing IDs.
Parameter DescriptionInput parameter:
callback: callback function that accepts two parameters:
slaveNum: slave number.
isMatched: boolean value, indicating whether the slave ID matches the preset ID.
Return ValueNone
NotesThis function is used to register a callback function that will be called when the master station checks whether the slave ID matches the preset ID.

registerCustomeAppWorkpd

Function Prototypevoid registerCustomeAppWorkpd(void (*callback)(EC_T_DWORD slaveNum, bool isMatched));
Function DescriptionRegister a custom work cycle callback function.
Parameter DescriptionInput parameter:
callback: callback function that accepts two parameters:
slaveNum: slave number.
isMatched: boolean value, indicating whether the slave ID matches the preset ID.
Return ValueNone
NotesThis function is used to register a work cycle callback function.

registerCustomeEcMasterStartFinish

Function Prototypevoid registerCustomeEcMasterStartFinish(void (*callback)(bool success));
Function DescriptionRegister a custom EtherCAT Master Station startup completion callback function.
Parameter DescriptionInput parameter:
callback: callback function that accepts a boolean value success, indicating whether the startup was successful.
Return ValueNone
NotesThis function is used to register a work cycle callback function.

registerCustomeAppLog

Function Prototypevoid registerCustomeAppLog(void (callback)(const char logMessage));
Function DescriptionRegister an application log callback function.
Parameter DescriptionInput parameter:
callback: callback function that accepts a string logMessage representing the log information.
Return ValueNone
NotesThis function is used to register a custom log callback function.