feat: xyr

This commit is contained in:
玖叁 2024-12-20 00:11:04 +08:00
parent 1cce583841
commit 438685ecef
3 changed files with 59 additions and 1 deletions

1
.gitignore vendored
View File

@ -4,3 +4,4 @@
.vscode/launch.json
.vscode/ipch
src/consts.h
.DS_Store

View File

@ -102,6 +102,14 @@
控制示例 `00 06 23 01 01 FF`
### XYR 控制 `0x24`
> ⚠️ 注意: X Y R 的取值范围为 -100 到 100为有符号 8 位整数
包体 `00 07 24 X Y R FF`
控制示例 `00 07 24 01 01 01 FF`
## 状态回报
### 电机状态汇报 `0xE0`

View File

@ -25,6 +25,7 @@
#define CMD_MOTOR_ROTATE_CONTROL 0x23
#define CMD_DEMO_PID 0xf0
#define CMD_DEMO_PATH 0xf1
#define CMD_MOTOR_XYR_CONTROL 0x24 // XYR轴向控制指令
#define CMD_STATUS_MOTOR 0xE0
@ -583,6 +584,17 @@ void handleSerialPacket(uint8_t *packet, int length, BLECharacteristic &characte
break;
case CMD_MOTOR_XYR_CONTROL:
if (length >= 7) { // 确保数据包长度正确
int8_t x = (int8_t)packet[3]; // X轴向速度
int8_t y = (int8_t)packet[4]; // Y轴向速度
int8_t r = (int8_t)packet[5]; // 旋转速度
Serial.printf("CMD_MOTOR_XYR_CONTROL: X=%d, Y=%d, R=%d\n", x, y, r);
motorXYRControl(x, y, r);
}
break;
default:
break;
}
@ -612,7 +624,7 @@ void processSerialIncomingByte(uint8_t incomingByte, BLECharacteristic &characte
expectedLength = incomingByte;
}
// 到预期长度时检查包尾
// 到预期长度时检查包尾
if (packetIndex == expectedLength - 1)
{
isReceivingPacket = false;
@ -680,3 +692,40 @@ void loop()
sendMotorStatus();
}
}
void motorXYRControl(int8_t x, int8_t y, int8_t r) {
// 将输入范围限制在 -100 到 100
x = constrain(x, -100, 100);
y = constrain(y, -100, 100);
r = constrain(r, -100, 100);
// 将 -100~100 映射到 -255~255
int16_t mappedX = map(x, -100, 100, -255, 255);
int16_t mappedY = map(y, -100, 100, -255, 255);
int16_t mappedR = map(r, -100, 100, -255, 255);
// 计算每个电机的速度
int16_t speedA = mappedY + mappedX + mappedR; // 左前轮
int16_t speedB = mappedY - mappedX + mappedR; // 右前轮
int16_t speedC = mappedY - mappedX - mappedR; // 右后轮
int16_t speedD = mappedY + mappedX - mappedR; // 左后轮
// 找出最大速度的绝对值
int16_t maxSpeed = max(max(abs(speedA), abs(speedB)),
max(abs(speedC), abs(speedD)));
// 如果最大速度超过255等比例缩放所有速度
if (maxSpeed > 255) {
float scale = 255.0f / maxSpeed;
speedA *= scale;
speedB *= scale;
speedC *= scale;
speedD *= scale;
}
// 控制各个电机
motorControl('A', speedA);
motorControl('B', speedB);
motorControl('C', speedC);
motorControl('D', speedD);
}