diff --git a/src/main/java/com/iflytop/gd/debug/services/cmds/DoorCloseCommandHandler.java b/src/main/java/com/iflytop/gd/debug/services/cmds/DoorCloseCommandHandler.java index 7c5e4eb..2ca199c 100644 --- a/src/main/java/com/iflytop/gd/debug/services/cmds/DoorCloseCommandHandler.java +++ b/src/main/java/com/iflytop/gd/debug/services/cmds/DoorCloseCommandHandler.java @@ -3,6 +3,7 @@ package com.iflytop.gd.debug.services.cmds; import com.iflytop.gd.app.model.dto.CmdDTO; import com.iflytop.gd.common.annotation.CommandMapping; import com.iflytop.gd.common.cmd.CommandHandler; +import com.iflytop.gd.infrastructure.devices.MotorDrivenDoor; import lombok.RequiredArgsConstructor; import lombok.extern.slf4j.Slf4j; import org.springframework.stereotype.Component; @@ -15,8 +16,9 @@ import org.springframework.stereotype.Component; @RequiredArgsConstructor @CommandMapping("debug_door_close") public class DoorCloseCommandHandler implements CommandHandler { + private final MotorDrivenDoor door; @Override public void handle(CmdDTO cmdDTO) { - + this.door.close(); } } diff --git a/src/main/java/com/iflytop/gd/debug/services/cmds/DoorOpenCommandHandler.java b/src/main/java/com/iflytop/gd/debug/services/cmds/DoorOpenCommandHandler.java index 2ebe9cb..6b47902 100644 --- a/src/main/java/com/iflytop/gd/debug/services/cmds/DoorOpenCommandHandler.java +++ b/src/main/java/com/iflytop/gd/debug/services/cmds/DoorOpenCommandHandler.java @@ -3,6 +3,8 @@ package com.iflytop.gd.debug.services.cmds; import com.iflytop.gd.app.model.dto.CmdDTO; import com.iflytop.gd.common.annotation.CommandMapping; import com.iflytop.gd.common.cmd.CommandHandler; +import com.iflytop.gd.infrastructure.devices.MotorDrivenDoor; +import com.iflytop.gd.system.devices.Door; import lombok.RequiredArgsConstructor; import lombok.extern.slf4j.Slf4j; import org.springframework.stereotype.Component; @@ -15,9 +17,10 @@ import org.springframework.stereotype.Component; @RequiredArgsConstructor @CommandMapping("debug_door_open") public class DoorOpenCommandHandler implements CommandHandler { + private final Door door; @Override public void handle(CmdDTO cmdDTO) { - + door.open(); } } diff --git a/src/main/java/com/iflytop/gd/infrastructure/devices/MotorDrivenDoor.java b/src/main/java/com/iflytop/gd/infrastructure/devices/MotorDrivenDoor.java new file mode 100644 index 0000000..d8f30c3 --- /dev/null +++ b/src/main/java/com/iflytop/gd/infrastructure/devices/MotorDrivenDoor.java @@ -0,0 +1,32 @@ +package com.iflytop.gd.infrastructure.devices; + +import com.iflytop.gd.infrastructure.drivers.ModuleId; +import com.iflytop.gd.system.devices.Door; +import com.iflytop.gd.system.devices.StepMotor; +import com.iflytop.gd.system.drivers.CommandBus; +import org.springframework.stereotype.Component; + +/** + * 步进电机驱动的门实现 + */ +@Component +public class MotorDrivenDoor implements Door { + + private final StepMotor doorMotor; + + public MotorDrivenDoor(CommandBus commandBus) { + this.doorMotor = new StandardStepMotor(ModuleId.DoorM, commandBus); + } + + @Override + public void open() { + + } + + + @Override + public void close() { + + } + +} diff --git a/src/main/java/com/iflytop/gd/infrastructure/devices/MotorDrivenLiquidFillingArm.java b/src/main/java/com/iflytop/gd/infrastructure/devices/MotorDrivenLiquidFillingArm.java new file mode 100644 index 0000000..d72ea54 --- /dev/null +++ b/src/main/java/com/iflytop/gd/infrastructure/devices/MotorDrivenLiquidFillingArm.java @@ -0,0 +1,21 @@ +package com.iflytop.gd.infrastructure.devices; + + +import com.iflytop.gd.infrastructure.drivers.ModuleId; +import com.iflytop.gd.system.devices.StepMotor; +import com.iflytop.gd.system.drivers.CommandBus; +import org.springframework.stereotype.Component; + +/** + * 液体加注机械臂 + */ +@Component +public class MotorDrivenLiquidFillingArm { + private final StepMotor largeArmMotor; + private final StepMotor smallArmMotor; + + public MotorDrivenLiquidFillingArm(CommandBus commandBus) { + this.largeArmMotor = new StandardStepMotor(ModuleId.DualRobotAxis1M, commandBus); + this.smallArmMotor = new StandardStepMotor(ModuleId.DualRobotAxis2M, commandBus); + } +} diff --git a/src/main/java/com/iflytop/gd/infrastructure/devices/MotorDrivenTransportationArm.java b/src/main/java/com/iflytop/gd/infrastructure/devices/MotorDrivenTransportationArm.java new file mode 100644 index 0000000..97cbc1a --- /dev/null +++ b/src/main/java/com/iflytop/gd/infrastructure/devices/MotorDrivenTransportationArm.java @@ -0,0 +1,23 @@ +package com.iflytop.gd.infrastructure.devices; + +import com.iflytop.gd.infrastructure.drivers.ModuleId; +import com.iflytop.gd.system.devices.StepMotor; +import com.iflytop.gd.system.drivers.CommandBus; +import org.springframework.stereotype.Component; + +/** + * 试管转移机械臂 + */ +@Component +public class MotorDrivenTransportationArm { + + private StepMotor xDimMotor; + private StepMotor yDimMotor; + private StepMotor zDimMotor; + + public MotorDrivenTransportationArm(CommandBus commandBus) { + this.xDimMotor = new StandardStepMotor(ModuleId.HBotXM, commandBus); + this.yDimMotor = new StandardStepMotor(ModuleId.HBotYM, commandBus); + this.zDimMotor = new StandardStepMotor(ModuleId.HBotZM, commandBus); + } +} diff --git a/src/main/java/com/iflytop/gd/system/devices/StandardStepMotor.java b/src/main/java/com/iflytop/gd/infrastructure/devices/StandardStepMotor.java similarity index 97% rename from src/main/java/com/iflytop/gd/system/devices/StandardStepMotor.java rename to src/main/java/com/iflytop/gd/infrastructure/devices/StandardStepMotor.java index c9ae12d..d8a0d1d 100644 --- a/src/main/java/com/iflytop/gd/system/devices/StandardStepMotor.java +++ b/src/main/java/com/iflytop/gd/infrastructure/devices/StandardStepMotor.java @@ -1,9 +1,10 @@ -package com.iflytop.gd.system.devices; +package com.iflytop.gd.infrastructure.devices; import com.iflytop.gd.infrastructure.drivers.CmdId; import com.iflytop.gd.infrastructure.drivers.ModuleId; import com.iflytop.gd.system.constants.DistanceUnit; import com.iflytop.gd.system.constants.RotationDirection; +import com.iflytop.gd.system.devices.StepMotor; import com.iflytop.gd.system.drivers.CommandBus; import com.iflytop.gd.system.exceptions.CommandExecTimeoutException; import com.iflytop.gd.system.exceptions.HardwareErrorException; diff --git a/src/main/java/com/iflytop/gd/infrastructure/drivers/WebSocketCommandBusImpl.java b/src/main/java/com/iflytop/gd/infrastructure/drivers/WebSocketCommandBusImpl.java index 000101e..a2dfde8 100644 --- a/src/main/java/com/iflytop/gd/infrastructure/drivers/WebSocketCommandBusImpl.java +++ b/src/main/java/com/iflytop/gd/infrastructure/drivers/WebSocketCommandBusImpl.java @@ -1,24 +1,23 @@ package com.iflytop.gd.infrastructure.drivers; import cn.hutool.core.util.ObjectUtil; -import com.iflytop.gd.common.exception.AppException; import com.iflytop.gd.system.drivers.CommandBus; import com.iflytop.gd.system.exceptions.CommandExecTimeoutException; import com.iflytop.gd.system.exceptions.HardwareErrorException; import com.iflytop.gd.system.models.DataPacket; import com.iflytop.gd.system.utils.ByteArray; +import jakarta.annotation.PostConstruct; import jakarta.websocket.*; import lombok.extern.slf4j.Slf4j; import org.springframework.beans.factory.annotation.Value; +import org.springframework.scheduling.annotation.EnableScheduling; +import org.springframework.scheduling.annotation.Scheduled; import org.springframework.stereotype.Component; -import org.springframework.web.socket.client.WebSocketClient; import java.io.IOException; import java.net.URI; import java.util.concurrent.CountDownLatch; import java.util.concurrent.TimeUnit; -import java.util.concurrent.atomic.AtomicReference; -import java.util.concurrent.locks.ReentrantLock; /** * 使用WebSocket实现命令总线功能 @@ -26,29 +25,47 @@ import java.util.concurrent.locks.ReentrantLock; @Slf4j @Component @ClientEndpoint +@EnableScheduling public class WebSocketCommandBusImpl implements CommandBus { private Session session; - private WebSocketClient webSocketClient; private CountDownLatch countDownLatch; private DataPacket lastDataPacket; private Integer packetIndex = 0; + private final String COMMAND_BUS_WEBSOCKET_URL; + public static final int PACKET_TYPE_CMD = 0xA0; public static final int PACKET_TYPE_ACK = 0xA1; public static final int PACKET_TYPE_ERROR_ACK = 0xA2; public static final int PACKET_TYPE_EVENT = 0xA3; //TODO 配置硬件服务段ws链接 - public WebSocketCommandBusImpl(@Value("${}") String websocketServerUrl) throws DeploymentException, IOException { - WebSocketContainer container = ContainerProvider.getWebSocketContainer(); - URI endpointURI = URI.create(websocketServerUrl); - container.connectToServer(this, endpointURI); + public WebSocketCommandBusImpl(@Value("${command_bus.websocket_server_url}") String websocketServerUrl) throws DeploymentException, IOException { + this.COMMAND_BUS_WEBSOCKET_URL = websocketServerUrl; + } + + @Scheduled(fixedRate = 5000) + @PostConstruct + public void connectToCommandBusWebSocketServer() throws DeploymentException, IOException { + if (this.session == null || !this.session.isOpen()) { + WebSocketContainer container = ContainerProvider.getWebSocketContainer(); + URI endpointURI = URI.create(COMMAND_BUS_WEBSOCKET_URL); + try { + this.session = container.connectToServer(this, endpointURI); + } catch (Exception e) { + log.error(e.getMessage(), e); + } + } + } @Override public synchronized DataPacket waitForCommandExec(DataPacket commandPacket, Integer timeout, TimeUnit unit) throws CommandExecTimeoutException, HardwareErrorException, IOException, InterruptedException { try { + if (!this.session.isOpen()) { + throw new IOException("Session is not open"); + } packetIndex = packetIndex + 1; if (packetIndex > 30000) { packetIndex = 1; diff --git a/src/main/java/com/iflytop/gd/system/constants/Dim.java b/src/main/java/com/iflytop/gd/system/constants/Dim.java new file mode 100644 index 0000000..14de525 --- /dev/null +++ b/src/main/java/com/iflytop/gd/system/constants/Dim.java @@ -0,0 +1,5 @@ +package com.iflytop.gd.system.constants; + +public enum Dim { + X, Y, Z +} diff --git a/src/main/java/com/iflytop/gd/system/constants/LiquidFillArmMotorIndex.java b/src/main/java/com/iflytop/gd/system/constants/LiquidFillArmMotorIndex.java new file mode 100644 index 0000000..78ba069 --- /dev/null +++ b/src/main/java/com/iflytop/gd/system/constants/LiquidFillArmMotorIndex.java @@ -0,0 +1,5 @@ +package com.iflytop.gd.system.constants; + +public enum LiquidFillArmMotorIndex { + LargeArm, SmallArm +} diff --git a/src/main/java/com/iflytop/gd/system/devices/BinaryDevice.java b/src/main/java/com/iflytop/gd/system/devices/BinaryDevice.java deleted file mode 100644 index 77f7ff9..0000000 --- a/src/main/java/com/iflytop/gd/system/devices/BinaryDevice.java +++ /dev/null @@ -1,11 +0,0 @@ -package com.iflytop.gd.system.devices; - -/** - * 开关类型设备驱动定义 - */ -public interface BinaryDevice { - void open(); - void close(); - boolean isOpen(); - boolean isClosed(); -} diff --git a/src/main/java/com/iflytop/gd/system/devices/ColdTray.java b/src/main/java/com/iflytop/gd/system/devices/ColdTray.java index 9627f15..80db602 100644 --- a/src/main/java/com/iflytop/gd/system/devices/ColdTray.java +++ b/src/main/java/com/iflytop/gd/system/devices/ColdTray.java @@ -4,4 +4,51 @@ package com.iflytop.gd.system.devices; * 冷阱 */ public interface ColdTray { + + /** + * 设置温度 + * @param temperature + * @return + */ + boolean setTemperature(Double temperature); + + /** + * 开启循环 + * @return + */ + boolean openRecycle(); + + + /** + * 关闭循环 + * @return + */ + boolean closeRecycle(); + + + /** + * 开启加热 + * @return + */ + boolean openHeating(); + + + /** + * 关闭加热 + * @return + */ + boolean closeHeating(); + + /** + * 开启制冷 + * @return + */ + boolean openRefrigeration(); + + + /** + * 关闭制冷 + * @return + */ + boolean closeRefrigeration(); } diff --git a/src/main/java/com/iflytop/gd/system/devices/Door.java b/src/main/java/com/iflytop/gd/system/devices/Door.java new file mode 100644 index 0000000..6ca8977 --- /dev/null +++ b/src/main/java/com/iflytop/gd/system/devices/Door.java @@ -0,0 +1,7 @@ +package com.iflytop.gd.system.devices; + +public interface Door { + void open(); + void close(); + +} diff --git a/src/main/java/com/iflytop/gd/system/devices/DoorStepMotor.java b/src/main/java/com/iflytop/gd/system/devices/DoorStepMotor.java deleted file mode 100644 index 14fdd24..0000000 --- a/src/main/java/com/iflytop/gd/system/devices/DoorStepMotor.java +++ /dev/null @@ -1,9 +0,0 @@ -package com.iflytop.gd.system.devices; - -/** - * 步进电机 - */ -public class DoorStepMotor { - - -} diff --git a/src/main/java/com/iflytop/gd/system/devices/Fan.java b/src/main/java/com/iflytop/gd/system/devices/Fan.java index b3410fb..94abec9 100644 --- a/src/main/java/com/iflytop/gd/system/devices/Fan.java +++ b/src/main/java/com/iflytop/gd/system/devices/Fan.java @@ -4,4 +4,18 @@ package com.iflytop.gd.system.devices; * 风扇 */ public interface Fan { + + /** + * 开启风扇 + * @return + */ + boolean open(); + + + /** + * 关闭风扇 + * @return + */ + boolean close(); + } diff --git a/src/main/java/com/iflytop/gd/system/devices/Heater.java b/src/main/java/com/iflytop/gd/system/devices/Heater.java index 31e9ee5..c659cb7 100644 --- a/src/main/java/com/iflytop/gd/system/devices/Heater.java +++ b/src/main/java/com/iflytop/gd/system/devices/Heater.java @@ -4,4 +4,17 @@ package com.iflytop.gd.system.devices; * 加热器 */ public interface Heater { + + /** + * 打开 + * @return + */ + boolean open(); + + + /** + * 关闭 + * @return + */ + boolean close(); } diff --git a/src/main/java/com/iflytop/gd/system/devices/LiquidFillingArm.java b/src/main/java/com/iflytop/gd/system/devices/LiquidFillingArm.java index 3487d81..587e468 100644 --- a/src/main/java/com/iflytop/gd/system/devices/LiquidFillingArm.java +++ b/src/main/java/com/iflytop/gd/system/devices/LiquidFillingArm.java @@ -1,21 +1,38 @@ package com.iflytop.gd.system.devices; +import com.iflytop.gd.system.constants.LiquidFillArmMotorIndex; +import com.iflytop.gd.system.constants.RotationDirection; +import com.iflytop.gd.system.constants.SpeedUnit; +import com.iflytop.gd.system.models.Point3D; -import com.iflytop.gd.infrastructure.drivers.ModuleId; -import com.iflytop.gd.system.drivers.CommandBus; -import jakarta.annotation.Resource; -import org.springframework.stereotype.Component; /** - * 液体加注机械臂 + * 加液机械臂 */ -@Component -public class LiquidFillingArm { - private final StepMotor largeArmMotor; - private final StepMotor smallArmMotor; +public interface LiquidFillingArm { - public LiquidFillingArm(CommandBus commandBus) { - this.largeArmMotor = new StandardStepMotor(ModuleId.DualRobotAxis1M, commandBus); - this.smallArmMotor = new StandardStepMotor(ModuleId.DualRobotAxis2M, commandBus); - } + /** + * 机械臂移动到指定点 + * @param point 点坐标 + * @return 是否移动完成 + */ + boolean moveTo(Point3D point); + + /** + * 旋转到指定角度 + * @param liquidFillArmMotorIndex 机械臂电机索引 + * @param angle 角度 + * @param direction 旋转方向 + * @return 是否旋转完成 + */ + boolean rotateTo(LiquidFillArmMotorIndex liquidFillArmMotorIndex, Integer angle, RotationDirection direction); + + + /** + * 设置旋转速率 + * @param speed 速率值 + * @param speedUnit 速率单位 + * @return 是否设置成功 + */ + boolean setRotationSpeed(Integer speed, SpeedUnit speedUnit); } diff --git a/src/main/java/com/iflytop/gd/system/devices/Relay.java b/src/main/java/com/iflytop/gd/system/devices/Relay.java index 0c6d88f..455033f 100644 --- a/src/main/java/com/iflytop/gd/system/devices/Relay.java +++ b/src/main/java/com/iflytop/gd/system/devices/Relay.java @@ -4,4 +4,16 @@ package com.iflytop.gd.system.devices; * 继电器 */ public interface Relay { + /** + * 打开继电器 + * @return + */ + boolean open(); + + + /** + * 闭合继电器 + * @return + */ + boolean close(); } diff --git a/src/main/java/com/iflytop/gd/system/devices/Sensor.java b/src/main/java/com/iflytop/gd/system/devices/Sensor.java new file mode 100644 index 0000000..a84cbfd --- /dev/null +++ b/src/main/java/com/iflytop/gd/system/devices/Sensor.java @@ -0,0 +1,4 @@ +package com.iflytop.gd.system.devices; + +public interface Sensor { +} diff --git a/src/main/java/com/iflytop/gd/system/devices/TransportationArm.java b/src/main/java/com/iflytop/gd/system/devices/TransportationArm.java index e499402..92452e5 100644 --- a/src/main/java/com/iflytop/gd/system/devices/TransportationArm.java +++ b/src/main/java/com/iflytop/gd/system/devices/TransportationArm.java @@ -1,22 +1,42 @@ package com.iflytop.gd.system.devices; -import com.iflytop.gd.infrastructure.drivers.ModuleId; -import com.iflytop.gd.system.drivers.CommandBus; -import org.springframework.stereotype.Component; +import com.iflytop.gd.system.constants.Dim; +import com.iflytop.gd.system.constants.DistanceUnit; +import com.iflytop.gd.system.constants.SpeedUnit; +import com.iflytop.gd.system.models.Point3D; -/** - * 试管转移机械臂 - */ -@Component -public class TransportationArm { +public interface TransportationArm { - private StepMotor xDimMotor; - private StepMotor yDimMotor; - private StepMotor zDimMotor; + /** + * 机械臂XYZ三个维度移动指定距离 + * @param dim 维度 + * @param distance 距离值 + * @param unit 距离单位 + * @return 是否移动完成 + */ + boolean move(Dim dim, Integer distance, DistanceUnit unit); - public TransportationArm(CommandBus commandBus) { - this.xDimMotor = new StandardStepMotor(ModuleId.HBotXM, commandBus); - this.yDimMotor = new StandardStepMotor(ModuleId.HBotYM, commandBus); - this.zDimMotor = new StandardStepMotor(ModuleId.HBotZM, commandBus); - } + /** + * 移动到指定点 + * @param point 点坐标 + * @param unit 距离单位 + * @return 是否移动完成 + */ + boolean move(Point3D point, DistanceUnit unit); + + /** + * 独立停止XYZ三个维度移动 + * @param dim 维度 + * @return 是否执行完成 + */ + boolean stop(Dim dim); + + /** + * 独立设置XYZ三个轴移动速率 + * @param dim 维度 + * @param speed 速率 + * @param unit 速率单位 + * @return 是否设置完成 + */ + boolean setSpeed(Dim dim, Integer speed, SpeedUnit unit); } diff --git a/src/main/java/com/iflytop/gd/system/models/Point3D.java b/src/main/java/com/iflytop/gd/system/models/Point3D.java new file mode 100644 index 0000000..0cb6470 --- /dev/null +++ b/src/main/java/com/iflytop/gd/system/models/Point3D.java @@ -0,0 +1,23 @@ +package com.iflytop.gd.system.models; + + +import lombok.Getter; + +@Getter +public class Point3D { + private final Integer x; + private final Integer y; + private final Integer z; + + public Point3D(Integer x, Integer y, Integer z) { + this.x = x; + this.y = y; + this.z = z; + } + + public Point3D(Integer x, Integer y) { + this.x = x; + this.y = y; + this.z = 0; + } +} diff --git a/src/main/resources/application.yml b/src/main/resources/application.yml index c1e3ae3..8781a47 100644 --- a/src/main/resources/application.yml +++ b/src/main/resources/application.yml @@ -31,4 +31,5 @@ logging: springdoc: default-flat-param-object: true - +command_bus: + websocket_server_url: ws://127.0.0.1