| | |
| | | return currentSteeringSpeed; |
| | | } |
| | | |
| | | /** |
| | | * 直接设置并发送转向和前进速度(用于持续发送控制指令) |
| | | * @param steeringSpeed 转向速度,范围-100到100 |
| | | * @param forwardSpeed 前进速度,范围-100到100 |
| | | * @return 是否发送成功 |
| | | */ |
| | | public static synchronized boolean setAndSendSpeeds(int steeringSpeed, int forwardSpeed) { |
| | | return sendSpeedsIfDebugSerialOpen(steeringSpeed, forwardSpeed); |
| | | } |
| | | |
| | | private static boolean sendSpeedsIfDebugSerialOpen(int nextSteering, int nextForward) { |
| | | SerialPortService service = sendmessage.getActiveService(); |
| | | if (service == null || !service.isOpen()) { |
| | |
| | | nextForward = clampSpeed(nextForward); |
| | | byte[] payload = buildSteeringCommandBytes((byte) nextSteering, (byte) nextForward); |
| | | |
| | | // 调试:打印发送的数据 |
| | | System.out.println("发送转向指令: " + bytesToHex(payload)); |
| | | |
| | | boolean success = sendmessage.sendViaActive(payload); |
| | | if (success) { |
| | | currentSteeringSpeed = nextSteering; |
| | |
| | | * 测试函数:验证转向指令构建 |
| | | */ |
| | | public static void testBuildSteeringCommand() { |
| | | System.out.println("=== 测试修正后的转向指令构建 ==="); |
| | | |
| | | // 测试1:直行前进 |
| | | byte[] cmd1 = buildSteeringCommandBytes((byte)0, (byte)50); |
| | | System.out.println("直行前进50: " + bytesToHex(cmd1)); |
| | | |
| | | // 测试2:左转前进 |
| | | byte[] cmd2 = buildSteeringCommandBytes((byte)-30, (byte)40); |
| | | System.out.println("左转前进: " + bytesToHex(cmd2)); |
| | | |
| | | // 测试3:右转后退 |
| | | byte[] cmd3 = buildSteeringCommandBytes((byte)20, (byte)-60); |
| | | System.out.println("右转后退: " + bytesToHex(cmd3)); |
| | | |
| | | // 测试4:停止 |
| | | byte[] cmd4 = buildSteeringCommandBytes((byte)0, (byte)0); |
| | | System.out.println("停止: " + bytesToHex(cmd4)); |
| | | } |
| | | |
| | | } |