From f043d5e265913e2956021a85571dc5e5762e3450 Mon Sep 17 00:00:00 2001 From: NeoZng Date: Thu, 8 Dec 2022 12:08:49 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E5=A4=8D=E4=BA=86total=20angle?= =?UTF-8?q?=E8=AE=A1=E7=AE=97=E5=BC=82=E5=B8=B8=E7=9A=84=E9=97=AE=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- application/cmd/robot_cmd.c | 62 +++++++++++++++----------------- modules/remote/remote.md | 4 +-- modules/remote/remote_control.c | 64 ++++++++++++++++----------------- modules/remote/remote_control.h | 7 ++++ 4 files changed, 70 insertions(+), 67 deletions(-) diff --git a/application/cmd/robot_cmd.c b/application/cmd/robot_cmd.c index 08711a2..a525911 100644 --- a/application/cmd/robot_cmd.c +++ b/application/cmd/robot_cmd.c @@ -17,7 +17,7 @@ static CANCommInstance *chasiss_can_comm; // 双板通信 #endif // !ONE_BOARD -static RC_ctrl_t *remote_control_data; // 遥控器数据,初始化时返回 +static RC_ctrl_t *rc_data; // 遥控器数据,初始化时返回 static Vision_Recv_s *vision_recv_data; // 视觉接收数据指针,初始化时返回 static Vision_Send_s vision_send_data; // 视觉发送数据 @@ -40,15 +40,15 @@ static Robot_Status_e robot_state; void GimbalCMDInit() { - remote_control_data = RemoteControlInit(&huart3); // 修改为对应串口,注意dbus协议串口需加反相器 - vision_recv_data = VisionInit(&huart1); // 视觉通信串口 + rc_data = RemoteControlInit(&huart3); // 修改为对应串口,注意dbus协议串口需加反相器 + vision_recv_data = VisionInit(&huart1); // 视觉通信串口 gimbal_cmd_pub = PubRegister("gimbal_cmd", sizeof(Gimbal_Ctrl_Cmd_s)); gimbal_feed_sub = SubRegister("gimbal_feed", sizeof(Gimbal_Upload_Data_s)); shoot_cmd_pub = PubRegister("shoot_cmd", sizeof(Shoot_Ctrl_Cmd_s)); shoot_feed_sub = SubRegister("shoot_feed", sizeof(Shoot_Upload_Data_s)); - chassis_cmd_pub = PubRegister("gimbal2chassis", sizeof(Chassis_Ctrl_Cmd_s)); - chassis_feed_sub = SubRegister("chassis2gimbal", sizeof(Chassis_Upload_Data_s)); + chassis_cmd_pub = PubRegister("chassis_cmd", sizeof(Chassis_Ctrl_Cmd_s)); + chassis_feed_sub = SubRegister("chassis_feed", sizeof(Chassis_Upload_Data_s)); } /** @@ -84,48 +84,44 @@ static void CalcOffsetAngle() static void RemoteControlSet() { // 控制底盘和云台运行模式,云台待添加,云台是否始终使用IMU数据? - if (switch_is_down(remote_control_data[0].rc.s[1])) // 右侧开关状态[下],底盘跟随云台 + if (switch_is_down(rc_data[TEMP].rc.s[0])) // 右侧开关状态[下],底盘跟随云台 chassis_cmd_send.chassis_mode = CHASSIS_FOLLOW_GIMBAL_YAW; - if (switch_is_mid(remote_control_data[0].rc.s[1])) // 右侧开关状态[中],底盘和云台分离,底盘保持不转动 + if (switch_is_mid(rc_data[TEMP].rc.s[0])) // 右侧开关状态[中],底盘和云台分离,底盘保持不转动 chassis_cmd_send.chassis_mode = CHASSIS_NO_FOLLOW; - // 云台参数,确定云台控制数据 - if (switch_is_mid(remote_control_data[0].rc.s[0])) // 左侧开关状态为[中],视觉模式 + if (switch_is_mid(rc_data[TEMP].rc.s[1])) // 左侧开关状态为[中],视觉模式 { - // 待添加 + // 待添加,视觉会发来和目标的误差,同样将其转化为total angle的增量进行控制 // ... } - // 侧开关状态为[下],或视觉未识别到目标,纯遥控器拨杆控制 - if (switch_is_down(remote_control_data[0].rc.s[0]) || vision_recv_data->target_state == NO_TARGET) + // 左侧开关状态为[下],或视觉未识别到目标,纯遥控器拨杆控制 + if (switch_is_down(rc_data[TEMP].rc.s[1]) || vision_recv_data->target_state == NO_TARGET) { // 按照摇杆的输出大小进行角度增量,增益系数需调整 - gimbal_cmd_send.yaw += 0.04f * (float)remote_control_data[0].rc.joystick[2]; - gimbal_cmd_send.pitch = 0.5f * (float)remote_control_data[0].rc.joystick[3]; + gimbal_cmd_send.yaw += 0.04f * (float)rc_data[TEMP].rc.joystick[2]; + gimbal_cmd_send.pitch += 0.5f * (float)rc_data[TEMP].rc.joystick[3]; gimbal_cmd_send.gimbal_mode = GIMBAL_GYRO_MODE; } - // 底盘参数,目前没有加入小陀螺(调试似乎没有必要),系数需要调整 - chassis_cmd_send.vx = 1.0f * (float)remote_control_data[0].rc.joystick[0]; - chassis_cmd_send.vy = 1.0f * (float)remote_control_data[0].rc.joystick[1]; - + chassis_cmd_send.vx = 1.0f * (float)rc_data[TEMP].rc.joystick[0]; + chassis_cmd_send.vy = 1.0f * (float)rc_data[TEMP].rc.joystick[1]; // 发射参数 - if (switch_is_up(remote_control_data[0].rc.s[1])) //右侧开关状态[上],弹舱打开 - ; //弹舱舵机控制,待添加servo_motor模块,开启 + if (switch_is_up(rc_data[TEMP].rc.s[0])) // 右侧开关状态[上],弹舱打开 + ; // 弹舱舵机控制,待添加servo_motor模块,开启 else - ;//弹舱舵机控制,待添加servo_motor模块,关闭 + ; // 弹舱舵机控制,待添加servo_motor模块,关闭 // 摩擦轮控制,后续可以根据左侧拨轮的值大小切换射频 - if(remote_control_data[0].rc.joystick[4]>100) - shoot_cmd_send.shoot_mode=FRICTION_ON; + if (rc_data[TEMP].rc.joystick[4] > 100) + shoot_cmd_send.shoot_mode = FRICTION_ON; else - shoot_cmd_send.shoot_mode=FRICTION_OFF; + shoot_cmd_send.shoot_mode = FRICTION_OFF; // 拨弹控制,目前固定为连发 - if(remote_control_data[0].rc.joystick[4]>500) - shoot_cmd_send.load_mode=LOAD_BURSTFIRE; + if (rc_data[TEMP].rc.joystick[4] > 500) + shoot_cmd_send.load_mode = LOAD_BURSTFIRE; else - shoot_cmd_send.load_mode=LOAD_STOP; - + shoot_cmd_send.load_mode = LOAD_STOP; } /** @@ -143,8 +139,8 @@ static void MouseKeySet() */ static void EmergencyHandler() { - // 拨轮的向下拨超过一半 - if (remote_control_data[0].rc.joystick[4] < -300) // 还需添加重要应用和模块离线的判断 + // 拨轮的向下拨超过一半,注意向下拨轮是正 + if (rc_data[TEMP].rc.joystick[4] > 300) // 还需添加重要应用和模块离线的判断 { robot_state = ROBOT_STOP; // 遥控器左上侧拨轮打满,进入紧急停止模式 gimbal_cmd_send.gimbal_mode == GIMBAL_ZERO_FORCE; @@ -152,7 +148,7 @@ static void EmergencyHandler() shoot_cmd_send.shoot_mode == SHOOT_STOP; return; } - // if(remote_control_data[0].rc.joystick[4]>300 && 各个模块正常) + // if(rc_data[TEMP].rc.joystick[4]<-300 && 各个模块正常) // { // //恢复运行 // //... @@ -169,9 +165,9 @@ void GimbalCMDTask() // 根据gimbal的反馈值计算云台和底盘正方向的夹角,不需要传参,通过私有变量完成 CalcOffsetAngle(); - if (switch_is_down(remote_control_data[0].rc.s[0])) // 遥控器左侧开关状态为[下],遥控器控制 + if (switch_is_down(rc_data[TEMP].rc.s[1])) // 遥控器左侧开关状态为[下],遥控器控制 RemoteControlSet(); - else if (switch_is_up(remote_control_data[0].rc.s[0])) // 遥控器左侧开关状态为[上],键盘控制 + else if (switch_is_up(rc_data[TEMP].rc.s[1])) // 遥控器左侧开关状态为[上],键盘控制 MouseKeySet(); EmergencyHandler(); // 处理模块离线和遥控器急停等紧急情况 diff --git a/modules/remote/remote.md b/modules/remote/remote.md index 21c12aa..dba2a7d 100644 --- a/modules/remote/remote.md +++ b/modules/remote/remote.md @@ -73,12 +73,12 @@ remote_control 拨轮向下打到底进入紧急停止模式;拨轮向上打开启摩擦轮,超过一半开始发射(速度环,连发) -左侧开关: +左侧开关`s[1]`: - 上:键鼠控制 - 中:视觉控制(没有识别到目标的时候仍然可以使用遥控器控制云台) - 下:遥控器控制 -右侧开关: +右侧开关`s[0]`: - 上:弹舱开 - 中:底盘云台分离(底盘不旋转,全向移动) - 下:底盘跟随云台 diff --git a/modules/remote/remote_control.c b/modules/remote/remote_control.c index a7e454c..9238a50 100644 --- a/modules/remote/remote_control.c +++ b/modules/remote/remote_control.c @@ -17,57 +17,57 @@ static USARTInstance *rc_usart_instance; */ static void sbus_to_rc(volatile const uint8_t *sbus_buf) { - memcpy(&rc_ctrl[1], &rc_ctrl[0], sizeof(RC_ctrl_t)); // 保存上一次的数据 + memcpy(&rc_ctrl[1], &rc_ctrl[TEMP], sizeof(RC_ctrl_t)); // 保存上一次的数据 // 摇杆 - rc_ctrl[0].rc.joystick[0] = (sbus_buf[0] | (sbus_buf[1] << 8)) & 0x07ff; //!< Channel 0 - rc_ctrl[0].rc.joystick[1] = ((sbus_buf[1] >> 3) | (sbus_buf[2] << 5)) & 0x07ff; //!< Channel 1 - rc_ctrl[0].rc.joystick[2] = ((sbus_buf[2] >> 6) | (sbus_buf[3] << 2) | (sbus_buf[4] << 10)) & 0x07ff; //!< Channel 2 - rc_ctrl[0].rc.joystick[3] = ((sbus_buf[4] >> 1) | (sbus_buf[5] << 7)) & 0x07ff; //!< Channel 3 - rc_ctrl[0].rc.joystick[4] = sbus_buf[16] | (sbus_buf[17] << 8); // 拨轮 + rc_ctrl[TEMP].rc.joystick[0] = (sbus_buf[0] | (sbus_buf[1] << 8)) & 0x07ff; //!< Channel 0 + rc_ctrl[TEMP].rc.joystick[1] = ((sbus_buf[1] >> 3) | (sbus_buf[2] << 5)) & 0x07ff; //!< Channel 1 + rc_ctrl[TEMP].rc.joystick[2] = ((sbus_buf[2] >> 6) | (sbus_buf[3] << 2) | (sbus_buf[4] << 10)) & 0x07ff; //!< Channel 2 + rc_ctrl[TEMP].rc.joystick[3] = ((sbus_buf[4] >> 1) | (sbus_buf[5] << 7)) & 0x07ff; //!< Channel 3 + rc_ctrl[TEMP].rc.joystick[4] = sbus_buf[16] | (sbus_buf[17] << 8); // 拨轮 // 开关,0左1右 - rc_ctrl[0].rc.s[0] = ((sbus_buf[5] >> 4) & 0x0003); //!< Switch left - rc_ctrl[0].rc.s[1] = ((sbus_buf[5] >> 4) & 0x000C) >> 2; //!< Switch right + rc_ctrl[TEMP].rc.s[0] = ((sbus_buf[5] >> 4) & 0x0003); //!< Switch left + rc_ctrl[TEMP].rc.s[1] = ((sbus_buf[5] >> 4) & 0x000C) >> 2; //!< Switch right // 鼠标解析 - rc_ctrl[0].mouse.x = sbus_buf[6] | (sbus_buf[7] << 8); //!< Mouse X axis - rc_ctrl[0].mouse.y = sbus_buf[8] | (sbus_buf[9] << 8); //!< Mouse Y axis - rc_ctrl[0].mouse.z = sbus_buf[10] | (sbus_buf[11] << 8); //!< Mouse Z axis - rc_ctrl[0].mouse.press_l = sbus_buf[12]; //!< Mouse Left Is Press ? - rc_ctrl[0].mouse.press_r = sbus_buf[13]; //!< Mouse Right Is Press ? + rc_ctrl[TEMP].mouse.x = sbus_buf[6] | (sbus_buf[7] << 8); //!< Mouse X axis + rc_ctrl[TEMP].mouse.y = sbus_buf[8] | (sbus_buf[9] << 8); //!< Mouse Y axis + rc_ctrl[TEMP].mouse.z = sbus_buf[10] | (sbus_buf[11] << 8); //!< Mouse Z axis + rc_ctrl[TEMP].mouse.press_l = sbus_buf[12]; //!< Mouse Left Is Press ? + rc_ctrl[TEMP].mouse.press_r = sbus_buf[13]; //!< Mouse Right Is Press ? // 按键值,每个键1bit,key_temp共16位;按键顺序在remote_control.h的宏定义中可见 // 使用位域后不再需要这一中间操作 - rc_ctrl[0].key_temp = sbus_buf[14] | (sbus_buf[15] << 8); //!< KeyBoard value + rc_ctrl[TEMP].key_temp = sbus_buf[14] | (sbus_buf[15] << 8); //!< KeyBoard value // @todo 似乎可以直接用位域操作进行,把key_temp通过强制类型转换变成key类型? 位域方案在下面,尚未测试 // 按键值解算,利用宏+循环减少代码长度 for (uint16_t i = 0x0001, j = 0; i != 0x8000; i *= 2, j++) // 依次查看每一个键 { // 如果键按下,对应键的key press状态置1,否则为0 - rc_ctrl[0].key[KEY_PRESS][j] = rc_ctrl[0].key_temp & i; - // 如果当前按下且上一次没按下,切换按键状态.一些工作要通过按键状态而不是按键是否按下来确定(实际上是大部分) - rc_ctrl[0].key[KEY_STATE][j] = rc_ctrl[0].key[KEY_PRESS][j] && !rc_ctrl[1].key[KEY_PRESS][j]; + rc_ctrl[TEMP].key[KEY_PRESS][j] = rc_ctrl[TEMP].key_temp & i; + // 如果当前按下且上一次没按下,切换按键状态.一些模式要通过按键状态而不是按键是否按下来确定(实际上是大部分) + rc_ctrl[TEMP].key[KEY_STATE][j] = rc_ctrl[TEMP].key[KEY_PRESS][j] && !rc_ctrl[1].key[KEY_PRESS][j]; // 检查是否有组合键按下 - if (rc_ctrl[0].key_temp & 0x0001u << Key_Shift) // 按下ctrl - rc_ctrl[0].key[KEY_PRESS_WITH_SHIFT][j] = rc_ctrl[0].key_temp & i; - if (rc_ctrl[0].key_temp & 0x0001u << Key_Ctrl) // 按下shift - rc_ctrl[0].key[KEY_PRESS_WITH_CTRL][j] = rc_ctrl[0].key_temp & i; + if (rc_ctrl[TEMP].key_temp & 0x0001u << Key_Shift) // 按下ctrl + rc_ctrl[TEMP].key[KEY_PRESS_WITH_SHIFT][j] = rc_ctrl[TEMP].key_temp & i; + if (rc_ctrl[TEMP].key_temp & 0x0001u << Key_Ctrl) // 按下shift + rc_ctrl[TEMP].key[KEY_PRESS_WITH_CTRL][j] = rc_ctrl[TEMP].key_temp & i; } // 位域的按键值解算,直接memcpy即可,注意小端低字节在前,即lsb在第一位 - *(uint16_t *)&rc_ctrl[0].key_test[KEY_PRESS] = (uint16_t)(sbus_buf[14] | (sbus_buf[15] << 8)); - *(uint16_t *)&rc_ctrl[0].key_test[KEY_STATE] = *(uint16_t *)&rc_ctrl[0].key_test[KEY_PRESS] & ~(*(uint16_t *)&(rc_ctrl[1].key_test[KEY_PRESS])); - if (rc_ctrl[0].key_test[KEY_PRESS].ctrl) - rc_ctrl[0].key_test[KEY_PRESS_WITH_CTRL] = rc_ctrl[0].key_test[KEY_PRESS]; - if (rc_ctrl[0].key_test[KEY_PRESS].shift) - rc_ctrl[0].key_test[Key_Shift] = rc_ctrl[0].key_test[KEY_PRESS]; + // *(uint16_t *)&rc_ctrl[TEMP].key_test[KEY_PRESS] = (uint16_t)(sbus_buf[14] | (sbus_buf[15] << 8)); + // *(uint16_t *)&rc_ctrl[TEMP].key_test[KEY_STATE] = *(uint16_t *)&rc_ctrl[TEMP].key_test[KEY_PRESS] & ~(*(uint16_t *)&(rc_ctrl[1].key_test[KEY_PRESS])); + // if (rc_ctrl[TEMP].key_test[KEY_PRESS].ctrl) + // rc_ctrl[TEMP].key_test[KEY_PRESS_WITH_CTRL] = rc_ctrl[TEMP].key_test[KEY_PRESS]; + // if (rc_ctrl[TEMP].key_test[KEY_PRESS].shift) + // rc_ctrl[TEMP].key_test[Key_Shift] = rc_ctrl[TEMP].key_test[KEY_PRESS]; // 减去偏置值 - rc_ctrl[0].rc.joystick[0] -= RC_CH_VALUE_OFFSET; - rc_ctrl[0].rc.joystick[1] -= RC_CH_VALUE_OFFSET; - rc_ctrl[0].rc.joystick[2] -= RC_CH_VALUE_OFFSET; - rc_ctrl[0].rc.joystick[3] -= RC_CH_VALUE_OFFSET; - rc_ctrl[0].rc.joystick[4] -= RC_CH_VALUE_OFFSET; + rc_ctrl[TEMP].rc.joystick[0] -= RC_CH_VALUE_OFFSET; + rc_ctrl[TEMP].rc.joystick[1] -= RC_CH_VALUE_OFFSET; + rc_ctrl[TEMP].rc.joystick[2] -= RC_CH_VALUE_OFFSET; + rc_ctrl[TEMP].rc.joystick[3] -= RC_CH_VALUE_OFFSET; + rc_ctrl[TEMP].rc.joystick[4] -= RC_CH_VALUE_OFFSET; } /** diff --git a/modules/remote/remote_control.h b/modules/remote/remote_control.h index 1dc6f76..1b5b3cb 100644 --- a/modules/remote/remote_control.h +++ b/modules/remote/remote_control.h @@ -17,12 +17,17 @@ #include "main.h" #include "usart.h" +// +#define LAST 1 +#define TEMP 0 + // 获取按键操作 #define KEY_PRESS 0 #define KEY_STATE 1 #define KEY_PRESS_WITH_CTRL 2 #define KEY_PRESS_WITH_SHIFT 3 +// 检查接受值是否出错 #define RC_CH_VALUE_MIN ((uint16_t)364) #define RC_CH_VALUE_OFFSET ((uint16_t)1024) #define RC_CH_VALUE_MAX ((uint16_t)1684) @@ -34,6 +39,8 @@ #define switch_is_down(s) (s == RC_SW_DOWN) #define switch_is_mid(s) (s == RC_SW_MID) #define switch_is_up(s) (s == RC_SW_UP) +#define LEFT_SW 1 +#define RIGHT_SW 0 /* ----------------------- PC Key Definition-------------------------------- */ // 对应key[x][0~16],获取对应的键;例如通过key[KEY_PRESS][Key_W]获取W键是否按下