From cf934cc92c0e527920299d54a44fbc551d72a3ae Mon Sep 17 00:00:00 2001 From: huangxiang <155373492@qq.com> Date: Tue, 29 Apr 2025 19:44:01 +0800 Subject: [PATCH] =?UTF-8?q?feat:=E5=AE=8C=E5=96=84=E8=AE=BE=E5=A4=87?= =?UTF-8?q?=E5=AE=9A=E4=B9=89?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../iflytop/gd/app/service/WebSocketService.java | 6 + .../gd/common/notification/Notification.java | 14 ++ .../gd/debug/controller/CmdDebugController.java | 10 +- .../cmds/ColdTrapStartHeatingCommandHandler.java | 3 + .../services/cmds/DoorCloseCommandHandler.java | 1 - .../services/cmds/DoorOpenCommandHandler.java | 13 +- .../gd/infrastructure/devices/MotorDrivenDoor.java | 70 -------- .../devices/MotorDrivenLiquidFillingArm.java | 43 ----- .../devices/MotorDrivenTransportationArm.java | 51 ------ .../infrastructure/devices/StandardServoMotor.java | 65 ------- .../infrastructure/devices/StandardStepMotor.java | 175 ------------------- .../gd/infrastructure/devices/VirtualDoor.java | 25 --- .../gd/infrastructure/devices/VirtualHeater.java | 33 ---- .../devices/VirtualLiquidFillingArm.java | 28 --- .../infrastructure/devices/VirtualServoMotor.java | 75 -------- .../infrastructure/devices/VirtualStepMotor.java | 194 --------------------- .../devices/VirtualTransportationArm.java | 92 ---------- .../devices/physical/PhysicalColdTray.java | 43 +++++ .../devices/physical/PhysicalDoor.java | 70 ++++++++ .../devices/physical/PhysicalFan.java | 18 ++ .../devices/physical/PhysicalHoldingJaw.java | 33 ++++ .../devices/physical/PhysicalLiquidFillingArm.java | 41 +++++ .../devices/physical/PhysicalPump.java | 9 + .../devices/physical/PhysicalRelay.java | 18 ++ .../devices/physical/PhysicalServoMotor.java | 65 +++++++ .../devices/physical/PhysicalStepMotor.java | 175 +++++++++++++++++++ .../devices/physical/PhysicalSwitchSensor.java | 14 ++ .../physical/PhysicalTransportationArm.java | 47 +++++ .../devices/virtual/VirtualColdTray.java | 43 +++++ .../devices/virtual/VirtualDoor.java | 25 +++ .../infrastructure/devices/virtual/VirtualFan.java | 18 ++ .../devices/virtual/VirtualHeater.java | 33 ++++ .../devices/virtual/VirtualHoldingJaw.java | 33 ++++ .../devices/virtual/VirtualLiquidFillingArm.java | 26 +++ .../devices/virtual/VirtualPump.java | 9 + .../devices/virtual/VirtualRelay.java | 15 ++ .../devices/virtual/VirtualServoMotor.java | 73 ++++++++ .../devices/virtual/VirtualStepMotor.java | 194 +++++++++++++++++++++ .../devices/virtual/VirtualSwitchSensor.java | 13 ++ .../devices/virtual/VirtualTransportationArm.java | 89 ++++++++++ .../infrastructure/repository/DeviceFactory.java | 27 +++ .../iflytop/gd/system/constants/SystemMode.java | 8 + .../java/com/iflytop/gd/system/devices/Sensor.java | 4 - .../iflytop/gd/system/devices/SwitchSensor.java | 5 + 44 files changed, 1185 insertions(+), 861 deletions(-) delete mode 100644 src/main/java/com/iflytop/gd/infrastructure/devices/MotorDrivenDoor.java delete mode 100644 src/main/java/com/iflytop/gd/infrastructure/devices/MotorDrivenLiquidFillingArm.java delete mode 100644 src/main/java/com/iflytop/gd/infrastructure/devices/MotorDrivenTransportationArm.java delete mode 100644 src/main/java/com/iflytop/gd/infrastructure/devices/StandardServoMotor.java delete mode 100644 src/main/java/com/iflytop/gd/infrastructure/devices/StandardStepMotor.java delete mode 100644 src/main/java/com/iflytop/gd/infrastructure/devices/VirtualDoor.java delete mode 100644 src/main/java/com/iflytop/gd/infrastructure/devices/VirtualHeater.java delete mode 100644 src/main/java/com/iflytop/gd/infrastructure/devices/VirtualLiquidFillingArm.java delete mode 100644 src/main/java/com/iflytop/gd/infrastructure/devices/VirtualServoMotor.java delete mode 100644 src/main/java/com/iflytop/gd/infrastructure/devices/VirtualStepMotor.java delete mode 100644 src/main/java/com/iflytop/gd/infrastructure/devices/VirtualTransportationArm.java create mode 100644 src/main/java/com/iflytop/gd/infrastructure/devices/physical/PhysicalColdTray.java create mode 100644 src/main/java/com/iflytop/gd/infrastructure/devices/physical/PhysicalDoor.java create mode 100644 src/main/java/com/iflytop/gd/infrastructure/devices/physical/PhysicalFan.java create mode 100644 src/main/java/com/iflytop/gd/infrastructure/devices/physical/PhysicalHoldingJaw.java create mode 100644 src/main/java/com/iflytop/gd/infrastructure/devices/physical/PhysicalLiquidFillingArm.java create mode 100644 src/main/java/com/iflytop/gd/infrastructure/devices/physical/PhysicalPump.java create mode 100644 src/main/java/com/iflytop/gd/infrastructure/devices/physical/PhysicalRelay.java create mode 100644 src/main/java/com/iflytop/gd/infrastructure/devices/physical/PhysicalServoMotor.java create mode 100644 src/main/java/com/iflytop/gd/infrastructure/devices/physical/PhysicalStepMotor.java create mode 100644 src/main/java/com/iflytop/gd/infrastructure/devices/physical/PhysicalSwitchSensor.java create mode 100644 src/main/java/com/iflytop/gd/infrastructure/devices/physical/PhysicalTransportationArm.java create mode 100644 src/main/java/com/iflytop/gd/infrastructure/devices/virtual/VirtualColdTray.java create mode 100644 src/main/java/com/iflytop/gd/infrastructure/devices/virtual/VirtualDoor.java create mode 100644 src/main/java/com/iflytop/gd/infrastructure/devices/virtual/VirtualFan.java create mode 100644 src/main/java/com/iflytop/gd/infrastructure/devices/virtual/VirtualHeater.java create mode 100644 src/main/java/com/iflytop/gd/infrastructure/devices/virtual/VirtualHoldingJaw.java create mode 100644 src/main/java/com/iflytop/gd/infrastructure/devices/virtual/VirtualLiquidFillingArm.java create mode 100644 src/main/java/com/iflytop/gd/infrastructure/devices/virtual/VirtualPump.java create mode 100644 src/main/java/com/iflytop/gd/infrastructure/devices/virtual/VirtualRelay.java create mode 100644 src/main/java/com/iflytop/gd/infrastructure/devices/virtual/VirtualServoMotor.java create mode 100644 src/main/java/com/iflytop/gd/infrastructure/devices/virtual/VirtualStepMotor.java create mode 100644 src/main/java/com/iflytop/gd/infrastructure/devices/virtual/VirtualSwitchSensor.java create mode 100644 src/main/java/com/iflytop/gd/infrastructure/devices/virtual/VirtualTransportationArm.java create mode 100644 src/main/java/com/iflytop/gd/infrastructure/repository/DeviceFactory.java create mode 100644 src/main/java/com/iflytop/gd/system/constants/SystemMode.java delete mode 100644 src/main/java/com/iflytop/gd/system/devices/Sensor.java create mode 100644 src/main/java/com/iflytop/gd/system/devices/SwitchSensor.java diff --git a/src/main/java/com/iflytop/gd/app/service/WebSocketService.java b/src/main/java/com/iflytop/gd/app/service/WebSocketService.java index 5a13e4c..9516794 100644 --- a/src/main/java/com/iflytop/gd/app/service/WebSocketService.java +++ b/src/main/java/com/iflytop/gd/app/service/WebSocketService.java @@ -1,6 +1,7 @@ package com.iflytop.gd.app.service; import cn.hutool.json.JSONUtil; +import com.iflytop.gd.common.notification.Notification; import com.iflytop.gd.infrastructure.config.WebSocketServer; import com.iflytop.gd.app.model.dto.WebsocketResult; import org.springframework.stereotype.Service; @@ -15,4 +16,9 @@ public class WebSocketService { WebSocketServer.sendMessageToClients(JSONUtil.toJsonStr(websocketResult)); } + + public void pushNotification(Notification notification) { + push("notification", notification); + } + } diff --git a/src/main/java/com/iflytop/gd/common/notification/Notification.java b/src/main/java/com/iflytop/gd/common/notification/Notification.java index 724b0ee..32d7865 100644 --- a/src/main/java/com/iflytop/gd/common/notification/Notification.java +++ b/src/main/java/com/iflytop/gd/common/notification/Notification.java @@ -1,6 +1,7 @@ package com.iflytop.gd.common.notification; import cn.hutool.core.date.DateTime; +import com.iflytop.gd.app.model.dto.CmdDTO; import lombok.Getter; @@ -41,6 +42,14 @@ public class Notification { return new Notification(commandId, command, "info", title, content); } + public static Notification infoNotification(String commandId, String command, String title) { + return new Notification(commandId, command, "info", title, ""); + } + + public static Notification infoNotification(CmdDTO cmdDTO, String title) { + return new Notification(cmdDTO.getCommandId(), cmdDTO.getCommand(), "info", title, ""); + } + /** * 创建Warn级别通知 @@ -60,6 +69,11 @@ public class Notification { return new Notification(commandId, command, "error", title, content); } + public static Notification errorNotification(CmdDTO cmdDTO, Exception e) { + String title = String.format("执行{}出错", cmdDTO.getCommand()); + return new Notification(cmdDTO.getCommandId(), cmdDTO.getCommand(), "error", title, e.getMessage()); + } + /** * 创建Fatal级别通知 diff --git a/src/main/java/com/iflytop/gd/debug/controller/CmdDebugController.java b/src/main/java/com/iflytop/gd/debug/controller/CmdDebugController.java index 708d71b..7dc7b95 100644 --- a/src/main/java/com/iflytop/gd/debug/controller/CmdDebugController.java +++ b/src/main/java/com/iflytop/gd/debug/controller/CmdDebugController.java @@ -1,6 +1,7 @@ package com.iflytop.gd.debug.controller; import com.iflytop.gd.app.model.dto.CmdDTO; +import com.iflytop.gd.app.service.WebSocketService; import com.iflytop.gd.app.service.exceptions.UnSupportCommandException; import com.iflytop.gd.common.result.Result; import com.iflytop.gd.common.cmd.CommandHandler; @@ -15,6 +16,8 @@ import org.springframework.web.bind.annotation.RequestBody; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; +import java.util.concurrent.CompletableFuture; + @Tag(name = "前端调试指令") @RestController @RequestMapping("/api/debug/cmd") @@ -28,10 +31,11 @@ public class CmdDebugController { public Result controlMethod(@Valid @RequestBody CmdDTO cmdDTO) { String commandName = cmdDTO.getCommand(); try { + log.info("收到调试指令{}", commandName); CommandHandler commandHandler = registry.getCommandHandler(commandName); - log.info("调试指令{}开始执行", commandName); - commandHandler.handle(cmdDTO); - log.info("调试指令{}执行结束", commandName); + log.info("找到指令处理器{}", commandHandler.getClass().getName()); + CompletableFuture.runAsync(() -> commandHandler.handle(cmdDTO)); + log.info("指令处理器提交异步执行"); } catch (UnSupportCommandException exception) { log.error("未找到对应的调试指令{}", commandName); String errorMsg = "未找到对应的调试指令, commandName=" + commandName; diff --git a/src/main/java/com/iflytop/gd/debug/services/cmds/ColdTrapStartHeatingCommandHandler.java b/src/main/java/com/iflytop/gd/debug/services/cmds/ColdTrapStartHeatingCommandHandler.java index aaa350d..b547e2c 100644 --- a/src/main/java/com/iflytop/gd/debug/services/cmds/ColdTrapStartHeatingCommandHandler.java +++ b/src/main/java/com/iflytop/gd/debug/services/cmds/ColdTrapStartHeatingCommandHandler.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.system.devices.ColdTray; import lombok.RequiredArgsConstructor; import lombok.extern.slf4j.Slf4j; import org.springframework.stereotype.Component; @@ -15,6 +16,8 @@ import org.springframework.stereotype.Component; @RequiredArgsConstructor @CommandMapping("debug_cold_trap_start_heating") public class ColdTrapStartHeatingCommandHandler implements CommandHandler { + private final ColdTray coldTray; + @Override public void handle(CmdDTO cmdDTO) { 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 01d571a..e94aebf 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,7 +3,6 @@ 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; 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 6b47902..f309740 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 @@ -1,9 +1,10 @@ package com.iflytop.gd.debug.services.cmds; import com.iflytop.gd.app.model.dto.CmdDTO; +import com.iflytop.gd.app.service.WebSocketService; 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.common.notification.Notification; import com.iflytop.gd.system.devices.Door; import lombok.RequiredArgsConstructor; import lombok.extern.slf4j.Slf4j; @@ -19,8 +20,16 @@ import org.springframework.stereotype.Component; public class DoorOpenCommandHandler implements CommandHandler { private final Door door; + private final WebSocketService webSocketService; + @Override public void handle(CmdDTO cmdDTO) { - door.open(); + webSocketService.pushNotification(Notification.infoNotification(cmdDTO.getCommandId(), cmdDTO.getCommand(), "开始执行开门指令")); + try { + door.open(); + } catch (Exception e) { + webSocketService.pushNotification(Notification.errorNotification(cmdDTO, e)); + } + webSocketService.pushNotification( Notification.infoNotification(cmdDTO.getCommandId(), cmdDTO.getCommand(), "开门指令执行完成")); } } diff --git a/src/main/java/com/iflytop/gd/infrastructure/devices/MotorDrivenDoor.java b/src/main/java/com/iflytop/gd/infrastructure/devices/MotorDrivenDoor.java deleted file mode 100644 index 257f13d..0000000 --- a/src/main/java/com/iflytop/gd/infrastructure/devices/MotorDrivenDoor.java +++ /dev/null @@ -1,70 +0,0 @@ -package com.iflytop.gd.infrastructure.devices; - -import com.iflytop.gd.common.exception.AppException; -import com.iflytop.gd.common.result.ResultCode; -import com.iflytop.gd.infrastructure.drivers.ModuleId; -import com.iflytop.gd.system.constants.DistanceUnit; -import com.iflytop.gd.system.devices.Door; -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; -import org.springframework.context.annotation.Profile; -import org.springframework.stereotype.Component; - -import java.io.IOException; - -/** - * 步进电机驱动的门实现 - */ -@Profile("test") -@Component -public class MotorDrivenDoor implements Door { - - private final StepMotor doorMotor; - - public MotorDrivenDoor(CommandBus commandBus) { - this.doorMotor = new VirtualStepMotor(ModuleId.DoorM); - } - - @Override - public void open() { - try { - doorMotor.easyMoveTo(100, DistanceUnit.MM); - } catch (HardwareErrorException e) { - throw new AppException(ResultCode.HARDWARE_ERROR); - } catch (CommandExecTimeoutException e) { - throw new AppException(ResultCode.COMMAND_EXEC_TIMEOUT); - } catch (IOException | InterruptedException e) { - throw new AppException(ResultCode.SYSTEM_ERROR); - } - } - - - @Override - public void close() { - try { - doorMotor.easyMoveToZero(); - } catch (HardwareErrorException e) { - throw new AppException(ResultCode.HARDWARE_ERROR); - } catch (CommandExecTimeoutException e) { - throw new AppException(ResultCode.COMMAND_EXEC_TIMEOUT); - } catch (IOException | InterruptedException e) { - throw new AppException(ResultCode.SYSTEM_ERROR); - } - } - - @Override - public void stop() { - try { - doorMotor.stop(); - } catch (HardwareErrorException e) { - throw new AppException(ResultCode.HARDWARE_ERROR); - } catch (CommandExecTimeoutException e) { - throw new AppException(ResultCode.COMMAND_EXEC_TIMEOUT); - } catch (IOException | InterruptedException e) { - throw new AppException(ResultCode.SYSTEM_ERROR); - } - } - -} diff --git a/src/main/java/com/iflytop/gd/infrastructure/devices/MotorDrivenLiquidFillingArm.java b/src/main/java/com/iflytop/gd/infrastructure/devices/MotorDrivenLiquidFillingArm.java deleted file mode 100644 index dbb961c..0000000 --- a/src/main/java/com/iflytop/gd/infrastructure/devices/MotorDrivenLiquidFillingArm.java +++ /dev/null @@ -1,43 +0,0 @@ -package com.iflytop.gd.infrastructure.devices; - - -import com.iflytop.gd.infrastructure.drivers.ModuleId; -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.devices.LiquidFillingArm; -import com.iflytop.gd.system.devices.StepMotor; -import com.iflytop.gd.system.drivers.CommandBus; -import com.iflytop.gd.system.models.Point3D; -import org.springframework.context.annotation.Profile; -import org.springframework.stereotype.Component; - -/** - * 液体加注机械臂 - */ -@Profile("test") -@Component -public class MotorDrivenLiquidFillingArm implements LiquidFillingArm { - 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); - } - - @Override - public void moveTo(Point3D point) { - - } - - @Override - public void rotateTo(LiquidFillArmMotorIndex liquidFillArmMotorIndex, Integer angle, RotationDirection direction) { - - } - - @Override - public void setRotationSpeed(Integer speed, SpeedUnit speedUnit) { - - } -} diff --git a/src/main/java/com/iflytop/gd/infrastructure/devices/MotorDrivenTransportationArm.java b/src/main/java/com/iflytop/gd/infrastructure/devices/MotorDrivenTransportationArm.java deleted file mode 100644 index f86e6c9..0000000 --- a/src/main/java/com/iflytop/gd/infrastructure/devices/MotorDrivenTransportationArm.java +++ /dev/null @@ -1,51 +0,0 @@ -package com.iflytop.gd.infrastructure.devices; - -import com.iflytop.gd.infrastructure.drivers.ModuleId; -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.devices.StepMotor; -import com.iflytop.gd.system.devices.TransportationArm; -import com.iflytop.gd.system.drivers.CommandBus; -import com.iflytop.gd.system.models.Point3D; -import org.springframework.context.annotation.Profile; -import org.springframework.stereotype.Component; - -/** - * 电机驱动转移机械臂 - */ -@Profile("test") -@Component -public class MotorDrivenTransportationArm implements TransportationArm { - - private final StepMotor xDimMotor; - private final StepMotor yDimMotor; - private final 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); - } - - @Override - public void moveTo(Dim dim, Integer distance, DistanceUnit unit) { - - } - - @Override - public void relativelyMove(Dim dim, Integer distance, DistanceUnit unit) { - - } - - - @Override - public void stop(Dim dim) { - - } - - @Override - public void setSpeed(Dim dim, Integer speed, SpeedUnit unit) { - - } -} diff --git a/src/main/java/com/iflytop/gd/infrastructure/devices/StandardServoMotor.java b/src/main/java/com/iflytop/gd/infrastructure/devices/StandardServoMotor.java deleted file mode 100644 index a8c723e..0000000 --- a/src/main/java/com/iflytop/gd/infrastructure/devices/StandardServoMotor.java +++ /dev/null @@ -1,65 +0,0 @@ -package com.iflytop.gd.infrastructure.devices; - -import com.iflytop.gd.infrastructure.drivers.RegIndex; -import com.iflytop.gd.system.devices.ServoMotor; - - -/** - * 标准物理伺服电机 - */ -public class StandardServoMotor implements ServoMotor { - @Override - public void enable() { - - } - - @Override - public void disable() { - - } - - @Override - public void stop() { - - } - - @Override - public Integer getCurrentPosition() { - return 0; - } - - @Override - public void moveToZero() { - - } - - @Override - public void moveTo(Integer position) { - - } - - @Override - public void setMaxVelocity(Integer maxVelocity) { - - } - - @Override - public void setMaxTorque(Integer maxTorque) { - - } - - @Override - public void setProtectiveTorque(Integer protectiveTorque) { - - } - - @Override - public Integer readReg(RegIndex regIndex) { - return 0; - } - - @Override - public void writeReg(RegIndex regIndex, Integer value) { - - } -} diff --git a/src/main/java/com/iflytop/gd/infrastructure/devices/StandardStepMotor.java b/src/main/java/com/iflytop/gd/infrastructure/devices/StandardStepMotor.java deleted file mode 100644 index 02f682e..0000000 --- a/src/main/java/com/iflytop/gd/infrastructure/devices/StandardStepMotor.java +++ /dev/null @@ -1,175 +0,0 @@ -package com.iflytop.gd.infrastructure.devices; - -import com.iflytop.gd.infrastructure.drivers.CmdId; -import com.iflytop.gd.infrastructure.drivers.ModuleId; -import com.iflytop.gd.infrastructure.drivers.RegIndex; -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; -import com.iflytop.gd.system.models.DataPacket; - -import java.io.IOException; -import java.util.Map; -import java.util.concurrent.TimeUnit; - -/** - * 标准电机实现 - */ -public class StandardStepMotor implements StepMotor { - protected final ModuleId moduleId; - protected final CommandBus commandBus; - protected final Integer DEFAULT_COMMAND_EXEC_TIMEOUT_SECONDS = 10; - - protected StandardStepMotor(ModuleId moduleId, CommandBus commandBus) { - this.moduleId = moduleId; - this.commandBus = commandBus; - } - - - @Override - public void easyMoveTo(Integer value, DistanceUnit unit) throws HardwareErrorException, CommandExecTimeoutException, IOException, InterruptedException { - DataPacket commandDataPacket = DataPacket.createCommandDataPacket(moduleId.index, CmdId.step_motor_easy_move_to.index, unit.toMM(value)); - commandBus.waitForCommandExec(commandDataPacket, DEFAULT_COMMAND_EXEC_TIMEOUT_SECONDS, TimeUnit.SECONDS); - } - - @Override - public void easyMoveBy(Integer value, DistanceUnit unit) throws HardwareErrorException, CommandExecTimeoutException, IOException, InterruptedException { - DataPacket commandDataPacket = DataPacket.createCommandDataPacket(moduleId.index, CmdId.step_motor_easy_move_by.index, unit.toMM(value)); - commandBus.waitForCommandExec(commandDataPacket, DEFAULT_COMMAND_EXEC_TIMEOUT_SECONDS, TimeUnit.SECONDS); - } - - @Override - public void easyMoveToZero() throws HardwareErrorException, CommandExecTimeoutException, IOException, InterruptedException { - DataPacket commandDataPacket = DataPacket.createCommandDataPacket(moduleId.index, CmdId.step_motor_easy_move_to_zero.index); - commandBus.waitForCommandExec(commandDataPacket, DEFAULT_COMMAND_EXEC_TIMEOUT_SECONDS, TimeUnit.SECONDS); - } - - @Override - public void easyMoveToZeroPointQuick() { - - } - - @Override - public void enable() { - - } - - @Override - public void disable() { - - } - - @Override - public void moveForward(RotationDirection direction, Integer distance, DistanceUnit unit) throws HardwareErrorException, CommandExecTimeoutException, IOException, InterruptedException { - - } - - @Override - public void moveBackward(RotationDirection direction, Integer distance, DistanceUnit unit) throws HardwareErrorException, CommandExecTimeoutException, IOException, InterruptedException { - - } - - @Override - public Integer readPosition() { - return 0; - } - - @Override - public Integer readEncoderPosition() { - return 0; - } - - - @Override - public void stop() throws HardwareErrorException, CommandExecTimeoutException, IOException, InterruptedException { - - } - - @Override - public void rotateForward() { - - } - - @Override - public void rotateBackward() { - - } - - - @Override - public Map readIOState() { - return null; - } - - - @Override - public void setReg(RegIndex regIndex, Integer value) throws HardwareErrorException, CommandExecTimeoutException, IOException, InterruptedException { - DataPacket dataPacket = DataPacket.createCommandDataPacket(moduleId.index, CmdId.module_set_reg.index, value); - commandBus.waitForCommandExec(dataPacket, DEFAULT_COMMAND_EXEC_TIMEOUT_SECONDS, TimeUnit.SECONDS); - } - - @Override - public Integer readReg(RegIndex regIndex) throws HardwareErrorException, CommandExecTimeoutException, IOException, InterruptedException { - DataPacket commandDataPacket = DataPacket.createCommandDataPacket(moduleId.index, CmdId.module_get_reg.index); - DataPacket ackDataPacket = commandBus.waitForCommandExec(commandDataPacket, DEFAULT_COMMAND_EXEC_TIMEOUT_SECONDS, TimeUnit.SECONDS); - return ackDataPacket.getContentI32(0); - } - - @Override - public void setMRes(Integer value) throws HardwareErrorException, CommandExecTimeoutException, IOException, InterruptedException { - - } - - @Override - public void setIRun(Integer value) throws HardwareErrorException, CommandExecTimeoutException, IOException, InterruptedException { - - } - - @Override - public void setIHold(Integer value) throws HardwareErrorException, CommandExecTimeoutException, IOException, InterruptedException { - - } - - @Override - public void setStartAndStopVelocity(Integer value) throws HardwareErrorException, CommandExecTimeoutException, IOException, InterruptedException { - - } - - @Override - public void setV1(Integer value) throws HardwareErrorException, CommandExecTimeoutException, IOException, InterruptedException { - - } - - @Override - public void setA1AndD1(Integer value) throws HardwareErrorException, CommandExecTimeoutException, IOException, InterruptedException { - - } - - @Override - public void setAmaxAndDmax(Integer value) throws HardwareErrorException, CommandExecTimeoutException, IOException, InterruptedException { - - } - - @Override - public void setDefaultVelocity(Integer value) throws HardwareErrorException, CommandExecTimeoutException, IOException, InterruptedException { - - } - - @Override - public void setVelocity(Integer low, Integer mid, Integer high) throws HardwareErrorException, CommandExecTimeoutException, IOException, InterruptedException { - - } - - @Override - public void setOneCyclePulse(Integer pause, Integer denominator) throws HardwareErrorException, CommandExecTimeoutException, IOException, InterruptedException { - - } - - @Override - public void setDZero(Integer value) throws HardwareErrorException, CommandExecTimeoutException, IOException, InterruptedException { - - } -} diff --git a/src/main/java/com/iflytop/gd/infrastructure/devices/VirtualDoor.java b/src/main/java/com/iflytop/gd/infrastructure/devices/VirtualDoor.java deleted file mode 100644 index a8de039..0000000 --- a/src/main/java/com/iflytop/gd/infrastructure/devices/VirtualDoor.java +++ /dev/null @@ -1,25 +0,0 @@ -package com.iflytop.gd.infrastructure.devices; - -import com.iflytop.gd.system.devices.Door; -import org.springframework.context.annotation.Profile; -import org.springframework.stereotype.Component; - -@Component -@Profile("dev") -public class VirtualDoor implements Door { - private boolean isOpen = false; - @Override - public void open() { - this.isOpen = true; - } - - @Override - public void close() { - this.isOpen = false; - } - - @Override - public void stop() { - - } -} diff --git a/src/main/java/com/iflytop/gd/infrastructure/devices/VirtualHeater.java b/src/main/java/com/iflytop/gd/infrastructure/devices/VirtualHeater.java deleted file mode 100644 index d40030f..0000000 --- a/src/main/java/com/iflytop/gd/infrastructure/devices/VirtualHeater.java +++ /dev/null @@ -1,33 +0,0 @@ -package com.iflytop.gd.infrastructure.devices; - -import com.iflytop.gd.system.devices.Heater; -import com.iflytop.gd.system.models.HeaterStatus; - - -/** - * 虚拟加热器 - */ -public class VirtualHeater implements Heater { - private final HeaterStatus heaterStatus = new HeaterStatus(); - - - @Override - public void open() { - this.heaterStatus.setOpen(true); - } - - @Override - public void close() { - this.heaterStatus.setOpen(false); - } - - @Override - public Double getCurrentTemperature() { - return this.heaterStatus.getCurrentTemperature(); - } - - @Override - public Double getTargetTemperature() { - return this.heaterStatus.getTargetTemperature(); - } -} diff --git a/src/main/java/com/iflytop/gd/infrastructure/devices/VirtualLiquidFillingArm.java b/src/main/java/com/iflytop/gd/infrastructure/devices/VirtualLiquidFillingArm.java deleted file mode 100644 index 9ac2c04..0000000 --- a/src/main/java/com/iflytop/gd/infrastructure/devices/VirtualLiquidFillingArm.java +++ /dev/null @@ -1,28 +0,0 @@ -package com.iflytop.gd.infrastructure.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.devices.LiquidFillingArm; -import com.iflytop.gd.system.models.Point3D; -import org.springframework.context.annotation.Profile; -import org.springframework.stereotype.Component; - -/** - * 虚拟加液机械臂 - */ -@Profile("dev") -@Component -public class VirtualLiquidFillingArm implements LiquidFillingArm { - @Override - public void moveTo(Point3D point) { - } - - @Override - public void rotateTo(LiquidFillArmMotorIndex liquidFillArmMotorIndex, Integer angle, RotationDirection direction) { - } - - @Override - public void setRotationSpeed(Integer speed, SpeedUnit speedUnit) { - } -} diff --git a/src/main/java/com/iflytop/gd/infrastructure/devices/VirtualServoMotor.java b/src/main/java/com/iflytop/gd/infrastructure/devices/VirtualServoMotor.java deleted file mode 100644 index 67715fe..0000000 --- a/src/main/java/com/iflytop/gd/infrastructure/devices/VirtualServoMotor.java +++ /dev/null @@ -1,75 +0,0 @@ -package com.iflytop.gd.infrastructure.devices; - -import com.iflytop.gd.infrastructure.drivers.RegIndex; -import com.iflytop.gd.system.devices.ServoMotor; -import com.iflytop.gd.system.models.ServoMotorStatus; -import org.springframework.context.annotation.Profile; -import org.springframework.stereotype.Component; - -import java.util.HashMap; -import java.util.Map; - - -/** - * 虚拟伺服电机 - */ -@Component -@Profile("dev") -public class VirtualServoMotor implements ServoMotor { - private final ServoMotorStatus servoMotorStatus = new ServoMotorStatus(); - private final Map registers = new HashMap(); - @Override - public void enable() { - this.servoMotorStatus.setEnabled(true); - } - - @Override - public void disable() { - this.servoMotorStatus.setEnabled(false); - } - - @Override - public void stop() { - this.servoMotorStatus.setStopped(true); - } - - @Override - public Integer getCurrentPosition() { - return this.servoMotorStatus.getCurrentPosition(); - } - - @Override - public void moveToZero() { - this.servoMotorStatus.setCurrentPosition(0); - } - - @Override - public void moveTo(Integer position) { - this.servoMotorStatus.setCurrentPosition(position); - } - - @Override - public void setMaxVelocity(Integer maxVelocity) { - //TODO 等待硬件给出寄存器索引 - } - - @Override - public void setMaxTorque(Integer maxTorque) { - //TODO 等待硬件给出寄存器索引 - } - - @Override - public void setProtectiveTorque(Integer protectiveTorque) { - //TODO 等待硬件给出寄存器索引 - } - - @Override - public Integer readReg(RegIndex regIndex) { - return this.registers.get(regIndex); - } - - @Override - public void writeReg(RegIndex regIndex, Integer value) { - this.registers.put(regIndex, value); - } -} diff --git a/src/main/java/com/iflytop/gd/infrastructure/devices/VirtualStepMotor.java b/src/main/java/com/iflytop/gd/infrastructure/devices/VirtualStepMotor.java deleted file mode 100644 index add4875..0000000 --- a/src/main/java/com/iflytop/gd/infrastructure/devices/VirtualStepMotor.java +++ /dev/null @@ -1,194 +0,0 @@ -package com.iflytop.gd.infrastructure.devices; - -import cn.hutool.json.JSONUtil; -import com.iflytop.gd.infrastructure.drivers.ModuleId; -import com.iflytop.gd.infrastructure.drivers.RegIndex; -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.exceptions.CommandExecTimeoutException; -import com.iflytop.gd.system.exceptions.HardwareErrorException; -import com.iflytop.gd.system.models.StepMotorStatus; -import lombok.extern.slf4j.Slf4j; - -import java.io.IOException; -import java.util.HashMap; -import java.util.Map; - -/** - * 虚拟步进电机 - */ -@Slf4j -public class VirtualStepMotor implements StepMotor { - private StepMotorStatus stepMotorStatus = new StepMotorStatus(); - private Map registers = new HashMap(); - private final ModuleId moduleId; - - public VirtualStepMotor(ModuleId moduleId) { - this.moduleId = moduleId; - } - - @Override - public void easyMoveBy(Integer value, DistanceUnit unit) throws HardwareErrorException, CommandExecTimeoutException, IOException, InterruptedException { - int position = this.stepMotorStatus.getCurrentPosition() + unit.toMM(value); - this.stepMotorStatus.setCurrentPosition(position); - this.stepMotorStatus.setStopped(true); - } - - @Override - public void easyMoveTo(Integer value, DistanceUnit unit) throws HardwareErrorException, CommandExecTimeoutException, IOException, InterruptedException { - log.debug("Moving to {}", value); - this.stepMotorStatus.setCurrentPosition(unit.toMM(value)); - this.stepMotorStatus.setZeroPosition(this.stepMotorStatus.getCurrentPosition() == 0); - this.stepMotorStatus.setStopped(true); - log.debug("MotorStatus {}", JSONUtil.toJsonStr(this.stepMotorStatus)); - } - - @Override - public void easyMoveToZero() throws HardwareErrorException, CommandExecTimeoutException, IOException, InterruptedException { - log.debug("Easy move to zero."); - this.stepMotorStatus.setCurrentPosition(0); - this.stepMotorStatus.setZeroPosition(this.stepMotorStatus.getCurrentPosition() == 0); - this.stepMotorStatus.setStopped(true); - log.debug("MotorStatus {}", JSONUtil.toJsonStr(this.stepMotorStatus)); - } - - @Override - public void easyMoveToZeroPointQuick() { - log.debug("Easy move to zero quick."); - this.stepMotorStatus.setCurrentPosition(0); - this.stepMotorStatus.setZeroPosition(this.stepMotorStatus.getCurrentPosition() == 0); - this.stepMotorStatus.setStopped(true); - log.debug("MotorStatus {}", JSONUtil.toJsonStr(this.stepMotorStatus)); - } - - @Override - public void enable() { - log.debug("Enable step motor."); - this.stepMotorStatus.setEnabled(true); - log.debug("MotorStatus {}", JSONUtil.toJsonStr(this.stepMotorStatus)); - } - - @Override - public void disable() { - log.debug("Disable step motor."); - this.stepMotorStatus.setEnabled(false); - log.debug("MotorStatus {}", JSONUtil.toJsonStr(this.stepMotorStatus)); - } - - @Override - public void moveForward(RotationDirection direction, Integer distance, DistanceUnit unit) throws HardwareErrorException, CommandExecTimeoutException, IOException, InterruptedException { - - } - - @Override - public void moveBackward(RotationDirection direction, Integer distance, DistanceUnit unit) throws HardwareErrorException, CommandExecTimeoutException, IOException, InterruptedException { - - } - - @Override - public void stop() throws HardwareErrorException, CommandExecTimeoutException, IOException, InterruptedException { - log.debug("Stop step motor."); - this.stepMotorStatus.setStopped(true); - log.debug("MotorStatus {}", JSONUtil.toJsonStr(this.stepMotorStatus)); - } - - @Override - public void rotateForward() { - - } - - @Override - public void rotateBackward() { - - } - - @Override - public Map readIOState() { - Map ioState = new HashMap<>(); - ioState.put("IO1", true); - ioState.put("IO2", false); - return ioState; - } - - @Override - public Integer readPosition() { - return this.stepMotorStatus.getCurrentPosition(); - } - - @Override - public Integer readEncoderPosition() { - return this.stepMotorStatus.getEncoderPosition(); - } - - @Override - public void setReg(RegIndex regIndex, Integer value) throws HardwareErrorException, CommandExecTimeoutException, IOException, InterruptedException { - this.registers.put(regIndex, value); - } - - @Override - public Integer readReg(RegIndex regIndex) throws HardwareErrorException, CommandExecTimeoutException, IOException, InterruptedException { - return this.registers.get(regIndex); - } - - @Override - public void setMRes(Integer value) throws HardwareErrorException, CommandExecTimeoutException, IOException, InterruptedException { - setReg(RegIndex.kreg_step_motor_mres, value); - } - - @Override - public void setIRun(Integer value) throws HardwareErrorException, CommandExecTimeoutException, IOException, InterruptedException { - setReg(RegIndex.kreg_step_motor_irun, value); - } - - @Override - public void setIHold(Integer value) throws HardwareErrorException, CommandExecTimeoutException, IOException, InterruptedException { - setReg(RegIndex.kreg_step_motor_ihold, value); - } - - @Override - public void setStartAndStopVelocity(Integer value) throws HardwareErrorException, CommandExecTimeoutException, IOException, InterruptedException { - setReg(RegIndex.kreg_step_motor_vstart, value); - setReg(RegIndex.kreg_step_motor_vstop, value); - } - - @Override - public void setV1(Integer value) throws HardwareErrorException, CommandExecTimeoutException, IOException, InterruptedException { - setReg(RegIndex.kreg_step_motor_v1, value); - } - - @Override - public void setA1AndD1(Integer value) throws HardwareErrorException, CommandExecTimeoutException, IOException, InterruptedException { - setReg(RegIndex.kreg_step_motor_a1, value); - setReg(RegIndex.kreg_step_motor_d1, value); - } - - @Override - public void setAmaxAndDmax(Integer value) throws HardwareErrorException, CommandExecTimeoutException, IOException, InterruptedException { - setReg(RegIndex.kreg_step_motor_amax, value); - setReg(RegIndex.kreg_step_motor_dmax, value); - } - - @Override - public void setDefaultVelocity(Integer value) throws HardwareErrorException, CommandExecTimeoutException, IOException, InterruptedException { - setReg(RegIndex.kreg_step_motor_default_velocity, value); - } - - @Override - public void setVelocity(Integer low, Integer mid, Integer high) throws HardwareErrorException, CommandExecTimeoutException, IOException, InterruptedException { - setReg(RegIndex.kreg_step_motor_low_velocity, low); - setReg(RegIndex.kreg_step_motor_mid_velocity, mid); - setReg(RegIndex.kreg_step_motor_high_velocity, high); - } - - @Override - public void setOneCyclePulse(Integer pause, Integer denominator) throws HardwareErrorException, CommandExecTimeoutException, IOException, InterruptedException { - setReg(RegIndex.kreg_step_motor_one_circle_pulse, pause); - setReg(RegIndex.kreg_step_motor_one_circle_pulse_denominator, denominator); - } - - @Override - public void setDZero(Integer value) throws HardwareErrorException, CommandExecTimeoutException, IOException, InterruptedException { - setReg(RegIndex.kreg_step_motor_dzero_pos, value); - } -} diff --git a/src/main/java/com/iflytop/gd/infrastructure/devices/VirtualTransportationArm.java b/src/main/java/com/iflytop/gd/infrastructure/devices/VirtualTransportationArm.java deleted file mode 100644 index bcd5b3b..0000000 --- a/src/main/java/com/iflytop/gd/infrastructure/devices/VirtualTransportationArm.java +++ /dev/null @@ -1,92 +0,0 @@ -package com.iflytop.gd.infrastructure.devices; - -import com.iflytop.gd.common.exception.AppException; -import com.iflytop.gd.common.result.ResultCode; -import com.iflytop.gd.infrastructure.drivers.ModuleId; -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.devices.StepMotor; -import com.iflytop.gd.system.devices.TransportationArm; -import com.iflytop.gd.system.exceptions.CommandExecTimeoutException; -import com.iflytop.gd.system.exceptions.HardwareErrorException; -import com.iflytop.gd.system.models.Point3D; -import org.springframework.context.annotation.Profile; -import org.springframework.stereotype.Component; - -import java.io.IOException; -import java.util.HashMap; -import java.util.Map; - -/** - * 虚拟转移机械臂 - */ -@Component -@Profile("dev") -public class VirtualTransportationArm implements TransportationArm { - private final StepMotor xDimMotor; - private final StepMotor yDimMotor; - private final StepMotor zDimMotor; - private final Map stepMotors = new HashMap(); - - public VirtualTransportationArm() { - this.xDimMotor = new VirtualStepMotor(ModuleId.HBotXM); - this.yDimMotor = new VirtualStepMotor(ModuleId.HBotYM); - this.zDimMotor = new VirtualStepMotor(ModuleId.HBotZM); - stepMotors.put(Dim.X, xDimMotor); - stepMotors.put(Dim.Y, yDimMotor); - stepMotors.put(Dim.Z, zDimMotor); - } - - @Override - public void moveTo(Dim dim, Integer distance, DistanceUnit unit) { - try { - stepMotors.get(dim).easyMoveTo(distance, unit); - } catch (HardwareErrorException e) { - throw new AppException(ResultCode.HARDWARE_ERROR); - } catch (CommandExecTimeoutException e) { - throw new AppException(ResultCode.COMMAND_EXEC_TIMEOUT); - } catch (IOException | InterruptedException e) { - throw new AppException(ResultCode.SYSTEM_ERROR); - } - } - - @Override - public void relativelyMove(Dim dim, Integer distance, DistanceUnit unit) { - try { - stepMotors.get(dim).easyMoveBy(distance, unit); - } catch (HardwareErrorException e) { - throw new AppException(ResultCode.HARDWARE_ERROR); - } catch (CommandExecTimeoutException e) { - throw new AppException(ResultCode.COMMAND_EXEC_TIMEOUT); - } catch (IOException | InterruptedException e) { - throw new AppException(ResultCode.SYSTEM_ERROR); - } - } - - @Override - public void stop(Dim dim) { - try { - stepMotors.get(dim).stop(); - } catch (HardwareErrorException e) { - throw new AppException(ResultCode.HARDWARE_ERROR); - } catch (CommandExecTimeoutException e) { - throw new AppException(ResultCode.COMMAND_EXEC_TIMEOUT); - } catch (IOException | InterruptedException e) { - throw new AppException(ResultCode.SYSTEM_ERROR); - } - } - - @Override - public void setSpeed(Dim dim, Integer speed, SpeedUnit unit) { - try { - stepMotors.get(dim).setDefaultVelocity(unit.toMM_PER_SEC(speed)); - } catch (HardwareErrorException e) { - throw new AppException(ResultCode.HARDWARE_ERROR); - } catch (CommandExecTimeoutException e) { - throw new AppException(ResultCode.COMMAND_EXEC_TIMEOUT); - } catch (IOException | InterruptedException e) { - throw new AppException(ResultCode.SYSTEM_ERROR); - } - } -} diff --git a/src/main/java/com/iflytop/gd/infrastructure/devices/physical/PhysicalColdTray.java b/src/main/java/com/iflytop/gd/infrastructure/devices/physical/PhysicalColdTray.java new file mode 100644 index 0000000..6b986f9 --- /dev/null +++ b/src/main/java/com/iflytop/gd/infrastructure/devices/physical/PhysicalColdTray.java @@ -0,0 +1,43 @@ +package com.iflytop.gd.infrastructure.devices.physical; + +import com.iflytop.gd.system.devices.ColdTray; + +/** + * 物理冷阱 + */ +public class PhysicalColdTray implements ColdTray { + @Override + public boolean setTemperature(Double temperature) { + return false; + } + + @Override + public boolean openRecycle() { + return false; + } + + @Override + public boolean closeRecycle() { + return false; + } + + @Override + public boolean openHeating() { + return false; + } + + @Override + public boolean closeHeating() { + return false; + } + + @Override + public boolean openRefrigeration() { + return false; + } + + @Override + public boolean closeRefrigeration() { + return false; + } +} diff --git a/src/main/java/com/iflytop/gd/infrastructure/devices/physical/PhysicalDoor.java b/src/main/java/com/iflytop/gd/infrastructure/devices/physical/PhysicalDoor.java new file mode 100644 index 0000000..a63d026 --- /dev/null +++ b/src/main/java/com/iflytop/gd/infrastructure/devices/physical/PhysicalDoor.java @@ -0,0 +1,70 @@ +package com.iflytop.gd.infrastructure.devices.physical; + +import com.iflytop.gd.common.exception.AppException; +import com.iflytop.gd.common.result.ResultCode; +import com.iflytop.gd.infrastructure.devices.virtual.VirtualStepMotor; +import com.iflytop.gd.infrastructure.drivers.ModuleId; +import com.iflytop.gd.system.constants.DistanceUnit; +import com.iflytop.gd.system.devices.Door; +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; +import org.springframework.context.annotation.Profile; +import org.springframework.stereotype.Component; + +import java.io.IOException; + +/** + * 物理门 + */ + +public class PhysicalDoor implements Door { + + private final StepMotor doorMotor; + + public PhysicalDoor(CommandBus commandBus) { + this.doorMotor = new VirtualStepMotor(ModuleId.DoorM); + } + + @Override + public void open() { + try { + doorMotor.easyMoveTo(100, DistanceUnit.MM); + } catch (HardwareErrorException e) { + throw new AppException(ResultCode.HARDWARE_ERROR); + } catch (CommandExecTimeoutException e) { + throw new AppException(ResultCode.COMMAND_EXEC_TIMEOUT); + } catch (IOException | InterruptedException e) { + throw new AppException(ResultCode.SYSTEM_ERROR); + } + } + + + @Override + public void close() { + try { + doorMotor.easyMoveToZero(); + } catch (HardwareErrorException e) { + throw new AppException(ResultCode.HARDWARE_ERROR); + } catch (CommandExecTimeoutException e) { + throw new AppException(ResultCode.COMMAND_EXEC_TIMEOUT); + } catch (IOException | InterruptedException e) { + throw new AppException(ResultCode.SYSTEM_ERROR); + } + } + + @Override + public void stop() { + try { + doorMotor.stop(); + } catch (HardwareErrorException e) { + throw new AppException(ResultCode.HARDWARE_ERROR); + } catch (CommandExecTimeoutException e) { + throw new AppException(ResultCode.COMMAND_EXEC_TIMEOUT); + } catch (IOException | InterruptedException e) { + throw new AppException(ResultCode.SYSTEM_ERROR); + } + } + +} diff --git a/src/main/java/com/iflytop/gd/infrastructure/devices/physical/PhysicalFan.java b/src/main/java/com/iflytop/gd/infrastructure/devices/physical/PhysicalFan.java new file mode 100644 index 0000000..611727b --- /dev/null +++ b/src/main/java/com/iflytop/gd/infrastructure/devices/physical/PhysicalFan.java @@ -0,0 +1,18 @@ +package com.iflytop.gd.infrastructure.devices.physical; + +import com.iflytop.gd.system.devices.Fan; + +/** + * 物理风扇 + */ +public class PhysicalFan implements Fan { + @Override + public boolean open() { + return false; + } + + @Override + public boolean close() { + return false; + } +} diff --git a/src/main/java/com/iflytop/gd/infrastructure/devices/physical/PhysicalHoldingJaw.java b/src/main/java/com/iflytop/gd/infrastructure/devices/physical/PhysicalHoldingJaw.java new file mode 100644 index 0000000..34a3a46 --- /dev/null +++ b/src/main/java/com/iflytop/gd/infrastructure/devices/physical/PhysicalHoldingJaw.java @@ -0,0 +1,33 @@ +package com.iflytop.gd.infrastructure.devices.physical; + +import com.iflytop.gd.system.devices.HoldingJaw; + +/** + * 物理夹爪 + */ +public class PhysicalHoldingJaw implements HoldingJaw { + @Override + public void open() { + + } + + @Override + public void close() { + + } + + @Override + public void pause() { + + } + + @Override + public void resume() { + + } + + @Override + public void setSpeed(int speed) { + + } +} diff --git a/src/main/java/com/iflytop/gd/infrastructure/devices/physical/PhysicalLiquidFillingArm.java b/src/main/java/com/iflytop/gd/infrastructure/devices/physical/PhysicalLiquidFillingArm.java new file mode 100644 index 0000000..560c42f --- /dev/null +++ b/src/main/java/com/iflytop/gd/infrastructure/devices/physical/PhysicalLiquidFillingArm.java @@ -0,0 +1,41 @@ +package com.iflytop.gd.infrastructure.devices.physical; + + +import com.iflytop.gd.infrastructure.drivers.ModuleId; +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.devices.LiquidFillingArm; +import com.iflytop.gd.system.devices.StepMotor; +import com.iflytop.gd.system.drivers.CommandBus; +import com.iflytop.gd.system.models.Point3D; +import org.springframework.context.annotation.Profile; +import org.springframework.stereotype.Component; + +/** + * 物理液体加注机械臂 + */ +public class PhysicalLiquidFillingArm implements LiquidFillingArm { + private final StepMotor largeArmMotor; + private final StepMotor smallArmMotor; + + public PhysicalLiquidFillingArm(CommandBus commandBus) { + this.largeArmMotor = new PhysicalStepMotor(ModuleId.DualRobotAxis1M, commandBus); + this.smallArmMotor = new PhysicalStepMotor(ModuleId.DualRobotAxis2M, commandBus); + } + + @Override + public void moveTo(Point3D point) { + + } + + @Override + public void rotateTo(LiquidFillArmMotorIndex liquidFillArmMotorIndex, Integer angle, RotationDirection direction) { + + } + + @Override + public void setRotationSpeed(Integer speed, SpeedUnit speedUnit) { + + } +} diff --git a/src/main/java/com/iflytop/gd/infrastructure/devices/physical/PhysicalPump.java b/src/main/java/com/iflytop/gd/infrastructure/devices/physical/PhysicalPump.java new file mode 100644 index 0000000..7b065e5 --- /dev/null +++ b/src/main/java/com/iflytop/gd/infrastructure/devices/physical/PhysicalPump.java @@ -0,0 +1,9 @@ +package com.iflytop.gd.infrastructure.devices.physical; + +import com.iflytop.gd.system.devices.Pump; + +/** + * 物理泵 + */ +public class PhysicalPump implements Pump { +} diff --git a/src/main/java/com/iflytop/gd/infrastructure/devices/physical/PhysicalRelay.java b/src/main/java/com/iflytop/gd/infrastructure/devices/physical/PhysicalRelay.java new file mode 100644 index 0000000..658a310 --- /dev/null +++ b/src/main/java/com/iflytop/gd/infrastructure/devices/physical/PhysicalRelay.java @@ -0,0 +1,18 @@ +package com.iflytop.gd.infrastructure.devices.physical; + +import com.iflytop.gd.system.devices.Relay; + +/** + * 物理继电器 + */ +public class PhysicalRelay implements Relay { + @Override + public boolean open() { + return false; + } + + @Override + public boolean close() { + return false; + } +} diff --git a/src/main/java/com/iflytop/gd/infrastructure/devices/physical/PhysicalServoMotor.java b/src/main/java/com/iflytop/gd/infrastructure/devices/physical/PhysicalServoMotor.java new file mode 100644 index 0000000..e3d564f --- /dev/null +++ b/src/main/java/com/iflytop/gd/infrastructure/devices/physical/PhysicalServoMotor.java @@ -0,0 +1,65 @@ +package com.iflytop.gd.infrastructure.devices.physical; + +import com.iflytop.gd.infrastructure.drivers.RegIndex; +import com.iflytop.gd.system.devices.ServoMotor; + + +/** + * 物理伺服电机 + */ +public class PhysicalServoMotor implements ServoMotor { + @Override + public void enable() { + + } + + @Override + public void disable() { + + } + + @Override + public void stop() { + + } + + @Override + public Integer getCurrentPosition() { + return 0; + } + + @Override + public void moveToZero() { + + } + + @Override + public void moveTo(Integer position) { + + } + + @Override + public void setMaxVelocity(Integer maxVelocity) { + + } + + @Override + public void setMaxTorque(Integer maxTorque) { + + } + + @Override + public void setProtectiveTorque(Integer protectiveTorque) { + + } + + @Override + public Integer readReg(RegIndex regIndex) { + return 0; + } + + @Override + public void writeReg(RegIndex regIndex, Integer value) { + + } +} diff --git a/src/main/java/com/iflytop/gd/infrastructure/devices/physical/PhysicalStepMotor.java b/src/main/java/com/iflytop/gd/infrastructure/devices/physical/PhysicalStepMotor.java new file mode 100644 index 0000000..b457c35 --- /dev/null +++ b/src/main/java/com/iflytop/gd/infrastructure/devices/physical/PhysicalStepMotor.java @@ -0,0 +1,175 @@ +package com.iflytop.gd.infrastructure.devices.physical; + +import com.iflytop.gd.infrastructure.drivers.CmdId; +import com.iflytop.gd.infrastructure.drivers.ModuleId; +import com.iflytop.gd.infrastructure.drivers.RegIndex; +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; +import com.iflytop.gd.system.models.DataPacket; + +import java.io.IOException; +import java.util.Map; +import java.util.concurrent.TimeUnit; + +/** + * 物理步进电机实现 + */ +public class PhysicalStepMotor implements StepMotor { + protected final ModuleId moduleId; + protected final CommandBus commandBus; + protected final Integer DEFAULT_COMMAND_EXEC_TIMEOUT_SECONDS = 10; + + public PhysicalStepMotor(ModuleId moduleId, CommandBus commandBus) { + this.moduleId = moduleId; + this.commandBus = commandBus; + } + + + @Override + public void easyMoveTo(Integer value, DistanceUnit unit) throws HardwareErrorException, CommandExecTimeoutException, IOException, InterruptedException { + DataPacket commandDataPacket = DataPacket.createCommandDataPacket(moduleId.index, CmdId.step_motor_easy_move_to.index, unit.toMM(value)); + commandBus.waitForCommandExec(commandDataPacket, DEFAULT_COMMAND_EXEC_TIMEOUT_SECONDS, TimeUnit.SECONDS); + } + + @Override + public void easyMoveBy(Integer value, DistanceUnit unit) throws HardwareErrorException, CommandExecTimeoutException, IOException, InterruptedException { + DataPacket commandDataPacket = DataPacket.createCommandDataPacket(moduleId.index, CmdId.step_motor_easy_move_by.index, unit.toMM(value)); + commandBus.waitForCommandExec(commandDataPacket, DEFAULT_COMMAND_EXEC_TIMEOUT_SECONDS, TimeUnit.SECONDS); + } + + @Override + public void easyMoveToZero() throws HardwareErrorException, CommandExecTimeoutException, IOException, InterruptedException { + DataPacket commandDataPacket = DataPacket.createCommandDataPacket(moduleId.index, CmdId.step_motor_easy_move_to_zero.index); + commandBus.waitForCommandExec(commandDataPacket, DEFAULT_COMMAND_EXEC_TIMEOUT_SECONDS, TimeUnit.SECONDS); + } + + @Override + public void easyMoveToZeroPointQuick() { + + } + + @Override + public void enable() { + + } + + @Override + public void disable() { + + } + + @Override + public void moveForward(RotationDirection direction, Integer distance, DistanceUnit unit) throws HardwareErrorException, CommandExecTimeoutException, IOException, InterruptedException { + + } + + @Override + public void moveBackward(RotationDirection direction, Integer distance, DistanceUnit unit) throws HardwareErrorException, CommandExecTimeoutException, IOException, InterruptedException { + + } + + @Override + public Integer readPosition() { + return 0; + } + + @Override + public Integer readEncoderPosition() { + return 0; + } + + + @Override + public void stop() throws HardwareErrorException, CommandExecTimeoutException, IOException, InterruptedException { + + } + + @Override + public void rotateForward() { + + } + + @Override + public void rotateBackward() { + + } + + + @Override + public Map readIOState() { + return null; + } + + + @Override + public void setReg(RegIndex regIndex, Integer value) throws HardwareErrorException, CommandExecTimeoutException, IOException, InterruptedException { + DataPacket dataPacket = DataPacket.createCommandDataPacket(moduleId.index, CmdId.module_set_reg.index, value); + commandBus.waitForCommandExec(dataPacket, DEFAULT_COMMAND_EXEC_TIMEOUT_SECONDS, TimeUnit.SECONDS); + } + + @Override + public Integer readReg(RegIndex regIndex) throws HardwareErrorException, CommandExecTimeoutException, IOException, InterruptedException { + DataPacket commandDataPacket = DataPacket.createCommandDataPacket(moduleId.index, CmdId.module_get_reg.index); + DataPacket ackDataPacket = commandBus.waitForCommandExec(commandDataPacket, DEFAULT_COMMAND_EXEC_TIMEOUT_SECONDS, TimeUnit.SECONDS); + return ackDataPacket.getContentI32(0); + } + + @Override + public void setMRes(Integer value) throws HardwareErrorException, CommandExecTimeoutException, IOException, InterruptedException { + + } + + @Override + public void setIRun(Integer value) throws HardwareErrorException, CommandExecTimeoutException, IOException, InterruptedException { + + } + + @Override + public void setIHold(Integer value) throws HardwareErrorException, CommandExecTimeoutException, IOException, InterruptedException { + + } + + @Override + public void setStartAndStopVelocity(Integer value) throws HardwareErrorException, CommandExecTimeoutException, IOException, InterruptedException { + + } + + @Override + public void setV1(Integer value) throws HardwareErrorException, CommandExecTimeoutException, IOException, InterruptedException { + + } + + @Override + public void setA1AndD1(Integer value) throws HardwareErrorException, CommandExecTimeoutException, IOException, InterruptedException { + + } + + @Override + public void setAmaxAndDmax(Integer value) throws HardwareErrorException, CommandExecTimeoutException, IOException, InterruptedException { + + } + + @Override + public void setDefaultVelocity(Integer value) throws HardwareErrorException, CommandExecTimeoutException, IOException, InterruptedException { + + } + + @Override + public void setVelocity(Integer low, Integer mid, Integer high) throws HardwareErrorException, CommandExecTimeoutException, IOException, InterruptedException { + + } + + @Override + public void setOneCyclePulse(Integer pause, Integer denominator) throws HardwareErrorException, CommandExecTimeoutException, IOException, InterruptedException { + + } + + @Override + public void setDZero(Integer value) throws HardwareErrorException, CommandExecTimeoutException, IOException, InterruptedException { + + } +} diff --git a/src/main/java/com/iflytop/gd/infrastructure/devices/physical/PhysicalSwitchSensor.java b/src/main/java/com/iflytop/gd/infrastructure/devices/physical/PhysicalSwitchSensor.java new file mode 100644 index 0000000..5e67827 --- /dev/null +++ b/src/main/java/com/iflytop/gd/infrastructure/devices/physical/PhysicalSwitchSensor.java @@ -0,0 +1,14 @@ +package com.iflytop.gd.infrastructure.devices.physical; + +import com.iflytop.gd.system.devices.SwitchSensor; + +/** + * 物理传感器 + */ + +public class PhysicalSwitchSensor implements SwitchSensor { + @Override + public boolean isOpen() { + return false; + } +} diff --git a/src/main/java/com/iflytop/gd/infrastructure/devices/physical/PhysicalTransportationArm.java b/src/main/java/com/iflytop/gd/infrastructure/devices/physical/PhysicalTransportationArm.java new file mode 100644 index 0000000..bc25e05 --- /dev/null +++ b/src/main/java/com/iflytop/gd/infrastructure/devices/physical/PhysicalTransportationArm.java @@ -0,0 +1,47 @@ +package com.iflytop.gd.infrastructure.devices.physical; + +import com.iflytop.gd.infrastructure.drivers.ModuleId; +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.devices.StepMotor; +import com.iflytop.gd.system.devices.TransportationArm; +import com.iflytop.gd.system.drivers.CommandBus; + +/** + * 物理转移机械臂 + */ + +public class PhysicalTransportationArm implements TransportationArm { + + private final StepMotor xDimMotor; + private final StepMotor yDimMotor; + private final StepMotor zDimMotor; + + public PhysicalTransportationArm(CommandBus commandBus) { + this.xDimMotor = new PhysicalStepMotor(ModuleId.HBotXM, commandBus); + this.yDimMotor = new PhysicalStepMotor(ModuleId.HBotYM, commandBus); + this.zDimMotor = new PhysicalStepMotor(ModuleId.HBotZM, commandBus); + } + + @Override + public void moveTo(Dim dim, Integer distance, DistanceUnit unit) { + + } + + @Override + public void relativelyMove(Dim dim, Integer distance, DistanceUnit unit) { + + } + + + @Override + public void stop(Dim dim) { + + } + + @Override + public void setSpeed(Dim dim, Integer speed, SpeedUnit unit) { + + } +} diff --git a/src/main/java/com/iflytop/gd/infrastructure/devices/virtual/VirtualColdTray.java b/src/main/java/com/iflytop/gd/infrastructure/devices/virtual/VirtualColdTray.java new file mode 100644 index 0000000..0e48680 --- /dev/null +++ b/src/main/java/com/iflytop/gd/infrastructure/devices/virtual/VirtualColdTray.java @@ -0,0 +1,43 @@ +package com.iflytop.gd.infrastructure.devices.virtual; + +import com.iflytop.gd.system.devices.ColdTray; + +/** + * 虚拟冷阱 + */ +public class VirtualColdTray implements ColdTray { + @Override + public boolean setTemperature(Double temperature) { + return false; + } + + @Override + public boolean openRecycle() { + return false; + } + + @Override + public boolean closeRecycle() { + return false; + } + + @Override + public boolean openHeating() { + return false; + } + + @Override + public boolean closeHeating() { + return false; + } + + @Override + public boolean openRefrigeration() { + return false; + } + + @Override + public boolean closeRefrigeration() { + return false; + } +} diff --git a/src/main/java/com/iflytop/gd/infrastructure/devices/virtual/VirtualDoor.java b/src/main/java/com/iflytop/gd/infrastructure/devices/virtual/VirtualDoor.java new file mode 100644 index 0000000..d80acfd --- /dev/null +++ b/src/main/java/com/iflytop/gd/infrastructure/devices/virtual/VirtualDoor.java @@ -0,0 +1,25 @@ +package com.iflytop.gd.infrastructure.devices.virtual; + +import com.iflytop.gd.system.devices.Door; + +/** + * 虚拟门 + */ + +public class VirtualDoor implements Door { + private boolean isOpen = false; + @Override + public void open() { + this.isOpen = true; + } + + @Override + public void close() { + this.isOpen = false; + } + + @Override + public void stop() { + + } +} diff --git a/src/main/java/com/iflytop/gd/infrastructure/devices/virtual/VirtualFan.java b/src/main/java/com/iflytop/gd/infrastructure/devices/virtual/VirtualFan.java new file mode 100644 index 0000000..fd8036e --- /dev/null +++ b/src/main/java/com/iflytop/gd/infrastructure/devices/virtual/VirtualFan.java @@ -0,0 +1,18 @@ +package com.iflytop.gd.infrastructure.devices.virtual; + +import com.iflytop.gd.system.devices.Fan; + +/** + * 虚拟风扇 + */ +public class VirtualFan implements Fan { + @Override + public boolean open() { + return false; + } + + @Override + public boolean close() { + return false; + } +} diff --git a/src/main/java/com/iflytop/gd/infrastructure/devices/virtual/VirtualHeater.java b/src/main/java/com/iflytop/gd/infrastructure/devices/virtual/VirtualHeater.java new file mode 100644 index 0000000..e0c8ff8 --- /dev/null +++ b/src/main/java/com/iflytop/gd/infrastructure/devices/virtual/VirtualHeater.java @@ -0,0 +1,33 @@ +package com.iflytop.gd.infrastructure.devices.virtual; + +import com.iflytop.gd.system.devices.Heater; +import com.iflytop.gd.system.models.HeaterStatus; + + +/** + * 虚拟加热器 + */ +public class VirtualHeater implements Heater { + private final HeaterStatus heaterStatus = new HeaterStatus(); + + + @Override + public void open() { + this.heaterStatus.setOpen(true); + } + + @Override + public void close() { + this.heaterStatus.setOpen(false); + } + + @Override + public Double getCurrentTemperature() { + return this.heaterStatus.getCurrentTemperature(); + } + + @Override + public Double getTargetTemperature() { + return this.heaterStatus.getTargetTemperature(); + } +} diff --git a/src/main/java/com/iflytop/gd/infrastructure/devices/virtual/VirtualHoldingJaw.java b/src/main/java/com/iflytop/gd/infrastructure/devices/virtual/VirtualHoldingJaw.java new file mode 100644 index 0000000..a329a67 --- /dev/null +++ b/src/main/java/com/iflytop/gd/infrastructure/devices/virtual/VirtualHoldingJaw.java @@ -0,0 +1,33 @@ +package com.iflytop.gd.infrastructure.devices.virtual; + +import com.iflytop.gd.system.devices.HoldingJaw; + +/** + * 虚拟夹爪 + */ +public class VirtualHoldingJaw implements HoldingJaw { + @Override + public void open() { + + } + + @Override + public void close() { + + } + + @Override + public void pause() { + + } + + @Override + public void resume() { + + } + + @Override + public void setSpeed(int speed) { + + } +} diff --git a/src/main/java/com/iflytop/gd/infrastructure/devices/virtual/VirtualLiquidFillingArm.java b/src/main/java/com/iflytop/gd/infrastructure/devices/virtual/VirtualLiquidFillingArm.java new file mode 100644 index 0000000..47b1c3e --- /dev/null +++ b/src/main/java/com/iflytop/gd/infrastructure/devices/virtual/VirtualLiquidFillingArm.java @@ -0,0 +1,26 @@ +package com.iflytop.gd.infrastructure.devices.virtual; + +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.devices.LiquidFillingArm; +import com.iflytop.gd.system.models.Point3D; + + +/** + * 虚拟加液机械臂 + */ + +public class VirtualLiquidFillingArm implements LiquidFillingArm { + @Override + public void moveTo(Point3D point) { + } + + @Override + public void rotateTo(LiquidFillArmMotorIndex liquidFillArmMotorIndex, Integer angle, RotationDirection direction) { + } + + @Override + public void setRotationSpeed(Integer speed, SpeedUnit speedUnit) { + } +} diff --git a/src/main/java/com/iflytop/gd/infrastructure/devices/virtual/VirtualPump.java b/src/main/java/com/iflytop/gd/infrastructure/devices/virtual/VirtualPump.java new file mode 100644 index 0000000..d1bbb0c --- /dev/null +++ b/src/main/java/com/iflytop/gd/infrastructure/devices/virtual/VirtualPump.java @@ -0,0 +1,9 @@ +package com.iflytop.gd.infrastructure.devices.virtual; + +import com.iflytop.gd.system.devices.Pump; + +/** + * 虚拟泵 + */ +public class VirtualPump implements Pump { +} diff --git a/src/main/java/com/iflytop/gd/infrastructure/devices/virtual/VirtualRelay.java b/src/main/java/com/iflytop/gd/infrastructure/devices/virtual/VirtualRelay.java new file mode 100644 index 0000000..e1b3820 --- /dev/null +++ b/src/main/java/com/iflytop/gd/infrastructure/devices/virtual/VirtualRelay.java @@ -0,0 +1,15 @@ +package com.iflytop.gd.infrastructure.devices.virtual; + +import com.iflytop.gd.system.devices.Relay; + +public class VirtualRelay implements Relay { + @Override + public boolean open() { + return false; + } + + @Override + public boolean close() { + return false; + } +} diff --git a/src/main/java/com/iflytop/gd/infrastructure/devices/virtual/VirtualServoMotor.java b/src/main/java/com/iflytop/gd/infrastructure/devices/virtual/VirtualServoMotor.java new file mode 100644 index 0000000..d5e7f0c --- /dev/null +++ b/src/main/java/com/iflytop/gd/infrastructure/devices/virtual/VirtualServoMotor.java @@ -0,0 +1,73 @@ +package com.iflytop.gd.infrastructure.devices.virtual; + +import com.iflytop.gd.infrastructure.drivers.RegIndex; +import com.iflytop.gd.system.devices.ServoMotor; +import com.iflytop.gd.system.models.ServoMotorStatus; +import org.springframework.context.annotation.Profile; +import org.springframework.stereotype.Component; + +import java.util.HashMap; +import java.util.Map; + + +/** + * 虚拟伺服电机 + */ +public class VirtualServoMotor implements ServoMotor { + private final ServoMotorStatus servoMotorStatus = new ServoMotorStatus(); + private final Map registers = new HashMap(); + @Override + public void enable() { + this.servoMotorStatus.setEnabled(true); + } + + @Override + public void disable() { + this.servoMotorStatus.setEnabled(false); + } + + @Override + public void stop() { + this.servoMotorStatus.setStopped(true); + } + + @Override + public Integer getCurrentPosition() { + return this.servoMotorStatus.getCurrentPosition(); + } + + @Override + public void moveToZero() { + this.servoMotorStatus.setCurrentPosition(0); + } + + @Override + public void moveTo(Integer position) { + this.servoMotorStatus.setCurrentPosition(position); + } + + @Override + public void setMaxVelocity(Integer maxVelocity) { + //TODO 等待硬件给出寄存器索引 + } + + @Override + public void setMaxTorque(Integer maxTorque) { + //TODO 等待硬件给出寄存器索引 + } + + @Override + public void setProtectiveTorque(Integer protectiveTorque) { + //TODO 等待硬件给出寄存器索引 + } + + @Override + public Integer readReg(RegIndex regIndex) { + return this.registers.get(regIndex); + } + + @Override + public void writeReg(RegIndex regIndex, Integer value) { + this.registers.put(regIndex, value); + } +} diff --git a/src/main/java/com/iflytop/gd/infrastructure/devices/virtual/VirtualStepMotor.java b/src/main/java/com/iflytop/gd/infrastructure/devices/virtual/VirtualStepMotor.java new file mode 100644 index 0000000..b5a43b0 --- /dev/null +++ b/src/main/java/com/iflytop/gd/infrastructure/devices/virtual/VirtualStepMotor.java @@ -0,0 +1,194 @@ +package com.iflytop.gd.infrastructure.devices.virtual; + +import cn.hutool.json.JSONUtil; +import com.iflytop.gd.infrastructure.drivers.ModuleId; +import com.iflytop.gd.infrastructure.drivers.RegIndex; +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.exceptions.CommandExecTimeoutException; +import com.iflytop.gd.system.exceptions.HardwareErrorException; +import com.iflytop.gd.system.models.StepMotorStatus; +import lombok.extern.slf4j.Slf4j; + +import java.io.IOException; +import java.util.HashMap; +import java.util.Map; + +/** + * 虚拟步进电机 + */ +@Slf4j +public class VirtualStepMotor implements StepMotor { + private StepMotorStatus stepMotorStatus = new StepMotorStatus(); + private Map registers = new HashMap(); + private final ModuleId moduleId; + + public VirtualStepMotor(ModuleId moduleId) { + this.moduleId = moduleId; + } + + @Override + public void easyMoveBy(Integer value, DistanceUnit unit) throws HardwareErrorException, CommandExecTimeoutException, IOException, InterruptedException { + int position = this.stepMotorStatus.getCurrentPosition() + unit.toMM(value); + this.stepMotorStatus.setCurrentPosition(position); + this.stepMotorStatus.setStopped(true); + } + + @Override + public void easyMoveTo(Integer value, DistanceUnit unit) throws HardwareErrorException, CommandExecTimeoutException, IOException, InterruptedException { + log.debug("Moving to {}", value); + this.stepMotorStatus.setCurrentPosition(unit.toMM(value)); + this.stepMotorStatus.setZeroPosition(this.stepMotorStatus.getCurrentPosition() == 0); + this.stepMotorStatus.setStopped(true); + log.debug("MotorStatus {}", JSONUtil.toJsonStr(this.stepMotorStatus)); + } + + @Override + public void easyMoveToZero() throws HardwareErrorException, CommandExecTimeoutException, IOException, InterruptedException { + log.debug("Easy move to zero."); + this.stepMotorStatus.setCurrentPosition(0); + this.stepMotorStatus.setZeroPosition(this.stepMotorStatus.getCurrentPosition() == 0); + this.stepMotorStatus.setStopped(true); + log.debug("MotorStatus {}", JSONUtil.toJsonStr(this.stepMotorStatus)); + } + + @Override + public void easyMoveToZeroPointQuick() { + log.debug("Easy move to zero quick."); + this.stepMotorStatus.setCurrentPosition(0); + this.stepMotorStatus.setZeroPosition(this.stepMotorStatus.getCurrentPosition() == 0); + this.stepMotorStatus.setStopped(true); + log.debug("MotorStatus {}", JSONUtil.toJsonStr(this.stepMotorStatus)); + } + + @Override + public void enable() { + log.debug("Enable step motor."); + this.stepMotorStatus.setEnabled(true); + log.debug("MotorStatus {}", JSONUtil.toJsonStr(this.stepMotorStatus)); + } + + @Override + public void disable() { + log.debug("Disable step motor."); + this.stepMotorStatus.setEnabled(false); + log.debug("MotorStatus {}", JSONUtil.toJsonStr(this.stepMotorStatus)); + } + + @Override + public void moveForward(RotationDirection direction, Integer distance, DistanceUnit unit) throws HardwareErrorException, CommandExecTimeoutException, IOException, InterruptedException { + + } + + @Override + public void moveBackward(RotationDirection direction, Integer distance, DistanceUnit unit) throws HardwareErrorException, CommandExecTimeoutException, IOException, InterruptedException { + + } + + @Override + public void stop() throws HardwareErrorException, CommandExecTimeoutException, IOException, InterruptedException { + log.debug("Stop step motor."); + this.stepMotorStatus.setStopped(true); + log.debug("MotorStatus {}", JSONUtil.toJsonStr(this.stepMotorStatus)); + } + + @Override + public void rotateForward() { + + } + + @Override + public void rotateBackward() { + + } + + @Override + public Map readIOState() { + Map ioState = new HashMap<>(); + ioState.put("IO1", true); + ioState.put("IO2", false); + return ioState; + } + + @Override + public Integer readPosition() { + return this.stepMotorStatus.getCurrentPosition(); + } + + @Override + public Integer readEncoderPosition() { + return this.stepMotorStatus.getEncoderPosition(); + } + + @Override + public void setReg(RegIndex regIndex, Integer value) throws HardwareErrorException, CommandExecTimeoutException, IOException, InterruptedException { + this.registers.put(regIndex, value); + } + + @Override + public Integer readReg(RegIndex regIndex) throws HardwareErrorException, CommandExecTimeoutException, IOException, InterruptedException { + return this.registers.get(regIndex); + } + + @Override + public void setMRes(Integer value) throws HardwareErrorException, CommandExecTimeoutException, IOException, InterruptedException { + setReg(RegIndex.kreg_step_motor_mres, value); + } + + @Override + public void setIRun(Integer value) throws HardwareErrorException, CommandExecTimeoutException, IOException, InterruptedException { + setReg(RegIndex.kreg_step_motor_irun, value); + } + + @Override + public void setIHold(Integer value) throws HardwareErrorException, CommandExecTimeoutException, IOException, InterruptedException { + setReg(RegIndex.kreg_step_motor_ihold, value); + } + + @Override + public void setStartAndStopVelocity(Integer value) throws HardwareErrorException, CommandExecTimeoutException, IOException, InterruptedException { + setReg(RegIndex.kreg_step_motor_vstart, value); + setReg(RegIndex.kreg_step_motor_vstop, value); + } + + @Override + public void setV1(Integer value) throws HardwareErrorException, CommandExecTimeoutException, IOException, InterruptedException { + setReg(RegIndex.kreg_step_motor_v1, value); + } + + @Override + public void setA1AndD1(Integer value) throws HardwareErrorException, CommandExecTimeoutException, IOException, InterruptedException { + setReg(RegIndex.kreg_step_motor_a1, value); + setReg(RegIndex.kreg_step_motor_d1, value); + } + + @Override + public void setAmaxAndDmax(Integer value) throws HardwareErrorException, CommandExecTimeoutException, IOException, InterruptedException { + setReg(RegIndex.kreg_step_motor_amax, value); + setReg(RegIndex.kreg_step_motor_dmax, value); + } + + @Override + public void setDefaultVelocity(Integer value) throws HardwareErrorException, CommandExecTimeoutException, IOException, InterruptedException { + setReg(RegIndex.kreg_step_motor_default_velocity, value); + } + + @Override + public void setVelocity(Integer low, Integer mid, Integer high) throws HardwareErrorException, CommandExecTimeoutException, IOException, InterruptedException { + setReg(RegIndex.kreg_step_motor_low_velocity, low); + setReg(RegIndex.kreg_step_motor_mid_velocity, mid); + setReg(RegIndex.kreg_step_motor_high_velocity, high); + } + + @Override + public void setOneCyclePulse(Integer pause, Integer denominator) throws HardwareErrorException, CommandExecTimeoutException, IOException, InterruptedException { + setReg(RegIndex.kreg_step_motor_one_circle_pulse, pause); + setReg(RegIndex.kreg_step_motor_one_circle_pulse_denominator, denominator); + } + + @Override + public void setDZero(Integer value) throws HardwareErrorException, CommandExecTimeoutException, IOException, InterruptedException { + setReg(RegIndex.kreg_step_motor_dzero_pos, value); + } +} diff --git a/src/main/java/com/iflytop/gd/infrastructure/devices/virtual/VirtualSwitchSensor.java b/src/main/java/com/iflytop/gd/infrastructure/devices/virtual/VirtualSwitchSensor.java new file mode 100644 index 0000000..bf0886b --- /dev/null +++ b/src/main/java/com/iflytop/gd/infrastructure/devices/virtual/VirtualSwitchSensor.java @@ -0,0 +1,13 @@ +package com.iflytop.gd.infrastructure.devices.virtual; + +import com.iflytop.gd.system.devices.SwitchSensor; + +/** + * 虚拟传感器 + */ +public class VirtualSwitchSensor implements SwitchSensor { + @Override + public boolean isOpen() { + return false; + } +} diff --git a/src/main/java/com/iflytop/gd/infrastructure/devices/virtual/VirtualTransportationArm.java b/src/main/java/com/iflytop/gd/infrastructure/devices/virtual/VirtualTransportationArm.java new file mode 100644 index 0000000..8013f63 --- /dev/null +++ b/src/main/java/com/iflytop/gd/infrastructure/devices/virtual/VirtualTransportationArm.java @@ -0,0 +1,89 @@ +package com.iflytop.gd.infrastructure.devices.virtual; + +import com.iflytop.gd.common.exception.AppException; +import com.iflytop.gd.common.result.ResultCode; +import com.iflytop.gd.infrastructure.drivers.ModuleId; +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.devices.StepMotor; +import com.iflytop.gd.system.devices.TransportationArm; +import com.iflytop.gd.system.exceptions.CommandExecTimeoutException; +import com.iflytop.gd.system.exceptions.HardwareErrorException; +import org.springframework.context.annotation.Profile; +import org.springframework.stereotype.Component; + +import java.io.IOException; +import java.util.HashMap; +import java.util.Map; + +/** + * 虚拟转移机械臂 + */ +public class VirtualTransportationArm implements TransportationArm { + private final StepMotor xDimMotor; + private final StepMotor yDimMotor; + private final StepMotor zDimMotor; + private final Map stepMotors = new HashMap(); + + public VirtualTransportationArm() { + this.xDimMotor = new VirtualStepMotor(ModuleId.HBotXM); + this.yDimMotor = new VirtualStepMotor(ModuleId.HBotYM); + this.zDimMotor = new VirtualStepMotor(ModuleId.HBotZM); + stepMotors.put(Dim.X, xDimMotor); + stepMotors.put(Dim.Y, yDimMotor); + stepMotors.put(Dim.Z, zDimMotor); + } + + @Override + public void moveTo(Dim dim, Integer distance, DistanceUnit unit) { + try { + stepMotors.get(dim).easyMoveTo(distance, unit); + } catch (HardwareErrorException e) { + throw new AppException(ResultCode.HARDWARE_ERROR); + } catch (CommandExecTimeoutException e) { + throw new AppException(ResultCode.COMMAND_EXEC_TIMEOUT); + } catch (IOException | InterruptedException e) { + throw new AppException(ResultCode.SYSTEM_ERROR); + } + } + + @Override + public void relativelyMove(Dim dim, Integer distance, DistanceUnit unit) { + try { + stepMotors.get(dim).easyMoveBy(distance, unit); + } catch (HardwareErrorException e) { + throw new AppException(ResultCode.HARDWARE_ERROR); + } catch (CommandExecTimeoutException e) { + throw new AppException(ResultCode.COMMAND_EXEC_TIMEOUT); + } catch (IOException | InterruptedException e) { + throw new AppException(ResultCode.SYSTEM_ERROR); + } + } + + @Override + public void stop(Dim dim) { + try { + stepMotors.get(dim).stop(); + } catch (HardwareErrorException e) { + throw new AppException(ResultCode.HARDWARE_ERROR); + } catch (CommandExecTimeoutException e) { + throw new AppException(ResultCode.COMMAND_EXEC_TIMEOUT); + } catch (IOException | InterruptedException e) { + throw new AppException(ResultCode.SYSTEM_ERROR); + } + } + + @Override + public void setSpeed(Dim dim, Integer speed, SpeedUnit unit) { + try { + stepMotors.get(dim).setDefaultVelocity(unit.toMM_PER_SEC(speed)); + } catch (HardwareErrorException e) { + throw new AppException(ResultCode.HARDWARE_ERROR); + } catch (CommandExecTimeoutException e) { + throw new AppException(ResultCode.COMMAND_EXEC_TIMEOUT); + } catch (IOException | InterruptedException e) { + throw new AppException(ResultCode.SYSTEM_ERROR); + } + } +} diff --git a/src/main/java/com/iflytop/gd/infrastructure/repository/DeviceFactory.java b/src/main/java/com/iflytop/gd/infrastructure/repository/DeviceFactory.java new file mode 100644 index 0000000..fed68bd --- /dev/null +++ b/src/main/java/com/iflytop/gd/infrastructure/repository/DeviceFactory.java @@ -0,0 +1,27 @@ +package com.iflytop.gd.infrastructure.repository; + +import com.iflytop.gd.infrastructure.devices.physical.PhysicalStepMotor; +import com.iflytop.gd.infrastructure.devices.virtual.VirtualStepMotor; +import com.iflytop.gd.infrastructure.drivers.ModuleId; +import com.iflytop.gd.system.devices.StepMotor; +import com.iflytop.gd.system.drivers.CommandBus; +import com.iflytop.gd.system.constants.SystemMode; +import lombok.RequiredArgsConstructor; +import org.springframework.stereotype.Component; + +@RequiredArgsConstructor +@Component +public class DeviceFactory { + private final CommandBus commandBus; + public StepMotor createStepMotor(SystemMode systemMode, ModuleId moduleId) { + if (SystemMode.VIRTUAL.equals(systemMode)) { + return new VirtualStepMotor(moduleId); + } + + if (SystemMode.PHYSICAL.equals(systemMode)) { + return new PhysicalStepMotor(moduleId, commandBus); + } + + throw new IllegalArgumentException("Unsupported system mode: " + systemMode); + } +} diff --git a/src/main/java/com/iflytop/gd/system/constants/SystemMode.java b/src/main/java/com/iflytop/gd/system/constants/SystemMode.java new file mode 100644 index 0000000..c660d67 --- /dev/null +++ b/src/main/java/com/iflytop/gd/system/constants/SystemMode.java @@ -0,0 +1,8 @@ +package com.iflytop.gd.system.constants; + +/** + * 系统运行模式 + */ +public enum SystemMode { + VIRTUAL, PHYSICAL +} diff --git a/src/main/java/com/iflytop/gd/system/devices/Sensor.java b/src/main/java/com/iflytop/gd/system/devices/Sensor.java deleted file mode 100644 index a84cbfd..0000000 --- a/src/main/java/com/iflytop/gd/system/devices/Sensor.java +++ /dev/null @@ -1,4 +0,0 @@ -package com.iflytop.gd.system.devices; - -public interface Sensor { -} diff --git a/src/main/java/com/iflytop/gd/system/devices/SwitchSensor.java b/src/main/java/com/iflytop/gd/system/devices/SwitchSensor.java new file mode 100644 index 0000000..702ee25 --- /dev/null +++ b/src/main/java/com/iflytop/gd/system/devices/SwitchSensor.java @@ -0,0 +1,5 @@ +package com.iflytop.gd.system.devices; + +public interface SwitchSensor { + boolean isOpen(); +}