From 9ce45c7fe5cabbe222fe63d36dee681072fe7bbc Mon Sep 17 00:00:00 2001 From: zhaohe Date: Mon, 22 Jul 2024 11:30:36 +0800 Subject: [PATCH] update --- .../java/a8k/base_hardware/A8kCanBusService.java | 130 ++++++++++++--------- .../service/hardware/CommonHardwareOpeartion.java | 2 +- 2 files changed, 76 insertions(+), 56 deletions(-) diff --git a/src/main/java/a8k/base_hardware/A8kCanBusService.java b/src/main/java/a8k/base_hardware/A8kCanBusService.java index d6320c2..0ce4a3a 100644 --- a/src/main/java/a8k/base_hardware/A8kCanBusService.java +++ b/src/main/java/a8k/base_hardware/A8kCanBusService.java @@ -3,6 +3,7 @@ package a8k.base_hardware; import a8k.a8k_can_protocol.*; import a8k.a8k_can_protocol.MId; import a8k.appbean.HardwareException; +import a8k.appbean.Pos2d; import a8k.appbean.appevent.A8kHardwareReport; import a8k.service.AppEventBusService; import a8k.utils.ByteArray; @@ -53,21 +54,21 @@ public class A8kCanBusService { @Autowired private AppEventBusService eventBusService; - public String uri; // assign by application.yml + public String uri; // assign by application.yml private WebSocketClient client; // 接收回执上下文 public BlockingQueue receiptQueue = new LinkedBlockingQueue(); // - boolean isWaitingReceipt = false; // - int waitingReceiptIndex = 0;// + boolean isWaitingReceipt = false; // + int waitingReceiptIndex = 0;// // 发送包的packetIndex int packetIndex = 0;// // 调试标志位 - boolean debugFlag = true;// + boolean debugFlag = true;// // websocket自动重连时间 - Timer autoConnectTimer = new Timer();// + Timer autoConnectTimer = new Timer();// @PostConstruct public void init() throws URISyntaxException { @@ -112,8 +113,7 @@ public class A8kCanBusService { client.connect(); } catch (IllegalStateException e) { } - } else if (client.getReadyState().equals(ReadyState.CLOSING) - || client.getReadyState().equals(ReadyState.CLOSED)) { + } else if (client.getReadyState().equals(ReadyState.CLOSING) || client.getReadyState().equals(ReadyState.CLOSED)) { client.reconnect(); } } @@ -122,8 +122,8 @@ public class A8kCanBusService { } private A8kPacket packParamsToPacket(Integer moduleId, Integer cmdId, Integer[] params) { - int subCmdId = cmdId & 0xFF; - int mainCmdId = (cmdId >> 8) & 0xFFFF; + int subCmdId = cmdId & 0xFF; + int mainCmdId = (cmdId >> 8) & 0xFFFF; int bufferSize = 2 + 2 + 1 + 1 + 2 + 4 * params.length; // PacketIndex - MainCmdId - SubCmdId - CmdType - // ModuleId - Parameters ByteBuffer buffer = ByteBuffer.allocate(bufferSize); @@ -140,9 +140,6 @@ public class A8kCanBusService { } - - - // // module opeation // @@ -182,24 +179,54 @@ public class A8kCanBusService { callcmd(id.toInt(), CmdId.kmodule_active_cfg); } -// public static final int kxymotor_enable = 0x0301; -// public static final int kxymotor_move_by = 0x0302; -// public static final int kxymotor_move_to = 0x0303; -// public static final int kxymotor_move_to_zero = 0x0304; -// public static final int kxymotor_read_pos = 0x0306; -// public static final int kxymotor_read_inio_index_in_stm32 = 0x0307; -// public static final int kxymotor_read_inio = 0x0308; -// public static final int kxymotor_set_pos = 0x0309; -// public static final int kxymotor_motor_move_by_direct = 0x030a; -// public static final int kxymotor_read_enc_direct = 0x030b; public void hbotEnable(MId mid, int enable) throws HardwareException { callcmd(mid.toInt(), CmdId.kxymotor_enable, enable); } - + public void hbotMoveBy(MId mid, int x, int y) throws HardwareException { + callcmd(mid.toInt(), CmdId.kxymotor_move_by, x, y); + } + + public void hbotMoveTo(MId mid, int x, int y) throws HardwareException { + callcmd(mid.toInt(), CmdId.kxymotor_move_to, x, y); + } + + public void hbotMoveToZero(MId mid) throws HardwareException { + callcmd(mid.toInt(), CmdId.kxymotor_move_to_zero); + } + + public Pos2d hbotReadPos(MId mid) throws HardwareException { + Pos2d pos = new Pos2d(0, 0); + var packet = callcmd(mid.toInt(), CmdId.kxymotor_read_pos); + pos.x = packet.getContentI32(0); + pos.y = packet.getContentI32(1); + return pos; + } - + public int hbotReadInio(MId mid) throws HardwareException { + var packet = callcmd(mid.toInt(), CmdId.kxymotor_read_inio); + return packet.getContentI32(0); + } + + public void hbotSetPos(MId mid, int x, int y) throws HardwareException { + callcmd(mid.toInt(), CmdId.kxymotor_set_pos, x, y); + } + + + public void hbotMoveByDirect(MId mid, int motor1_dpos, int motor2_dpos) throws HardwareException { + callcmd(mid.toInt(), CmdId.kxymotor_motor_move_by_direct, motor1_dpos, motor2_dpos); + } + + public int hbotReadEncDirect(MId mid) throws HardwareException { + var packet = callcmd(mid.toInt(), CmdId.kxymotor_read_enc_direct); + return packet.getContentI32(0); + } + + public int hbotReadInioIndexInStm32(MId mid, int ioIndex) throws HardwareException { + var packet = callcmd(mid.toInt(), CmdId.kxymotor_read_inio_index_in_stm32, ioIndex); + return packet.getContentI32(0); + } // @@ -344,50 +371,43 @@ public class A8kCanBusService { } public A8kPacket callcmd(Integer moduleId, Integer cmdId) throws HardwareException { - return this.callcmd(moduleId, cmdId, new Integer[] {}); + return this.callcmd(moduleId, cmdId, new Integer[]{}); } public A8kPacket callcmd(Integer moduleId, Integer cmdId, Integer p0) throws HardwareException { - return this.callcmd(moduleId, cmdId, new Integer[] { p0 }); + return this.callcmd(moduleId, cmdId, new Integer[]{p0}); } public A8kPacket callcmd(Integer moduleId, Integer cmdId, Integer p0, Integer p1) throws HardwareException { - return this.callcmd(moduleId, cmdId, new Integer[] { p0, p1 }); + return this.callcmd(moduleId, cmdId, new Integer[]{p0, p1}); } - public A8kPacket callcmd(Integer moduleId, Integer cmdId, Integer p0, Integer p1, Integer p2) - throws HardwareException { - return this.callcmd(moduleId, cmdId, new Integer[] { p0, p1, p2 }); + public A8kPacket callcmd(Integer moduleId, Integer cmdId, Integer p0, Integer p1, Integer p2) throws HardwareException { + return this.callcmd(moduleId, cmdId, new Integer[]{p0, p1, p2}); } - public A8kPacket callcmd(Integer moduleId, Integer cmdId, Integer p0, Integer p1, Integer p2, Integer p3) - throws HardwareException { - return this.callcmd(moduleId, cmdId, new Integer[] { p0, p1, p2, p3 }); + public A8kPacket callcmd(Integer moduleId, Integer cmdId, Integer p0, Integer p1, Integer p2, Integer p3) throws HardwareException { + return this.callcmd(moduleId, cmdId, new Integer[]{p0, p1, p2, p3}); } - public void callblockcmd(Integer moduleId, Integer cmdId, Integer p0, Integer p1, Integer p2, Integer p3, - Integer acitionOvertime) throws HardwareException, InterruptedException { - this.callblockcmd(moduleId, cmdId, new Integer[] { p0, p1, p2, p3 }, acitionOvertime); + public void callblockcmd(Integer moduleId, Integer cmdId, Integer p0, Integer p1, Integer p2, Integer p3, Integer acitionOvertime) throws HardwareException, InterruptedException { + this.callblockcmd(moduleId, cmdId, new Integer[]{p0, p1, p2, p3}, acitionOvertime); } - public void callblockcmd(Integer moduleId, Integer cmdId, Integer p0, Integer p1, Integer p2, - Integer acitionOvertime) throws HardwareException, InterruptedException { - this.callblockcmd(moduleId, cmdId, new Integer[] { p0, p1, p2 }, acitionOvertime); + public void callblockcmd(Integer moduleId, Integer cmdId, Integer p0, Integer p1, Integer p2, Integer acitionOvertime) throws HardwareException, InterruptedException { + this.callblockcmd(moduleId, cmdId, new Integer[]{p0, p1, p2}, acitionOvertime); } - public void callblockcmd(Integer moduleId, Integer cmdId, Integer p0, Integer p1, Integer acitionOvertime) - throws HardwareException, InterruptedException { - this.callblockcmd(moduleId, cmdId, new Integer[] { p0, p1 }, acitionOvertime); + public void callblockcmd(Integer moduleId, Integer cmdId, Integer p0, Integer p1, Integer acitionOvertime) throws HardwareException, InterruptedException { + this.callblockcmd(moduleId, cmdId, new Integer[]{p0, p1}, acitionOvertime); } - public void callblockcmd(Integer moduleId, Integer cmdId, Integer p0, Integer acitionOvertime) - throws HardwareException, InterruptedException { - this.callblockcmd(moduleId, cmdId, new Integer[] { p0 }, acitionOvertime); + public void callblockcmd(Integer moduleId, Integer cmdId, Integer p0, Integer acitionOvertime) throws HardwareException, InterruptedException { + this.callblockcmd(moduleId, cmdId, new Integer[]{p0}, acitionOvertime); } - public void callblockcmd(Integer moduleId, Integer cmdId, Integer acitionOvertime) - throws HardwareException, InterruptedException { - this.callblockcmd(moduleId, cmdId, new Integer[] {}, acitionOvertime); + public void callblockcmd(Integer moduleId, Integer cmdId, Integer acitionOvertime) throws HardwareException, InterruptedException { + this.callblockcmd(moduleId, cmdId, new Integer[]{}, acitionOvertime); } // TODO @@ -413,7 +433,9 @@ public class A8kCanBusService { try { return this.sendCmd(pack, A8kPacket.CMD_OVERTIME); } catch (HardwareException e) { - if (e.getErrorCode() != A8kEcode.Overtime.toInt()); { + if (e.getErrorCode() != A8kEcode.Overtime.toInt()) + ; + { throw e; } } @@ -421,8 +443,7 @@ public class A8kCanBusService { return null; } - public void callblockcmd(Integer moduleId, Integer cmdId, Integer[] params, int acitionOvertime) - throws HardwareException, InterruptedException { + public void callblockcmd(Integer moduleId, Integer cmdId, Integer[] params, int acitionOvertime) throws HardwareException, InterruptedException { var packet = this.packParamsToPacket(moduleId, cmdId, params); this.sendCmdAutoResend(packet, A8kPacket.CMD_OVERTIME); @@ -432,7 +453,7 @@ public class A8kCanBusService { long startedAt = System.currentTimeMillis(); do { - var pack = this.callcmd(moduleId, CmdId.kmodule_get_status, new Integer[] {}); + var pack = this.callcmd(moduleId, CmdId.kmodule_get_status, new Integer[]{}); var status = pack.getContentI32(0); if (0 == status) { break; @@ -450,7 +471,7 @@ public class A8kCanBusService { private A8kPacket sendCmd(A8kPacket pack, int overtime) throws HardwareException { pack.setPacketIndex(packetIndex); waitingReceiptIndex = packetIndex; - packetIndex = packetIndex + 1; + packetIndex = packetIndex + 1; if (packetIndex > 10000) { packetIndex = 1; } @@ -505,8 +526,7 @@ public class A8kCanBusService { logger.info("RX {}:({})", packet, rx); } - if (packet.getPacketType() == A8kPacket.PACKET_TYPE_ACK - || packet.getPacketType() == A8kPacket.PACKET_TYPE_ERROR_ACK) { + if (packet.getPacketType() == A8kPacket.PACKET_TYPE_ACK || packet.getPacketType() == A8kPacket.PACKET_TYPE_ERROR_ACK) { if (isWaitingReceipt) { if (waitingReceiptIndex == packet.getPacketIndex()) { receiptQueue.add(packet); diff --git a/src/main/java/a8k/service/hardware/CommonHardwareOpeartion.java b/src/main/java/a8k/service/hardware/CommonHardwareOpeartion.java index e1e4daa..5a03e6d 100644 --- a/src/main/java/a8k/service/hardware/CommonHardwareOpeartion.java +++ b/src/main/java/a8k/service/hardware/CommonHardwareOpeartion.java @@ -147,7 +147,7 @@ public class CommonHardwareOpeartion { try {canBus.stepMotorEnable(MId.OptModScannerM, 0);} catch (HardwareException ignored) {} //HBot初始化 -// try {canBus.stepMotorEnable(MId.HbotM, 0);} catch (HardwareException ignored) {} + try {canBus.stepMotorEnable(MId.HbotM, 0);} catch (HardwareException ignored) {} try {canBus.stepMotorEnable(MId.PipetteModZM, 0);} catch (HardwareException ignored) {} //转盘归零