Skip to content

Commit

Permalink
Modified the communication between proc_mcu and ctrl_mcu
Browse files Browse the repository at this point in the history
  • Loading branch information
166256 committed Apr 13, 2022
1 parent ccb9953 commit b70f66f
Show file tree
Hide file tree
Showing 10 changed files with 1,038 additions and 968 deletions.
4 changes: 2 additions & 2 deletions program/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,8 @@ add_compile_options(-mcpu=cortex-m3 -mthumb -mthumb-interwork)
add_compile_options(-ffunction-sections -fdata-sections -fno-common -fmessage-length=0)

message(STATUS "Optimization for speed")
#add_compile_options(-O0)
add_compile_options(-O2)
add_compile_options(-O0)
#add_compile_options(-O2)

#add_definitions(-DUSE_HAL_DRIVER)

Expand Down
55 changes: 28 additions & 27 deletions program/ctrl_mcu/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,12 @@

#include "main.h"

extern unsigned int uart6_dma_buffer_1[PROC_MCU_SEND_AMOUNT];
extern unsigned int uart6_dma_buffer_2[PROC_MCU_SEND_AMOUNT];
volatile short angle = 150;
volatile unsigned short speed = 0;
unsigned short playground_ind = 0;

extern unsigned int uart6_dma_buffer_1[CTRL_MCU_RECEIVE_AMOUNT];
extern unsigned int uart6_dma_buffer_2[CTRL_MCU_RECEIVE_AMOUNT];

unsigned int uart7_dma_send_buffer[UART7_DMA_SEND_BUFFER] = {0};

Expand All @@ -19,8 +23,8 @@ int main(void) {
uart1_config();
uart6_config();
uart6_dma_nvic_config();
uart6_dma_receive_config(uart6_dma_buffer_1, PROC_MCU_SEND_AMOUNT);
uart6_dma_set_transmit_buffer(uart6_dma_buffer_1, PROC_MCU_SEND_AMOUNT);
uart6_dma_receive_config(uart6_dma_buffer_1, CTRL_MCU_RECEIVE_AMOUNT);
uart6_dma_set_transmit_buffer(uart6_dma_buffer_1, CTRL_MCU_RECEIVE_AMOUNT);
uart7_config();
// uart7_dma_nvic_config();
// uart7_dma_receive_config(uart7_dma_receive_buffer, UART7_DMA_RECEIVE_BUFFER);
Expand Down Expand Up @@ -72,33 +76,30 @@ int main(void) {
// delayms(1000);
// }

volatile unsigned short angle = 150;
volatile unsigned short speed = 0;

unsigned char counter = 0;
while (1) {
// 前,左是 0~32766
angle = 200 - control_signal.joystick_x / 655;
if (control_signal.joystick_y > 32766)
speed = 2000;
else
speed = 2000 + (32767 - control_signal.joystick_y) * 3000 / 32766;
// if (counter == 20) {
// angle = 100;
// } else if (counter == 40) {
// angle = 200;
// }
// counter++;
// if (counter == 50) {
// if (speed <= 3000)
// speed = 6000;
// else if (speed >= 6000)
// speed = 3000;
// counter = 0;
// }
// angle = 200 - control_signal.joystick_x / 655;
// if (control_signal.joystick_y > 32766)
// speed = 2000;
// else
// speed = 2000 + (32767 - control_signal.joystick_y) * 3000 / 32766;
if (proc_data.distance_east != 0) {
if (playground_ind < 837)
playground_ind =
dichotomy(((playground_ind - 2) <= 0) ? 0 : (playground_ind - 2),
(playground_ind + INDEX_OFFSET > 837) ? 837 : (playground_ind + INDEX_OFFSET));

lqr_control(playground_ind);
WRITE_REG(TIM3->CCR1, angle);
}

WRITE_REG(TIM3->CCR1, angle);
sdtp_data_transmit_speed(speed, uart7_dma_send_buffer);
uart7_dma_set_send_buffer(uart7_dma_send_buffer, UART7_DMA_SEND_BUFFER);

// sdtp_data_transmit_speed(speed, uart7_dma_send_buffer);
// uart7_dma_set_send_buffer(uart7_dma_send_buffer, UART7_DMA_SEND_BUFFER);
delayms(10);
// printf("%.3f, %.3f", proc_data.distance_north, proc_data.distance_east);

}
}
10 changes: 6 additions & 4 deletions program/ctrl_mcu/mm32f3x_it.c
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ void TIM2_IRQHandler(void) {
debugger_scan_variable(global_time_stamp);
}

/* LQR控制 */

void TIM4_IRQHandler(void) {
TIM_ClearITPendingBit(TIM4, TIM_IT_Update);

Expand All @@ -48,20 +50,20 @@ void UART7_IRQHandler(void) {

typedef enum { buffer_no_1 = 1, buffer_no_2 = 2 } buffer_no;
static buffer_no uart6_free_buffer_no = buffer_no_1;
unsigned int uart6_dma_buffer_1[PROC_MCU_SEND_AMOUNT];
unsigned int uart6_dma_buffer_2[PROC_MCU_SEND_AMOUNT];
unsigned int uart6_dma_buffer_1[CTRL_MCU_RECEIVE_AMOUNT];
unsigned int uart6_dma_buffer_2[CTRL_MCU_RECEIVE_AMOUNT];
void DMA1_Channel1_IRQHandler(void) {
if (DMA_GetITStatus(DMA1_IT_TC1)) {
/* Clear all interrupt flags */
DMA_ClearITPendingBit(DMA1_IT_TC1);

/* Double ping pong buffer */
if (uart6_free_buffer_no == buffer_no_1) {
uart6_dma_set_transmit_buffer(uart6_dma_buffer_2, PROC_MCU_SEND_AMOUNT);
uart6_dma_set_transmit_buffer(uart6_dma_buffer_2, CTRL_MCU_RECEIVE_AMOUNT);
uart6_free_buffer_no = buffer_no_2;
deal_uart6_dma_proc(uart6_dma_buffer_1);
} else {
uart6_dma_set_transmit_buffer(uart6_dma_buffer_1, PROC_MCU_SEND_AMOUNT);
uart6_dma_set_transmit_buffer(uart6_dma_buffer_1, CTRL_MCU_RECEIVE_AMOUNT);
uart6_free_buffer_no = buffer_no_1;
deal_uart6_dma_proc(uart6_dma_buffer_2); // 解包,从proc传过来的,双乒乓缓冲区有时会导致乱
}
Expand Down
6 changes: 3 additions & 3 deletions program/device/sensor_decode.c
Original file line number Diff line number Diff line change
Expand Up @@ -48,8 +48,8 @@ void precossing_proc_to_control(unsigned int packets[PROC_MCU_SEND_AMOUNT], cons
void unpacking_proc_to_control(unsigned int packets[PROC_MCU_SEND_AMOUNT - 2]) {
unsigned int temp;
short checksum = verification_crc8((unsigned int *) packets, 13);
small_packets.checksum = (short) packets[13];
if (checksum != small_packets.checksum) return;
proc_data.checksum = (short) packets[13];
if (checksum != proc_data.checksum) return;
// 调整位恢复原始数据,和校验的顺序不能换,因为封包的时候是先算校验,再计算调整位
for (unsigned char i = 0; i < 12; ++i)
if (packets[12] & (0x80 >> i))
Expand Down Expand Up @@ -128,7 +128,7 @@ static unsigned char package_counter = 0;
//static unsigned char package_counter_1 = 0;

void deal_uart6_dma_proc(const unsigned int *p) {
for (unsigned char counter = 0; counter < PROC_MCU_SEND_AMOUNT; ++counter) {
for (unsigned char counter = 0; counter < CTRL_MCU_RECEIVE_AMOUNT; ++counter) {
switch (status) {
case 0:if (p[counter] == 0xff) status = 1;
break;
Expand Down
2 changes: 2 additions & 0 deletions program/device/sensor_decode.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@

#define READ_MCU_AMOUNT 158
#define PROC_MCU_SEND_AMOUNT 16
#define CTRL_MCU_RECEIVE_AMOUNT 48
#define SHORT_SPLIT_CHAR(x, y) packets[x] = ((buffer[y] & 0x0000ff00) >> 8);\
packets[(x)+1] = (buffer[y] & 0x000000ff);
#define INT_SPLIT_CHAR(x, y) packets[x] = ((buffer[y] & 0x00ff0000) >> 16);\
Expand Down Expand Up @@ -71,6 +72,7 @@ typedef struct {
float distance_north; // 先发北再发东
float distance_east;
float north_angle;
short checksum;
} decode_proc;

extern decode_fixed small_packets;
Expand Down
Loading

0 comments on commit b70f66f

Please sign in to comment.