From 614cb70134cfc25450f62f3036b7bf9a190f7859 Mon Sep 17 00:00:00 2001 From: murmur Date: Mon, 23 Dec 2024 23:14:35 +0800 Subject: [PATCH] update function --- protocol.c | 83 ++++++++++++++++++++++++++++++------------------------ protocol.h | 30 +++++++++++--------- 2 files changed, 62 insertions(+), 51 deletions(-) diff --git a/protocol.c b/protocol.c index 7ded0b9..8df77e2 100644 --- a/protocol.c +++ b/protocol.c @@ -2,7 +2,7 @@ #include DeviceStatus_t deviceStatus = { - .deviceStatus = 1, + .sensorStatus = 1, .valves = {210, 120}, .pumps = {0, 0, 50, 50}, .bubbleStatus = 0, @@ -10,8 +10,12 @@ DeviceStatus_t deviceStatus = { .errorCode = 0, .initStatus = 1 }; +SystemStatus_t systemStatus = { + .speed = {0, 0}, + .pos = {0, 0} +}; -uint8_t isValveMovingBackToOrigin[2] = 0; +uint8_t isValveMovingBackToOrigin[2] = {0,0}; DeviceParam_t dp = { .pump = { @@ -87,8 +91,8 @@ uint16_t CalculateCRC16(uint8_t *data, uint16_t length) { * * @param status 设备状态值(在线/离线) */ -void updateDeviceStatus(DeviceStatus_t status) { - deviceStatus.deviceStatus = status; +void updateDeviceStatus(SensorStatus status) { + deviceStatus.sensorStatus = status; } /** @@ -97,7 +101,7 @@ void updateDeviceStatus(DeviceStatus_t status) { * @param index 阀门索引(1或2) * @param angle 阀门角度值(0-360,绝对角度) */ -void updateValveStatus(uint8_t index, ValveAngle_t angle) { +void updateValveStatus(uint8_t index, ValveAngle angle) { if (index == 1) { deviceStatus.valves.angle1 = angle; } else if (index == 2) { @@ -111,7 +115,7 @@ void updateValveStatus(uint8_t index, ValveAngle_t angle) { * @param index 泵索引(1或2) * @param status 泵状态值(停止/顺时针/逆时针) */ -void updatePumpStatus(uint8_t index, PumpStatus_t status) { +void updatePumpStatus(uint8_t index, PumpStatus status) { if (index == 1) { deviceStatus.pumps.status1 = status; } else if (index == 2) { @@ -138,7 +142,7 @@ void updatePumpSpeedStatus(uint8_t index, uint8_t speed) { * * @param value 气泡传感器状态值(有/无气泡) */ -void updateBubbleSensor(BubbleStatus_t value) { +void updateBubbleSensor(BubbleStatus value) { deviceStatus.bubbleStatus = value; } @@ -312,7 +316,7 @@ uint32_t ReadPump2Reg(uint8_t id, uint16_t reg) { /* +----------+--------+------------+----------+------------+ -| 从机地址 | 功能码 | 寄存器地址 | 寄存器值 | CRC校验值 | +| 从机地址 | 功能码 | 寄存器地址 | 寄存器��� | CRC校验值 | +----------+--------+------------+----------+------------+ | 1字节 | 1字节 | 2字节 | 2字节 | 2字节 | +----------+--------+------------+----------+------------+ @@ -555,7 +559,7 @@ uint16_t ReadPumpAlarm(uint8_t index) { } /** - * 解码泵的告警信息 + * 解��泵的告警信息 * * @param reg4001 告警寄存器值 */ @@ -690,7 +694,7 @@ uint8_t InitPump(void) { 2.4设置位移的减速度 (03FEh)(用户单位/s2); 3.写控制字使电机使能 (0380h)= 0x06→0x07→ 0x0F,电机使能: -4.使电机运行 +4.使电机��行 (0380h)= 0x2F→0x3F,电机运行 5.监控参数: 实际位置反馈:(03C8h) (用户单位) @@ -711,10 +715,10 @@ uint8_t InitPump(void) { */ // 与pump通用 -void (*writeValve1Reg)(uint8_t index, uint16_t reg, uint16_t value) = WritePump1Reg; -void (*writeValve2Reg)(uint8_t index, uint16_t reg, uint32_t value) = WritePump2Reg; -void (*readValve1Reg)(uint8_t index, uint16_t reg) = ReadPump1Reg; -void (*readValve2Reg)(uint8_t index, uint16_t reg) = ReadPump2Reg; +uint8_t (*writeValve1Reg)(uint8_t index, uint16_t reg, uint16_t value) = WritePump1Reg; +uint8_t (*writeValve2Reg)(uint8_t index, uint16_t reg, uint32_t value) = WritePump2Reg; +uint8_t (*readValve1Reg)(uint8_t index, uint16_t reg) = ReadPump1Reg; +uint8_t (*readValve2Reg)(uint8_t index, uint16_t reg) = ReadPump2Reg; /** * 读取阀门1个输入寄存器 @@ -778,7 +782,7 @@ uint32_t ReadValve2InputReg(uint8_t id, uint16_t reg) * @return 0:成功 其他:失败 */ static uint8_t SetValveCOMMMode(uint8_t index, uint16_t mode) { - return WriteValve1Reg(dp.valve[index].id, RTU_VALVE_CMD_CTL_MODE, mode); + return writeValve1Reg(dp.valve[index].id, RTU_VALVE_CMD_CTL_MODE, mode); } /** @@ -789,7 +793,7 @@ static uint8_t SetValveCOMMMode(uint8_t index, uint16_t mode) { * @return 0:成功 其他:失败 */ static uint8_t SetValveRunMode(uint8_t index, uint16_t mode) { - return WriteValve1Reg(dp.valve[index].id, RTU_VALVE_CMD_RUN_MODE, mode); + return writeValve1Reg(dp.valve[index].id, RTU_VALVE_CMD_RUN_MODE, mode); } // PP=轮廓位置模式 @@ -931,9 +935,7 @@ static uint32_t ReadValvePos(uint8_t index) { * @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; @@ -1057,13 +1059,9 @@ uint8_t ValveRunToAngle(uint8_t index, uint32_t angle) { */ uint8_t InitValve(void) { printf("InitValve\n"); - SetValvePPSpeed(0, dp.valve[0].maxSpeed); - SetValvePPAcc(0, dp.valve[0].maxAccel); - SetValvePPDec(0, dp.valve[0].maxDecel); - SetValvePPSpeed(1, dp.valve[1].maxSpeed); - SetValvePPAcc(1, dp.valve[1].maxAccel); - SetValvePPDec(1, dp.valve[1].maxDecel); + ValvePPInit(0); + ValvePPInit(1); } /** @@ -1075,7 +1073,7 @@ void InitDeviceStatus() { // 更新设备状态 - updateDeviceStatus(DEVICE_ONLINE); + updateDeviceStatus(SENSOR_ONLINE); updateValveStatus(1, 120); updateValveStatus(2, 210); updatePumpStatus(1, PUMP_CLOCKWISE); @@ -1092,16 +1090,17 @@ void InitDeviceStatus() { */ void updateVPStatus(uint8_t index) { - - // 检查是否回归成功 - static uint32_t st[2] = {HAL_GetTick(),HAL_GetTick()}; + static uint32_t st[2] = {0, 0}; // Initialize with constants if (isValveMovingBackToOrigin[index]) { - uint8_t rst = valveCheckBTOResult(index,st[index]); + if (st[index] == 0) { // First time initialization + st[index] = HAL_GetTick(); + } + uint8_t rst = valveCheckBTOResult(index, st[index]); switch (rst) { case 0: - log_i("BTO success!"); + log_i("BTO success!delta=%d",HAL_GetTick()-st[index]); updateInitStatus(INIT_SUCCESS); break; case 1: @@ -1123,13 +1122,15 @@ void updateVPStatus(uint8_t index) // 获取实时角度 readPumpPos(index); - uint32_t pos[index] = ReadValve2InputReg(index,RTU_VALVE_CMD_POS); - log_d("pos[%d]=%d",index,pos[index]); + uint32_t pos = ReadValve2InputReg(index,RTU_VALVE_CMD_POS); + log_d("pos[%d]=%d",index,pos); + systemStatus.pos[index] = pos; // 获取运动状态 - uint32_t speed[index] = ReadValve2InputReg(index,RTU_VALVE_CMD_SPEED); - log_d("speed[%d]=",index,speed[index]); + uint32_t speed = ReadValve2InputReg(index,RTU_VALVE_CMD_SPEED); + log_d("speed[%d]=",index,speed); + systemStatus.speed[index] = speed; } @@ -1143,6 +1144,14 @@ void updateSystemStatus(void) updateVPStatus(1); } +/** + * 初始化系统 + */ +void initSystem(void) +{ + systemStatus.ds = deviceStatus; + InitValve(); +} // 初始化处理 static uint8_t HandleInit(void) { // 实现初始化逻辑 @@ -1205,7 +1214,7 @@ static uint8_t HandleValveControl(uint8_t *Buff, uint8_t len) { /** * 处理泵时长控制命令 * - * @param Buff 接收到的数据缓冲区 + * @param Buff 接收��的数据缓冲区 * @param len 接收到的数据长度 * @return 0:成功 其他:失败 */ @@ -1323,7 +1332,7 @@ void packMsgToHost(uint16_t funcCode, uint8_t isOK) { FillBigEndian16(msgBuf+sizeof(FRAME_HEADER), funcCode); if(funcCode == HOST_CMD_STATUS_QUERY) { - dlen = sizeof(DeviceStatus); + dlen = sizeof(DeviceStatus_t); index = sizeof(FRAME_HEADER)+2; msgBuf[index] = dlen; index += 1; diff --git a/protocol.h b/protocol.h index f91b674..f671596 100644 --- a/protocol.h +++ b/protocol.h @@ -334,20 +334,20 @@ typedef enum { typedef enum { SENSOR_OFFLINE = 0, SENSOR_ONLINE = 1 -} SensorStatus_t; +} SensorStatus; // 2. 三通阀角度 typedef enum { VALVE_ANGLE_120 = 120, VALVE_ANGLE_210 = 210 -} ValveAngle_t; +} ValveAngle; // 3. 泵状态 typedef enum { PUMP_STOP = 0, PUMP_CLOCKWISE = 1, PUMP_ANTICLOCKWISE = 2 -} PumpStatus_t; +} PumpStatus; // 4. 泵速度范围 #define PUMP_SPEED_MIN 0 @@ -357,7 +357,7 @@ typedef enum { typedef enum { BUBBLE_NONE = 0, BUBBLE_DETECTED = 1 -} BubbleStatus_t; +} BubbleStatus; // 6. 急停按键状态 typedef enum { @@ -388,9 +388,9 @@ typedef struct { // 设备状态结构体,用于上报HOST typedef struct { - uint8_t deviceStatus; // 下挂设备状态 - ValveStatus valves; // 两个三通阀状态 - PumpStatus pumps; // 两个泵状态 + uint8_t sensorStatus; // 下挂设备状态 + ValveStatus_t valves; // 两个三通阀状态 + PumpStatus_t pumps; // 两个泵状态 uint8_t bubbleStatus; // 气泡状态 uint8_t stopStatus; // 急停状态 uint8_t errorCode; // 错误码 @@ -401,11 +401,8 @@ typedef struct { typedef struct { DeviceStatus_t ds; - typedef struct - { - uint32_t speed[2];//实时速度 - uint32_t pos[2];//实时位置 - }; + uint32_t speed[2];//实时速度 + uint32_t pos[2];//实时位置 } SystemStatus_t; @@ -426,8 +423,8 @@ typedef struct { } DeviceParam_t; -extern DeviceParam dp; -extern DeviceStatus deviceStatus; +extern DeviceParam_t dp; +extern DeviceStatus_t deviceStatus; // 函数声明 CmdFrameError_t ProcessHostCommand(uint8_t *rxBuf, uint8_t rxLen); @@ -439,4 +436,9 @@ void UpdatePumpStatus(); void ProcessMotorMsg(uint8_t *rxBuf, uint16_t rxLen); void runPumpDemo(void); void runVavleDemo(void); +void updateSystemStatus(void); +void initSystem(void); + + + #endif // PROTOCOL_H \ No newline at end of file