From 947e685717123232e153db5dd555be70926f7f71 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E7=8E=8B?= <1063331231@qq.com> Date: Fri, 13 Jun 2025 19:29:43 +0800 Subject: [PATCH] =?UTF-8?q?=E5=A2=9E=E5=8A=A0=E8=8E=B7=E5=8F=96=E9=A3=8E?= =?UTF-8?q?=E6=89=87=E5=BC=80=E5=85=B3=E5=92=8C=E4=BC=A0=E6=84=9F=E5=99=A8?= =?UTF-8?q?=E5=BC=80=E5=85=B3=E7=9A=84=E6=96=B9=E6=B3=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../sgs/common/service/GDDeviceStatusService.java | 29 ++++++++++++ .../drivers/DODriver/OutputIOCtrlDriver.java | 9 +++- .../sgs/hardware/drivers/HeaterRodDriver.java | 31 ++++++++++++ .../service/GDDeviceStatusServiceImpl.java | 55 ++++++++++++++++++++++ 4 files changed, 123 insertions(+), 1 deletion(-) diff --git a/src/main/java/com/iflytop/sgs/common/service/GDDeviceStatusService.java b/src/main/java/com/iflytop/sgs/common/service/GDDeviceStatusService.java index 389d88d..47a9649 100644 --- a/src/main/java/com/iflytop/sgs/common/service/GDDeviceStatusService.java +++ b/src/main/java/com/iflytop/sgs/common/service/GDDeviceStatusService.java @@ -1,13 +1,36 @@ package com.iflytop.sgs.common.service; +import com.iflytop.sgs.common.enums.cmd.CmdDevice; import com.iflytop.sgs.hardware.exception.HardwareException; import com.iflytop.sgs.hardware.type.Servo.LeisaiServoMId; import com.iflytop.sgs.hardware.type.StepMotor.StepMotorMId; public interface GDDeviceStatusService { + /** + * 判断加热棒是否已开启 + * + * @param cmdDevice + * @return true: 开启,false: 关闭 + * @throws Exception + */ + public boolean isPowerOn(CmdDevice cmdDevice) throws Exception; + + /** + * 获取设定温度 + * + * @param cmdDevice + * @return * @throws Exception + */ + public double getSVTemperature(CmdDevice cmdDevice) throws Exception; + + /** + * 获取风扇的开关状态 + */ + public boolean getIOState(CmdDevice cmdDevice) throws HardwareException; /** * 获取步进电机的原点状态 + * * @param stepMotorMId * @return true 在原点 false 不在原点 */ @@ -15,6 +38,7 @@ public interface GDDeviceStatusService { /** * 获取XY 伺服的原点状态 + * * @param mid * @return * @throws HardwareException @@ -24,6 +48,7 @@ public interface GDDeviceStatusService { /** * 获取 传感器信号 + * * @param inputIOMId * @return * @throws HardwareException @@ -32,6 +57,7 @@ public interface GDDeviceStatusService { /** * 获取加热棒温度 + * * @param heaterRodSlavedId * @return * @throws Exception @@ -40,6 +66,7 @@ public interface GDDeviceStatusService { /** * 获取电机位置 + * * @param stepMotorMId * @return * @throws HardwareException @@ -48,6 +75,7 @@ public interface GDDeviceStatusService { /** * 获取伺服类电机位置 + * * @param mid * @return * @throws HardwareException @@ -56,6 +84,7 @@ public interface GDDeviceStatusService { /** * 获取液体阀门位置 + * * @param mid * @return * @throws HardwareException diff --git a/src/main/java/com/iflytop/sgs/hardware/drivers/DODriver/OutputIOCtrlDriver.java b/src/main/java/com/iflytop/sgs/hardware/drivers/DODriver/OutputIOCtrlDriver.java index 8f096b5..b41e734 100644 --- a/src/main/java/com/iflytop/sgs/hardware/drivers/DODriver/OutputIOCtrlDriver.java +++ b/src/main/java/com/iflytop/sgs/hardware/drivers/DODriver/OutputIOCtrlDriver.java @@ -19,7 +19,14 @@ public class OutputIOCtrlDriver { public void setIOState(OutputIOMId IOOutput, Boolean state) throws HardwareException { canBus.callcmd(IOOutput.mid, CmdId.write_out_io, IOOutput.ioIndex, state ? 1 : 0); } - + public boolean getIOState(OutputIOMId IOOutput) throws HardwareException { + boolean isOpen = canBus.callcmd(IOOutput.mid, CmdId.read_out_io, IOOutput.ioIndex).getContentI32(0) != 0; + if(IOOutput.mirror) + { + isOpen = !isOpen; + } + return isOpen; + } public void open(OutputIOMId IOOutput) throws HardwareException { log.info("Opening IO {}", IOOutput.description); diff --git a/src/main/java/com/iflytop/sgs/hardware/drivers/HeaterRodDriver.java b/src/main/java/com/iflytop/sgs/hardware/drivers/HeaterRodDriver.java index 5161672..55e34ae 100644 --- a/src/main/java/com/iflytop/sgs/hardware/drivers/HeaterRodDriver.java +++ b/src/main/java/com/iflytop/sgs/hardware/drivers/HeaterRodDriver.java @@ -30,7 +30,38 @@ public class HeaterRodDriver { private final Map dpCache = new ConcurrentHashMap<>(); private static final int DEFAULT_DP = 1; + /** + * 获取设定温度 + * @param heaterRodId + * @return + */ + public double getSVTemperature(HeaterRodSlavedId heaterRodId) throws Exception { + int slaveId = heaterRodId.getSlaveId(); + int decimalPoint = getDecimalPoint(slaveId); + short[] response = modbusMaster.readHoldingRegisters(slaveId, SV_ADDRESS, 1); + if (response == null || response.length == 0) { + throw new Exception(StrUtil.format("Failed to read temperature {}", heaterRodId.name())); + } + double temperature = response[0] / Math.pow(10, decimalPoint); + log.info("{} GET SV temperature {}", heaterRodId.name(), temperature); + return temperature; + } + /** + * 判断加热棒是否已开启 + * @param heaterRodId + * @return true: 开启,false: 关闭 + * @throws Exception + */ + public boolean isPowerOn(HeaterRodSlavedId heaterRodId) throws Exception { + int slaveId = heaterRodId.getSlaveId(); + short[] response = modbusMaster.readHoldingRegisters(slaveId, CONTROL_REGISTER, 1); + if (response == null || response.length == 0) { + throw new Exception("Failed to read power state for heater rod with ID: " + heaterRodId.name()); + } + // 1 = 开启,2 = 关闭 + return response[0] == 1; + } public void setDeviceAddress(HeaterRodSlavedId heaterRodId, int newAddress) throws Exception { int slaveId = heaterRodId.getSlaveId(); diff --git a/src/main/java/com/iflytop/sgs/hardware/service/GDDeviceStatusServiceImpl.java b/src/main/java/com/iflytop/sgs/hardware/service/GDDeviceStatusServiceImpl.java index 25a73d9..825a7d4 100644 --- a/src/main/java/com/iflytop/sgs/hardware/service/GDDeviceStatusServiceImpl.java +++ b/src/main/java/com/iflytop/sgs/hardware/service/GDDeviceStatusServiceImpl.java @@ -1,13 +1,18 @@ package com.iflytop.sgs.hardware.service; +import cn.hutool.core.util.StrUtil; +import com.iflytop.sgs.common.enums.cmd.CmdDevice; import com.iflytop.sgs.common.service.GDDeviceStatusService; import com.iflytop.sgs.hardware.drivers.DIDriver.InputDetectDriver; +import com.iflytop.sgs.hardware.drivers.DODriver.OutputIOCtrlDriver; import com.iflytop.sgs.hardware.drivers.HeaterRodDriver; import com.iflytop.sgs.hardware.drivers.LeiSaiServoDriver; import com.iflytop.sgs.hardware.drivers.LiquidValveDriver; import com.iflytop.sgs.hardware.drivers.StepMotorDriver.StepMotorCtrlDriver; import com.iflytop.sgs.hardware.exception.HardwareException; +import com.iflytop.sgs.hardware.type.CmdId; import com.iflytop.sgs.hardware.type.IO.InputIOMId; +import com.iflytop.sgs.hardware.type.IO.OutputIOMId; import com.iflytop.sgs.hardware.type.Servo.LeisaiServoMId; import com.iflytop.sgs.hardware.type.StepMotor.StepMotorMId; import com.iflytop.sgs.hardware.type.driver.HeaterRodSlavedId; @@ -26,6 +31,55 @@ public class GDDeviceStatusServiceImpl implements GDDeviceStatusService { private final StepMotorCtrlDriver stepMotorCtrlDriver; private final LiquidValveDriver liquidValveDriver; private final LeiSaiServoDriver leisaiServoDriver; + private final OutputIOCtrlDriver outputIOCtrlDriver; + + /** + * 判断加热棒是否已开启 + * + * @param cmdDevice + * @return true: 开启,false: 关闭 + * @throws Exception + */ + public boolean isPowerOn(CmdDevice cmdDevice) throws Exception { + return switch (cmdDevice) { + case heat_rod_1 -> heaterRodDriver.isPowerOn(HeaterRodSlavedId.HEATER_ROD1_ID); + case heat_rod_2 -> heaterRodDriver.isPowerOn(HeaterRodSlavedId.HEATER_ROD2_ID); + case heat_rod_3 -> heaterRodDriver.isPowerOn(HeaterRodSlavedId.HEATER_ROD3_ID); + case heat_rod_4 -> heaterRodDriver.isPowerOn(HeaterRodSlavedId.HEATER_ROD4_ID); + default -> throw new IllegalStateException("Unexpected value: " + cmdDevice); + }; + } + + /** + * 获取设定温度 + * + * @param cmdDevice + * @return + * * @throws Exception + */ + public double getSVTemperature(CmdDevice cmdDevice) throws Exception { + return switch (cmdDevice) { + case heat_rod_1 -> heaterRodDriver.getSVTemperature(HeaterRodSlavedId.HEATER_ROD1_ID); + case heat_rod_2 -> heaterRodDriver.getSVTemperature(HeaterRodSlavedId.HEATER_ROD2_ID); + case heat_rod_3 -> heaterRodDriver.getSVTemperature(HeaterRodSlavedId.HEATER_ROD3_ID); + case heat_rod_4 -> heaterRodDriver.getSVTemperature(HeaterRodSlavedId.HEATER_ROD4_ID); + default -> throw new IllegalStateException("Unexpected value: " + cmdDevice); + }; + } + + /** + * 获取风扇的开关状态 + */ + public boolean getIOState(CmdDevice cmdDevice) throws HardwareException { + return switch (cmdDevice) { + case fan_1 -> outputIOCtrlDriver.getIOState(OutputIOMId.DO_FAN1); + case fan_2 -> outputIOCtrlDriver.getIOState(OutputIOMId.DO_FAN2); + case fan_3 -> outputIOCtrlDriver.getIOState(OutputIOMId.DO_FAN3); + case fan_4 -> outputIOCtrlDriver.getIOState(OutputIOMId.DO_FAN4); + default -> throw new IllegalStateException("Unexpected value: " + cmdDevice); + }; + } + /** * 获取步进电机的原点状态 @@ -101,6 +155,7 @@ public class GDDeviceStatusServiceImpl implements GDDeviceStatusService { /** * 获取液体阀门位置 + * * @param mid * @return * @throws HardwareException