4 changed files with 197 additions and 85 deletions
-
7src/main/java/com/qyft/ms/app/controller/TestController.java
-
199src/main/java/com/qyft/ms/app/device/spray/SprayTaskExecutor.java
-
69src/main/java/com/qyft/ms/app/service/TestService.java
-
7src/main/resources/application.yml
@ -1,13 +1,82 @@ |
|||
package com.qyft.ms.app.service; |
|||
|
|||
import cn.hutool.json.JSONObject; |
|||
import com.qyft.ms.system.common.device.command.CommandFuture; |
|||
import com.qyft.ms.system.common.device.command.DeviceCommandGenerator; |
|||
import com.qyft.ms.system.model.bo.DeviceCommand; |
|||
import com.qyft.ms.system.service.device.DeviceCommandService; |
|||
import lombok.RequiredArgsConstructor; |
|||
import org.springframework.stereotype.Service; |
|||
|
|||
import java.util.Arrays; |
|||
import java.util.concurrent.CompletableFuture; |
|||
import java.util.concurrent.TimeUnit; |
|||
|
|||
/** |
|||
* 测试用 |
|||
*/ |
|||
@Service |
|||
@RequiredArgsConstructor |
|||
public class TestService { |
|||
private final DeviceCommandService deviceCommandService; |
|||
|
|||
/** |
|||
* 测试指定轴从当前点移动到目标点所耗时,并计算预计时间(毫秒级)。 |
|||
* |
|||
* @param axis 指定轴:x、y 或 z(不区分大小写) |
|||
* @param target 目标位置 |
|||
* @param speed 速度(单位与设备协议保持一致,单位:位置/秒) |
|||
* @return 格式化的结果字符串,例如 "实际用时:123ms,预计用时:1200ms" |
|||
* @throws Exception 设备通信异常或非法轴参数 |
|||
*/ |
|||
public String testSpeed(String axis, Double target, Double speed) throws Exception { |
|||
// 1. 获取当前三轴位置 |
|||
DeviceCommand getPosCmd = DeviceCommandGenerator.motorXyzPositionGet(); |
|||
CommandFuture getPosFuture = deviceCommandService.sendCommandNoFront(getPosCmd); |
|||
commandWait(getPosFuture); |
|||
JSONObject data = getPosFuture.getResponseResult().getJSONObject("data"); |
|||
|
|||
// 2. 根据 axis 获取当前轴位置 |
|||
double current = switch (axis.toLowerCase()) { |
|||
case "x" -> data.getDouble("xAxisPosition"); |
|||
case "y" -> data.getDouble("yAxisPosition"); |
|||
case "z" -> data.getDouble("zAxisPosition"); |
|||
default -> throw new IllegalArgumentException("Unknown axis: " + axis); |
|||
}; |
|||
|
|||
// 3. 构造对应轴的移动指令 |
|||
DeviceCommand moveCmd = switch (axis.toLowerCase()) { |
|||
case "x" -> DeviceCommandGenerator.motorXPositionSet(target, speed); |
|||
case "y" -> DeviceCommandGenerator.motorYPositionSet(target, speed); |
|||
case "z" -> DeviceCommandGenerator.motorZPositionSet(target, speed); |
|||
default -> |
|||
// 该分支在上面已处理,理论上不会到这里 |
|||
throw new IllegalArgumentException("Unknown axis: " + axis); |
|||
}; |
|||
|
|||
// 4. 发送移动指令并计时(毫秒级) |
|||
long startMs = System.currentTimeMillis(); |
|||
CommandFuture moveFuture = deviceCommandService.sendCommandNoFront(moveCmd); |
|||
long cmdMs = System.currentTimeMillis(); |
|||
commandWait(moveFuture); |
|||
long endMs = System.currentTimeMillis(); |
|||
|
|||
// 5. 计算实际用时与预计用时(毫秒) |
|||
long actualMs = endMs - startMs; |
|||
long cmdActualMs = cmdMs - startMs; |
|||
long estimatedMs = (long) (Math.abs(current - target) / speed * 1000); |
|||
long backMs = endMs - cmdMs - estimatedMs; |
|||
|
|||
// 6. 返回结果 |
|||
return String.format("实际:%dms,预计:%dms,通信:%dms,包装:%dms", actualMs, estimatedMs, cmdActualMs, backMs); |
|||
} |
|||
|
|||
|
|||
private void commandWait(CommandFuture... futures) throws Exception { |
|||
CompletableFuture<?>[] responseFutures = Arrays.stream(futures) |
|||
.map(CommandFuture::getResponseFuture) |
|||
.toArray(CompletableFuture[]::new); |
|||
CompletableFuture.allOf(responseFutures) |
|||
.get(120, TimeUnit.SECONDS); |
|||
} |
|||
} |
Write
Preview
Loading…
Cancel
Save
Reference in new issue