Browse Source

fix:TCP硬件通信方法修改,机械臂操作增加队列

master
白凤吉 6 months ago
parent
commit
b06efb85fb
  1. 7
      src/main/java/com/qyft/gd/device/controller/TestController.java
  2. 140
      src/main/java/com/qyft/gd/device/service/DeviceService.java

7
src/main/java/com/qyft/gd/device/controller/TestController.java

@ -40,4 +40,11 @@ public class TestController {
} }
} }
@Operation(summary = "队列测试")
@GetMapping("/test")
public Result<DeviceStatus> test() {
boolean success = deviceService.test();
return Result.success();
}
} }

140
src/main/java/com/qyft/gd/device/service/DeviceService.java

@ -2,13 +2,16 @@ package com.qyft.gd.device.service;
import cn.hutool.json.JSONUtil; import cn.hutool.json.JSONUtil;
import com.qyft.gd.device.client.TcpClient; import com.qyft.gd.device.client.TcpClient;
import com.qyft.gd.device.common.jsonrpc.JsonRpcRequest;
import com.qyft.gd.device.model.bo.DeviceFeedback; import com.qyft.gd.device.model.bo.DeviceFeedback;
import jakarta.annotation.PostConstruct;
import lombok.RequiredArgsConstructor; import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j; import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
import java.util.HashMap; import java.util.HashMap;
import java.util.Map; import java.util.Map;
import java.util.concurrent.*;
@Slf4j @Slf4j
@Service @Service
@ -17,20 +20,72 @@ public class DeviceService {
private final TcpClient tcpClient; private final TcpClient tcpClient;
private final BlockingDeque<Callable<Boolean>> taskQueue = new LinkedBlockingDeque<>();
private final ExecutorService executorService = Executors.newSingleThreadExecutor();
@PostConstruct
private void initExecutorThread() {
new Thread(this::executeTasks).start();
}
private boolean enqueueMoveRailTask(String method) {
JsonRpcRequest request = new JsonRpcRequest();
request.setMethod(method);
return this.enqueueMoveRailTask(request);
}
private boolean enqueueMoveRailTask(String method, Map<String, Object> params) {
JsonRpcRequest request = new JsonRpcRequest();
request.setMethod(method);
request.setParams(params);
return this.enqueueMoveRailTask(request);
}
private boolean enqueueMoveRailTask(JsonRpcRequest request) {
Callable<Boolean> task = () -> {
DeviceFeedback deviceFeedback = tcpClient.sendCommand(request);
if (deviceFeedback == null || deviceFeedback.getError() != null) {
log.error("TCP 指令执行错误 request:{} feedback:{}", JSONUtil.toJsonStr(request), JSONUtil.toJsonStr(deviceFeedback));
return false;
}
Thread.sleep(5000);
return true;
};
try {
taskQueue.put(task);
return executorService.submit(taskQueue.take()).get();
} catch (Exception e) {
log.error("TCP 指令执行错误 request:{}", JSONUtil.toJsonStr(request), e);
}
return false;
}
private void executeTasks() {
while (true) {
try {
Callable<Boolean> task = taskQueue.take();
task.call();
} catch (Exception e) {
Thread.currentThread().interrupt();
}
}
}
public boolean test() {
return this.enqueueMoveRailTask("moveRailArmRail", null);
}
/** /**
* 移动导轨机械臂的导轨 * 移动导轨机械臂的导轨
* *
* @param distance 移动距离 * @param distance 移动距离
*/ */
public synchronized boolean moveRailArmRail(double distance) {
public boolean moveRailArmRail(double distance) {
Map<String, Object> params = new HashMap<>(); Map<String, Object> params = new HashMap<>();
params.put("distance", distance); params.put("distance", distance);
DeviceFeedback deviceFeedback = tcpClient.sendCommand("moveRailArmRail", params);
if (deviceFeedback == null || deviceFeedback.getError() != null) {
log.error("TCP moveRailArmRail 指令执行错误 {}", JSONUtil.toJsonStr(deviceFeedback));
return false;
}
return true;
return this.enqueueMoveRailTask("moveRailArmRail", params);
} }
/** /**
@ -40,17 +95,12 @@ public class DeviceService {
* @param joint2 关节2角度 * @param joint2 关节2角度
* @param distance 移动距离 * @param distance 移动距离
*/ */
public synchronized boolean moveRailArmJoint(double joint1, double joint2, double distance) {
public boolean moveRailArmJoint(double joint1, double joint2, double distance) {
Map<String, Object> params = new HashMap<>(); Map<String, Object> params = new HashMap<>();
params.put("joint1", joint1); params.put("joint1", joint1);
params.put("joint2", joint2); params.put("joint2", joint2);
params.put("distance", distance); params.put("distance", distance);
DeviceFeedback deviceFeedback = tcpClient.sendCommand("moveRailArmJoint", params);
if (deviceFeedback == null || deviceFeedback.getError() != null) {
log.error("TCP moveRailArmJoint 指令执行错误 {}", JSONUtil.toJsonStr(deviceFeedback));
return false;
}
return true;
return this.enqueueMoveRailTask("moveRailArmJoint", params);
} }
/** /**
@ -60,17 +110,12 @@ public class DeviceService {
* @param y 坐标y * @param y 坐标y
* @param z 坐标z * @param z 坐标z
*/ */
public synchronized boolean moveRailArmToPoint(int x, int y, int z) {
public boolean moveRailArmToPoint(int x, int y, int z) {
Map<String, Object> params = new HashMap<>(); Map<String, Object> params = new HashMap<>();
params.put("x", x); params.put("x", x);
params.put("y", y); params.put("y", y);
params.put("z", z); params.put("z", z);
DeviceFeedback deviceFeedback = tcpClient.sendCommand("moveRailArmToPoint", params);
if (deviceFeedback == null || deviceFeedback.getError() != null) {
log.error("TCP moveRailArmToPoint 指令执行错误 {}", JSONUtil.toJsonStr(deviceFeedback));
return false;
}
return true;
return this.enqueueMoveRailTask("moveRailArmToPoint", params);
} }
/** /**
@ -78,27 +123,17 @@ public class DeviceService {
* *
* @param speed 速度值 * @param speed 速度值
*/ */
public synchronized boolean setRailArmSpeed(int speed) {
public boolean setRailArmSpeed(int speed) {
Map<String, Object> params = new HashMap<>(); Map<String, Object> params = new HashMap<>();
params.put("speed", speed); params.put("speed", speed);
DeviceFeedback deviceFeedback = tcpClient.sendCommand("setRailArmSpeed", params);
if (deviceFeedback == null || deviceFeedback.getError() != null) {
log.error("TCP setRailArmSpeed 指令执行错误 {}", JSONUtil.toJsonStr(deviceFeedback));
return false;
}
return true;
return this.enqueueMoveRailTask("setRailArmSpeed", params);
} }
/** /**
* 轨道机械臂移动停止移动 * 轨道机械臂移动停止移动
*/ */
public synchronized boolean stopRailArm() {
DeviceFeedback deviceFeedback = tcpClient.sendCommand("setRailArmSpeed");
if (deviceFeedback == null || deviceFeedback.getError() != null) {
log.error("TCP stopRailArm 指令执行错误 {}", JSONUtil.toJsonStr(deviceFeedback));
return false;
}
return true;
public boolean stopRailArm() {
return this.enqueueMoveRailTask("stopRailArm");
} }
/** /**
@ -111,12 +146,7 @@ public class DeviceService {
Map<String, Object> params = new HashMap<>(); Map<String, Object> params = new HashMap<>();
params.put("joint1", joint1); params.put("joint1", joint1);
params.put("joint2", joint2); params.put("joint2", joint2);
DeviceFeedback deviceFeedback = tcpClient.sendCommand("moveLiquidArmJoint", params);
if (deviceFeedback == null || deviceFeedback.getError() != null) {
log.error("TCP moveLiquidArmJoint 指令执行错误 {}", JSONUtil.toJsonStr(deviceFeedback));
return false;
}
return true;
return this.enqueueMoveRailTask("moveLiquidArmJoint", params);
} }
/** /**
@ -131,12 +161,7 @@ public class DeviceService {
params.put("x", x); params.put("x", x);
params.put("y", y); params.put("y", y);
params.put("z", z); params.put("z", z);
DeviceFeedback deviceFeedback = tcpClient.sendCommand("moveLiquidArmToPoint", params);
if (deviceFeedback == null || deviceFeedback.getError() != null) {
log.error("TCP moveLiquidArmToPoint 指令执行错误 {}", JSONUtil.toJsonStr(deviceFeedback));
return false;
}
return true;
return this.enqueueMoveRailTask("moveLiquidArmToPoint", params);
} }
/** /**
@ -221,12 +246,7 @@ public class DeviceService {
Map<String, Object> params = new HashMap<>(); Map<String, Object> params = new HashMap<>();
params.put("pumpId", pumpId); params.put("pumpId", pumpId);
params.put("volume", volume); params.put("volume", volume);
DeviceFeedback deviceFeedback = tcpClient.sendCommand("addLiquid", params);
if (deviceFeedback == null || deviceFeedback.getError() != null) {
log.error("TCP addLiquid 指令执行错误 {}", JSONUtil.toJsonStr(deviceFeedback));
return false;
}
return true;
return this.enqueueMoveRailTask("addLiquid", params);
} }
/** /**
@ -252,24 +272,14 @@ public class DeviceService {
* 开门 * 开门
*/ */
public synchronized boolean openDoor() { public synchronized boolean openDoor() {
DeviceFeedback deviceFeedback = tcpClient.sendCommand("openDoor");
if (deviceFeedback == null || deviceFeedback.getError() != null) {
log.error("TCP openDoor 指令执行错误 {}", JSONUtil.toJsonStr(deviceFeedback));
return false;
}
return true;
return this.enqueueMoveRailTask("openDoor");
} }
/** /**
* 关门 * 关门
*/ */
public synchronized boolean closeDoor() { public synchronized boolean closeDoor() {
DeviceFeedback deviceFeedback = tcpClient.sendCommand("closeDoor");
if (deviceFeedback == null || deviceFeedback.getError() != null) {
log.error("TCP closeDoor 指令执行错误 {}", JSONUtil.toJsonStr(deviceFeedback));
return false;
}
return true;
return this.enqueueMoveRailTask("openDoor");
} }
/** /**

Loading…
Cancel
Save