|
|
@ -21,6 +21,10 @@ import org.mgc.utils.StringHelper; |
|
|
|
|
|
|
|
import java.io.IOException; |
|
|
|
import java.security.InvalidParameterException; |
|
|
|
import java.util.ArrayDeque; |
|
|
|
import java.util.ArrayList; |
|
|
|
import java.util.LinkedList; |
|
|
|
import java.util.Queue; |
|
|
|
import java.util.concurrent.ExecutionException; |
|
|
|
|
|
|
|
@Slf4j |
|
|
@ -120,15 +124,18 @@ public class MotorDevice extends BaseCommandHandler { |
|
|
|
if (motorDirect == null || motorDirect == MotorDirect.MotorBackward) { |
|
|
|
throw new InvalidParameterException("Motor [Direction]"); |
|
|
|
} |
|
|
|
int timeout = param.has("timeout") ? param.get("timeout").asInt() : MOVE_TIMEOUT_INTERVAL; |
|
|
|
double absPosition = Math.abs(position); |
|
|
|
position = motorDirect == MotorDirect.MotorForward ? absPosition : -absPosition; |
|
|
|
res = moveMotorToPosition(cmdId, axis_, position); |
|
|
|
res = moveMotorToPosition(cmdId, axis_, position, timeout); |
|
|
|
log.info("[Motor {}] move To Position {}", axis_.getAxisValue(), position); |
|
|
|
} |
|
|
|
break; |
|
|
|
} |
|
|
|
case MotorActionOrigin: { |
|
|
|
res = motorMoveToHome(cmdId, axis_); |
|
|
|
JsonNode param = command.getParam(); |
|
|
|
int timeout = param != null && param.has("timeout") ? param.get("timeout").asInt() : HOME_TIMEOUT_INTERVAL; |
|
|
|
res = motorMoveToHome(cmdId, axis_, timeout); |
|
|
|
log.info("[Motor {}] Move to Home {}", axis_.getAxisValue(), axis_); |
|
|
|
break; |
|
|
|
} |
|
|
@ -149,16 +156,16 @@ public class MotorDevice extends BaseCommandHandler { |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
public int motorMoveToHome(int cmdId, MotorAxis axis) { |
|
|
|
public int motorMoveToHome(int cmdId, MotorAxis axis, int timeout) { |
|
|
|
int res = -1; |
|
|
|
SerialCommand motorMoveHomeCommand = DeviceCommandGenerator.ctrlWaitCmdControl(cmdId, getMotorSlavedId(), |
|
|
|
MOVE_HOME_ADDRESS, true, getMoveHomeFinishedEvent()); |
|
|
|
SerialCommand motorMoveHomeCommand = DeviceCommandGenerator.ctrlWaitCmdControl(cmdId, getMotorSlavedId(axis), |
|
|
|
MOVE_HOME_ADDRESS, true, getMoveHomeFinishedEvent(axis)); |
|
|
|
try { |
|
|
|
// 发送 485 指令 |
|
|
|
DeviceCommandManager deviceCommandManager = SubSprayAppSingleton.getInstance().getDeviceCommandManager();; |
|
|
|
CommandFuture motorMoveHomeFuture = deviceCommandManager.sendCommand(motorMoveHomeCommand); |
|
|
|
// 等待 30 s |
|
|
|
commandWait(HOME_TIMEOUT_INTERVAL, motorMoveHomeFuture); |
|
|
|
commandWait(timeout, motorMoveHomeFuture); |
|
|
|
|
|
|
|
// 485 指令 转 data |
|
|
|
SerialResult result = motorMoveHomeFuture.getAckFuture().get(); |
|
|
@ -178,7 +185,7 @@ public class MotorDevice extends BaseCommandHandler { |
|
|
|
// TODO Open Device |
|
|
|
// 拼 485 指令 |
|
|
|
int res = 0; |
|
|
|
SerialCommand ctrlCommand = DeviceCommandGenerator.ctrlSlaveSingleRegCmdControl(cmdId, getMotorSlavedId(), I_RUN_ADDRESS, |
|
|
|
SerialCommand ctrlCommand = DeviceCommandGenerator.ctrlSlaveSingleRegCmdControl(cmdId, getMotorSlavedId(axis), I_RUN_ADDRESS, |
|
|
|
current); |
|
|
|
try { |
|
|
|
// 发送 485 指令 |
|
|
@ -201,7 +208,7 @@ public class MotorDevice extends BaseCommandHandler { |
|
|
|
return res; |
|
|
|
} |
|
|
|
|
|
|
|
public int moveMotorToPosition(int cmdId, MotorAxis axis, double position) { |
|
|
|
public int moveMotorToPosition(int cmdId, MotorAxis axis, double position, int timeout) { |
|
|
|
// 拼 485 指令 |
|
|
|
double pos_limit = MotorAxis.getPosLimit(axis); |
|
|
|
if(Math.abs(position) > pos_limit) |
|
|
@ -214,14 +221,14 @@ public class MotorDevice extends BaseCommandHandler { |
|
|
|
int res = 0; |
|
|
|
short[] shortArray = IntToShortArray.intToShortArrayBigEndian(motorPos); |
|
|
|
|
|
|
|
SerialCommand ctrlCommand = DeviceCommandGenerator.ctrlWaitMultiRegCmdControl(cmdId, getMotorSlavedId(), V_TARGET_POSITION_ADDRESS, |
|
|
|
shortArray, getMoveFinishedEvent()); |
|
|
|
SerialCommand ctrlCommand = DeviceCommandGenerator.ctrlWaitMultiRegCmdControl(cmdId, getMotorSlavedId(axis), V_TARGET_POSITION_ADDRESS, |
|
|
|
shortArray, getMoveFinishedEvent(axis)); |
|
|
|
try { |
|
|
|
// 发送 485 指令 |
|
|
|
DeviceCommandManager deviceCommandManager = SubSprayAppSingleton.getInstance().getDeviceCommandManager();; |
|
|
|
CommandFuture ctrlCommandFuture = deviceCommandManager.sendCommand(ctrlCommand); |
|
|
|
// 等待 |
|
|
|
commandWait(MOVE_TIMEOUT_INTERVAL, ctrlCommandFuture); |
|
|
|
commandWait(timeout, ctrlCommandFuture); |
|
|
|
|
|
|
|
// 485 指令 转 data |
|
|
|
SerialResult result = ctrlCommandFuture.getAckFuture().get(); |
|
|
@ -259,7 +266,7 @@ public class MotorDevice extends BaseCommandHandler { |
|
|
|
|
|
|
|
// 拼 485 指令 |
|
|
|
int res = 0; |
|
|
|
SerialCommand ctrlCommand = DeviceCommandGenerator.ctrlSlaveMultiRegCmdControl(cmdId, getMotorSlavedId(), V_SPEED_ADDRESS, |
|
|
|
SerialCommand ctrlCommand = DeviceCommandGenerator.ctrlSlaveMultiRegCmdControl(cmdId, getMotorSlavedId(axis), V_SPEED_ADDRESS, |
|
|
|
shortArray); |
|
|
|
try { |
|
|
|
// 发送 485 指令 |
|
|
@ -287,9 +294,9 @@ public class MotorDevice extends BaseCommandHandler { |
|
|
|
return 0; |
|
|
|
} |
|
|
|
|
|
|
|
int getMotorSlavedId() |
|
|
|
int getMotorSlavedId(MotorAxis axis) |
|
|
|
{ |
|
|
|
switch (axis_) |
|
|
|
switch (axis) |
|
|
|
{ |
|
|
|
case X -> { |
|
|
|
return FrameSlaveIdInfo.MotorXSlavaID; |
|
|
@ -304,24 +311,24 @@ public class MotorDevice extends BaseCommandHandler { |
|
|
|
return 0; |
|
|
|
} |
|
|
|
|
|
|
|
BoardEvent getMoveFinishedEvent() { |
|
|
|
switch (axis_) |
|
|
|
BoardEvent getMoveFinishedEvent(MotorAxis axis) { |
|
|
|
switch (axis) |
|
|
|
{ |
|
|
|
case X -> { |
|
|
|
return BoardEvent.MOTOR_X_MOVE_FINISHED; |
|
|
|
} |
|
|
|
case Y -> { |
|
|
|
case X -> { |
|
|
|
return BoardEvent.MOTOR_X_MOVE_FINISHED; |
|
|
|
} |
|
|
|
case Y -> { |
|
|
|
return BoardEvent.MOTOR_Y_MOVE_FINISHED; |
|
|
|
} |
|
|
|
case Z -> { |
|
|
|
} |
|
|
|
case Z -> { |
|
|
|
return BoardEvent.MOTOR_Z_MOVE_FINISHED; |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
return null; |
|
|
|
} |
|
|
|
|
|
|
|
BoardEvent getMoveHomeFinishedEvent() { |
|
|
|
switch (axis_) |
|
|
|
BoardEvent getMoveHomeFinishedEvent(MotorAxis axis) { |
|
|
|
switch (axis) |
|
|
|
{ |
|
|
|
case X -> { |
|
|
|
return BoardEvent.MOTOR_X_HOME_FINISHED; |
|
|
@ -339,7 +346,7 @@ public class MotorDevice extends BaseCommandHandler { |
|
|
|
public int stopMotor(int cmdId, MotorAxis axis) { |
|
|
|
// 拼 485 指令 |
|
|
|
int res = 0; |
|
|
|
SerialCommand ctrlCommand = DeviceCommandGenerator.ctrlSlaveCmdControl(cmdId, getMotorSlavedId(), STOP_ADDRESS, |
|
|
|
SerialCommand ctrlCommand = DeviceCommandGenerator.ctrlSlaveCmdControl(cmdId, getMotorSlavedId(axis), STOP_ADDRESS, |
|
|
|
true); |
|
|
|
try { |
|
|
|
// 发送 485 指令 |
|
|
@ -351,6 +358,10 @@ public class MotorDevice extends BaseCommandHandler { |
|
|
|
// 485 指令 转 data |
|
|
|
SerialResult result = ctrlCommandFuture.getAckFuture().get(); |
|
|
|
res = result.getCode(); |
|
|
|
|
|
|
|
deviceCommandManager.pushEvent(getMoveHomeFinishedEvent(axis)); |
|
|
|
deviceCommandManager.pushEvent(getMoveFinishedEvent(axis)); |
|
|
|
log.info("\nMotor stopped\n"); |
|
|
|
// 返回data |
|
|
|
} catch (IOException e) { |
|
|
|
res = -1; |
|
|
@ -359,6 +370,7 @@ public class MotorDevice extends BaseCommandHandler { |
|
|
|
} catch (InterruptedException e) { |
|
|
|
res = -1; |
|
|
|
} |
|
|
|
|
|
|
|
return res; |
|
|
|
} |
|
|
|
|
|
|
|