add functions
This commit is contained in:
parent
10ea569b03
commit
551328506b
97
protocol.c
97
protocol.c
@ -2,6 +2,14 @@
|
||||
#include <string.h>
|
||||
|
||||
static DeviceStatus deviceStatus = {0};
|
||||
DeviceDefaultParam dp = {
|
||||
{"pump1", 1, 100, 100, 100},
|
||||
{"pump2", 2, 100, 100, 100},
|
||||
{"valve1", 3, 100, 100, 100},
|
||||
{"valve2", 4, 100, 100, 100}
|
||||
};
|
||||
|
||||
|
||||
|
||||
// CRC16 查表法实现
|
||||
static const uint16_t crcTable[] = {
|
||||
@ -100,17 +108,27 @@ void updateInitStatus(InitStatus_t status) {
|
||||
deviceStatus.initStatus = status;
|
||||
}
|
||||
|
||||
uint8_t InitPump(void) {
|
||||
// 初始化泵
|
||||
WriteJogAcc(dp.pump[0].id, dp.pump[0].maxAccel);
|
||||
WriteJogDec(dp.pump[0].id, dp.pump[0].maxDecel);
|
||||
WriteJogSpeed(dp.pump[0].id, dp.pump[0].maxSpeed);
|
||||
WriteStepAcc(dp.pump[0].id, dp.pump[0].maxAccel);
|
||||
WriteStepDec(dp.pump[0].id, dp.pump[0].maxDecel);
|
||||
WriteStepSpeed(dp.pump[0].id, dp.pump[0].maxSpeed);
|
||||
|
||||
WriteJogAcc(dp.pump[1].id, dp.pump[1].maxAccel);
|
||||
WriteJogDec(dp.pump[1].id, dp.pump[1].maxDecel);
|
||||
WriteJogSpeed(dp.pump[1].id, dp.pump[1].maxSpeed);
|
||||
WriteStepAcc(dp.pump[1].id, dp.pump[1].maxAccel);
|
||||
WriteStepDec(dp.pump[1].id, dp.pump[1].maxDecel);
|
||||
WriteStepSpeed(dp.pump[1].id, dp.pump[1].maxSpeed);
|
||||
}
|
||||
|
||||
// 初始化设备状态
|
||||
void InitDeviceStatus() {
|
||||
// 初始化泵
|
||||
WriteJogAcc(1, 100);
|
||||
WriteJogDec(1, 100);
|
||||
WriteJogSpeed(1, 10);
|
||||
WriteStepAcc(1, 100);
|
||||
WriteStepDec(1, 100);
|
||||
WriteStepSpeed(1, 10);
|
||||
InitPump();
|
||||
|
||||
|
||||
// 更新设备状态
|
||||
@ -179,11 +197,11 @@ static void FillBigEndian16(uint8_t *data, uint16_t value) {
|
||||
+----------+--------+------------+------------+------------+
|
||||
*/
|
||||
// pump 读寄存器
|
||||
uint16_t ReadPump1Reg(uint8_t index, uint16_t reg) {
|
||||
uint8_t ReadPump1Reg(uint8_t index, uint16_t reg) {
|
||||
|
||||
uint8_t data[8] = {0};
|
||||
data[0] = index;
|
||||
data[1] = RTU_PUMP_FUNC_READ_REG;
|
||||
data[1] = RTU_FUNC_READ_HOLD_REG;
|
||||
FillBigEndian16(&data[2], reg);
|
||||
FillBigEndian16(&data[4], 1);
|
||||
|
||||
@ -191,6 +209,19 @@ uint16_t ReadPump1Reg(uint8_t index, uint16_t reg) {
|
||||
// 小端序填充
|
||||
memcpy(&data[6], &crc, 2);
|
||||
|
||||
writeCMD(data, 8);
|
||||
}
|
||||
|
||||
uint8_t ReadPump2Reg(uint8_t index, uint16_t reg) {
|
||||
uint8_t data[8] = {0};
|
||||
data[0] = index;
|
||||
data[1] = RTU_FUNC_READ_HOLD_REG;
|
||||
FillBigEndian16(&data[2], reg);
|
||||
FillBigEndian16(&data[4], 2);
|
||||
|
||||
uint16_t crc = CalculateCRC16(data, 6);
|
||||
// 小端序填充
|
||||
memcpy(&data[6], &crc, 2);
|
||||
|
||||
writeCMD(data, 8);
|
||||
}
|
||||
@ -207,7 +238,7 @@ uint8_t WritePump1Reg(uint8_t index, uint16_t reg, uint16_t value) {
|
||||
// 写一个寄存器不需要指定寄存器长度
|
||||
uint8_t data[8] = {0};
|
||||
data[0] = index;
|
||||
data[1] = RTU_PUMP_FUNC_WRITE_REG;
|
||||
data[1] = RTU_FUNC_WRITE_REG;
|
||||
FillBigEndian16(&data[2], reg);
|
||||
FillBigEndian16(&data[4], value);
|
||||
uint16_t crc = CalculateCRC16(data, 6);
|
||||
@ -229,7 +260,7 @@ uint8_t WritePump2Reg(uint8_t index, uint16_t reg, uint32_t value) {
|
||||
// 写2个寄存器需要指定寄存器长度
|
||||
uint8_t data[13] = {0};
|
||||
data[0] = index;
|
||||
data[1] = RTU_PUMP_FUNC_WRITE_MULTI_REG;
|
||||
data[1] = RTU_FUNC_WRITE_MULTI_REG;
|
||||
FillBigEndian16(&data[2], reg);
|
||||
FillBigEndian16(&data[4], 2);
|
||||
data[6] = 8;
|
||||
@ -298,6 +329,8 @@ static uint8_t WriteStepDec(uint8_t index, uint16_t dec) {
|
||||
WritePump1Reg(index, RTU_PUMP_CMD_DE, dec);
|
||||
}
|
||||
static uint8_t WriteStepSpeed(uint8_t index, uint16_t speed) {
|
||||
// 目标速度转换为实际速度
|
||||
speed = (uint16_t)(speed * dp.pump[index].maxSpeed / 100);
|
||||
WritePump1Reg(index, RTU_PUMP_CMD_VE, speed);
|
||||
}
|
||||
static uint8_t WriteStepTarget(uint8_t index, uint32_t target) {
|
||||
@ -435,6 +468,8 @@ void DecodePumpStatusMsg(uint16_t reg4002) {
|
||||
// 与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;
|
||||
|
||||
static uint8_t SetValveCOMMMode(uint8_t index, uint16_t mode) {
|
||||
WritePump1Reg(index, RTU_VALVE_CMD_CTL_MODE, mode);
|
||||
@ -502,9 +537,9 @@ uint8_t ValveBackToOrigin(uint8_t index) {
|
||||
SetValveHomeAcc(index, 200000);
|
||||
// 6.写控制字
|
||||
// (0380h)= 0x06→0x07→0x0F→0x1F,电机运行
|
||||
SetValveFunc(index, RTU_VALVE_CFG_ENABLE);
|
||||
SetValveFunc(index, RTU_VALVE_CFG_PREPARE);
|
||||
SetValveFunc(index, RTU_VALVE_CFG_DISABLE);
|
||||
SetValveFunc(index, RTU_VALVE_CFG_RUN);
|
||||
SetValveFunc(index, RTU_VALVE_CFG_ENABLE);
|
||||
SetValveFunc(index, RTU_VALVE_CFG_RUN_ORIGIN);
|
||||
}
|
||||
|
||||
@ -513,26 +548,44 @@ uint8_t ValveRunInit(uint8_t index) {
|
||||
// 00B1h=0、运行模式 03C2h=0x01,使设备工作在轮廓位置模式;
|
||||
SetValveCOMMMode(index, RTU_VALVE_CFG_COMM_CIA402);
|
||||
SetValveRunMode(index, RTU_VALVE_CFG_MODE_PP);
|
||||
// 2.参数配置:
|
||||
// 2.1写目标位置 (03E7h)(用户单位);
|
||||
// SetValvePPPos(index, 0);
|
||||
// 2.2写当前段位移指令匀速运行速度 (03F8h) (用户单位/s);
|
||||
SetValvePPSpeed(index, 1000);
|
||||
SetValvePPSpeed(index, 10000);
|
||||
// 2.3设置位移的加速度 (03FCh)(用户单位/s2)
|
||||
SetValvePPAcc(index, 10000);
|
||||
SetValvePPAcc(index, 40000);
|
||||
// 2.4设置位移的减速度 (03FEh)(用户单位/s2);
|
||||
SetValvePPDec(index, 10000);
|
||||
SetValvePPDec(index, 40000);
|
||||
// 3.写控制字使电机使能
|
||||
// (0380h)= 0x06→0x07→ 0x0F,电机使能:
|
||||
SetValveFunc(index, RTU_VALVE_CFG_ENABLE);
|
||||
SetValveFunc(index, RTU_VALVE_CFG_PREPARE);
|
||||
SetValveFunc(index, RTU_VALVE_CFG_DISABLE);
|
||||
SetValveFunc(index, RTU_VALVE_CFG_RUN);
|
||||
// 4.使电机运行
|
||||
// (0380h)= 0x2F→0x3F,电机运行
|
||||
SetValveFunc(index, RTU_VALVE_CFG_ENABLE);
|
||||
}
|
||||
|
||||
uint8_t ValveRunToAngle(uint8_t index, uint32_t angle) {
|
||||
// 限制角度为0,120,210
|
||||
if(angle != 0 && angle != 120 && angle != 210) {
|
||||
printf("阀门角度设置错误\r\n");
|
||||
return 1;
|
||||
}
|
||||
// 其它配置不变的情况下只需要写3个控制字
|
||||
SetValvePPPos(index, (uint32_t)(angle*VALVE_PULSE_PER_ROUND/360));
|
||||
// 电机以绝对位置,立即更新的方式运行
|
||||
// (电机是以控制字 6040h(0380h)的 bit4 的上升沿接收新的位置命令,
|
||||
// 所以每次执行完一次运行后需 要把此位清零。)
|
||||
SetValveFunc(index, 0x2F);
|
||||
SetValveFunc(index, 0x3F);
|
||||
}
|
||||
|
||||
uint8_t InitValve(void) {
|
||||
SetValvePPSpeed(dp.valve[0].id, dp.valve[0].maxSpeed);
|
||||
SetValvePPAcc(dp.valve[0].id, dp.valve[0].maxAccel);
|
||||
SetValvePPDec(dp.valve[0].id, dp.valve[0].maxDecel);
|
||||
|
||||
SetValvePPSpeed(dp.valve[1].id, dp.valve[1].maxSpeed);
|
||||
SetValvePPAcc(dp.valve[1].id, dp.valve[1].maxAccel);
|
||||
SetValvePPDec(dp.valve[1].id, dp.valve[1].maxDecel);
|
||||
}
|
||||
|
||||
// 定时1s更新设备状态
|
||||
// 活度计通过网口获取
|
||||
// 下挂设备通过485获取
|
||||
|
35
protocol.h
35
protocol.h
@ -31,10 +31,11 @@ typedef enum {
|
||||
// MOONS’驱动器支持的Modbus功能码如下:
|
||||
// 0x03:读取保持寄存器
|
||||
// 0x06:写单个寄存器
|
||||
// 0x10:写多个寄存器
|
||||
#define RTU_PUMP_FUNC_READ_REG 0x03 // 读取保持寄存器
|
||||
#define RTU_PUMP_FUNC_WRITE_REG 0x06 // 写单个寄存器
|
||||
#define RTU_PUMP_FUNC_WRITE_MULTI_REG 0x10 // 写多个寄存器
|
||||
// 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 // 写多个寄存器
|
||||
|
||||
/*寄存器4001告警代码
|
||||
报警代码用来表示驱动器当前的报警信息,用户可以通过查询报警代码寄存器了解具体的报警信息。报
|
||||
@ -269,6 +270,8 @@ const uint8_t statusInfo[16][32]={
|
||||
| 使电机运行原点回归 | 0380h |
|
||||
+------------------------------------+------------+
|
||||
*/
|
||||
#define VALVE_PULSE_PER_ROUND 10000 //阀门一圈的脉冲数
|
||||
|
||||
#define RTU_VALVE_CMD_CTL_MODE 0x00B1 // 阀门控制模式
|
||||
#define RTU_VALVE_CMD_RUN_MODE 0x03C2 // 阀门运行模式
|
||||
|
||||
@ -276,6 +279,9 @@ const uint8_t statusInfo[16][32]={
|
||||
#define RTU_VALVE_CMD_PP_SPEED 0x03F8 // 阀门轮廓速度
|
||||
#define RTU_VALVE_CMD_PP_ACCEL 0x03FC // 阀门轮廓加速度
|
||||
#define RTU_VALVE_CMD_PP_DECEL 0x03FE // 阀门轮廓减速度
|
||||
#define RTU_VALVE_CMD_SC 0x0381 // 阀门运行状态
|
||||
#define RTU_VALVE_CMD_AL 0x037F // 阀门运行告警
|
||||
#define RTU_VALVE_CMD_POS 0x03C8 // 阀门运行位置,用户单位
|
||||
|
||||
#define RTU_VALVE_CMD_HOME_MODE 0x0416 // 阀门原点回归方式
|
||||
#define RTU_VALVE_CMD_HOME_SWT_SPEED 0x0417 // 阀门回归寻找开关的速度
|
||||
@ -288,9 +294,9 @@ const uint8_t statusInfo[16][32]={
|
||||
#define RTU_VALVE_CFG_COMM_CIA402 0x0000 // 为CIA402模式
|
||||
#define RTU_VALVE_CFG_MODE_HM 0x0006 // 原点回归模式(HM)
|
||||
#define RTU_VALVE_CFG_MODE_PP 0x0001 // 轮廓位置模式(PP)
|
||||
#define RTU_VALVE_CFG_ENABLE 0x0006 // 准备
|
||||
#define RTU_VALVE_CFG_PREPARE 0x0006 // 准备
|
||||
#define RTU_VALVE_CFG_DISABLE 0x0007 // 失能
|
||||
#define RTU_VALVE_CFG_RUN 0x000F // 运行
|
||||
#define RTU_VALVE_CFG_ENABLE 0x000F // 使能
|
||||
#define RTU_VALVE_CFG_RUN_ORIGIN 0x001F // 运行原点回归
|
||||
|
||||
// 错误码定义
|
||||
@ -387,6 +393,23 @@ typedef struct {
|
||||
// uint8_t crc[2];
|
||||
} RTU_Frame;
|
||||
|
||||
typedef struct {
|
||||
uint8_t name[10];
|
||||
uint8_t id;
|
||||
uint32_t maxSpeed;
|
||||
uint32_t maxAccel;
|
||||
uint32_t maxDecel;
|
||||
} MotorDefaultParam;
|
||||
|
||||
|
||||
// 定义设备默认参数
|
||||
typedef struct {
|
||||
MotorDefaultParam pump[2];
|
||||
MotorDefaultParam valve[2];
|
||||
} DeviceDefaultParam;
|
||||
|
||||
|
||||
extern DeviceDefaultParam dp;
|
||||
|
||||
// 函数声明
|
||||
CmdFrameError_t ProcessHostCommand(uint8_t *rxBuf, uint16_t rxLen);
|
||||
|
Loading…
Reference in New Issue
Block a user