63 lines
4.6 KiB
Java
63 lines
4.6 KiB
Java
|
|
package com.cmvr.arm;
|
|||
|
|
import com.alibaba.fastjson2.*;
|
|||
|
|
import com.cmvr.common.exception.GlobalException;
|
|||
|
|
import java.nio.charset.StandardCharsets;
|
|||
|
|
import java.util.*;
|
|||
|
|
public final class ArmTeachingParameters {
|
|||
|
|
private ArmTeachingParameters(){}
|
|||
|
|
public static void validate(ArmTeachingOperation op,ArmTeachingRequest p) {
|
|||
|
|
if(op==null||p==null)throw new GlobalException("机械臂操作参数不能为空");
|
|||
|
|
if(p.getRobotId()==null||p.getRobotId().isBlank()||p.getDeviceId()==null||p.getDeviceId().isBlank())throw new GlobalException("请选择机器人和机械臂设备");
|
|||
|
|
timeout(op,p.getTimeoutMs());
|
|||
|
|
if(op==ArmTeachingOperation.START_TRAJECTORY_RECORDING||op==ArmTeachingOperation.MOVE_TO_TRAJECTORY_START||op==ArmTeachingOperation.REPLAY_RECORDED_TRAJECTORY)file(p.getFileName());
|
|||
|
|
if(op==ArmTeachingOperation.START_TRAJECTORY_RECORDING)range(p.getSamplingFrequencyHz(),1,100,"samplingFrequencyHz(Hz)",true);
|
|||
|
|
if(op==ArmTeachingOperation.MOVE_TO_TRAJECTORY_START||op==ArmTeachingOperation.REPLAY_RECORDED_TRAJECTORY) {
|
|||
|
|
range(p.getSpeedRatio(),0.1,1,"speedRatio",true);
|
|||
|
|
for(Double number:List.of(value(p.getMaxVelocity()),value(p.getMaxAcceleration()),value(p.getMaxJerk())))
|
|||
|
|
if(!Double.isFinite(number)||number<0)throw new GlobalException("速度、加速度和加加速度限值必须为有限非负数;0使用服务端默认值");
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
private static double value(Double value){return value==null?0:value;}
|
|||
|
|
public static long timeout(ArmTeachingOperation op,Long timeout) {
|
|||
|
|
long result=timeout==null?op.defaultTimeout():timeout;
|
|||
|
|
if(result<100||result>21600000)throw new GlobalException("timeoutMs 必须为100至21600000的整数毫秒,回放超时需覆盖整条轨迹");
|
|||
|
|
return result;
|
|||
|
|
}
|
|||
|
|
public static String file(String name) {
|
|||
|
|
if(name==null||name.isBlank()||!name.equals(name.trim())||name.startsWith(".")||name.contains("..")
|
|||
|
|
||name.getBytes(StandardCharsets.UTF_8).length>120||name.chars().anyMatch(c->c<32||c==127||"/\\:*?\"<>|".indexOf(c)>=0))
|
|||
|
|
throw new GlobalException("轨迹文件名须为不超过120个UTF-8字节的文件名,不能包含路径、..或保留字符");
|
|||
|
|
return name;
|
|||
|
|
}
|
|||
|
|
private static void range(Double value,double min,double max,String label,boolean allowZero) {
|
|||
|
|
if(value==null)return;
|
|||
|
|
if(!Double.isFinite(value)||!(allowZero&&value==0)&&!(value>=min&&value<=max))throw new GlobalException(label+"必须为"+min+"至"+max+",0使用默认值");
|
|||
|
|
}
|
|||
|
|
public static ArmTeachingRequest fromInputs(ArmTeachingOperation op,JSONObject input) {
|
|||
|
|
if(input==null)throw new GlobalException("机械臂参数必须为对象");
|
|||
|
|
if(input.containsKey("fileName")&&!(input.get("fileName") instanceof String))throw new GlobalException("fileName必须为文件名字符串");
|
|||
|
|
Set<String> numeric=Set.of("samplingFrequencyHz","speedRatio","maxVelocity","maxAcceleration","maxJerk","timeoutMs");
|
|||
|
|
for(String name:numeric)if(input.containsKey(name)) {
|
|||
|
|
Object value=input.get(name);
|
|||
|
|
if(!(value instanceof Number n)||!Double.isFinite(n.doubleValue())||name.equals("timeoutMs")&&n.doubleValue()!=Math.rint(n.doubleValue()))throw new GlobalException(name+"必须为有效数值,超时为整数毫秒");
|
|||
|
|
}
|
|||
|
|
var request=input.to(ArmTeachingRequest.class);validate(op,request);return request;
|
|||
|
|
}
|
|||
|
|
public static void validateLiterals(ArmTeachingOperation op,JSONArray params) {
|
|||
|
|
JSONObject literals=JSONObject.of("robotId","validation","deviceId","validation");
|
|||
|
|
Set<String> names=new HashSet<>(),dynamic=new HashSet<>();
|
|||
|
|
if(params!=null)for(int i=0;i<params.size();i++) {
|
|||
|
|
var param=params.getJSONObject(i);String name=param.getString("name");
|
|||
|
|
if(name==null||!names.add(name))throw new GlobalException("机械臂参数名称为空或重复");
|
|||
|
|
if("input".equals(param.getString("type")))literals.put(name,param.get("input"));
|
|||
|
|
else if("quote".equals(param.getString("type"))) {
|
|||
|
|
var path=param.getJSONArray("quote");
|
|||
|
|
if(path==null||path.size()<3||path.stream().anyMatch(v->!(v instanceof String s)||s.isBlank())||!Set.of("input","output").contains(param.getString("quoteType")))throw new GlobalException(name+"的引用不完整");
|
|||
|
|
dynamic.add(name);
|
|||
|
|
}else throw new GlobalException(name+"的参数来源无效");
|
|||
|
|
}
|
|||
|
|
if(dynamic.contains("fileName"))literals.put("fileName","runtime.csv");
|
|||
|
|
fromInputs(op,literals);
|
|||
|
|
}
|
|||
|
|
}
|