diff --git a/protocol.c b/protocol.c index 52a57d1..2bc2a70 100644 --- a/protocol.c +++ b/protocol.c @@ -19,12 +19,12 @@ uint8_t isValveMovingBackToOrigin[2] = {0,0}; DeviceParam_t dp = { .pump = { - {"pump1", 4, 60, 100, 100}, - {"pump2", 3, 10, 100, 100} + {"pump1", 4, 60, 100, 100,40000}, + {"pump2", 3, 10, 100, 100,40000} }, .valve = { - {"valve1", 1, 10, 100, 100}, - {"valve2", 2, 10, 100, 100} + {"valve1", 1, 10, 100, 100,47620}, + {"valve2", 2, 10, 100, 100,47620} } }; @@ -198,7 +198,7 @@ static uint8_t writeCMD(uint8_t *txBuf, uint16_t txLen) { uint16_t rxLen = txLen; uint8_t rst = HAL_UART_Receive(&huart2, rxBuf, rxLen, READ_ACK_TIMEOUT); if(memcmp(rxBuf, txBuf, 2) != 0) { - log_e("writeCMD error"); + log_e("writeCMD error!"); printf("%d readCMD: ", rst); for(uint16_t i = 0; i < rxLen; i++) { printf("%02X ", rxBuf[i]); @@ -206,6 +206,9 @@ static uint8_t writeCMD(uint8_t *txBuf, uint16_t txLen) { printf("\r\n"); return 1; } + else { + log_d("writeCMD success!"); + } return 0; } @@ -316,7 +319,7 @@ uint32_t ReadPump2Reg(uint8_t id, uint16_t reg) { /* +----------+--------+------------+----------+------------+ -| 从机地址 | 功能码 | 寄存器地址 | 寄存器��� | CRC校验值 | +| 从机地址 | 功能码 | 寄存器地址 | 寄存器值 | CRC校验值 | +----------+--------+------------+----------+------------+ | 1字节 | 1字节 | 2字节 | 2字节 | 2字节 | +----------+--------+------------+----------+------------+ @@ -901,7 +904,7 @@ static uint8_t SetValveFunc(uint8_t index, uint16_t func) { * 设置阀门原点回归堵转检测力矩 * * @param index 阀门索引 - * @param torque 力矩值,百分比,太低容易误判,默认为60即60% + * @param torque 力矩值,百分比,太低容易误判,默认为30即30% * @return 0:成功 其他:失败 */ static uint8_t SetValveHomeTorque(uint8_t index, uint16_t torque) { @@ -912,7 +915,7 @@ static uint8_t SetValveHomeTorque(uint8_t index, uint16_t torque) { * 设置阀门原点回归堵转检测时间 * * @param index 阀门索引 - * @param time 检测时间(ms),太短容易误判,默认为1000ms + * @param time 检测时间(ms),太短容易误判,默认为5即5ms * @return 0:成功 其他:失败 */ static uint8_t SetValveHomeTime(uint8_t index, uint16_t time) { @@ -968,12 +971,13 @@ static uint8_t valveCheckBTOResult(uint8_t index, uint32_t startTime) * 包含设置原点回归方式、堵转检测、运行模式等配置 * * @param index 阀门索引 + * @param direction 方向,正数正方向堵转,负数反方向堵转 * @return 0:成功 其他:失败 */ -uint8_t ValveBackToOrigin(uint8_t index,uint8_t direction) { +uint8_t ValveBackToOrigin(uint8_t index,int8_t direction) { // 1.设置原点回归方式 // (0416h)=37;17=负限位,18=正限位 - if(direction) { + if(direction > 0) { printf("back to Origin, +\r\n"); SetValveHomeDetectMode(index, 37);//正方向堵转 } @@ -983,8 +987,8 @@ uint8_t ValveBackToOrigin(uint8_t index,uint8_t direction) { } // 2.设置堵转检测力矩和堵转检测时间 // (0170h)=300,(0172h)=50 - SetValveHomeTorque(index, 600);//60% - SetValveHomeTime(index, 10000);//1000ms + SetValveHomeTorque(index, 30);//30% + SetValveHomeTime(index, 5);//50ms // 3.写 (00B1h)=0、运行模式 (03C2h)=0x06,使其工作在原点回归模式; SetValveCOMMMode(index, RTU_VALVE_CFG_COMM_CIA402); SetValveRunMode(index, RTU_VALVE_CFG_MODE_HM); @@ -1048,7 +1052,7 @@ uint8_t ValveRunToAngle(uint8_t index, uint32_t angle) { } // 其它配置不变的情况下只需要写3个控制字 - SetValvePPPos(index, (uint32_t)(angle*VALVE_PULSE_PER_ROUND/360)); + SetValvePPPos(index, (uint32_t)(angle*dp.valve[index].fullCount/360)); // 电机以绝对位置,立即更新的方式运行 // (电机是以控制字 6040h(0380h)的 bit4 的上升沿接收新的位置命令, // 所以每次执行完一次运行后需 要把此位清零。) @@ -1066,7 +1070,7 @@ uint8_t InitValve(void) { printf("InitValve\n"); ValvePPInit(0); - ValvePPInit(1); + // ValvePPInit(1); } /** @@ -1476,9 +1480,8 @@ void runPumpDemo(void) { void runValveDemo(void) { printf("runValveDemo\r\n"); - InitValve(); - ValveBackToOrigin(0,1); - + ValveBackToOrigin(0,-1); + HAL_Delay(5000); // 阀门1正转120度 printf("ValveRunToAngle(0, 120)\n"); ValvePPInit(0); diff --git a/protocol.h b/protocol.h index f671596..ffdb0b9 100644 --- a/protocol.h +++ b/protocol.h @@ -302,7 +302,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 0x03D5 // 阀门当前速度,用户单位/s +#define RTU_VALVE_CMD_SPEED 0x03D5 // 阀门当前速度,用户单位/s,rpm #define RTU_VALVE_CMD_HOME_MODE 0x0416 // 阀门原点回归方式 #define RTU_VALVE_CMD_HOME_SWT_SPEED 0x0417 // 阀门回归寻找开关的速度 @@ -413,6 +413,7 @@ typedef struct { uint32_t maxSpeed; uint32_t maxAccel; uint32_t maxDecel; + uint16_t fullCount;//电机总步数,用于根据角度估算需要移动的步数 } MotorDefaultParam_t;