add function
This commit is contained in:
parent
dee9ff6204
commit
4e8fd78421
255
protocol.c
255
protocol.c
@ -11,6 +11,8 @@ DeviceStatus deviceStatus = {
|
||||
.initStatus = 1
|
||||
};
|
||||
|
||||
uint8_t isValveMovingBackToOrigin = 0;
|
||||
|
||||
DeviceParam dp = {
|
||||
.pump = {
|
||||
{"pump1", 4, 60, 100, 100},
|
||||
@ -176,8 +178,9 @@ void updateInitStatus(InitStatus_t status) {
|
||||
*
|
||||
* @param txBuf 发送数据缓冲区
|
||||
* @param txLen 发送数据长度
|
||||
* @return 0:成功 其他:失败
|
||||
*/
|
||||
void writeCMD(uint8_t *txBuf, uint16_t txLen) {
|
||||
static uint8_t writeCMD(uint8_t *txBuf, uint16_t txLen) {
|
||||
printf("writeCMD: ");
|
||||
for(uint16_t i = 0; i < txLen; i++) {
|
||||
printf("%02X ", txBuf[i]);
|
||||
@ -189,7 +192,7 @@ void writeCMD(uint8_t *txBuf, uint16_t txLen) {
|
||||
transDataToMotorValve(txBuf, txLen);
|
||||
uint8_t rxBuf[30] = {0};
|
||||
uint16_t rxLen = txLen;
|
||||
uint8_t rst = HAL_UART_Receive(&huart2, rxBuf, rxLen, 50);
|
||||
uint8_t rst = HAL_UART_Receive(&huart2, rxBuf, rxLen, READ_ACK_TIMEOUT);
|
||||
if(memcmp(rxBuf, txBuf, 2) != 0) {
|
||||
log_e("writeCMD error");
|
||||
printf("%d readCMD: ", rst);
|
||||
@ -197,7 +200,9 @@ void writeCMD(uint8_t *txBuf, uint16_t txLen) {
|
||||
printf("%02X ", rxBuf[i]);
|
||||
}
|
||||
printf("\r\n");
|
||||
return 1;
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
/**
|
||||
@ -210,7 +215,7 @@ void writeCMD(uint8_t *txBuf, uint16_t txLen) {
|
||||
void sendMsgToHost(uint8_t *txBuf, uint16_t txLen) {
|
||||
// 发送数据
|
||||
// HAL_UART_Transmit_DMA(&huart1, txBuf, txLen);
|
||||
// transDataToHost(txBuf, txLen);
|
||||
transDataToHost(txBuf, txLen);
|
||||
}
|
||||
|
||||
|
||||
@ -270,12 +275,12 @@ uint16_t ReadPump1Reg(uint8_t index, uint16_t reg) {
|
||||
writeCMD(data, 8);
|
||||
uint8_t rxBuf[30] = {0};
|
||||
uint16_t rxLen = 8;
|
||||
uint8_t rst = HAL_UART_Receive(&huart2, rxBuf, rxLen, 50);
|
||||
uint8_t rst = HAL_UART_Receive(&huart2, rxBuf, rxLen, READ_ACK_TIMEOUT);
|
||||
return rxBuf[3]<<8|rxBuf[4];
|
||||
}
|
||||
|
||||
uint8_t ReadPump2Reg(uint8_t index, uint16_t reg) {
|
||||
uint8_t data[8] = {0};
|
||||
uint32_t ReadPump2Reg(uint8_t index, uint16_t reg) {
|
||||
uint8_t data[12] = {0};
|
||||
data[0] = index;
|
||||
data[1] = RTU_FUNC_READ_HOLD_REG;
|
||||
FillBigEndian16(&data[2], reg);
|
||||
@ -285,7 +290,12 @@ uint8_t ReadPump2Reg(uint8_t index, uint16_t reg) {
|
||||
// 小端序填充
|
||||
memcpy(&data[6], &crc, 2);
|
||||
|
||||
writeCMD(data, 8);
|
||||
writeCMD(data, 12);
|
||||
uint8_t rxBuf[30] = {0};
|
||||
uint16_t rxLen = 12;
|
||||
uint8_t rst = HAL_UART_Receive(&huart2, rxBuf, rxLen, READ_ACK_TIMEOUT*2);
|
||||
// 逻辑需进一步完善
|
||||
return rxBuf[6]<<24|rxBuf[7]<<16|rxBuf[8]<<8|rxBuf[9];
|
||||
}
|
||||
|
||||
/*
|
||||
@ -307,7 +317,7 @@ uint8_t WritePump1Reg(uint8_t id, uint16_t reg, int16_t value) {
|
||||
// 小端序填充
|
||||
memcpy(&data[6], &crc, 2);
|
||||
|
||||
writeCMD(data, 8);
|
||||
return writeCMD(data, 8);
|
||||
}
|
||||
|
||||
/*
|
||||
@ -331,7 +341,7 @@ uint8_t WritePump2Reg(uint8_t id , uint16_t reg, int32_t value) {
|
||||
// 小端序填充
|
||||
memcpy(&data[11], &crc, 2);
|
||||
|
||||
writeCMD(data, 13);
|
||||
return writeCMD(data, 13);
|
||||
}
|
||||
|
||||
/*
|
||||
@ -354,7 +364,7 @@ uint8_t WritePump2Reg(uint8_t id , uint16_t reg, int32_t value) {
|
||||
* @return 0:成功 其他:失败
|
||||
*/
|
||||
static uint8_t SetPumpJogAcc(uint8_t index, uint16_t acc) {
|
||||
WritePump1Reg(index, RTU_PUMP_CMD_JA, acc*PUMP_ACCEL_RPS);
|
||||
return WritePump1Reg(dp.pump[index].id, RTU_PUMP_CMD_JA, acc*PUMP_ACCEL_RPS);
|
||||
}
|
||||
|
||||
/**
|
||||
@ -365,7 +375,7 @@ static uint8_t SetPumpJogAcc(uint8_t index, uint16_t acc) {
|
||||
* @return 0:成功 其他:失败
|
||||
*/
|
||||
static uint8_t SetPumpJogDec(uint8_t index, uint16_t dec) {
|
||||
WritePump1Reg(index, RTU_PUMP_CMD_JL, dec*PUMP_DECEL_RPS);
|
||||
return WritePump1Reg(dp.pump[index].id, RTU_PUMP_CMD_JL, dec*PUMP_DECEL_RPS);
|
||||
}
|
||||
|
||||
/**
|
||||
@ -376,14 +386,14 @@ static uint8_t SetPumpJogDec(uint8_t index, uint16_t dec) {
|
||||
* @return 0:成功 其他:失败
|
||||
*/
|
||||
static uint8_t SetPumpJogSpeed(uint8_t index, uint16_t speed) {
|
||||
WritePump1Reg(index, RTU_PUMP_CMD_JS, speed*PUMP_SPEED_RPS);
|
||||
return WritePump1Reg(dp.pump[index].id, RTU_PUMP_CMD_JS, speed*PUMP_SPEED_RPS);
|
||||
}
|
||||
|
||||
// Jog=慢跑
|
||||
// CJ=start jogging
|
||||
// 写入命令操作码寄存器(40125)数据0x0096(CJ),即执行启动Jog控制
|
||||
static uint8_t StartPumpJog(uint8_t index) {
|
||||
WritePump1Reg(index, RTU_PUMP_CMD_CO, 0x0096);
|
||||
return WritePump1Reg(dp.pump[index].id, RTU_PUMP_CMD_CO, 0x0096);
|
||||
}
|
||||
|
||||
// SJ=stop jogging
|
||||
@ -391,7 +401,7 @@ static uint8_t StartPumpJog(uint8_t index) {
|
||||
// CJ与SJ一一对应,单次SJ无法停止所有全部CJ
|
||||
// 直接停止泵需要使用SK命令
|
||||
static uint8_t StopPumpJog(uint8_t index) {
|
||||
WritePump1Reg(index, RTU_PUMP_CMD_CO, 0x00D8);
|
||||
return WritePump1Reg(dp.pump[index].id, RTU_PUMP_CMD_CO, 0x00D8);
|
||||
}
|
||||
|
||||
/*
|
||||
@ -415,7 +425,7 @@ static uint8_t StopPumpJog(uint8_t index) {
|
||||
* @return 0:成功 其他:失败
|
||||
*/
|
||||
static uint8_t SetPumpStepAcc(uint8_t index, uint16_t acc) {
|
||||
WritePump1Reg(index, RTU_PUMP_CMD_AC, acc*PUMP_ACCEL_RPS);
|
||||
return WritePump1Reg(dp.pump[index].id, RTU_PUMP_CMD_AC, acc*PUMP_ACCEL_RPS);
|
||||
}
|
||||
/**
|
||||
* 设置泵的步进减速度
|
||||
@ -425,7 +435,7 @@ static uint8_t SetPumpStepAcc(uint8_t index, uint16_t acc) {
|
||||
* @return 0:成功 其他:失败
|
||||
*/
|
||||
static uint8_t SetPumpStepDec(uint8_t index, uint16_t dec) {
|
||||
WritePump1Reg(index, RTU_PUMP_CMD_DE, dec*PUMP_DECEL_RPS);
|
||||
return WritePump1Reg(dp.pump[index].id, RTU_PUMP_CMD_DE, dec*PUMP_DECEL_RPS);
|
||||
}
|
||||
/**
|
||||
* 设置泵的步进速度
|
||||
@ -438,8 +448,7 @@ static uint8_t SetPumpStepDec(uint8_t index, uint16_t dec) {
|
||||
static uint8_t SetPumpStepSpeed(uint8_t index, uint16_t speed) {
|
||||
// 速度类寄存器参数设定值单位为 1/240 rps
|
||||
speed = speed * PUMP_SPEED_RPS;
|
||||
WritePump1Reg(index, RTU_PUMP_CMD_VE, speed);
|
||||
return 0;
|
||||
return WritePump1Reg(dp.pump[index].id, RTU_PUMP_CMD_VE, speed);
|
||||
}
|
||||
/**
|
||||
* 设置泵的步进目标位置
|
||||
@ -449,7 +458,7 @@ static uint8_t SetPumpStepSpeed(uint8_t index, uint16_t speed) {
|
||||
* @return 0:成功 其他:失败
|
||||
*/
|
||||
static uint8_t SetPumpStepTarget(uint8_t index, int32_t target) {
|
||||
WritePump2Reg(dp.pump[index].id, RTU_PUMP_CMD_DI, target);
|
||||
return WritePump2Reg(dp.pump[index].id, RTU_PUMP_CMD_DI, target);
|
||||
}
|
||||
|
||||
/**
|
||||
@ -460,7 +469,7 @@ static uint8_t SetPumpStepTarget(uint8_t index, int32_t target) {
|
||||
* @return 0:成功 其他:失败
|
||||
*/
|
||||
static uint8_t StartPumpRelativeMove(uint8_t index) {
|
||||
WritePump1Reg(dp.pump[index].id, RTU_PUMP_CMD_CO, 0x0066);
|
||||
return WritePump1Reg(dp.pump[index].id, RTU_PUMP_CMD_CO, 0x0066);
|
||||
}
|
||||
|
||||
/**
|
||||
@ -471,7 +480,7 @@ static uint8_t StartPumpRelativeMove(uint8_t index) {
|
||||
* @return 0:成功 其他:失败
|
||||
*/
|
||||
static uint8_t StartPumpAbsoluteMove(uint8_t index) {
|
||||
WritePump1Reg(index, RTU_PUMP_CMD_CO, 0x0067);
|
||||
return WritePump1Reg(dp.pump[index].id, RTU_PUMP_CMD_CO, 0x0067);
|
||||
}
|
||||
|
||||
/**
|
||||
@ -482,7 +491,7 @@ static uint8_t StartPumpAbsoluteMove(uint8_t index) {
|
||||
* @return 0:成功 其他:失败
|
||||
*/
|
||||
static uint8_t StopPump(uint8_t index) {
|
||||
WritePump1Reg(dp.pump[index].id, RTU_PUMP_CMD_CO, 0x00E1);
|
||||
return WritePump1Reg(dp.pump[index].id, RTU_PUMP_CMD_CO, 0x00E1);
|
||||
}
|
||||
|
||||
/**
|
||||
@ -492,7 +501,7 @@ static uint8_t StopPump(uint8_t index) {
|
||||
* @return 0:成功 其他:失败
|
||||
*/
|
||||
uint8_t ReadPumpHWReg(uint8_t index) {
|
||||
ReadPump1Reg(index, RTU_PUMP_CMD_HW);
|
||||
return ReadPump1Reg(dp.pump[index].id, RTU_PUMP_CMD_HW);
|
||||
}
|
||||
/**
|
||||
* 设置泵的通信波特率
|
||||
@ -502,7 +511,7 @@ uint8_t ReadPumpHWReg(uint8_t index) {
|
||||
* @return 0:成功 其他:失败
|
||||
*/
|
||||
uint8_t SetPumpBR(uint8_t index, uint16_t br) {
|
||||
WritePump1Reg(dp.pump[index].id, RTU_PUMP_CMD_BR, br);
|
||||
return WritePump1Reg(dp.pump[index].id, RTU_PUMP_CMD_BR, br);
|
||||
}
|
||||
/**
|
||||
* 设置泵的通信协议
|
||||
@ -512,7 +521,7 @@ uint8_t SetPumpBR(uint8_t index, uint16_t br) {
|
||||
* @return 0:成功 其他:失败
|
||||
*/
|
||||
uint8_t SetPumpPR(uint8_t index, uint16_t pr) {
|
||||
WritePump1Reg(dp.pump[index].id, RTU_PUMP_CMD_PR, pr);
|
||||
return WritePump1Reg(dp.pump[index].id, RTU_PUMP_CMD_PR, pr);
|
||||
}
|
||||
|
||||
/**
|
||||
@ -618,19 +627,20 @@ void UpdatePumpStatus() {
|
||||
uint8_t InitPump(void) {
|
||||
// 初始化泵
|
||||
log_e("InitPump");
|
||||
SetPumpJogAcc(dp.pump[0].id, dp.pump[0].maxAccel);
|
||||
SetPumpJogDec(dp.pump[0].id, dp.pump[0].maxDecel);
|
||||
SetPumpJogSpeed(dp.pump[0].id, dp.pump[0].maxSpeed);
|
||||
SetPumpStepAcc(dp.pump[0].id, dp.pump[0].maxAccel);
|
||||
SetPumpStepDec(dp.pump[0].id, dp.pump[0].maxDecel);
|
||||
SetPumpStepSpeed(dp.pump[0].id, dp.pump[0].maxSpeed);
|
||||
uint8_t rst = 0;
|
||||
rst |= SetPumpJogAcc(dp.pump[0].id, dp.pump[0].maxAccel);
|
||||
rst |= SetPumpJogDec(dp.pump[0].id, dp.pump[0].maxDecel);
|
||||
rst |= SetPumpJogSpeed(dp.pump[0].id, dp.pump[0].maxSpeed);
|
||||
rst |= SetPumpStepAcc(dp.pump[0].id, dp.pump[0].maxAccel);
|
||||
rst |= SetPumpStepDec(dp.pump[0].id, dp.pump[0].maxDecel);
|
||||
rst |= SetPumpStepSpeed(dp.pump[0].id, dp.pump[0].maxSpeed);
|
||||
|
||||
SetPumpJogAcc(dp.pump[1].id, dp.pump[1].maxAccel);
|
||||
SetPumpJogDec(dp.pump[1].id, dp.pump[1].maxDecel);
|
||||
SetPumpJogSpeed(dp.pump[1].id, dp.pump[1].maxSpeed);
|
||||
SetPumpStepAcc(dp.pump[1].id, dp.pump[1].maxAccel);
|
||||
SetPumpStepDec(dp.pump[1].id, dp.pump[1].maxDecel);
|
||||
SetPumpStepSpeed(dp.pump[1].id, dp.pump[1].maxSpeed);
|
||||
rst |= SetPumpJogAcc(dp.pump[1].id, dp.pump[1].maxAccel);
|
||||
rst |= SetPumpJogDec(dp.pump[1].id, dp.pump[1].maxDecel);
|
||||
rst |= SetPumpJogSpeed(dp.pump[1].id, dp.pump[1].maxSpeed);
|
||||
rst |= SetPumpStepAcc(dp.pump[1].id, dp.pump[1].maxAccel);
|
||||
rst |= SetPumpStepDec(dp.pump[1].id, dp.pump[1].maxDecel);
|
||||
rst |= SetPumpStepSpeed(dp.pump[1].id, dp.pump[1].maxSpeed);
|
||||
|
||||
uint16_t reg4001 = ReadPumpAlarm(0);
|
||||
DecodePumpAlarmMsg(reg4001);
|
||||
@ -641,6 +651,8 @@ uint8_t InitPump(void) {
|
||||
DecodePumpAlarmMsg(reg4001);
|
||||
reg4002 = ReadPumpStatus(1);
|
||||
DecodePumpStatusMsg(reg4002);
|
||||
|
||||
return rst;
|
||||
}
|
||||
|
||||
|
||||
@ -684,6 +696,60 @@ void (*writeValve2Reg)(uint8_t index, uint16_t reg, uint32_t value) = WritePump2
|
||||
void (*readValve1Reg)(uint8_t index, uint16_t reg) = ReadPump1Reg;
|
||||
void (*readValve2Reg)(uint8_t index, uint16_t reg) = ReadPump2Reg;
|
||||
|
||||
/**
|
||||
* 读取阀门1个输入寄存器
|
||||
*
|
||||
* @param index 阀门索引
|
||||
* @param reg 寄存器地址
|
||||
* @return 寄存器值
|
||||
*/
|
||||
uint16_t ReadValve1InputReg(uint8_t index, uint16_t reg)
|
||||
{
|
||||
uint8_t data[8] = {0};
|
||||
data[0] = index;
|
||||
data[1] = RTU_FUNC_READ_INPUT_REG;
|
||||
FillBigEndian16(&data[2], reg);
|
||||
FillBigEndian16(&data[4], 1);
|
||||
|
||||
uint16_t crc = CalculateCRC16(data, 6);
|
||||
// 小端序填充
|
||||
memcpy(&data[6], &crc, 2);
|
||||
|
||||
writeCMD(data, 8);
|
||||
uint8_t rxBuf[30] = {0};
|
||||
uint16_t rxLen = 8;
|
||||
uint8_t rst = HAL_UART_Receive(&huart2, rxBuf, rxLen, READ_ACK_TIMEOUT);
|
||||
return rxBuf[3]<<8|rxBuf[4];
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* 读取阀门2个输入寄存器
|
||||
*
|
||||
* @param index 阀门索引
|
||||
* @param reg 寄存器地址
|
||||
* @return 寄存器值
|
||||
*/
|
||||
uint32_t ReadValve2InputReg(uint8_t index, uint16_t reg)
|
||||
{
|
||||
uint8_t data[8] = {0};
|
||||
data[0] = index;
|
||||
data[1] = RTU_FUNC_READ_INPUT_REG;
|
||||
FillBigEndian16(&data[2], reg);
|
||||
FillBigEndian16(&data[4], 2);
|
||||
|
||||
uint16_t crc = CalculateCRC16(data, 6);
|
||||
// 小端序填充
|
||||
memcpy(&data[6], &crc, 2);
|
||||
writeCMD(data, 8);
|
||||
|
||||
uint8_t rxBuf[30] = {0};
|
||||
uint16_t rxLen = 9;
|
||||
uint8_t rst = HAL_UART_Receive(&huart2, rxBuf, rxLen, READ_ACK_TIMEOUT);
|
||||
return rxBuf[3]<<24|rxBuf[4]<<16|rxBuf[5]<<8|rxBuf[6];
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* 设置阀门通信模式
|
||||
*
|
||||
@ -692,7 +758,7 @@ void (*readValve2Reg)(uint8_t index, uint16_t reg) = ReadPump2Reg;
|
||||
* @return 0:成功 其他:失败
|
||||
*/
|
||||
static uint8_t SetValveCOMMMode(uint8_t index, uint16_t mode) {
|
||||
WritePump1Reg(dp.valve[index].id, RTU_VALVE_CMD_CTL_MODE, mode);
|
||||
return WriteValve1Reg(dp.valve[index].id, RTU_VALVE_CMD_CTL_MODE, mode);
|
||||
}
|
||||
|
||||
/**
|
||||
@ -703,7 +769,7 @@ static uint8_t SetValveCOMMMode(uint8_t index, uint16_t mode) {
|
||||
* @return 0:成功 其他:失败
|
||||
*/
|
||||
static uint8_t SetValveRunMode(uint8_t index, uint16_t mode) {
|
||||
WritePump1Reg(dp.valve[index].id, RTU_VALVE_CMD_RUN_MODE, mode);
|
||||
return WriteValve1Reg(dp.valve[index].id, RTU_VALVE_CMD_RUN_MODE, mode);
|
||||
}
|
||||
|
||||
// PP=轮廓位置模式
|
||||
@ -715,7 +781,7 @@ static uint8_t SetValveRunMode(uint8_t index, uint16_t mode) {
|
||||
* @return 0:成功 其他:失败
|
||||
*/
|
||||
static uint8_t SetValvePPPos(uint8_t index, uint32_t pos) {
|
||||
writeValve2Reg(dp.valve[index].id, RTU_VALVE_CMD_PP_POS, pos);
|
||||
return writeValve2Reg(dp.valve[index].id, RTU_VALVE_CMD_PP_POS, pos);
|
||||
}
|
||||
|
||||
/**
|
||||
@ -726,7 +792,7 @@ static uint8_t SetValvePPPos(uint8_t index, uint32_t pos) {
|
||||
* @return 0:成功 其他:失败
|
||||
*/
|
||||
static uint8_t SetValvePPSpeed(uint8_t index, uint32_t speed) {
|
||||
writeValve2Reg(dp.valve[index].id, RTU_VALVE_CMD_PP_SPEED, speed);
|
||||
return writeValve2Reg(dp.valve[index].id, RTU_VALVE_CMD_PP_SPEED, speed);
|
||||
}
|
||||
|
||||
/**
|
||||
@ -737,7 +803,7 @@ static uint8_t SetValvePPSpeed(uint8_t index, uint32_t speed) {
|
||||
* @return 0:成功 其他:失败
|
||||
*/
|
||||
static uint8_t SetValvePPAcc(uint8_t index, uint32_t acc) {
|
||||
writeValve2Reg(dp.valve[index].id, RTU_VALVE_CMD_PP_ACCEL, acc);
|
||||
return writeValve2Reg(dp.valve[index].id, RTU_VALVE_CMD_PP_ACCEL, acc);
|
||||
}
|
||||
|
||||
/**
|
||||
@ -748,7 +814,7 @@ static uint8_t SetValvePPAcc(uint8_t index, uint32_t acc) {
|
||||
* @return 0:成功 其他:失败
|
||||
*/
|
||||
static uint8_t SetValvePPDec(uint8_t index, uint32_t dec) {
|
||||
writeValve2Reg(dp.valve[index].id, RTU_VALVE_CMD_PP_DECEL, dec);
|
||||
return writeValve2Reg(dp.valve[index].id, RTU_VALVE_CMD_PP_DECEL, dec);
|
||||
}
|
||||
|
||||
// HM=原点回归模式
|
||||
@ -760,7 +826,7 @@ static uint8_t SetValvePPDec(uint8_t index, uint32_t dec) {
|
||||
* @return 0:成功 其他:失败
|
||||
*/
|
||||
static uint8_t SetValveHomeDetectMode(uint8_t index, uint16_t mode) {
|
||||
writeValve1Reg(dp.valve[index].id, RTU_VALVE_CMD_HOME_MODE, mode);
|
||||
return writeValve1Reg(dp.valve[index].id, RTU_VALVE_CMD_HOME_MODE, mode);
|
||||
}
|
||||
|
||||
/**
|
||||
@ -771,7 +837,7 @@ static uint8_t SetValveHomeDetectMode(uint8_t index, uint16_t mode) {
|
||||
* @return 0:成功 其他:失败
|
||||
*/
|
||||
static uint8_t SetValveHomeSwtSpeed(uint8_t index, uint32_t speed) {
|
||||
writeValve2Reg(dp.valve[index].id, RTU_VALVE_CMD_HOME_SWT_SPEED, speed);
|
||||
return writeValve2Reg(dp.valve[index].id, RTU_VALVE_CMD_HOME_SWT_SPEED, speed);
|
||||
}
|
||||
|
||||
/**
|
||||
@ -782,7 +848,7 @@ static uint8_t SetValveHomeSwtSpeed(uint8_t index, uint32_t speed) {
|
||||
* @return 0:成功 其他:失败
|
||||
*/
|
||||
static uint8_t SetValveHomeOriSpeed(uint8_t index, uint32_t speed) {
|
||||
writeValve2Reg(dp.valve[index].id, RTU_VALVE_CMD_HOME_ORI_SPEED, speed);
|
||||
return writeValve2Reg(dp.valve[index].id, RTU_VALVE_CMD_HOME_ORI_SPEED, speed);
|
||||
}
|
||||
|
||||
/**
|
||||
@ -793,7 +859,7 @@ static uint8_t SetValveHomeOriSpeed(uint8_t index, uint32_t speed) {
|
||||
* @return 0:成功 其他:失败
|
||||
*/
|
||||
static uint8_t SetValveHomeAcc(uint8_t index, uint32_t acc) {
|
||||
writeValve2Reg(dp.valve[index].id, RTU_VALVE_CMD_HOME_ACCEL, acc);
|
||||
return writeValve2Reg(dp.valve[index].id, RTU_VALVE_CMD_HOME_ACCEL, acc);
|
||||
}
|
||||
|
||||
/**
|
||||
@ -804,7 +870,7 @@ static uint8_t SetValveHomeAcc(uint8_t index, uint32_t acc) {
|
||||
* @return 0:成功 其他:失败
|
||||
*/
|
||||
static uint8_t SetValveFunc(uint8_t index, uint16_t func) {
|
||||
writeValve1Reg(dp.valve[index].id, RTU_VALVE_CMD_FUNC, func);
|
||||
return writeValve1Reg(dp.valve[index].id, RTU_VALVE_CMD_FUNC, func);
|
||||
}
|
||||
|
||||
/**
|
||||
@ -815,7 +881,7 @@ static uint8_t SetValveFunc(uint8_t index, uint16_t func) {
|
||||
* @return 0:成功 其他:失败
|
||||
*/
|
||||
static uint8_t SetValveHomeTorque(uint8_t index, uint16_t torque) {
|
||||
writeValve1Reg(dp.valve[index].id, RTU_VALVE_CMD_HOME_TORQUE, torque*10);
|
||||
return writeValve1Reg(dp.valve[index].id, RTU_VALVE_CMD_HOME_TORQUE, torque*10);
|
||||
}
|
||||
|
||||
/**
|
||||
@ -826,9 +892,54 @@ static uint8_t SetValveHomeTorque(uint8_t index, uint16_t torque) {
|
||||
* @return 0:成功 其他:失败
|
||||
*/
|
||||
static uint8_t SetValveHomeTime(uint8_t index, uint16_t time) {
|
||||
writeValve1Reg(dp.valve[index].id, RTU_VALVE_CMD_HOME_TIME, time*10);
|
||||
return writeValve1Reg(dp.valve[index].id, RTU_VALVE_CMD_HOME_TIME, time*10);
|
||||
}
|
||||
|
||||
/**
|
||||
* 读取阀门位置
|
||||
*
|
||||
* @param index 阀门索引
|
||||
* @return 位置值(用户单位)
|
||||
*/
|
||||
static uint32_t ReadValvePos(uint8_t index) {
|
||||
return ReadValve2InputReg(index, RTU_VALVE_CMD_POS);
|
||||
}
|
||||
|
||||
/**
|
||||
* 检查原点回归结果
|
||||
* @return 0:成功 1:运动中 2:失败
|
||||
*/
|
||||
static uint8_t valveCheckBTOResult(uint8_t index, uint32_t startTime)
|
||||
{
|
||||
static uint32_t ts = HAL_GetTick();
|
||||
|
||||
if (HAL_GetTick() - startTime > 1000*20) {
|
||||
// 超时=失败
|
||||
return 2;
|
||||
}
|
||||
|
||||
uint16_t rst = ReadValve1InputReg(index,RTU_VALVE_CMD_SC);
|
||||
// 如果原点回归完成,状态字第12位会从0变为1,
|
||||
// 如果原点回归失败,状态字第13位会从0变为1。
|
||||
// 此外也可以附加判断电机当前位置是否在0附近的200个脉冲以内。
|
||||
if(rst & 0x0001<<12) {
|
||||
// 成功
|
||||
uint32_t pos = ReadValvePos(index);
|
||||
if(pos > 200 || pos < (VALVE_PULSE_PER_ROUND-200)) {
|
||||
// 位置超出范围
|
||||
return 2;
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
if(rst & 0x0001<<13) {
|
||||
// 失败
|
||||
return 2;
|
||||
}
|
||||
|
||||
// 运动中,未超时
|
||||
return 1;
|
||||
|
||||
}
|
||||
/**
|
||||
* 阀门回归原点控制
|
||||
* 包含设置原点回归方式、堵转检测、运行模式等配置
|
||||
@ -874,7 +985,7 @@ uint8_t ValveBackToOrigin(uint8_t index,uint8_t direction) {
|
||||
* @param index 阀门索引
|
||||
* @return 0:成功 其他:失败
|
||||
*/
|
||||
uint8_t ValveRunInit(uint8_t index) {
|
||||
uint8_t ValvePPInit(uint8_t index) {
|
||||
printf("set mode to PP\r\n");
|
||||
// 1.配置模式:
|
||||
// 00B1h=0、运行模式 03C2h=0x01,使设备工作在轮廓位置模式;
|
||||
@ -955,9 +1066,49 @@ void InitDeviceStatus() {
|
||||
updateInitStatus(INIT_SUCCESS);
|
||||
}
|
||||
|
||||
void updateValveStatus(uint8_t index)
|
||||
{
|
||||
|
||||
// 检查是否回归成功
|
||||
static uint32_t st = HAL_GetTick();
|
||||
if (isValveMovingBackToOrigin)
|
||||
{
|
||||
uint8_t rst = valveCheckBTOResult(index,st);
|
||||
switch (rst)
|
||||
{
|
||||
case 0:
|
||||
log_i("BTO success!");
|
||||
updateInitStatus(INIT_SUCCESS);
|
||||
break;
|
||||
case 1:
|
||||
log_d("BTO moving...");
|
||||
updateInitStatus(INIT_IN_PROGRESS);
|
||||
break;
|
||||
case 2:
|
||||
log_e("BTO failure!!!");
|
||||
updateInitStatus(INIT_FAILED);
|
||||
default:
|
||||
break;
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
st = HAL_GetTick();
|
||||
}
|
||||
|
||||
// 获取实时角度
|
||||
|
||||
|
||||
// 获取运动状态
|
||||
|
||||
}
|
||||
|
||||
// 初始化处理
|
||||
static uint8_t HandleInit(void) {
|
||||
// 实现初始化逻辑
|
||||
// 1.更新状态为“初始化中”
|
||||
// 2.执行初始化流程,按协议要求执行,不含默认的初始化内容
|
||||
// 3.检查初始化结果,更新状态“成功”或“失败”
|
||||
InitDeviceStatus();
|
||||
return 1;
|
||||
}
|
||||
@ -1287,6 +1438,6 @@ void runValveDemo(void) {
|
||||
|
||||
// 阀门1正转120度
|
||||
printf("ValveRunToAngle(0, 120)\n");
|
||||
ValveRunInit(0);
|
||||
ValvePPInit(0);
|
||||
ValveRunToAngle(0, 120);
|
||||
}
|
||||
|
@ -18,6 +18,7 @@
|
||||
// 帧头帧尾定义
|
||||
#define FRAME_HEADER 0xA55A5AA5
|
||||
#define FRAME_TAIL 0x5AA5A55A
|
||||
#define READ_ACK_TIMEOUT 50
|
||||
|
||||
// 功能码定义
|
||||
#define HOST_CMD_STATUS_QUERY 0x0001 // 状态查询
|
||||
@ -300,6 +301,7 @@ static const uint8_t statusInfo[16][60]={
|
||||
#define RTU_VALVE_CMD_SC 0x0381 // 阀门运行状态
|
||||
#define RTU_VALVE_CMD_AL 0x037F // 阀门运行告警
|
||||
#define RTU_VALVE_CMD_POS 0x03C8 // 阀门运行位置,用户单位
|
||||
#define RTU_VALVE_CMD_SPEED 0x03D0 // 阀门当前速度,用户单位/s
|
||||
|
||||
#define RTU_VALVE_CMD_HOME_MODE 0x0416 // 阀门原点回归方式
|
||||
#define RTU_VALVE_CMD_HOME_SWT_SPEED 0x0417 // 阀门回归寻找开关的速度
|
||||
|
Loading…
Reference in New Issue
Block a user