48 lines
3.4 KiB
JavaScript
48 lines
3.4 KiB
JavaScript
|
|
// Read-only stationary gate. The exact passing pair is exported by the loop.
|
|||
|
|
function handler(params) {
|
|||
|
|
function finite(value) { return typeof value === 'number' && isFinite(value); }
|
|||
|
|
function fail(message) { throw new Error(message); }
|
|||
|
|
var now = Date.now(), deadline = params.deadlineMs;
|
|||
|
|
if (!finite(deadline) || deadline <= now || deadline > now + 120000)
|
|||
|
|
fail('等待机械臂连续停稳超时(120秒),未发送后续动作');
|
|||
|
|
if (!finite(params.index) || params.index < 0 || params.index !== Math.floor(params.index))
|
|||
|
|
fail('停稳复核循环次数无效');
|
|||
|
|
function snapshot(s) {
|
|||
|
|
if (!s || s.connected !== true || s.poweredOn !== true || s.brakeReleased !== true ||
|
|||
|
|
s.protectiveStopped !== false || s.emergencyStopped !== false || s.fault !== false ||
|
|||
|
|
s.safetyMode !== 'ARM_SAFETY_MODE_NORMAL' || s.controlMode !== 'ARM_CONTROL_MODE_POSITION')
|
|||
|
|
fail('停稳复核发现连接、上电、制动或安全状态异常');
|
|||
|
|
if (!finite(s.timestamp) || Math.abs(now / 1000 - s.timestamp) > 10) fail('停稳复核状态已过期');
|
|||
|
|
if (typeof s.moving !== 'boolean' || typeof s.programRunning !== 'boolean' ||
|
|||
|
|
typeof s.robotMode !== 'string' || !s.robotMode || s.robotMode === 'ARM_ROBOT_MODE_UNKNOWN')
|
|||
|
|
fail('停稳复核缺少有效运动状态');
|
|||
|
|
var q = s.actualJointState, p = s.actualTcpPose, velocity = s.actualTcpVelocity;
|
|||
|
|
if (!q || !Array.isArray(q.position) || q.position.length !== 6 || !Array.isArray(q.velocity) || q.velocity.length !== 6 || !p || !velocity)
|
|||
|
|
fail('停稳复核缺少实际关节、TCP位姿或速度');
|
|||
|
|
var stopped = s.moving === false && s.programRunning === false && s.robotMode === 'ARM_ROBOT_MODE_IDLE';
|
|||
|
|
var axes = ['x','y','z','rx','ry','rz'], speeds = ['vx','vy','vz','wx','wy','wz'];
|
|||
|
|
for (var i = 0; i < 6; i++) {
|
|||
|
|
if (!finite(q.position[i]) || !finite(q.velocity[i]) || !finite(p[axes[i]]) || !finite(velocity[speeds[i]]))
|
|||
|
|
fail('停稳复核关节或TCP数值无效');
|
|||
|
|
if (Math.abs(q.velocity[i]) > 0.001 || Math.abs(velocity[speeds[i]]) > 0.0001) stopped = false;
|
|||
|
|
}
|
|||
|
|
return stopped;
|
|||
|
|
}
|
|||
|
|
function quaternion(p) {
|
|||
|
|
var x=p.rx/2,y=p.ry/2,z=p.rz/2,cx=Math.cos(x),sx=Math.sin(x),cy=Math.cos(y),sy=Math.sin(y),cz=Math.cos(z),sz=Math.sin(z);
|
|||
|
|
return [sx*cy*cz-cx*sy*sz,cx*sy*cz+sx*cy*sz,cx*cy*sz-sx*sy*cz,cx*cy*cz+sx*sy*sz];
|
|||
|
|
}
|
|||
|
|
function angle(a,b) {
|
|||
|
|
var qa=quaternion(a),qb=quaternion(b),minus=0,plus=0;
|
|||
|
|
for(var i=0;i<4;i++){minus+=(qa[i]-qb[i])*(qa[i]-qb[i]);plus+=(qa[i]+qb[i])*(qa[i]+qb[i]);}
|
|||
|
|
return 4*Math.asin(Math.min(1,Math.sqrt(Math.min(minus,plus))/2));
|
|||
|
|
}
|
|||
|
|
var first=params.previousState,second=params.state,firstStopped=snapshot(first),secondStopped=snapshot(second);
|
|||
|
|
var dt=second.timestamp-first.timestamp,ready=firstStopped&&secondStopped&&dt>=0.2&&dt<=2;
|
|||
|
|
var a=first.actualTcpPose,b=second.actualTcpPose,dx=b.x-a.x,dy=b.y-a.y,dz=b.z-a.z;
|
|||
|
|
if(Math.sqrt(dx*dx+dy*dy+dz*dz)>0.00002||angle(a,b)>0.0001)ready=false;
|
|||
|
|
for(var i=0;i<6;i++)if(!(dt>0)||Math.abs(second.actualJointState.position[i]-first.actualJointState.position[i])/dt>0.001)ready=false;
|
|||
|
|
if(!ready&¶ms.index>=479)fail('等待机械臂连续停稳超时(120秒),未发送后续动作');
|
|||
|
|
return {ready:ready,reason:ready?'同一对回读已通过连续停稳':'尚未满足连续停稳,继续只读等待',previousState:first,state:second,deadlineMs:deadline,observedAt:now};
|
|||
|
|
}
|