update function

This commit is contained in:
CSSC-WORK\murmur 2024-12-23 17:27:49 +08:00
parent 2c124de7f3
commit bea2eea42e
2 changed files with 80 additions and 28 deletions

View File

@ -1,7 +1,7 @@
#include "protocol.h" #include "protocol.h"
#include <string.h> #include <string.h>
DeviceStatus deviceStatus = { DeviceStatus_t deviceStatus = {
.deviceStatus = 1, .deviceStatus = 1,
.valves = {210, 120}, .valves = {210, 120},
.pumps = {0, 0, 50, 50}, .pumps = {0, 0, 50, 50},
@ -11,9 +11,9 @@ DeviceStatus deviceStatus = {
.initStatus = 1 .initStatus = 1
}; };
uint8_t isValveMovingBackToOrigin = 0; uint8_t isValveMovingBackToOrigin[2] = 0;
DeviceParam dp = { DeviceParam_t dp = {
.pump = { .pump = {
{"pump1", 4, 60, 100, 100}, {"pump1", 4, 60, 100, 100},
{"pump2", 3, 10, 100, 100} {"pump2", 3, 10, 100, 100}
@ -260,10 +260,16 @@ static void FillBigEndian16(uint8_t *data, uint16_t value) {
+----------+--------+------------+------------+------------+ +----------+--------+------------+------------+------------+
*/ */
// pump 读寄存器 // pump 读寄存器
uint16_t ReadPump1Reg(uint8_t index, uint16_t reg) { /**
* 0x031
* @param id id
* @param reg
* @param return
*/
uint16_t ReadPump1Reg(uint8_t id, uint16_t reg) {
uint8_t data[8] = {0}; uint8_t data[8] = {0};
data[0] = index; data[0] = id;
data[1] = RTU_FUNC_READ_HOLD_REG; data[1] = RTU_FUNC_READ_HOLD_REG;
FillBigEndian16(&data[2], reg); FillBigEndian16(&data[2], reg);
FillBigEndian16(&data[4], 1); FillBigEndian16(&data[4], 1);
@ -279,9 +285,15 @@ uint16_t ReadPump1Reg(uint8_t index, uint16_t reg) {
return rxBuf[3]<<8|rxBuf[4]; return rxBuf[3]<<8|rxBuf[4];
} }
uint32_t ReadPump2Reg(uint8_t index, uint16_t reg) { /**
* 0x032
* @param id id
* @param reg
* @param return
*/
uint32_t ReadPump2Reg(uint8_t id, uint16_t reg) {
uint8_t data[12] = {0}; uint8_t data[12] = {0};
data[0] = index; data[0] = id;
data[1] = RTU_FUNC_READ_HOLD_REG; data[1] = RTU_FUNC_READ_HOLD_REG;
FillBigEndian16(&data[2], reg); FillBigEndian16(&data[2], reg);
FillBigEndian16(&data[4], 2); 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); updateInitStatus(INIT_SUCCESS);
} }
void updateValveStatus(uint8_t index) /**
* valve和pump的状态
*/
void updateVPStatus(uint8_t index)
{ {
// 检查是否回归成功 // 检查是否回归成功
static uint32_t st = HAL_GetTick(); static uint32_t st[2] = {HAL_GetTick(),HAL_GetTick()};
if (isValveMovingBackToOrigin) if (isValveMovingBackToOrigin[index])
{ {
uint8_t rst = valveCheckBTOResult(index,st); uint8_t rst = valveCheckBTOResult(index,st[index]);
switch (rst) switch (rst)
{ {
case 0: case 0:
@ -1093,16 +1116,32 @@ void updateValveStatus(uint8_t index)
} }
else 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) { static uint8_t HandleInit(void) {
// 实现初始化逻辑 // 实现初始化逻辑
@ -1413,7 +1452,7 @@ CmdFrameError_t ProcessHostCommand(uint8_t *rxBuf, uint8_t rxLen) {
void runPumpDemo(void) { void runPumpDemo(void) {
printf("runPumpDemo\r\n"); printf("runPumpDemo\r\n");
// printf("InitPump\n"); // printf("InitPump\n");
// InitPump(); InitPump();
// 泵1正转100步 // 泵1正转100步
printf("SetPumpStepTarget(0, 100)\n"); printf("SetPumpStepTarget(0, 100)\n");
SetPumpStepTarget(0, 200000); SetPumpStepTarget(0, 200000);
@ -1426,8 +1465,8 @@ void runPumpDemo(void) {
printf("StartPumpRelativeMove(0)\n"); printf("StartPumpRelativeMove(0)\n");
StartPumpRelativeMove(1); StartPumpRelativeMove(1);
// HAL_Delay(1000); // HAL_Delay(1000);
printf("StopPump(0)\n"); // printf("StopPump(0)\n");
StopPump(0); // StopPump(0);
// HAL_Delay(1000); // HAL_Delay(1000);
} }

View File

@ -213,7 +213,8 @@ static const uint8_t statusInfo[16][60]={
#define RTU_PUMP_CMD_HW 0x006C // 硬件版本40109 #define RTU_PUMP_CMD_HW 0x006C // 硬件版本40109
#define RTU_PUMP_CMD_BR 0x0094 //波特率BR40149 #define RTU_PUMP_CMD_BR 0x0094 //波特率BR40149
#define RTU_PUMP_CMD_PR 0x0095 //通信协议PR40150 #define RTU_PUMP_CMD_PR 0x0095 //通信协议PR40150
#define RTU_PUMP_CMD_SPEED 0x000A //瞬时实际速度,40011
#define RTU_PUMP_CMD_POS 0x0008 //绝对位置,40009-40010
// 速度控制慢跑Jogging模式 // 速度控制慢跑Jogging模式
/* /*
@ -301,7 +302,7 @@ static const uint8_t statusInfo[16][60]={
#define RTU_VALVE_CMD_SC 0x0381 // 阀门运行状态 #define RTU_VALVE_CMD_SC 0x0381 // 阀门运行状态
#define RTU_VALVE_CMD_AL 0x037F // 阀门运行告警 #define RTU_VALVE_CMD_AL 0x037F // 阀门运行告警
#define RTU_VALVE_CMD_POS 0x03C8 // 阀门运行位置,用户单位 #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_MODE 0x0416 // 阀门原点回归方式
#define RTU_VALVE_CMD_HOME_SWT_SPEED 0x0417 // 阀门回归寻找开关的速度 #define RTU_VALVE_CMD_HOME_SWT_SPEED 0x0417 // 阀门回归寻找开关的速度
@ -331,9 +332,9 @@ typedef enum {
// 1. 下挂设备状态 // 1. 下挂设备状态
typedef enum { typedef enum {
DEVICE_OFFLINE = 0, SENSOR_OFFLINE = 0,
DEVICE_ONLINE = 1 SENSOR_ONLINE = 1
} DeviceStatus_t; } SensorStatus_t;
// 2. 三通阀角度 // 2. 三通阀角度
typedef enum { typedef enum {
@ -375,7 +376,7 @@ typedef enum {
typedef struct { typedef struct {
uint8_t angle1; // 阀门1角度 (120/210) uint8_t angle1; // 阀门1角度 (120/210)
uint8_t angle2; // 阀门2角度 (120/210) uint8_t angle2; // 阀门2角度 (120/210)
} ValveStatus; } ValveStatus_t;
// 泵结构体 // 泵结构体
typedef struct { typedef struct {
@ -383,9 +384,9 @@ typedef struct {
uint8_t status2; // 泵2运行状态 (停止/顺时针/逆时针) uint8_t status2; // 泵2运行状态 (停止/顺时针/逆时针)
uint8_t speed1; // 泵1速度百分比 (0-100) uint8_t speed1; // 泵1速度百分比 (0-100)
uint8_t speed2; // 泵2速度百分比 (0-100) uint8_t speed2; // 泵2速度百分比 (0-100)
} PumpStatus; } PumpStatus_t;
// 设备状态结构体 // 设备状态结构体用于上报HOST
typedef struct { typedef struct {
uint8_t deviceStatus; // 下挂设备状态 uint8_t deviceStatus; // 下挂设备状态
ValveStatus valves; // 两个三通阀状态 ValveStatus valves; // 两个三通阀状态
@ -394,7 +395,19 @@ typedef struct {
uint8_t stopStatus; // 急停状态 uint8_t stopStatus; // 急停状态
uint8_t errorCode; // 错误码 uint8_t errorCode; // 错误码
uint8_t initStatus; // 初始化状态 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 { typedef struct {
@ -403,14 +416,14 @@ typedef struct {
uint32_t maxSpeed; uint32_t maxSpeed;
uint32_t maxAccel; uint32_t maxAccel;
uint32_t maxDecel; uint32_t maxDecel;
} MotorDefaultParam; } MotorDefaultParam_t;
// 定义设备<E8AEBE><E5A487>认参数 // 定义设备<E8AEBE><E5A487>认参数
typedef struct { typedef struct {
MotorDefaultParam pump[2]; MotorDefaultParam_t pump[2];
MotorDefaultParam valve[2]; MotorDefaultParam_t valve[2];
} DeviceParam; } DeviceParam_t;
extern DeviceParam dp; extern DeviceParam dp;