diff --git a/protocol.c b/protocol.c index 118396d..84d437e 100644 --- a/protocol.c +++ b/protocol.c @@ -1,7 +1,7 @@ #include "protocol.h" #include -DeviceStatus deviceStatus = { +DeviceStatus_t deviceStatus = { .deviceStatus = 1, .valves = {210, 120}, .pumps = {0, 0, 50, 50}, @@ -11,9 +11,9 @@ DeviceStatus deviceStatus = { .initStatus = 1 }; -uint8_t isValveMovingBackToOrigin = 0; +uint8_t isValveMovingBackToOrigin[2] = 0; -DeviceParam dp = { +DeviceParam_t dp = { .pump = { {"pump1", 4, 60, 100, 100}, {"pump2", 3, 10, 100, 100} @@ -260,10 +260,16 @@ static void FillBigEndian16(uint8_t *data, uint16_t value) { +----------+--------+------------+------------+------------+ */ // pump 读寄存器 -uint16_t ReadPump1Reg(uint8_t index, uint16_t reg) { +/** + * 以0x03指定读取1个寄存器 + * @param id 设备id + * @param reg 寄存器地址 + * @param return 寄存器值 + */ +uint16_t ReadPump1Reg(uint8_t id, uint16_t reg) { uint8_t data[8] = {0}; - data[0] = index; + data[0] = id; data[1] = RTU_FUNC_READ_HOLD_REG; FillBigEndian16(&data[2], reg); FillBigEndian16(&data[4], 1); @@ -279,9 +285,15 @@ uint16_t ReadPump1Reg(uint8_t index, uint16_t reg) { return rxBuf[3]<<8|rxBuf[4]; } -uint32_t ReadPump2Reg(uint8_t index, uint16_t reg) { +/** + * 以0x03指定读取2个寄存器 + * @param id 设备id + * @param reg 寄存器起始地址 + * @param return 寄存器值 + */ +uint32_t ReadPump2Reg(uint8_t id, uint16_t reg) { uint8_t data[12] = {0}; - data[0] = index; + data[0] = id; data[1] = RTU_FUNC_READ_HOLD_REG; FillBigEndian16(&data[2], reg); FillBigEndian16(&data[4], 2); @@ -605,6 +617,14 @@ void DecodePumpStatusMsg(uint16_t reg4002) { } +uint32_t readPumpPos(uint8_t index) +{ + uint16_t speed = ReadPump2Reg(dp.pump[index].id,RTU_PUMP_CMD_SPEED); + log_d("speed[40001] = %d",speed); + uint32_t pos = ReadPump2Reg(dp.pump[index].id,RTU_PUMP_CMD_POS); + log_d("pos[] = %d",pos); +} + /** * 定时更新泵的状态 @@ -1066,14 +1086,17 @@ void InitDeviceStatus() { updateInitStatus(INIT_SUCCESS); } -void updateValveStatus(uint8_t index) +/** + * 更新valve和pump的状态 + */ +void updateVPStatus(uint8_t index) { // 检查是否回归成功 - static uint32_t st = HAL_GetTick(); - if (isValveMovingBackToOrigin) + static uint32_t st[2] = {HAL_GetTick(),HAL_GetTick()}; + if (isValveMovingBackToOrigin[index]) { - uint8_t rst = valveCheckBTOResult(index,st); + uint8_t rst = valveCheckBTOResult(index,st[index]); switch (rst) { case 0: @@ -1093,16 +1116,32 @@ void updateValveStatus(uint8_t index) } else { - st = HAL_GetTick(); + log_d("NOT in BTO."); + st[index] = HAL_GetTick(); } // 获取实时角度 + readPumpPos(index); + uint32_t pos[index] = ReadValve2InputReg(index,RTU_VALVE_CMD_POS); + log_d("pos[%d]=%d",index,pos[index]); // 获取运动状态 + uint32_t speed[index] = ReadValve2InputReg(index,RTU_VALVE_CMD_SPEED); + log_d("speed[%d]=",index,speed[index]); } +//在主循环中调用 +/** + * 更新系统的所有状态数据 + */ +void updateSystemStatus(void) +{ + updateVPStatus(0); + updateVPStatus(1); +} + // 初始化处理 static uint8_t HandleInit(void) { // 实现初始化逻辑 @@ -1413,7 +1452,7 @@ CmdFrameError_t ProcessHostCommand(uint8_t *rxBuf, uint8_t rxLen) { void runPumpDemo(void) { printf("runPumpDemo\r\n"); // printf("InitPump\n"); - // InitPump(); + InitPump(); // 泵1正转100步 printf("SetPumpStepTarget(0, 100)\n"); SetPumpStepTarget(0, 200000); @@ -1426,8 +1465,8 @@ void runPumpDemo(void) { printf("StartPumpRelativeMove(0)\n"); StartPumpRelativeMove(1); // HAL_Delay(1000); - printf("StopPump(0)\n"); - StopPump(0); + // printf("StopPump(0)\n"); + // StopPump(0); // HAL_Delay(1000); } diff --git a/protocol.h b/protocol.h index a93a8b9..f91b674 100644 --- a/protocol.h +++ b/protocol.h @@ -213,7 +213,8 @@ static const uint8_t statusInfo[16][60]={ #define RTU_PUMP_CMD_HW 0x006C // 硬件版本40109 #define RTU_PUMP_CMD_BR 0x0094 //波特率BR,40149 #define RTU_PUMP_CMD_PR 0x0095 //通信协议PR,40150 - +#define RTU_PUMP_CMD_SPEED 0x000A //瞬时实际速度,40011 +#define RTU_PUMP_CMD_POS 0x0008 //绝对位置,40009-40010 // 速度控制,慢跑Jogging模式 /* @@ -301,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 0x03D0 // 阀门当前速度,用户单位/s +#define RTU_VALVE_CMD_SPEED 0x03D5 // 阀门当前速度,用户单位/s #define RTU_VALVE_CMD_HOME_MODE 0x0416 // 阀门原点回归方式 #define RTU_VALVE_CMD_HOME_SWT_SPEED 0x0417 // 阀门回归寻找开关的速度 @@ -331,9 +332,9 @@ typedef enum { // 1. 下挂设备状态 typedef enum { - DEVICE_OFFLINE = 0, - DEVICE_ONLINE = 1 -} DeviceStatus_t; + SENSOR_OFFLINE = 0, + SENSOR_ONLINE = 1 +} SensorStatus_t; // 2. 三通阀角度 typedef enum { @@ -375,7 +376,7 @@ typedef enum { typedef struct { uint8_t angle1; // 阀门1角度 (120/210) uint8_t angle2; // 阀门2角度 (120/210) -} ValveStatus; +} ValveStatus_t; // 泵结构体 typedef struct { @@ -383,9 +384,9 @@ typedef struct { uint8_t status2; // 泵2运行状态 (停止/顺时针/逆时针) uint8_t speed1; // 泵1速度百分比 (0-100) uint8_t speed2; // 泵2速度百分比 (0-100) -} PumpStatus; +} PumpStatus_t; -// 设备状态结构体 +// 设备状态结构体,用于上报HOST typedef struct { uint8_t deviceStatus; // 下挂设备状态 ValveStatus valves; // 两个三通阀状态 @@ -394,7 +395,19 @@ typedef struct { uint8_t stopStatus; // 急停状态 uint8_t errorCode; // 错误码 uint8_t initStatus; // 初始化状态 -} DeviceStatus; +} DeviceStatus_t; + + +typedef struct +{ + DeviceStatus_t ds; + typedef struct + { + uint32_t speed[2];//实时速度 + uint32_t pos[2];//实时位置 + }; + +} SystemStatus_t; typedef struct { @@ -403,14 +416,14 @@ typedef struct { uint32_t maxSpeed; uint32_t maxAccel; uint32_t maxDecel; -} MotorDefaultParam; +} MotorDefaultParam_t; // 定义设备��认参数 typedef struct { - MotorDefaultParam pump[2]; - MotorDefaultParam valve[2]; -} DeviceParam; + MotorDefaultParam_t pump[2]; + MotorDefaultParam_t valve[2]; +} DeviceParam_t; extern DeviceParam dp;