|
@ -3,6 +3,7 @@ package a8k.base_hardware; |
|
|
import a8k.a8k_can_protocol.*; |
|
|
import a8k.a8k_can_protocol.*; |
|
|
import a8k.a8k_can_protocol.MId; |
|
|
import a8k.a8k_can_protocol.MId; |
|
|
import a8k.appbean.HardwareException; |
|
|
import a8k.appbean.HardwareException; |
|
|
|
|
|
import a8k.appbean.Pos2d; |
|
|
import a8k.appbean.appevent.A8kHardwareReport; |
|
|
import a8k.appbean.appevent.A8kHardwareReport; |
|
|
import a8k.service.AppEventBusService; |
|
|
import a8k.service.AppEventBusService; |
|
|
import a8k.utils.ByteArray; |
|
|
import a8k.utils.ByteArray; |
|
@ -53,21 +54,21 @@ public class A8kCanBusService { |
|
|
@Autowired |
|
|
@Autowired |
|
|
private AppEventBusService eventBusService; |
|
|
private AppEventBusService eventBusService; |
|
|
|
|
|
|
|
|
public String uri; // assign by application.yml |
|
|
|
|
|
|
|
|
public String uri; // assign by application.yml |
|
|
private WebSocketClient client; |
|
|
private WebSocketClient client; |
|
|
|
|
|
|
|
|
// 接收回执上下文 |
|
|
// 接收回执上下文 |
|
|
public BlockingQueue<A8kPacket> receiptQueue = new LinkedBlockingQueue<A8kPacket>(); // |
|
|
public BlockingQueue<A8kPacket> receiptQueue = new LinkedBlockingQueue<A8kPacket>(); // |
|
|
boolean isWaitingReceipt = false; // |
|
|
|
|
|
int waitingReceiptIndex = 0;// |
|
|
|
|
|
|
|
|
boolean isWaitingReceipt = false; // |
|
|
|
|
|
int waitingReceiptIndex = 0;// |
|
|
|
|
|
|
|
|
// 发送包的packetIndex |
|
|
// 发送包的packetIndex |
|
|
int packetIndex = 0;// |
|
|
int packetIndex = 0;// |
|
|
|
|
|
|
|
|
// 调试标志位 |
|
|
// 调试标志位 |
|
|
boolean debugFlag = true;// |
|
|
|
|
|
|
|
|
boolean debugFlag = true;// |
|
|
// websocket自动重连时间 |
|
|
// websocket自动重连时间 |
|
|
Timer autoConnectTimer = new Timer();// |
|
|
|
|
|
|
|
|
Timer autoConnectTimer = new Timer();// |
|
|
|
|
|
|
|
|
@PostConstruct |
|
|
@PostConstruct |
|
|
public void init() throws URISyntaxException { |
|
|
public void init() throws URISyntaxException { |
|
@ -112,8 +113,7 @@ public class A8kCanBusService { |
|
|
client.connect(); |
|
|
client.connect(); |
|
|
} catch (IllegalStateException e) { |
|
|
} 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(); |
|
|
client.reconnect(); |
|
|
} |
|
|
} |
|
|
} |
|
|
} |
|
@ -122,8 +122,8 @@ public class A8kCanBusService { |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
private A8kPacket packParamsToPacket(Integer moduleId, Integer cmdId, Integer[] params) { |
|
|
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 - |
|
|
int bufferSize = 2 + 2 + 1 + 1 + 2 + 4 * params.length; // PacketIndex - MainCmdId - SubCmdId - CmdType - |
|
|
// ModuleId - Parameters |
|
|
// ModuleId - Parameters |
|
|
ByteBuffer buffer = ByteBuffer.allocate(bufferSize); |
|
|
ByteBuffer buffer = ByteBuffer.allocate(bufferSize); |
|
@ -140,9 +140,6 @@ public class A8kCanBusService { |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// |
|
|
// |
|
|
// module opeation |
|
|
// module opeation |
|
|
// |
|
|
// |
|
@ -182,24 +179,54 @@ public class A8kCanBusService { |
|
|
callcmd(id.toInt(), CmdId.kmodule_active_cfg); |
|
|
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 { |
|
|
public void hbotEnable(MId mid, int enable) throws HardwareException { |
|
|
callcmd(mid.toInt(), CmdId.kxymotor_enable, enable); |
|
|
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 { |
|
|
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 { |
|
|
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 { |
|
|
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 |
|
|
// TODO |
|
@ -413,7 +433,9 @@ public class A8kCanBusService { |
|
|
try { |
|
|
try { |
|
|
return this.sendCmd(pack, A8kPacket.CMD_OVERTIME); |
|
|
return this.sendCmd(pack, A8kPacket.CMD_OVERTIME); |
|
|
} catch (HardwareException e) { |
|
|
} catch (HardwareException e) { |
|
|
if (e.getErrorCode() != A8kEcode.Overtime.toInt()); { |
|
|
|
|
|
|
|
|
if (e.getErrorCode() != A8kEcode.Overtime.toInt()) |
|
|
|
|
|
; |
|
|
|
|
|
{ |
|
|
throw e; |
|
|
throw e; |
|
|
} |
|
|
} |
|
|
} |
|
|
} |
|
@ -421,8 +443,7 @@ public class A8kCanBusService { |
|
|
return null; |
|
|
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); |
|
|
var packet = this.packParamsToPacket(moduleId, cmdId, params); |
|
|
this.sendCmdAutoResend(packet, A8kPacket.CMD_OVERTIME); |
|
|
this.sendCmdAutoResend(packet, A8kPacket.CMD_OVERTIME); |
|
|
|
|
|
|
|
@ -432,7 +453,7 @@ public class A8kCanBusService { |
|
|
|
|
|
|
|
|
long startedAt = System.currentTimeMillis(); |
|
|
long startedAt = System.currentTimeMillis(); |
|
|
do { |
|
|
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); |
|
|
var status = pack.getContentI32(0); |
|
|
if (0 == status) { |
|
|
if (0 == status) { |
|
|
break; |
|
|
break; |
|
@ -450,7 +471,7 @@ public class A8kCanBusService { |
|
|
private A8kPacket sendCmd(A8kPacket pack, int overtime) throws HardwareException { |
|
|
private A8kPacket sendCmd(A8kPacket pack, int overtime) throws HardwareException { |
|
|
pack.setPacketIndex(packetIndex); |
|
|
pack.setPacketIndex(packetIndex); |
|
|
waitingReceiptIndex = packetIndex; |
|
|
waitingReceiptIndex = packetIndex; |
|
|
packetIndex = packetIndex + 1; |
|
|
|
|
|
|
|
|
packetIndex = packetIndex + 1; |
|
|
if (packetIndex > 10000) { |
|
|
if (packetIndex > 10000) { |
|
|
packetIndex = 1; |
|
|
packetIndex = 1; |
|
|
} |
|
|
} |
|
@ -505,8 +526,7 @@ public class A8kCanBusService { |
|
|
logger.info("RX {}:({})", packet, rx); |
|
|
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 (isWaitingReceipt) { |
|
|
if (waitingReceiptIndex == packet.getPacketIndex()) { |
|
|
if (waitingReceiptIndex == packet.getPacketIndex()) { |
|
|
receiptQueue.add(packet); |
|
|
receiptQueue.add(packet); |
|
|