Skip to main content

IO status monitoring

Real-time monitoring of IO involves the use of threads or timers. Readers who are not familiar with these mechanisms may first need to acquaint themselves with the knowledge in this area. In this example, the use of interfaces such as int NRC_ReadDigOut(int port); and int NRC_ReadDigIn(int port); and other interfaces are mainly explained in combination with threads for developers' reference. First, developers need to understand the use of these interfaces. Taking NRC_ReadDigOut as an example, the introduction is as follows:

/**
* @brief Read the status of the digital IO output port
* @param port The digital IO port to read, with a maximum range of 1 <= port <= 16, the actual range depends on the number of IO ports on the connected IO module
* @return Return the current status of the port
* @retval 0 Currently at a low level
* @retval 1 Currently at a high level
* @retval -1 The port does not exist
*/
int NRC_ReadDigOut(int port);

Demo program for viewing IO status:

For example, when the status of IO port 1 is obtained as 1, execute a simple motion queue

#include "nrcAPI.h"
#include <stdio.h>
#include <unistd.h>
#include <thread>
#include <time.h>

void SystemStartup()
{
... // Refer to the previous case
}

// Get the real-time status of the IO board port
*void getIoStatus()
{
int port = 1;
while (true) // In this example, a while loop is used to obtain the IO status in real time, which is effective throughout the entire program lifecycle. The actual effective period depends on the developer's usage
{
// Call NRC_ReadDigOut to read the status of the IO output port with port number 1
static int status = 0;
static int waitingtime = 0;
static bool first = true;
static bool flag = false;
int port_1_status = NRC_ReadDigOut(port);
if (port_1_status == 1)
{
if ((status == 0))
{
if(first)
{
flag = true;
first = false;
waitingtime = std::time(NULL);
}
else
{
if ((std::time(NULL) - waitingtime ) * (1000 / sysconf(_SC_CLK_TCK) ) > 500)
{
flag = true;
waitingtime = std::time(NULL);
}
}
}
status = 1;
}
else
{
status = 0;
}
if (flag)
{
runRobot();
}
// Delay 1s
NRC_Delayms(1000);
}
}

void runRobot()
{
NRC_Position posACS1 = {NRC_ACS, 10,0,0,18.1,0,0}; // Input points are defined according to the actual situation, be safe!!!
NRC_Position posACS2 = {NRC_ACS, 2.1,36.1,33.96,13.13,2.88,23};
NRC_Position posACS3 = {NRC_ACS, 33, 0, 38, 5.5, 4, 0};
NRC_CreateNoFlieRunqueue();
NRC_RunqueueInsertMOVJ(60, 60, 60, posACS1);
NRC_RunqueueInsertMOVJ(60, 60, 60, posACS2);
NRC_RunqueueInsertMOVJ(60, 60, 60, posACS3);
NRC_RunqueueInsertDOUT(1, 0);
NRC_PowerOn();
NRC_StartRunNoFlieRunqueue();
}

int main()
{
SystemStartup();// System startup
NRC_ClearServoError();
/*
...
Other function codes can be edited here
...
*/
std::thread IoStatusThread(getIoStatus); // Start the thread that monitors IO status
while(1) // Keep the program running, indispensable
{
NRC_Delayms(200);
}
}