Browse Source

feat:增加设备接口定义;修复系统无法启动的问题

tags/freeze
黄翔 3 months ago
parent
commit
6850877c0a
  1. 4
      src/main/java/com/iflytop/gd/debug/services/cmds/DoorCloseCommandHandler.java
  2. 5
      src/main/java/com/iflytop/gd/debug/services/cmds/DoorOpenCommandHandler.java
  3. 32
      src/main/java/com/iflytop/gd/infrastructure/devices/MotorDrivenDoor.java
  4. 21
      src/main/java/com/iflytop/gd/infrastructure/devices/MotorDrivenLiquidFillingArm.java
  5. 23
      src/main/java/com/iflytop/gd/infrastructure/devices/MotorDrivenTransportationArm.java
  6. 3
      src/main/java/com/iflytop/gd/infrastructure/devices/StandardStepMotor.java
  7. 35
      src/main/java/com/iflytop/gd/infrastructure/drivers/WebSocketCommandBusImpl.java
  8. 5
      src/main/java/com/iflytop/gd/system/constants/Dim.java
  9. 5
      src/main/java/com/iflytop/gd/system/constants/LiquidFillArmMotorIndex.java
  10. 11
      src/main/java/com/iflytop/gd/system/devices/BinaryDevice.java
  11. 47
      src/main/java/com/iflytop/gd/system/devices/ColdTray.java
  12. 7
      src/main/java/com/iflytop/gd/system/devices/Door.java
  13. 9
      src/main/java/com/iflytop/gd/system/devices/DoorStepMotor.java
  14. 14
      src/main/java/com/iflytop/gd/system/devices/Fan.java
  15. 13
      src/main/java/com/iflytop/gd/system/devices/Heater.java
  16. 43
      src/main/java/com/iflytop/gd/system/devices/LiquidFillingArm.java
  17. 12
      src/main/java/com/iflytop/gd/system/devices/Relay.java
  18. 4
      src/main/java/com/iflytop/gd/system/devices/Sensor.java
  19. 52
      src/main/java/com/iflytop/gd/system/devices/TransportationArm.java
  20. 23
      src/main/java/com/iflytop/gd/system/models/Point3D.java
  21. 3
      src/main/resources/application.yml

4
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();
}
}

5
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();
}
}

32
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() {
}
}

21
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);
}
}

23
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);
}
}

3
src/main/java/com/iflytop/gd/system/devices/StandardStepMotor.java → 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;

35
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;

5
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
}

5
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
}

11
src/main/java/com/iflytop/gd/system/devices/BinaryDevice.java

@ -1,11 +0,0 @@
package com.iflytop.gd.system.devices;
/**
* 开关类型设备驱动定义
*/
public interface BinaryDevice {
void open();
void close();
boolean isOpen();
boolean isClosed();
}

47
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();
}

7
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();
}

9
src/main/java/com/iflytop/gd/system/devices/DoorStepMotor.java

@ -1,9 +0,0 @@
package com.iflytop.gd.system.devices;
/**
* 步进电机
*/
public class DoorStepMotor {
}

14
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();
}

13
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();
}

43
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);
}

12
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();
}

4
src/main/java/com/iflytop/gd/system/devices/Sensor.java

@ -0,0 +1,4 @@
package com.iflytop.gd.system.devices;
public interface Sensor {
}

52
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);
}

23
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;
}
}

3
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
Loading…
Cancel
Save