跳到主要内容

IO状态监控

实时监控IO涉及到线程或者定时器的使用,不了解这些机制的读者,可以先自己对这一方面的知识进行了解。 本例中,主要对int NRC_ReadDigOut(int port); int NRC_ReadDigIn(int port);等接口的使用结合线程进行讲解开发,供开发者参考。 首先需要开发者了解这些接口的使用介绍,以 NRC_ReadDigOut 为例介绍如下:

/**
* @brief 读取数字IO输出端口的状态
* @param port 要读取的数字IO端口,最大范围为 1 <= port <= 16,实际范围取决于所连接点IO模块上的IO端口数目
* @return 返回该端口当前状态
* @retval 0 当前处于低电平
* @retval 1 当前处于高电平
* @retval -1 该端口不存在
*/
int NRC_ReadDigOut(int port);

IO状态查看的 demo 程序:

如获取IO端口 1 的状态为 1 时,执行一个简单的运动队列

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

void SystemStartup()
{
... // 参考前面案例
}

// 获取IO板端口的实时状态
*void getIoStatus()
{
int port = 1;
while (true) // 本例利用循环实时获取,在整个程序生命周期中有效,实际需要按照开发者的使用情况而定
{
// 调用 NRC_ReadDigOut 读取 端口号为 1 的输出IO口的状态
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();
}
// 延时 1 秒
NRC_Delayms(1000);
}
}

void runRobot()
{
NRC_Position posACS1 = {NRC_ACS, 10,0,0,18.1,0,0}; // 输入点位根据实际情况定义,注意安全!!!
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();//系统启动
NRC_ClearServoError();
/*
...
此处可编辑其他功能代码
...
*/
std::thread IoStatusThread(getIoStatus); // 启动监控IO状态的线程
while(1) //保持程序运行,不可缺少
{
NRC_Delayms(200);
}
}