From d56405a1bacc3a81acc6c0a988de7dd89433c4bb Mon Sep 17 00:00:00 2001 From: ws <1621320660@qq.com> Date: Fri, 14 Mar 2025 20:44:39 +0800 Subject: [PATCH] =?UTF-8?q?=E8=BF=99=E4=B8=AAspi=E8=AF=BB=E7=BC=96?= =?UTF-8?q?=E7=A0=81=E5=99=A8=E7=BB=9D=E5=AF=B9=E5=A5=BD=E4=BD=BF?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Core/Src/usart.c | 2 +- MDK-ARM/.vscode/keil-assistant.log | 10 +++++ MDK-ARM/.vscode/settings.json | 3 +- MDK-ARM/.vscode/uv4.log | 13 ++++-- MDK-ARM/.vscode/uv4.log.lock | 2 +- MDK-ARM/mycode1.uvoptx | 39 +++++++++++++++++- User/device/read_spi.c | 64 +++++++++++++----------------- User/device/read_spi.h | 15 ++++++- User/module/odrive_shoot.c | 28 ++++++------- User/module/shoot.c | 39 ++++++++++++------ User/module/shoot.h | 1 + User/task/go_task.c | 4 +- User/task/led_task.c | 2 +- User/task/shoot_task.c | 18 ++++++--- 14 files changed, 160 insertions(+), 80 deletions(-) diff --git a/Core/Src/usart.c b/Core/Src/usart.c index 48ece78..cee4f04 100644 --- a/Core/Src/usart.c +++ b/Core/Src/usart.c @@ -102,7 +102,7 @@ void MX_USART6_UART_Init(void) /* USER CODE END USART6_Init 1 */ huart6.Instance = USART6; - huart6.Init.BaudRate = 4000000; + huart6.Init.BaudRate = 115200; huart6.Init.WordLength = UART_WORDLENGTH_8B; huart6.Init.StopBits = UART_STOPBITS_1; huart6.Init.Parity = UART_PARITY_NONE; diff --git a/MDK-ARM/.vscode/keil-assistant.log b/MDK-ARM/.vscode/keil-assistant.log index f82290b..6fc5bab 100644 --- a/MDK-ARM/.vscode/keil-assistant.log +++ b/MDK-ARM/.vscode/keil-assistant.log @@ -48,3 +48,13 @@ [info] Log at : 2025/3/13|14:46:01|GMT+0800 +[info] Log at : 2025/3/13|19:24:02|GMT+0800 + +[info] Log at : 2025/3/13|21:58:39|GMT+0800 + +[info] Log at : 2025/3/14|16:14:48|GMT+0800 + +[info] Log at : 2025/3/14|16:14:59|GMT+0800 + +[info] Log at : 2025/3/14|20:12:19|GMT+0800 + diff --git a/MDK-ARM/.vscode/settings.json b/MDK-ARM/.vscode/settings.json index 3e1fb17..c5bcc85 100644 --- a/MDK-ARM/.vscode/settings.json +++ b/MDK-ARM/.vscode/settings.json @@ -16,6 +16,7 @@ "ball.h": "c", "shoot.h": "c", "dji.h": "c", - "calc_lib.h": "c" + "calc_lib.h": "c", + "odrive_shoot.h": "c" } } \ No newline at end of file diff --git a/MDK-ARM/.vscode/uv4.log b/MDK-ARM/.vscode/uv4.log index 2a2628d..202c86c 100644 --- a/MDK-ARM/.vscode/uv4.log +++ b/MDK-ARM/.vscode/uv4.log @@ -1,3 +1,10 @@ -Load "mycode1\\mycode1.axf" -Erase Done.Programming Done.Verify OK.Application running ... -Flash Load finished at 19:18:06 +*** Using Compiler 'V5.06 update 7 (build 960)', folder: 'D:\keil\ARM\ARMCC\Bin' +Build target 'mycode1' +compiling read_spi.c... +compiling odrive_shoot.c... +compiling shoot_task.c... +linking... +Program Size: Code=26528 RO-data=1904 RW-data=280 ZI-data=22496 +FromELF: creating hex file... +"mycode1\mycode1.axf" - 0 Error(s), 0 Warning(s). +Build Time Elapsed: 00:00:03 diff --git a/MDK-ARM/.vscode/uv4.log.lock b/MDK-ARM/.vscode/uv4.log.lock index 13666e4..f7c9ebe 100644 --- a/MDK-ARM/.vscode/uv4.log.lock +++ b/MDK-ARM/.vscode/uv4.log.lock @@ -1 +1 @@ -2025/3/13 19:18:07 \ No newline at end of file +2025/3/14 20:41:26 \ No newline at end of file diff --git a/MDK-ARM/mycode1.uvoptx b/MDK-ARM/mycode1.uvoptx index b4bc059..84f2bfa 100644 --- a/MDK-ARM/mycode1.uvoptx +++ b/MDK-ARM/mycode1.uvoptx @@ -205,6 +205,41 @@ 1 basketball,0x0A + + 11 + 1 + angleo,0x0A + + + 12 + 1 + shoot + + + 13 + 1 + multi_turn_angle,0x0A + + + 14 + 1 + angle_encoder + + + 15 + 1 + back_angle,0x0A + + + 16 + 1 + encoder,0x0A + + + 17 + 1 + angles + @@ -287,7 +322,7 @@ Application/User/Core - 0 + 1 0 0 0 @@ -1083,7 +1118,7 @@ User/task - 0 + 1 0 0 0 diff --git a/User/device/read_spi.c b/User/device/read_spi.c index e52e0ae..c4beb10 100644 --- a/User/device/read_spi.c +++ b/User/device/read_spi.c @@ -40,41 +40,31 @@ uint16_t AS5047_read(uint16_t add) return data; } -// void Update_MultiTurn_Angle(void) -// { -// uint16_t current_angle = AS5047_read(ANGLEUNC); -// int16_t angle_diff = current_angle - last_angle; - -// // 检测角度跳变 -// if (angle_diff > 8192) // 角度从低到高跳变 -// { -// multi_turn_angle -= (16384 - angle_diff); -// } -// else if (angle_diff < -8192) // 角度从高到低跳变 -// { -// multi_turn_angle += (16384 + angle_diff); -// } -// else -// { -// multi_turn_angle += angle_diff; -// } - -// last_angle = current_angle; -// } -void Update_MultiTurn_Angle(void) -{ - uint16_t current_angle = AS5047_read(ANGLEUNC); - int16_t angle_diff = current_angle - last_angle; - - // 检测角度跳变 - if (angle_diff > 8192) // 角度从低到高跳变 - { - multi_turn_angle -= 1; - } - else if (angle_diff < -8192) // 角度从高到低跳变 - { - multi_turn_angle += 1; - } - - last_angle = current_angle; +void Encoder_Init(Encoder_t *ptr) { + ptr->round_cnt = 0; + ptr->ecd = AS5047_read(ANGLEUNC); + ptr->last_ecd = ptr->ecd; + ptr->offset_ecd = ptr->ecd; + ptr->total_angle = 0.0; +} + +void Update_Encoder(Encoder_t *ptr) { + ptr->ecd = AS5047_read(ANGLEUNC); + + if (ptr->ecd - ptr->last_ecd > 4096) { + ptr->round_cnt--; + } else if (ptr->ecd - ptr->last_ecd < -4096) { + ptr->round_cnt++; + } + + ptr->total_angle = (double)((ptr->round_cnt * 8192 + ptr->ecd - ptr->offset_ecd) ); + ptr->last_ecd = ptr->ecd; +} + +int32_t Get_Encoder_Rounds(Encoder_t *ptr) { + return ptr->round_cnt; +} + +double Get_Encoder_Angle(Encoder_t *ptr) { + return ptr->total_angle; } diff --git a/User/device/read_spi.h b/User/device/read_spi.h index 6ddab85..1f3674a 100644 --- a/User/device/read_spi.h +++ b/User/device/read_spi.h @@ -18,6 +18,14 @@ #define SETTINGS1 0x0018 #define SETTINGS2 0x0019 +typedef struct { + int32_t round_cnt; + uint16_t ecd; + uint16_t last_ecd; + uint16_t offset_ecd; + double total_angle; +} Encoder_t; + #define AS5407P_CS_H() \ HAL_GPIO_WritePin(SPI_CS_GPIO_Port, SPI_CS_Pin, GPIO_PIN_SET) #define AS5407P_CS_L() \ @@ -26,6 +34,11 @@ uint16_t Parity_bit_Calculate(uint16_t data_2_cal); uint16_t SPI_ReadWrite_OneByte(uint16_t _txdata); uint16_t AS5047_read(uint16_t add); - void Update_MultiTurn_Angle(void); + + + void Encoder_Init(Encoder_t *ptr); + void Update_Encoder(Encoder_t *ptr); + int32_t Get_Encoder_Rounds(Encoder_t *ptr); + double Get_Encoder_Angle(Encoder_t *ptr); #endif diff --git a/User/module/odrive_shoot.c b/User/module/odrive_shoot.c index d9a3b68..59b3c0f 100644 --- a/User/module/odrive_shoot.c +++ b/User/module/odrive_shoot.c @@ -4,6 +4,8 @@ #include #include "read_spi.h" +#include "vofa.h" + extern RC_mess_t RC_mess; extern float angle_encoder; @@ -12,31 +14,28 @@ extern volatile uint16_t last_angle ; #define POS_SET -10.6 #define POS_ZERO 2.705 -#define ACCEL_SEND 10//梯形加速度 -#define ACCEL_BACK 5//返回时的加速度 +#define ACCEL_SEND 100//梯形加速度 +#define ACCEL_BACK 100//返回时的加速度 #define DELAY 1000 -fp32 shoot = 10; - +fp32 shoot = 0; +fp32 back_angle = 0; +float vofa_spi[8]; void shootStep(void) { - - angle_encoder=AS5047_read(ANGLEUNC); - Update_MultiTurn_Angle(); + // back_angle=Get_Angle(); + // angle_encoder=AS5047_read(ANGLEUNC); + // Update_MultiTurn_Angle(); odrive_accel_cmd(AXIS0_NODE,ACCEL_SEND,ACCEL_SEND); // osDelay(2);//不知道为什么要延时 odrive_pos_cmd(AXIS0_NODE,shoot); - -// osDelay(5000); -// odrive_accel_cmd(AXIS0_NODE,5,5); -// odrive_pos_cmd(AXIS0_NODE,10); - // osDelay(DELAY); -// odrive_accel_cmd(AXIS1_NODE,ACCEL_BACK,ACCEL_BACK); -// odrive_pos_cmd(AXIS1_NODE,POS_ZERO); + // vofa_spi[0]=shoot; + // vofa_spi[0]=multi_turn_angle; + // vofa_tx_main(vofa_spi); } @@ -45,5 +44,6 @@ void shootBack(void) odrive_accel_cmd(AXIS0_NODE,ACCEL_BACK,ACCEL_BACK); osDelay(2); odrive_pos_cmd(AXIS0_NODE,POS_ZERO); + } diff --git a/User/module/shoot.c b/User/module/shoot.c index 02dc1ca..c2c01d4 100644 --- a/User/module/shoot.c +++ b/User/module/shoot.c @@ -5,8 +5,8 @@ extern RC_mess_t RC_mess; extern motor_measure_t *trigger_motor_data;//3508电机数据 -#define GO1_SHOOT 1 -#define ODRIVE_SHOOT 0 +#define GO1_SHOOT 0 +#define ODRIVE_SHOOT 1 #define KP 0.12 #define KD 0.008 @@ -59,18 +59,33 @@ void shoot_ball(int key) #elif ODRIVE_SHOOT == 1 +//初始位置在最上面, 有球放过来, 电机转动到最下面等待发射 +//发射指令,当到最高点时,扳机离合 +//返回扳机,远动到最下面 -void shoot_odrive(void) +extern float angle_encoder; +extern volatile int32_t multi_turn_angle ; +extern volatile uint16_t last_angle ; + +void shoot_odrive(int angle) { - //扳机 - if(trigger_motor_data->real_angle == 60) - { - trigger_pos(0); - } - else - { - trigger_pos(60); - } + // if(ball) + // { + // if(multi_turn_angle==8) + // { + // //di + // odrive_accel_cmd(AXIS0_NODE,200,200); + // odrive_pos_cmd(AXIS0_NODE,0); + // } + // else if(multi_turn_angle==0) + // { + + // } + // } + odrive_accel_cmd(AXIS0_NODE,200,200); + osDelay(2); + odrive_pos_cmd(AXIS0_NODE,angle); + } #endif diff --git a/User/module/shoot.h b/User/module/shoot.h index 497a4ca..707140d 100644 --- a/User/module/shoot.h +++ b/User/module/shoot.h @@ -22,5 +22,6 @@ typedef struct void shooterInit(void); void shoot_ball(int key); +void shoot_odrive(int angle); #endif diff --git a/User/task/go_task.c b/User/task/go_task.c index ed508f7..5137263 100644 --- a/User/task/go_task.c +++ b/User/task/go_task.c @@ -16,14 +16,14 @@ void Task_Go(void *argument) #if GOUSE==1 //HAL_Delay(2000); - gimbalInit(); +// gimbalInit(); //HAL_GPIO_WritePin(ledGreen_GPIO_Port,ledGreen_Pin,GPIO_PIN_RESET); while(1) { LED_green(); - gimbalFlow(); + // gimbalFlow(); osDelay(1); } #else diff --git a/User/task/led_task.c b/User/task/led_task.c index 4cf90ba..bc26e8c 100644 --- a/User/task/led_task.c +++ b/User/task/led_task.c @@ -12,7 +12,7 @@ void Task_Led(void *argument) { // LED_red(); // LED_bule(); - see_you_again(); + // see_you_again(); // LED_green(); // osDelay(500); diff --git a/User/task/shoot_task.c b/User/task/shoot_task.c index 4ad88fa..cc70c62 100644 --- a/User/task/shoot_task.c +++ b/User/task/shoot_task.c @@ -5,16 +5,22 @@ #include #include "remote_control.h" #include "shoot.h" +#include "odrive_shoot.h" +#include "read_spi.h" extern RC_mess_t RC_mess; - -#define GO1_SHOOT 1 -#define ODRIVE_SHOOT 0 +Encoder_t encoder; +double angles; +int32_t rounds; +int angleo = 0; //发射角度 +#define GO1_SHOOT 0 +#define ODRIVE_SHOOT 1 void Task_Shoot(void *argument) { (void)argument; /* 未使用argument,消除警告 */ const uint32_t delay_tick = osKernelGetTickFreq() / TASK_FREQ_CAN; + Encoder_Init(&encoder); #if GO1_SHOOT == 1 shooterInit(); #endif @@ -28,9 +34,11 @@ void Task_Shoot(void *argument) shoot_ball(0); #elif ODRIVE_SHOOT == 1 - shootStep(); + shootStep(); + //shoot_odrive(angleo); #endif - + // 更新编码器数据 + Update_Encoder(&encoder); tick += delay_tick; /* 计算下一个唤醒时刻 */ osDelayUntil(tick); /* 运行结束,等待下一个周期唤醒 */