injectorCTL/protocol.h

449 lines
19 KiB
C
Raw Normal View History

2024-12-10 12:01:16 +00:00
#ifndef PROTOCOL_H
#define PROTOCOL_H
2024-12-13 08:00:24 +00:00
#include "main.h"
#include "usart.h"
2024-12-10 12:01:16 +00:00
#include <stdint.h>
2024-12-15 09:35:32 +00:00
#ifdef USE_ELOG
#define LOG_TAG "motor"
#include <elog.h>
#else
2024-12-23 08:18:02 +00:00
// 重定向log_e等函数到printf并自动添加换行符
#define log_e(format, ...) printf(format "\r\n", ##__VA_ARGS__)
#define log_i(format, ...) printf(format "\r\n", ##__VA_ARGS__)
#define log_d(format, ...) printf(format "\r\n", ##__VA_ARGS__)
#define log_w(format, ...) printf(format "\r\n", ##__VA_ARGS__)
2024-12-15 09:35:32 +00:00
#endif
2024-12-23 08:18:02 +00:00
2024-12-10 12:01:16 +00:00
// 帧头帧尾定义
2024-12-11 09:41:56 +00:00
#define FRAME_HEADER 0xA55A5AA5
#define FRAME_TAIL 0x5AA5A55A
2024-12-23 07:52:28 +00:00
#define READ_ACK_TIMEOUT 50
#define ACK_OK 0x0000
#define ACK_FAILED 0x0001
#define ACK_OTHER 0x0002
2024-12-10 12:01:16 +00:00
// 功能码定义
2024-12-13 08:00:24 +00:00
#define HOST_CMD_STATUS_QUERY 0x0001 // 状态查询
#define HOST_CMD_VALVE_CTRL 0x0002 // 三通阀控制
#define HOST_CMD_PUMP_RUN_TIME 0x0003 // 泵运行时长控制
#define HOST_CMD_PUMP_RUN_SPEED 0x0004 // 泵运行速度设置
#define HOST_CMD_SOFT_STOP 0x0005 // 软急停功能
#define HOST_CMD_PUMP_RUN_STEP 0x0006 // 泵步进设置
#define HOST_CMD_SYSTEM_INIT 0x0007 // 系统初始化
2024-12-11 09:41:56 +00:00
#define CMD_BR_SET 0x0008 // 波特率设置
#define CMD_PR_SET 0x0009 // 通信协议设置
2024-12-22 14:29:17 +00:00
#define PUMP_SPEED_RPS 240//速度单位为1/240 rps
#define PUMP_ACCEL_RPS 6//加速度单位为1/6 rps/s
#define PUMP_DECEL_RPS 6//减速度单位为1/6 rps/s
#define PUMP_SPEED_RPS_MAX 50//
2024-12-11 02:32:35 +00:00
// 命令帧错误码定义
typedef enum {
CMD_FRAME_OK = 0,
CMD_FRAME_HEADER_ERROR = -1,
CMD_FRAME_TAIL_ERROR = -2,
CMD_FRAME_CHECK_ERROR = -3,
CMD_FRAME_CMD_ERROR = -4,
} CmdFrameError_t;
2024-12-10 12:01:16 +00:00
// MOONS驱动器支持的Modbus功能码如下
// 0x03读取保持寄存器
// 0x06写单个寄存器
2024-12-12 08:38:51 +00:00
// 0x10写多个寄存器
#define RTU_FUNC_READ_HOLD_REG 0x03 // 读取保持寄存器
#define RTU_FUNC_READ_INPUT_REG 0x04 // 读取输入寄存器
#define RTU_FUNC_WRITE_REG 0x06 // 写单个寄存器
#define RTU_FUNC_WRITE_MULTI_REG 0x10 // 写多个寄存器
2024-12-10 12:01:16 +00:00
2024-12-10 15:52:29 +00:00
/*寄存器4001告警代码
1
0
1 CCW方向禁止限位
2 CW方向禁止限位
3
4
5
6
7
8
9
10
11
12 使
13
14 Q程序段为空
15
*/
2024-12-11 02:32:35 +00:00
// 使用联合体表示
// 告警寄存器联合体定义
typedef union {
struct {
uint16_t position_error:1; // 位0: 位置误差超限
uint16_t ccw_limit:1; // 位1: CCW方向禁止限位
uint16_t cw_limit:1; // 位2: CW方向禁止限位
uint16_t over_temp:1; // 位3: 驱动器过温
uint16_t voltage_error:1; // 位4: 驱动器内部电压错误
uint16_t over_voltage:1; // 位5: 驱动器过压
uint16_t under_voltage:1; // 位6: 驱动器欠压
uint16_t over_current:1; // 位7: 驱动器过流
uint16_t winding_switch:1; // 位8: 电机绕组开关
uint16_t encoder_error:1; // 位9: 电机编码器信号错误
uint16_t comm_error:1; // 位10: 通讯异常
uint16_t param_save_failed:1; // 位11: 参数保存失败
uint16_t motor_disabled:1; // 位12: 在电机未使能时命令其运转
uint16_t motor_overload:1; // 位13: 电机重载状态
uint16_t empty_q_program:1; // 位14: 调用的Q程序段为空
uint16_t memory_error:1; // 位15: 存储器错误
} bits;
uint16_t all; // 访问完整的16位寄存器
}AlarmCode_t;
// 用于输出具体的告警信息字符串
2024-12-23 03:13:24 +00:00
static const uint8_t alarmInfo[16][50]={
2024-12-10 15:52:29 +00:00
"位置误差超限",
"CCW方向禁止限位",
"CW方向禁止限位",
"驱动器过温",
"驱动器内部电压错误",
"驱动器过压",
"驱动器欠压",
"驱动器过流",
2024-12-23 08:18:02 +00:00
"电机绕组开<EFBFBD><EFBFBD>",
2024-12-10 15:52:29 +00:00
"电机编码器信号错误",
"通讯异常",
"参数保存失败",
"在电机未使能时命令其运转",
"电机重载状态",
"调用的Q程序段为空",
"存储器错误",
};
/*
4002
1
0 使
1
2
3
4
5
6
7 WI指令
8
9
10
11 WTWD指令
12 使
13
14 Q程序运行中
15
*/
2024-12-11 02:32:35 +00:00
typedef union {
struct {
uint16_t enable:1; // 位0: 使能
uint16_t sample:1; // 位1: 采样中(软件示波器功能开启)
uint16_t fault:1; // 位2: 驱动器报故障
uint16_t position_reached:1; // 位3: 运动到位
uint16_t moving:1; // 位4: 运动中
uint16_t point_move:1; // 位5: 点动运行中
uint16_t decelerating:1; // 位6: 减速中
uint16_t wait_input:1; // 位7: 等待输入信号例如执行WI指令
uint16_t parameter_save:1; // 位8: 参数保存中
uint16_t alarm:1; // 位9: 驱动器报警告
uint16_t return_home:1; // 位10: 回原点中
uint16_t wait_time:1; // 位11: 等待时间例如执行WT、WD指令
uint16_t encoder_check:1; // 位12: 编码器检测中
uint16_t q_program_run:1; // 位13: Q程序运行中
uint16_t init:1; // 位14: 初始化(步进系),伺服准备好(伺服系)
} bits;
uint16_t all;
2024-12-10 15:52:29 +00:00
} StatusCode_t;
2024-12-23 03:13:24 +00:00
static const uint8_t statusInfo[16][60]={
2024-12-10 15:52:29 +00:00
"使能",
"采样中(软件示波器功能开启)",
"驱动器报故障",
"运动到位",
"运动中",
"点动运行中",
"减速中",
"等待输入信号例如执行WI指令",
"参数保存中",
"驱动器报警告",
"回原点中",
"等待时间例如执行WT、WD指令",
"内部使用",
"编码器检测中",
"Q程序运行中",
"初始化(步进系),伺服准备好(伺服系)",
};
2024-12-11 07:55:40 +00:00
// 位置控制,点对点模式
2024-12-10 12:01:16 +00:00
/* 设置加速度、减速度、速度和目标位置对应MOONS SCL指令如下
+----------+----------+----------+--------------+--------------+--------------+--------------------------------------------------+
| SCL指令 | | | | | | |
| | | | | | | |
+----------+----------+----------+--------------+--------------+--------------+--------------------------------------------------+
| AC | 100 | Rps/sec | 40028 | 001B | 600 | 10040028 |
| | | | | | | 600(0x0258) |
+----------+----------+----------+--------------+--------------+--------------+--------------------------------------------------+
| DE | 100 | Rps/sec | 40029 | 001C | 600 | 10040029 |
| | | | | | | 600(0x0258) |
+----------+----------+----------+--------------+--------------+--------------+--------------------------------------------------+
| VE | 1 | Rps | 40030 | 001D | 240 | 140030 |
| | | | | | | 240(0x00F0) |
+----------+----------+----------+--------------+--------------+--------------+--------------------------------------------------+
| DI | 200000 | Counts | 40031,40032 | 001E,001F | 200000 | 2000004003140032 |
| | | | | | | 200000(0x00030D40) |
+----------+----------+----------+--------------+--------------+--------------+--------------------------------------------------+
*/
#define RTU_PUMP_CMD_AC 0x001B // 加速度
#define RTU_PUMP_CMD_DE 0x001C // 减速度
#define RTU_PUMP_CMD_VE 0x001D // 速度
#define RTU_PUMP_CMD_DI 0x001E // 目标位置
2024-12-11 07:55:40 +00:00
#define RTU_PUMP_CMD_HW 0x006C // 硬件版本40109
2024-12-11 09:41:56 +00:00
#define RTU_PUMP_CMD_BR 0x0094 //波特率BR40149
#define RTU_PUMP_CMD_PR 0x0095 //通信协议PR40150
2024-12-23 09:27:49 +00:00
#define RTU_PUMP_CMD_SPEED 0x000A //瞬时实际速度,40011
#define RTU_PUMP_CMD_POS 0x0008 //绝对位置,40009-40010
2024-12-11 07:55:40 +00:00
// 速度控制慢跑Jogging模式
/*
+----------+----------+----------+------------+--------------+--------------+--------------------------------------+----------+
| SCL指令 | | | | | | | |
| | | | | | | | |
+----------+----------+----------+------------+--------------+--------------+--------------------------------------+----------+
| JA | 100 | Rps/sec | 40047 | 002E | 600 | 100 | |
| | | | | | | 40047600600(0x0258) | |
+----------+----------+----------+------------+--------------+--------------+--------------------------------------+----------+
| JL | 100 | Rps/sec | 40048 | 002F | 600 | 100 | |
| | | | | | | 40048600(0x0258) | |
+----------+----------+----------+------------+--------------+--------------+--------------------------------------+----------+
| JS | 10 | Rps | 40049 | 0030 | 2400 | 10 | (0x0960) |
| | | | | | | 400492400 | |
+----------+----------+----------+------------+--------------+--------------+--------------------------------------+----------+
*/
#define RTU_PUMP_CMD_JA 0x002E // Jog加速度
#define RTU_PUMP_CMD_JL 0x002F // Jog减速度
#define RTU_PUMP_CMD_JS 0x0030 // Jog速度
/* 备注:
1. Modbus报文读写时401250x007C
40125-40000-1=124(0x007C)
2. PR=5PR=133:
DI=200000(40032 ,40031)32
200000(0x030D40)
PR=5Big Endian模式下3216
16
PR=133Little Endian模式下3216
16
3. /
1
240 rps
/ 1
6 rps/s
*/
#define RTU_PUMP_CMD_CO 0x007C // 操作码Command Opcode
2024-12-10 12:01:16 +00:00
2024-12-22 14:29:17 +00:00
#define RTU_PUMP_CMD_SC 0x0002 // 状态寄存器
#define RTU_PUMP_CMD_AL 0x0001 // 告警寄存器
2024-12-10 12:01:16 +00:00
2024-12-11 09:41:56 +00:00
// valve
/*
+------------------------------------+------------+
| | |
+------------------------------------+------------+
| CIA402模式 | 00B1h |
+------------------------------------+------------+
| | 03C2h |
+------------------------------------+------------+
| 17 | 0416h |
| 18 | |
+------------------------------------+------------+
| 10000 | 0417h |
+------------------------------------+------------+
| 1000 | 0419h |
+------------------------------------+------------+
| 200000 | 041Bh |
+------------------------------------+------------+
| 使 | 0380h |
+------------------------------------+------------+
2024-12-23 08:18:02 +00:00
| 使<EFBFBD><EFBFBD><EFBFBD> | 0380h |
2024-12-11 09:41:56 +00:00
+------------------------------------+------------+
| 使使 | 0380h |
+------------------------------------+------------+
| 使 | 0380h |
+------------------------------------+------------+
*/
2024-12-12 08:38:51 +00:00
#define VALVE_PULSE_PER_ROUND 10000 //阀门一圈的脉冲数
2024-12-11 15:32:34 +00:00
#define RTU_VALVE_CMD_CTL_MODE 0x00B1 // 阀门控制模式
#define RTU_VALVE_CMD_RUN_MODE 0x03C2 // 阀门运行模式
2024-12-11 09:41:56 +00:00
2024-12-11 15:32:34 +00:00
#define RTU_VALVE_CMD_PP_POS 0x03E7 // 阀门轮廓位置
#define RTU_VALVE_CMD_PP_SPEED 0x03F8 // 阀门轮廓速度
#define RTU_VALVE_CMD_PP_ACCEL 0x03FC // 阀门轮廓加速度
#define RTU_VALVE_CMD_PP_DECEL 0x03FE // 阀门轮廓减速度
2024-12-12 08:38:51 +00:00
#define RTU_VALVE_CMD_SC 0x0381 // 阀门运行状态
#define RTU_VALVE_CMD_AL 0x037F // 阀门运行告警
#define RTU_VALVE_CMD_POS 0x03C8 // 阀门运行位置,用户单位
#define RTU_VALVE_CMD_SPEED 0x03D5 // 阀门当前速度,用户单位/srpm
2024-12-11 15:32:34 +00:00
#define RTU_VALVE_CMD_HOME_MODE 0x0416 // 阀门原点回归方式
2024-12-12 03:35:40 +00:00
#define RTU_VALVE_CMD_HOME_SWT_SPEED 0x0417 // 阀门回归寻找开关的速度
2024-12-23 08:18:02 +00:00
#define RTU_VALVE_CMD_HOME_ORI_SPEED 0x0419 // 阀门回归寻找<E5AFBB><E689BE><EFBFBD>点的速度
2024-12-12 03:35:40 +00:00
#define RTU_VALVE_CMD_HOME_ACCEL 0x041B // 阀门回归加速度
2024-12-11 15:32:34 +00:00
#define RTU_VALVE_CMD_FUNC 0x0380 // 阀门控制
2024-12-12 03:35:40 +00:00
#define RTU_VALVE_CMD_HOME_TORQUE 0x0170 // 阀门原点回归堵转检测力矩
#define RTU_VALVE_CMD_HOME_TIME 0x0172 // 阀门原点回归堵转检测时间
2024-12-11 15:32:34 +00:00
2024-12-12 03:35:40 +00:00
#define RTU_VALVE_CFG_COMM_CIA402 0x0000 // 为CIA402模式
2024-12-12 07:40:28 +00:00
#define RTU_VALVE_CFG_MODE_HM 0x0006 // 原点回归模式(HM)
#define RTU_VALVE_CFG_MODE_PP 0x0001 // 轮廓位置模式(PP)
2024-12-12 08:38:51 +00:00
#define RTU_VALVE_CFG_PREPARE 0x0006 // 准备
2024-12-11 15:32:34 +00:00
#define RTU_VALVE_CFG_DISABLE 0x0007 // 失能
2024-12-12 08:38:51 +00:00
#define RTU_VALVE_CFG_ENABLE 0x000F // 使能
2024-12-11 15:32:34 +00:00
#define RTU_VALVE_CFG_RUN_ORIGIN 0x001F // 运行原点回归
2024-12-11 09:41:56 +00:00
2024-12-10 12:01:16 +00:00
// 错误码定义
typedef enum {
ERR_NONE = 0x00,
ERR_COMM = 0x01,
ERR_CTRL = 0x02,
// 其他错误码待定
} ErrorCode_t;
// 设备状态相关定义
// 1. 下挂设备状态
typedef enum {
2024-12-23 09:27:49 +00:00
SENSOR_OFFLINE = 0,
SENSOR_ONLINE = 1
2024-12-23 15:14:35 +00:00
} SensorStatus;
2024-12-10 12:01:16 +00:00
// 2. 三通阀角度
typedef enum {
VALVE_ANGLE_120 = 120,
VALVE_ANGLE_210 = 210
2024-12-23 15:14:35 +00:00
} ValveAngle;
2024-12-10 12:01:16 +00:00
// 3. 泵状态
typedef enum {
PUMP_STATUS_STOP = 0,
PUMP_STATUS_CLOCKWISE = 1,
PUMP_STATUS_ANTICLOCKWISE = 2
2024-12-23 15:14:35 +00:00
} PumpStatus;
2024-12-10 12:01:16 +00:00
// 4. 泵速度范围
#define PUMP_SPEED_MIN 0
#define PUMP_SPEED_MAX 100
// 5. 气泡状态
typedef enum {
BUBBLE_NONE = 0,
BUBBLE_DETECTED = 1
2024-12-23 15:14:35 +00:00
} BubbleStatus;
2024-12-10 12:01:16 +00:00
// 6. 急停按键状态
typedef enum {
ESTOP_NORMAL = 0,
ESTOP_PRESSED = 1
} EstopStatus_t;
// 7. 初始化状态
typedef enum {
INIT_IN_PROGRESS = 0,
INIT_SUCCESS = 1,
INIT_FAILED = 2
} InitStatus_t;
// 三通阀结构体
typedef struct {
uint8_t angle[2]; // 阀门角度 (120/210)
2024-12-23 09:27:49 +00:00
} ValveStatus_t;
2024-12-10 12:01:16 +00:00
// 泵结构体
typedef struct {
uint8_t status[2]; // 泵运行状态 (停止/顺时针/逆时针)
uint8_t speed[2]; // 泵速度百分比 (0-100),设置值
2024-12-23 09:27:49 +00:00
} PumpStatus_t;
2024-12-10 12:01:16 +00:00
2024-12-23 09:27:49 +00:00
// 设备状态结构体用于上报HOST
2024-12-10 12:01:16 +00:00
typedef struct {
2024-12-23 15:14:35 +00:00
uint8_t sensorStatus; // 下挂设备状态
ValveStatus_t valves; // 两个三通阀状态
PumpStatus_t pumps; // 两个泵状态
2024-12-10 12:01:16 +00:00
uint8_t bubbleStatus; // 气泡状态
uint16_t activityMeter; // 活度计mCi
uint8_t estopStatus; // 急停状态
2024-12-10 12:01:16 +00:00
uint8_t errorCode; // 错误码
uint8_t initStatus; // 初始化状态
2024-12-23 09:27:49 +00:00
} DeviceStatus_t;
typedef struct
{
DeviceStatus_t ds;
uint32_t valvesSpeed[2];//实时速度
uint8_t valvesSpeedPercent[2];//实时速度百分比
uint32_t valvesPos[2];//实时位置
uint32_t pumpsSpeed[2];//实时速度
uint8_t pumpsSpeedPercent[2];//实时速度百分比
uint32_t pumpsPos[2];//实时位置
uint16_t rst;//RTU命令执行结果
2024-12-23 09:27:49 +00:00
} SystemStatus_t;//包含需要上报的状态及附加状态
2024-12-10 12:01:16 +00:00
2024-12-12 08:38:51 +00:00
typedef struct {
2024-12-13 08:00:24 +00:00
uint8_t name[20];
2024-12-12 08:38:51 +00:00
uint8_t id;
uint32_t speed;//满速
uint32_t accel;
uint32_t decel;
uint16_t fullCount;//电机总步数,用于根据角度估算需要移动的步数
int16_t offsetPos;//电机偏移位置,用于补偿电机移动误差
uint8_t speedPercent;//设置速度百分比数如100表示100%
uint8_t torque;//堵转力矩百分比,堵转转矩阈值应大于找寻原点过程中的实际运行转矩,且一般应小于最大转矩,以避免触发堵转故障保护
uint8_t timeout;//回归超时时间单位s
2024-12-23 09:27:49 +00:00
} MotorDefaultParam_t;
2024-12-12 08:38:51 +00:00
// 定义设备默认参数
2024-12-12 08:38:51 +00:00
typedef struct {
2024-12-23 09:27:49 +00:00
MotorDefaultParam_t pump[2];
MotorDefaultParam_t valve[2];
} DeviceParam_t;
2024-12-12 08:38:51 +00:00
2024-12-23 15:14:35 +00:00
extern DeviceParam_t dp;
extern DeviceStatus_t deviceStatus;
2024-12-10 12:01:16 +00:00
// 函数声明
void ProcessHostCommand(uint8_t *rxBuf, uint8_t rxLen);
2024-12-19 14:49:20 +00:00
void runPumpDemo(void);
void runValveDemo(void);
2024-12-23 15:14:35 +00:00
void updateSystemStatus(void);
void initCTLSystem(void);
2024-12-23 15:14:35 +00:00
2024-12-10 12:01:16 +00:00
#endif // PROTOCOL_H