优化xbox,盖世小鸡蓝牙手柄控制,增加动作集控制

This commit is contained in:
myperworld committed 2025-10-17 13:18:12 +08:00
1 parent 9cc5186a58
commit e33a6b894d
10 files changed
+606 -356

No files matched your search

+158 -177
View File
@@ -8,18 +8,13 @@ Modify by Tommy 2025.10.30
#include <SMS_STS.h>
#include <SimpleFOC.h>
#include <FS.h>
#include <esp_adc_cal.h>
#include <XboxSeriesXControllerESP32_asukiaaa.hpp> // xbox手柄
#include "VoltageMonitor.h" // 电源
#include "XboxBluetooth.h" // 蓝牙手柄
#include "RobotActions.h"
/************ xbox手柄实体 *************/
XboxSeriesXControllerESP32_asukiaaa::Core xboxController("0a:27:12:5c:71:79");
/************函数申明*************/
// 电压检测函数
void adc_calibration_init();
void bat_check();
// 平衡算法函数
void lqr_balance_loop();
void yaw_loop();
@@ -29,12 +24,11 @@ void resetZeroPoint();
void jump_charge(); // 腿部弯曲(蓄能)
// RGB相关函数
void updateBluetoothLED();
void handleLEDBlink();
// Xbox函数
void process_xbox_notif();
// 动作处理函数
void processControllerData(const XboxControllerNotificationParser& data);
void runActionSequence();
const int deadZone = 10; // 摇杆死区阈值
bool last_btnA = 0;
@@ -43,6 +37,14 @@ bool last_btnX = 0;
bool last_btnY = 0;
bool last_btnRS = 0;
bool last_btnLS = 0;
bool last_btnDirRight = 0;
bool last_btnDirLeft = 0;
bool last_btnDirUp = 0;
bool last_btnDirDown = 0;
bool last_btnSelect = 0;
bool last_btnStart = 0;
bool last_btnShare = 0;
/************实例定义*************/
@@ -81,7 +83,7 @@ SMS_STS sms_sts;
// MPU6050实例
MPU6050 mpu6050(I2Ctwo);
// rgb 配置
// FastLED配置
#define RGB_PIN 21
#define NUM_LEDS 2
CRGB leds[NUM_LEDS];
@@ -91,12 +93,6 @@ int blinkCount = 0;
bool isBlinking = false;
CRGB blinkColors[6] = {CRGB::Red, CRGB::Red, CRGB::Yellow, CRGB::Yellow, CRGB::Green, CRGB::Green};
// 蓝牙连接状态标志
bool isBluetoothConnected = false;
unsigned long previousBlueBlinkMillis = 0; // 蓝牙蓝灯闪烁计时(新增)
bool blueBlinkState = 0; // 蓝牙蓝灯闪烁状态(新增,0=灭,1=亮)
/************参数定义*************/
#define pi 3.1415927
@@ -106,7 +102,6 @@ float roll_adjust = 0.0f;
// 机身高度
float leg_height_base = 20.0f; // 改为全局变量,初始值20 腿部默认高度基准值,值越小,机身越高, 实测范围 -10(最高),52(最低)
// #define LEG_HEIGHT_BASE 20 //20 腿部默认高度基准值,值越小,机身越高, 实测范围 -10(最高),52(最低)
#define SERVO0_MIN 2047 // 舵机1最低位置,大腿与小腿碰到一起, 从小车正面看右手边舵机
#define SERVO1_MIN 2047 // 舵机2最低位置,大腿与小腿碰到一起, 从小车正面看左手边舵机
#define SERVO0_MAX (2047 + 12 + 8.4 * (35 + 10)) // 2438 舵机1最高
@@ -163,24 +158,15 @@ const unsigned long SPEED_RECORD_INTERVAL = 100; // 转速记录间隔(毫秒)
float last_lqr_speed = 0; // 记录上一时刻的轮部转速
float robot_speed_diff = 0; // 速度差
// 跳跃状态标志位
int jump_flag = 0; // 跳跃过程计数
int forward_jump_flag = 0; // 前跳跃标志位
int back_jump_flag = 0; // 后跳跃标志位
int left_jump_flag = 0; // 左跳跃标志位
int right_jump_flag = 0; // 右跳跃标志位
int jump_pre_flag = 0; // 跳跃前蓄能计数
int wheel_ground_flag = 1; // 轮子是否离地标记 默认着地
int wheel_ground_flag = 1; // 轮子是否离地标记 默认离地
// 电压检测
#define LED_BAT 13
uint16_t bat_check_num = 0;
int BAT_PIN = 35; // select the input pin for the ADC
static esp_adc_cal_characteristics_t adc_chars;
static const adc1_channel_t channel = ADC1_CHANNEL_7;
static const adc_bits_width_t width = ADC_WIDTH_BIT_12;
static const adc_atten_t atten = ADC_ATTEN_DB_12;
static const adc_unit_t unit = ADC_UNIT_1;
// 倒地起来辅助计数
int loopCounter = 0; // loop函数执行计数
int resetToDefaultCounter = 0; // resetToDefault执行计数
@@ -190,10 +176,17 @@ bool robot_enabled = true; // 小车使能状态,true为起立状态,false
bool last_robot_enabled = true; // 记录上一次的小车状态
int sitting_down = 0; // 标记是否正在执行坐下动作
// 动作执行状态管理
bool isExecutingAction = false;
bool isPausing = false;
unsigned long currentPauseDuration = 0;
unsigned long stepStartTime = 0;
unsigned long pauseStartTime = 0;
int currentStepIndex = 0;
RobotAction* currentAction = NULL;
typedef struct
{
int mode = 0; // 0:左摇杆左右转向控制(智能),1:左摇杆上下摇摆控制(手动)
int height = 30;
int roll;
int jump = 0; // 0 =不跳跃 1= 跳跃
@@ -290,8 +283,7 @@ void setup()
FastLED.show();
// Xbox手柄初始化
Serial.println("Starting Xbox Bluetooth Client");
xboxController.begin();
xboxControllerInit();
delay(500);
@@ -300,22 +292,21 @@ void setup()
void loop()
{
// 处理LED闪烁
// 处理LED闪烁
handleLEDBlink();
// 处理蓝牙状态蓝灯,优先级低于自定义闪烁
updateBluetoothLED();
// 处理xbox手柄通知
process_xbox_notif();
process_xbox_notif(); // 处理xbox手柄通知
bat_check(); // 电压检测
// delayMicroseconds(1500);
delayMicroseconds(1500);
mpu6050.update(); // IMU数据更新
lqr_balance_loop(); // lqr自平衡控制
yaw_loop(); // yaw轴转向控制
jump_loop(); // 跳跃动作
leg_loop(); // 腿部动作控制
runActionSequence();
// 记录上一次的遥控数据数据
wrobot.joyx_last = wrobot.joyx;
wrobot.joyy_last = wrobot.joyy;
@@ -355,6 +346,7 @@ void loop()
jump_pre_flag = 1;
}
}
// 向右跳跃
if(right_jump_flag){
right_jump_flag ++ ;
@@ -583,7 +575,6 @@ void leg_loop()
LegHeightDiff = 0; // 初始化 roll 角度补偿值
// 智能计算左右脚高度差, 如果同时有转向和前后指令 ,自动计算腿部高度补偿
if(wrobot.mode == 0){
if (abs(wrobot.joyx) > 10 && abs(wrobot.joyy) > 10)
{
LegHeightDiff = map(abs(wrobot.joyy), 0, 100, 0, 10);
@@ -601,7 +592,6 @@ void leg_loop()
right_height = wrobot.height + LegHeightDiff;
// Serial.print("右脚抬高");
}
}
// 根据新的高度计算舵机位置
// id[0] 正面看右手,id[1] 正面看左手
@@ -727,8 +717,14 @@ void yaw_loop()
wrobot.height = lpf_height(wrobot.height);
// 增加rgb效果
if(abs(wrobot.joyx) > 60){
handleLEDBlink();
if(abs(wrobot.joyx) > 30){
if(!isBlinking){ // 防止重复启动闪烁
isBlinking = true;
blinkCount = 0; // 重置闪烁计数
blinkState = 0; // 重置闪烁状态
previousBlinkMillis = millis(); // 重置计时
}
handleLEDBlink(); // 执行闪烁逻辑
}
}
@@ -741,78 +737,12 @@ void yaw_loop()
YAW_output = yaw_angle_control + yaw_gyro_control;
}
// 电压检测初始化
void adc_calibration_init()
{
if (esp_adc_cal_check_efuse(ESP_ADC_CAL_VAL_EFUSE_TP) == ESP_OK) {
printf("eFuse Two Point: Supported\n");
} else {
printf("eFuse Two Point: NOT supported\n");
}
// Check Vref is burned into eFuse
if (esp_adc_cal_check_efuse(ESP_ADC_CAL_VAL_EFUSE_VREF) == ESP_OK) {
printf("eFuse Vref: Supported\n");
} else {
printf("eFuse Vref: NOT supported\n");
}
}
// 电压检测
void bat_check()
{
if (bat_check_num > 1000)
{
// 电压读取
uint32_t sum = 0;
sum = analogRead(BAT_PIN);
uint32_t voltage = esp_adc_cal_raw_to_voltage(sum, &adc_chars);
double battery = (voltage * 3.97) / 1000.0;
// Serial.println(battery);
// 电量显示
if (battery >= 7.8)
digitalWrite(LED_BAT, HIGH);
else
digitalWrite(LED_BAT, LOW);
bat_check_num = 0;
}
else
bat_check_num++;
}
// 重置距离零点
void resetZeroPoint() {
distance_zeropoint = LQR_distance;
pid_lqr_u.error_prev = 0;
}
// 蓝牙状态对应的蓝灯控制
void updateBluetoothLED() {
// 仅在未执行自定义闪烁时,才显示蓝牙状态蓝灯
if (!isBlinking) {
if (isBluetoothConnected) {
// 蓝牙已连:蓝灯常亮
fill_solid(leds, NUM_LEDS, CRGB::Blue);
FastLED.show();
} else {
// 蓝牙未连:蓝灯500ms闪烁(周期1秒)
unsigned long currentMillis = millis();
if (currentMillis - previousBlueBlinkMillis >= 500) {
previousBlueBlinkMillis = currentMillis;
// 切换蓝灯状态(亮/灭)
if (blueBlinkState == 0) {
fill_solid(leds, NUM_LEDS, CRGB::Blue);
blueBlinkState = 1;
} else {
fill_solid(leds, NUM_LEDS, CRGB::Black);
blueBlinkState = 0;
}
FastLED.show();
}
}
}
}
// 添加LED闪烁处理函数
void handleLEDBlink() {
if (!isBlinking) return;
@@ -873,9 +803,12 @@ void processControllerData(const XboxControllerNotificationParser& remoteData) {
// String(xboxController.xboxNotif.joyRVert) + "," + // 右摇杆 垂直轴 (上-下 0 -65535)
// String(xboxController.xboxNotif.trigLT) + "," + // 左扳机键 (松开- 按下 0 -1023)
// String(xboxController.xboxNotif.trigRT) ; // 右扳机键 (松开- 按下 0 -1023)
// Serial.println(str);
// Serial.println(str);
// 正在执行动作组,忽略其他指令
if (isExecutingAction) {
return;
}
// 坐下
if (remoteData.btnLB && robot_enabled) {
@@ -915,10 +848,6 @@ void processControllerData(const XboxControllerNotificationParser& remoteData) {
wrobot.joyy = 0;
}
if(wrobot.mode == 0) // 智能模式
{
// 左右转向 左摇杆X轴:先映射到-100~100,再按比例放大到-100~100
int convertedJ1PotX = constrain(
map(remoteData.joyLHori, 0, 65535, -100, 100),
@@ -936,50 +865,35 @@ void processControllerData(const XboxControllerNotificationParser& remoteData) {
wrobot.height = constrain(wrobot.height, 30, 50);
wrobot.height = lpf_height(wrobot.height);
}
else if(wrobot.mode == 1) // 手动模式
{
// 摇摆角度 左摇杆X轴,先映射到-60~60,再按比例放大到-100~100
int convertedJ1PotX = constrain(
map(remoteData.joyLHori, 0, 65535, -30, 30) * (100.0/30), // 先映射到-30~30,再按比例放大到-100~100
-100, 100
);
if(abs(convertedJ1PotX) > (deadZone * 2) ) { // 防止同一个摇杆上下左右移动相互干扰
roll_adjust = convertedJ1PotX;
}else{
roll_adjust = 0;
// 右摇摆
if(remoteData.btnDirRight) {
roll_adjust += 0.20;
}
// 左摇摆
if(remoteData.btnDirLeft) {
roll_adjust -= 0.20;
}
// 上下高度 左摇杆Y轴:原始0-65535 → 目标MAX到MIN
int convertedJ1PotY = constrain(
map(remoteData.joyLVert, 0, 65535, 100, -100), // 原始范围0-65535,目标范围100到-100,默认中间0
-100, 100
);
if(abs(convertedJ1PotY) < (deadZone * 2) ) {
convertedJ1PotY = 0;
// 抬升
if(remoteData.btnDirUp) {
leg_height_base -= 0.20;
}
// 下降
if(remoteData.btnDirDown) {
leg_height_base += 0.20;
}
wrobot.height = map( convertedJ1PotY, -100, 100, 30, 70);
wrobot.height = constrain(wrobot.height, 30, 70);
wrobot.height = lpf_height(wrobot.height);
// 左右转向 右摇杆X轴:原始0-960 → 目标-100到100
int convertedJ2PotX = constrain(
map(remoteData.joyRHori, 0, 65535, -100, 100), // 原始范围0-65535,目标范围-100到100,默认中间0
-100, 100
);
if(abs(convertedJ2PotX) > ( deadZone * 2)) {
wrobot.joyx = convertedJ2PotX;
}else{
wrobot.joyx = 0;
// 还原
if (remoteData.btnLS) {
roll_adjust = 0.0f;
leg_height_base = 20.0f;
}
}
// 右扳机 高度向下 :原始0-1023
leg_height_base = constrain(
map(remoteData.trigRT, 0, 1023, 20, 60), // 原始范围0-1023,默认0,所以leg_height_base=20
20, 60
);
// leg_height_base = lpf_height(leg_height_base);
// leg_height_base = constrain(
// map(remoteData.trigRT, 0, 1023, 20, 60), // 原始范围0-1023,默认0,所以leg_height_base=20
// 20, 60
// );
// 左扳机 rgb led 上键 闪烁6次:红红黄黄绿绿
if(remoteData.trigLT && !isBlinking){
@@ -997,44 +911,111 @@ void processControllerData(const XboxControllerNotificationParser& remoteData) {
}
last_btnRS = remoteData.btnRS;
// 让左跳跃
// 向左跳跃
if (remoteData.btnX && !last_btnX && robot_enabled) {
left_jump_flag = 1; // 跳跃
}
last_btnX = remoteData.btnX;
// 让右跳跃
// 向右跳跃
if (remoteData.btnB && !last_btnB && robot_enabled) {
right_jump_flag = 1; // 跳跃
}
last_btnB = remoteData.btnB;
// 切换智能模式和手动模式
if (remoteData.btnY) { // 1:左摇杆上下摇摆控制(手动)
wrobot.mode = 1;
// btnY
if (remoteData.btnY && !last_btnY && robot_enabled && !isExecutingAction) {
isExecutingAction = true; // 标记开始执行动作
currentAction = &action_lift_sway; // 指定执行的动作
stepStartTime = millis(); // 记录第一步开始时间
currentStepIndex = 0; // 从第一步开始
}
if (remoteData.btnA) { // 0:左摇杆左右转向控制(智能)
wrobot.mode = 0;
roll_adjust = 0.0f;
leg_height_base = 20.0f;
last_btnY = remoteData.btnY; // 更新btnY的上一次状态
// btnA
if (remoteData.btnA && !last_btnA && robot_enabled && !isExecutingAction) {
isExecutingAction = true;
currentAction = &action_wheel_sway;
stepStartTime = millis();
currentStepIndex = 0;
}
last_btnA = remoteData.btnA;
}
/* Xbox数据处理函数(直接传递原生数据) */
void process_xbox_notif(){
xboxController.onLoop();
if (xboxController.isConnected()){
isBluetoothConnected = true; // 手柄已连接
if (xboxController.isWaitingForFirstNotification()){
Serial.println("Waiting for first Xbox data...");
}else{
// Serial.println("Xbox controller connected !!!"); // 手柄蓝灯常亮
processControllerData(xboxController.xboxNotif);
// 执行连续动作的函数
void runActionSequence() {
// 若未执行动作或动作指针为空,直接返回
if (!isExecutingAction || currentAction == NULL) {
return;
}
// 获取当前时间
unsigned long currentTime = millis();
// --------------- 停顿逻辑处理 ---------------
if (isPausing) {
// 停顿期间不更新任何控制参数(保持上一步状态)
unsigned long pauseElapsed = currentTime - pauseStartTime;
// 停顿时间结束,切换到下一步
if (pauseElapsed >= currentPauseDuration) {
isPausing = false;
currentStepIndex++;
stepStartTime = currentTime; // 记录新步骤开始时间
// Serial.print("停顿结束,切换到步骤:");
// Serial.println(currentStepIndex);
}
return; // 停顿期间直接返回,不执行后续逻辑
}
// --------------- 正常步骤执行逻辑 ---------------
unsigned long elapsedTime = currentTime - stepStartTime;
ActionStep currentStep = currentAction->steps[currentStepIndex];
// 检查当前步骤是否为结束标记
if (currentStep.stepDuration == 0) {
isExecutingAction = false;
currentStepIndex = 0;
currentAction = NULL;
// 重置参数
wrobot.joyx = 0;
wrobot.joyy = 0;
wrobot.jump = 0;
roll_adjust = 0.0f;
leg_height_base = 20.0f;
// Serial.println("动作集执行完毕,已重置参数");
return;
}
// 更新当前步骤的控制参数
wrobot.joyx = currentStep.joyx;
wrobot.joyy = currentStep.joyy;
wrobot.jump = currentStep.jump;
roll_adjust = currentStep.roll_adjust;
leg_height_base = currentStep.leg_height_base;
// 检查当前步骤是否执行完毕
if (elapsedTime >= currentStep.stepDuration) {
// 若设置了停顿时间,进入停顿状态
if (currentStep.pauseDuration > 0) {
isPausing = true;
currentPauseDuration = currentStep.pauseDuration; // 记录当前需要停顿的时间
pauseStartTime = currentTime;
// Serial.print("步骤执行完毕,开始停顿:");
// Serial.print(currentStep.pauseDuration);
// Serial.println("ms");
} else {
// 无停顿,直接切换到下一步
currentStepIndex++;
stepStartTime = currentTime;
// Serial.print("步骤切换:第");
// Serial.print(currentStepIndex);
// Serial.println("步");
}
}
else{
isBluetoothConnected = false; // 手柄未连接
// Serial.println("Xbox controller not connected");
}
}