diff --git a/.vscode/settings.json b/.vscode/settings.json index ee7f37f1a3a5b1e78079ac4ee9dca756638c9bd0..6737570d911f398a966b6dc0da2a81966bb07962 100644 --- a/.vscode/settings.json +++ b/.vscode/settings.json @@ -80,7 +80,9 @@ "gimbal_yaw_pitch_direct.h": "c", "stdint.h": "c", "ahrs.h": "c", - "robot_param_penguin_mini_arm.h": "c" + "robot_param_penguin_mini_arm.h": "c", + "chassis_steering.h": "c", + "develop_task.h": "c" }, "C_Cpp.codeAnalysis.clangTidy.enabled": true, "[cpp]": { diff --git a/Src/freertos.c b/Src/freertos.c index 91b8a41ec3a42ffc8e01c22b709a43824c0b35c3..82817647a460213150b6db95f61d11922710d72a 100644 --- a/Src/freertos.c +++ b/Src/freertos.c @@ -42,6 +42,7 @@ #include "shoot_task.h" #include "mechanical_arm_task.h" #include "music_task.h" +#include "develop_task.h" /* USER CODE END Includes */ /* Private typedef -----------------------------------------------------------*/ @@ -71,6 +72,10 @@ osThreadId mechanical_armTaskHandle; osThreadId musicTaskHandle; #endif +#if (__DEVELOP) +osThreadId developTaskHandle; +#endif + osThreadId imuTaskHandle; osThreadId led_RGB_flow_handle; @@ -210,6 +215,11 @@ void MX_FREERTOS_Init(void) { musicTaskHandle = osThreadCreate(osThread(musicTask), NULL); #endif +#if (__DEVELOP) + osThreadDef(developTask, develop_task, osPriorityNormal, 0, 256); + developTaskHandle = osThreadCreate(osThread(developTask), NULL); +#endif + osThreadDef(imuTask, IMU_task, osPriorityRealtime, 0, 1024); imuTaskHandle = osThreadCreate(osThread(imuTask), NULL); diff --git a/application/assist/develop_task.c b/application/assist/develop_task.c new file mode 100644 index 0000000000000000000000000000000000000000..a8aa5a1ded255722d0ce3785aa3f05f61231e969 --- /dev/null +++ b/application/assist/develop_task.c @@ -0,0 +1,15 @@ +// 开发新功能时可以使用本任务进行功能测试 + +#include "develop_task.h" + +#include "cmsis_os.h" +void develop_task(void const * pvParameters) +{ + // 空闲一段时间 + vTaskDelay(10); + + while (1) { + // code here + vTaskDelay(2); + } +} diff --git a/application/assist/develop_task.h b/application/assist/develop_task.h new file mode 100644 index 0000000000000000000000000000000000000000..0a0ddacec27b282d05f602e0a86fedcf72d8d61f --- /dev/null +++ b/application/assist/develop_task.h @@ -0,0 +1,4 @@ +#ifndef __DEVELOP_TASK_H +#define __DEVELOP_TASK_H +extern void develop_task(void const * pvParameters); +#endif diff --git a/application/assist/usb_task.c b/application/assist/usb_task.c index d2ea69da85cf9d8e4c83f4d15374714594c92bc9..1df1a3229041b2eee8b13d506dd516ca5e19a0c7 100644 --- a/application/assist/usb_task.c +++ b/application/assist/usb_task.c @@ -379,26 +379,27 @@ static void usb_send_outputPC(uint8_t t) OutputPCData.header = SET_OUTPUT_PC_HEDER; OutputPCData.length = sizeof(OutputPCData_s); - char_to_uint(OutputPCData.packets[0].name, "mode_0"); - char_to_uint(OutputPCData.packets[1].name, "mode_1"); - char_to_uint(OutputPCData.packets[2].name, "mode_2"); - char_to_uint(OutputPCData.packets[3].name, "p_0"); - char_to_uint(OutputPCData.packets[4].name, "p_1"); - char_to_uint(OutputPCData.packets[5].name, "p_2"); - char_to_uint(OutputPCData.packets[6].name, "tor_0"); - char_to_uint(OutputPCData.packets[7].name, "tor_1"); - char_to_uint(OutputPCData.packets[8].name, "tor_2"); - char_to_uint(OutputPCData.packets[9].name, "mode"); - char_to_uint(OutputPCData.packets[10].name, "zeroseted"); - char_to_uint(OutputPCData.packets[11].name, "fdbpos_0"); - char_to_uint(OutputPCData.packets[12].name, "fdbpos_1"); - char_to_uint(OutputPCData.packets[13].name, "fdbpos_2"); - char_to_uint(OutputPCData.packets[14].name, "refpos_0"); - char_to_uint(OutputPCData.packets[15].name, "refpos_1"); - char_to_uint(OutputPCData.packets[16].name, "refpos_2"); - // char_to_uint(OutputPCData.packets[17].name, "zAccel"); - // char_to_uint(OutputPCData.packets[18].name, "o_r"); - // char_to_uint(OutputPCData.packets[19].name, "o_p"); + // char_to_uint(OutputPCData.packets[0].name, "mode_0"); + // char_to_uint(OutputPCData.packets[1].name, "mode_1"); + // char_to_uint(OutputPCData.packets[2].name, "mode_2"); + // char_to_uint(OutputPCData.packets[3].name, "p_0"); + // char_to_uint(OutputPCData.packets[4].name, "p_1"); + // char_to_uint(OutputPCData.packets[5].name, "p_2"); + // char_to_uint(OutputPCData.packets[6].name, "tor_0"); + // char_to_uint(OutputPCData.packets[7].name, "tor_1"); + // char_to_uint(OutputPCData.packets[8].name, "tor_2"); + // char_to_uint(OutputPCData.packets[9].name, "mode"); + // char_to_uint(OutputPCData.packets[10].name, "zeroseted"); + // char_to_uint(OutputPCData.packets[11].name, "fdbpos_0"); + // char_to_uint(OutputPCData.packets[12].name, "fdbpos_1"); + // char_to_uint(OutputPCData.packets[13].name, "fdbpos_2"); + // char_to_uint(OutputPCData.packets[14].name, "refpos_0"); + // char_to_uint(OutputPCData.packets[15].name, "refpos_1"); + // char_to_uint(OutputPCData.packets[16].name, "refpos_2"); + char_to_uint(OutputPCData.packets[17].name, "data1"); + char_to_uint(OutputPCData.packets[18].name, "data2"); + char_to_uint(OutputPCData.packets[19].name, "data3"); + char_to_uint(OutputPCData.packets[20].name, "data4"); append_CRC16_check_sum((uint8_t *)&OutputPCData, sizeof(OutputPCData_s)); memcpy(usb_tx_buf, &OutputPCData, sizeof(OutputPCData_s)); diff --git a/application/assist/usb_task.h b/application/assist/usb_task.h index 5c777db16704779aca06fd9097cf1df0fbb26d3a..2ff6c94e24ee2db56821be992785d2a8755c099e 100644 --- a/application/assist/usb_task.h +++ b/application/assist/usb_task.h @@ -85,7 +85,7 @@ #include #define PRESET_SELF_COLOR 1 -#define PC_PACKET_NUM 20 +#define PC_PACKET_NUM 22 typedef struct { uint8_t vision; diff --git a/application/mechanical_arm/mechanical_arm_penguin_mini.c b/application/mechanical_arm/mechanical_arm_penguin_mini.c index c22213b5fffa32f65c5795db22214d705ae7e48d..73a63275104b415e643b3484ba5205e607926ed9 100644 --- a/application/mechanical_arm/mechanical_arm_penguin_mini.c +++ b/application/mechanical_arm/mechanical_arm_penguin_mini.c @@ -294,6 +294,11 @@ void MechanicalArmObserver(void) OutputPCData.packets[14].data = MECHANICAL_ARM.ref.pos[0]; OutputPCData.packets[15].data = MECHANICAL_ARM.ref.pos[1]; OutputPCData.packets[16].data = MECHANICAL_ARM.ref.pos[2]; + + OutputPCData.packets[17].data = GetOtherBoardDataUint16(1, 0); + OutputPCData.packets[18].data = GetOtherBoardDataUint16(1, 1); + OutputPCData.packets[19].data = GetOtherBoardDataUint16(1, 2); + OutputPCData.packets[20].data = GetOtherBoardDataUint16(1, 3); } /*-------------------- Reference --------------------*/ diff --git a/application/robot_cmd/CAN_communication.c b/application/robot_cmd/CAN_communication.c new file mode 100644 index 0000000000000000000000000000000000000000..563a6f2945e5e22781dc31deee61cf8ef6ab8dc8 --- /dev/null +++ b/application/robot_cmd/CAN_communication.c @@ -0,0 +1,124 @@ +/** + ****************************(C) COPYRIGHT 2024 Polarbear**************************** + * @file CAN_communication.c/h + * @brief CAN通信部分 + * @history + * Version Date Author Modification + * V1.0.0 May-27-2024 Penguin 1. done + * + @verbatim + ============================================================================== +板间通信时stdid的内容如下 +data_type + (data_id << 4) + target_id + +bit 0-3: target_id +bit 4-7: data_id +bit 8-11: data_type + + ============================================================================== + @endverbatim + ****************************(C) COPYRIGHT 2024 Polarbear**************************** + */ +#include "CAN_communication.h" + +#include "bsp_can.h" + +static CanCtrlData_s CAN_CTRL_DATA = { + .tx_header.IDE = CAN_ID_STD, + .tx_header.RTR = CAN_RTR_DATA, + .tx_header.DLC = 8, +}; + +/*-------------------- Private functions --------------------*/ +// 板间通信 + +/** + * @brief 发送自定义数据 + * @param[in] hcan CAN句柄 + * @param[in] data_id 数据包ID + * @param[in] target_id 目标板ID + * @param[in] data 包含8个字节的数据的指针 + * @retval none + */ +static void SendData(hcan_t * hcan, uint16_t data_id, uint16_t target_id, uint8_t * data) +{ + CAN_CTRL_DATA.hcan = hcan; + + CAN_CTRL_DATA.tx_header.StdId = BOARD_DATA_ANY + (data_id << 4) + target_id; + + for (size_t i = 0; i < 8; i++) { + CAN_CTRL_DATA.tx_data[i] = data[i]; + } + + CAN_SendTxMessage(&CAN_CTRL_DATA); +} + +/** + * @brief 发送uint16数据 + * @param[in] hcan CAN句柄 + * @param[in] data_id 数据包ID + * @param[in] target_id 目标板ID + * @param[in] data_1 数据1 + * @param[in] data_2 数据2 + * @param[in] data_3 数据3 + * @param[in] data_4 数据4 + * @retval none + */ +static void Uint16SendData( + hcan_t * hcan, uint16_t data_id, uint16_t target_id, uint16_t data_1, uint16_t data_2, + uint16_t data_3, uint16_t data_4) +{ + CAN_CTRL_DATA.hcan = hcan; + + CAN_CTRL_DATA.tx_header.StdId = BOARD_DATA_UINT16 + (data_id << 4) + target_id; + + CAN_CTRL_DATA.tx_data[0] = data_1 >> 8; + CAN_CTRL_DATA.tx_data[1] = data_1; + CAN_CTRL_DATA.tx_data[2] = data_2 >> 8; + CAN_CTRL_DATA.tx_data[3] = data_2; + CAN_CTRL_DATA.tx_data[4] = data_3 >> 8; + CAN_CTRL_DATA.tx_data[5] = data_3; + CAN_CTRL_DATA.tx_data[6] = data_4 >> 8; + CAN_CTRL_DATA.tx_data[7] = data_4; + + CAN_SendTxMessage(&CAN_CTRL_DATA); +} + +/*-------------------- Public functions --------------------*/ + +/** + * @brief CAN发送数据到目标板 + * @param[in] can can口 + * @param[in] data_id 数据包ID + * @param[in] target_id 目标板ID + * @param[in] data 包含8个字节的数据的指针 + * @retval none + */ +void CanSendDataToBoard(uint8_t can, uint16_t data_id, uint16_t target_id, uint8_t * data) +{ + if (can == 1) + SendData(&hcan1, data_id, target_id, data); + else if (can == 2) + SendData(&hcan2, data_id, target_id, data); +} + +/** + * @brief CAN发送uint16数据到目标板 + * @param[in] can can口 + * @param[in] data_id 数据包ID + * @param[in] target_id 目标板ID + * @param[in] data_1 数据1 + * @param[in] data_2 数据2 + * @param[in] data_3 数据3 + * @param[in] data_4 数据4 + * @retval none + */ +void CanSendUint16DataToBoard( + uint8_t can, uint16_t data_id, uint16_t target_id, uint16_t data_1, uint16_t data_2, + uint16_t data_3, uint16_t data_4) +{ + if (can == 1) + Uint16SendData(&hcan1, data_id, target_id, data_1, data_2, data_3, data_4); + else if (can == 2) + Uint16SendData(&hcan2, data_id, target_id, data_1, data_2, data_3, data_4); +} diff --git a/application/robot_cmd/CAN_communication.h b/application/robot_cmd/CAN_communication.h index 827cf1a67f2aabaafbed36de819f86d4fc271402..7d818554f836c4e3531e23e968776a0af40847b2 100644 --- a/application/robot_cmd/CAN_communication.h +++ b/application/robot_cmd/CAN_communication.h @@ -1,10 +1,39 @@ +/** + ****************************(C) COPYRIGHT 2024 Polarbear**************************** + * @file CAN_communication.c/h + * @brief CAN通信部分 + * @history + * Version Date Author Modification + * V1.0.0 May-27-2024 Penguin 1. done + * + @verbatim + ============================================================================== +板间通信时stdid的内容如下 +data_type + (data_id << 4) + target_id + +bit 0-3: target_id +bit 4-7: data_id +bit 8-11: data_type + + ============================================================================== + @endverbatim + ****************************(C) COPYRIGHT 2024 Polarbear**************************** + */ + #ifndef CAN_COMMUNICATION_H #define CAN_COMMUNICATION_H -#include "CAN_cmd_dji.h" #include "CAN_cmd_cybergear.h" #include "CAN_cmd_damiao.h" +#include "CAN_cmd_dji.h" #include "CAN_cmd_lingkong.h" #include "CAN_receive.h" + +extern void CanSendDataToBoard(uint8_t can, uint16_t data_id, uint16_t target_id, uint8_t * data); + +extern void CanSendUint16DataToBoard( + uint8_t can, uint16_t data_id, uint16_t target_id, uint16_t data_1, uint16_t data_2, + uint16_t data_3, uint16_t data_4); + #endif // CAN_COMMUNICATION_H diff --git a/application/robot_cmd/CAN_receive.c b/application/robot_cmd/CAN_receive.c index 084b648f0b7879336eadc0b18f0b2b5970436443..dc11a1bca7af4edd88c70c923cf10587421a856f 100644 --- a/application/robot_cmd/CAN_receive.c +++ b/application/robot_cmd/CAN_receive.c @@ -11,10 +11,12 @@ * V2.0.0 Mar-27-2024 Penguin 1. 添加CAN发送函数和新的电机控制函数,解码中将CAN1 CAN2分开。 * V2.1.0 Mar-20-2024 Penguin 1. 添加DM电机的适配 * V2.2.0 May-22-2024 Penguin 1. 添加LK电机的适配 + * V2.3.0 May-22-2024 Penguin 1. 添加板间通信数据解码 * @verbatim ============================================================================== - + dm电机设置: + 为了配合本框架,请在使用上位机进行设置时,将dm电机的master id 设置为 slave id + 0x50 ============================================================================== @endverbatim ****************************(C) COPYRIGHT 2024 Polarbear**************************** @@ -22,14 +24,16 @@ #include "CAN_receive.h" -#include - #include "bsp_can.h" #include "cmsis_os.h" #include "detect_task.h" +#include "robot_param.h" +#include "string.h" #include "usb_task.h" #include "user_lib.h" +#define DATA_NUM 10 + // 接收数据 static DjiMotorMeasure_t CAN1_DJI_MEASURE[11]; static DjiMotorMeasure_t CAN2_DJI_MEASURE[11]; @@ -42,6 +46,10 @@ static DmMeasure_s CAN2_DM_MEASURE[DM_NUM]; static LkMeasure_s CAN1_LK_MEASURE[LK_NUM]; static LkMeasure_s CAN2_LK_MEASURE[LK_NUM]; + +static uint8_t OTHER_BOARD_DATA_ANY[DATA_NUM][8]; +static uint16_t OTHER_BOARD_DATA_UINT16[DATA_NUM][4]; + /*-------------------- Decode --------------------*/ /** @@ -108,7 +116,7 @@ void LkFdbData(LkMeasure_s * lk_measure, uint8_t * rx_data) */ static void DecodeStdIdData(hcan_t * CAN, CAN_RxHeaderTypeDef * rx_header, uint8_t rx_data[8]) { - switch (rx_header->StdId) { + switch (rx_header->StdId) { //电机解码 case DJI_M1_ID: case DJI_M2_ID: case DJI_M3_ID: @@ -129,7 +137,7 @@ static void DecodeStdIdData(hcan_t * CAN, CAN_RxHeaderTypeDef * rx_header, uint8 { DjiFdbData(&CAN2_DJI_MEASURE[i], rx_data); } - break; + return; } case DM_M1_ID: case DM_M2_ID: @@ -146,7 +154,8 @@ static void DecodeStdIdData(hcan_t * CAN, CAN_RxHeaderTypeDef * rx_header, uint8 { DmFdbData(&CAN2_DM_MEASURE[i], rx_data); } - } break; + return; + } case LK_M1_ID: case LK_M2_ID: case LK_M3_ID: @@ -160,11 +169,29 @@ static void DecodeStdIdData(hcan_t * CAN, CAN_RxHeaderTypeDef * rx_header, uint8 { LkFdbData(&CAN2_LK_MEASURE[i], rx_data); } - } break; + return; + } default: { break; } } + + //板间通信数据解码 + // clang-format off + uint16_t data_type = rx_header->StdId & 0xF00; + uint16_t data_id = (rx_header->StdId & 0x0F0) >> 4; + uint16_t target_id = rx_header->StdId & 0x00F; + // clang-format on + if (target_id != __SELF_BOARD_ID) return; + + if (data_type == BOARD_DATA_UINT16) { + OTHER_BOARD_DATA_UINT16[data_id][0] = (rx_data[0] << 8) | rx_data[1]; + OTHER_BOARD_DATA_UINT16[data_id][1] = (rx_data[2] << 8) | rx_data[3]; + OTHER_BOARD_DATA_UINT16[data_id][2] = (rx_data[4] << 8) | rx_data[5]; + OTHER_BOARD_DATA_UINT16[data_id][3] = (rx_data[6] << 8) | rx_data[7]; + } else if (data_type == BOARD_DATA_ANY) { + memcpy(OTHER_BOARD_DATA_ANY[data_id], rx_data, 8); + } } /** @@ -268,7 +295,7 @@ const DjiMotorMeasure_t * GetDjiMotorMeasurePoint(uint8_t can, uint8_t i) static void GetDjiFdbData(Motor_s * p_motor, const DjiMotorMeasure_t * p_dji_motor_measure) { p_motor->fdb.vel = p_dji_motor_measure->speed_rpm * RPM_TO_OMEGA * p_motor->reduction_ratio * - p_motor->direction; + p_motor->direction; p_motor->fdb.pos = p_dji_motor_measure->ecd * 2 * M_PI / 8192 - M_PI; p_motor->fdb.temp = p_dji_motor_measure->temperate; p_motor->fdb.curr = p_dji_motor_measure->given_current; @@ -332,12 +359,10 @@ static void GetDmFdbData(Motor_s * motor, const DmMeasure_s * dm_measure) */ static void GetLkFdbData(Motor_s * motor, const LkMeasure_s * lk_measure) { - // clang-format off - motor->fdb.pos = uint_to_float(lk_measure->encoder, -M_PI, M_PI, 16); - motor->fdb.vel = lk_measure->speed * DEGREE_TO_RAD; + motor->fdb.pos = uint_to_float(lk_measure->encoder, -M_PI, M_PI, 16); + motor->fdb.vel = lk_measure->speed * DEGREE_TO_RAD; motor->fdb.curr = lk_measure->iq * MF_CONTROL_TO_CURRENT; motor->fdb.temp = lk_measure->temprature; - // clang-format on uint32_t now = HAL_GetTick(); if (now - lk_measure->last_fdb_time > MOTOR_STABLE_RUNNING_TIME) { @@ -391,3 +416,14 @@ void GetMotorMeasure(Motor_s * p_motor) break; } } + +/** + * @brief 获取板间通信数据 + * @param data_id 数据ID + * @param data_offset 数据位置偏移 + * @return none + */ +uint16_t GetOtherBoardDataUint16(uint8_t data_id, uint8_t data_offset) +{ + return OTHER_BOARD_DATA_UINT16[data_id][data_offset]; +} diff --git a/application/robot_cmd/CAN_receive.h b/application/robot_cmd/CAN_receive.h index 5c4d20dbcbdad37c96c35354025c328a63efc17b..cb5a52e25b7dc491b9124b979092d57331bff2da 100644 --- a/application/robot_cmd/CAN_receive.h +++ b/application/robot_cmd/CAN_receive.h @@ -11,10 +11,12 @@ * V2.0.0 Mar-27-2024 Penguin 1. 添加CAN发送函数和新的电机控制函数,解码中将CAN1 CAN2分开。 * V2.1.0 Mar-20-2024 Penguin 1. 添加DM电机的适配 * V2.2.0 May-22-2024 Penguin 1. 添加LK电机的适配 + * V2.3.0 May-22-2024 Penguin 1. 添加板间通信数据解码 * @verbatim ============================================================================== - + dm电机设置: + 为了配合本框架,请在使用上位机进行设置时,将dm电机的master id 设置为 slave id + 0x50 ============================================================================== @endverbatim ****************************(C) COPYRIGHT 2024 Polarbear**************************** @@ -71,4 +73,6 @@ extern CybergearModeState_e GetCybergearModeState(Motor_s * p_motor); extern void GetMotorMeasure(Motor_s * p_motor); +extern uint16_t GetOtherBoardDataUint16(uint8_t data_id, uint8_t data_offset); + #endif diff --git a/application/robot_param.h b/application/robot_param.h index 882bcb16acb01436b790a4c4a9406163cf45c51b..91b74c2ad27c56c1037409090b602c7fdde0ce91 100644 --- a/application/robot_param.h +++ b/application/robot_param.h @@ -25,9 +25,11 @@ #include "robot_param_penguin_mini_arm.h" // 选择机器人的各种类型 +#define __DEVELOP 0 // 开发模式 #define __DEBUG 0 // 调试模式 #define __TUNING 0 // 调参模式 #define __MUSIC_ON 0 // 开启音乐 #define __TUNING_MODE TUNING_NONE // 调参模式 +#define __SELF_BOARD_ID 2 // 本板ID #endif /* ROBOT_PARAM_H */ diff --git a/bsp/boards/bsp_can.h b/bsp/boards/bsp_can.h index 13b29f039874d1b336973974ca2a1a55446d1269..52bd538b1b75be62498a0f80b78e5d9872c91ec2 100644 --- a/bsp/boards/bsp_can.h +++ b/bsp/boards/bsp_can.h @@ -3,6 +3,11 @@ #include "struct_typedef.h" #include "stm32f4xx_hal.h" +// clang-format off +#define BOARD_DATA_ANY ((uint16_t)0x500) +#define BOARD_DATA_UINT16 ((uint16_t)0x600) +// clang-format on + typedef CAN_HandleTypeDef hcan_t; typedef struct __CanCtrlData diff --git "a/doc/\346\263\250\346\204\217\344\272\213\351\241\271.md" "b/doc/\346\263\250\346\204\217\344\272\213\351\241\271.md" index 95f1ba84de462dbfbced2f7f0e3e58f378a299b2..91ae62e14dd528acd0dd40d89e1a1a28cfbe7194 100644 --- "a/doc/\346\263\250\346\204\217\344\272\213\351\241\271.md" +++ "b/doc/\346\263\250\346\204\217\344\272\213\351\241\271.md" @@ -5,7 +5,7 @@ 本本项目采取Google的开源代码风格 ### 编译要求 -最终编译结果要做到 `0-error,0-warning` ,若审核时将对代码进行完全编译,若发现有 error 或 warning,我们将会提醒。 +最终编译结果要做到 `0-error,0-warning` ,本项目启用了 `--diag_error=warning` 在编译时将 `warning` 视作 `error`。 ## 硬件上的 **达妙电机设置零点时先失能!!!!** \ No newline at end of file