From a5ca1cb26aec0bfa53c66d596d1bd5fbf519d888 Mon Sep 17 00:00:00 2001 From: Brendan Bartels <bbartels@iastate.edu> Date: Sun, 22 Jan 2017 13:25:30 -0600 Subject: [PATCH] quad: finish setting up benchmarking code and scripts. - Fix one bug in uart buff size calculation --- quad/scripts/uart_comm_listen.py | 16 +++------------- quad/scripts/uart_comm_send.py | 6 +++--- quad/sw/modular_quad_pid/src/communication.c | 6 +----- quad/sw/modular_quad_pid/src/main.c | 16 ++++++++++++++++ quad/sw/modular_quad_pid/src/uart_buff.c | 20 +++++++++++++------- quad/sw/modular_quad_pid/src/uart_buff.h | 3 ++- 6 files changed, 38 insertions(+), 29 deletions(-) diff --git a/quad/scripts/uart_comm_listen.py b/quad/scripts/uart_comm_listen.py index c41209f44..0a251b5df 100755 --- a/quad/scripts/uart_comm_listen.py +++ b/quad/scripts/uart_comm_listen.py @@ -12,17 +12,6 @@ def read_packet(ser): checksum = ser.read() return data -def query_received(ser): - # Send request - query_msg = create_msg(0, 3, 0, b'') - ser.write(query_msg) - ser.flush() - - resp = read_packet(ser) - received_str = resp[:-1].decode() - - return tuple(map(int, received_str.split(','))) - if __name__ == '__main__': with serial.Serial('/dev/ttyUSB0', 921600, timeout=5) as ser: i = 0 @@ -31,8 +20,9 @@ if __name__ == '__main__': time.sleep(0.05) while ser.in_waiting != 0: resp = read_packet(ser) - elapsed = int.from_bytes(resp, byteorder='little') - print("{} {}".format(i, elapsed)) + elapsed = int.from_bytes(resp[0:3], byteorder='little') + processed = int.from_bytes(resp[4:7], byteorder='little') + print("{} {} {}".format(i, elapsed, processed)) i += 1 ser.flush() diff --git a/quad/scripts/uart_comm_send.py b/quad/scripts/uart_comm_send.py index c6528f041..d915278e8 100755 --- a/quad/scripts/uart_comm_send.py +++ b/quad/scripts/uart_comm_send.py @@ -26,8 +26,8 @@ def create_test_packet(size=8): if __name__ == '__main__': with serial.Serial('/dev/ttyUSB0', 921600, timeout=5) as ser: - for i in range(0, 1): - time.sleep(0.5) + for i in range(0, 12): ser.reset_input_buffer() ser.write(create_test_packet(8)) - ser.flush() + #ser.flush() + #time.sleep(0.5) diff --git a/quad/sw/modular_quad_pid/src/communication.c b/quad/sw/modular_quad_pid/src/communication.c index ea86526aa..c4e166b20 100644 --- a/quad/sw/modular_quad_pid/src/communication.c +++ b/quad/sw/modular_quad_pid/src/communication.c @@ -93,7 +93,7 @@ int process_packet(modular_structs_t *structs) { // Call appropriate function for packet (* (MessageTypes[meta_data.msg_type].subtypes[meta_data.msg_subtype].functionPtr))(structs); - uart_buff_flush_packet(); + uart_buff_consume_packet(); return 0; @@ -119,11 +119,7 @@ void restore_interrupts(u32 intr_state) { void process_received(modular_structs_t *structs) { // Parse as many packets as possible while (uart_buff_packet_ready()) { - u32 start = timer_get_count(); process_packet(structs); - u32 end = timer_get_count(); - u32 duration = end - start; - send_data(0, 0, 0, (char *) &duration, 4); } } diff --git a/quad/sw/modular_quad_pid/src/main.c b/quad/sw/modular_quad_pid/src/main.c index 712afda89..43a9b9dd3 100644 --- a/quad/sw/modular_quad_pid/src/main.c +++ b/quad/sw/modular_quad_pid/src/main.c @@ -17,6 +17,7 @@ #include "update_gui.h" #define BENCH_TEST +#define UART_BENCHMARK int main() { @@ -50,8 +51,23 @@ int main() timer_start_loop(); // Process all received data + +#ifdef UART_BENCHMARK + usleep(500000); + u32 start_time = timer_get_count(); +#endif + process_received(&structs); +#ifdef UART_BENCHMARK + u32 end_time = timer_get_count(); + u32 duration = end_time - start_time; + u32 packets_processed = uart_buff_packets_processed(); + u32 data[2]; + data[0] = duration; + data[1] = packets_processed; + send_data(0, 0, 0, (char *) &data, 8); +#endif #ifndef BENCH_TEST // Get the user input and put it into user_input_struct diff --git a/quad/sw/modular_quad_pid/src/uart_buff.c b/quad/sw/modular_quad_pid/src/uart_buff.c index c2b157902..a7a28e509 100644 --- a/quad/sw/modular_quad_pid/src/uart_buff.c +++ b/quad/sw/modular_quad_pid/src/uart_buff.c @@ -2,16 +2,17 @@ #include "uart_buff.h" #include <stdlib.h> -#define UART_MAX_BUFF_SIZE 20 -#define UART_MAX_PACKET_SIZE 10 +#define UART_MAX_BUFF_SIZE 2048 +#define UART_MAX_PACKET_SIZE 256 static volatile u8 buff[UART_MAX_BUFF_SIZE]; static size_t start = UART_MAX_BUFF_SIZE; static size_t packet_data_length = 0; static volatile size_t end = 0; -static int packet_start_found = 0; -static int packet_size_found = 0; -static int packet_ready = 0; +static u8 packet_start_found = 0; +static u8 packet_size_found = 0; +static u8 packet_ready = 0; +static u32 packets_processed = 0; /** * Put a byte into the buffer. @@ -172,20 +173,21 @@ float uart_buff_data_get_float(size_t offset) { * Move the start index of the buffer to the end of the current packet. * If a packet is not ready, this function does nothing. */ -void uart_buff_flush_packet() { +void uart_buff_consume_packet() { if (!packet_ready) { return; } start = uart_buff_calc_index(start + uart_buff_packet_size() + 1); uart_buff_reset_flags(); + packets_processed += 1; } /** * Return the current number of bytes held in the buffer. */ size_t uart_buff_size() { - return uart_buff_calc_index(end - start); + return uart_buff_calc_index(end - start - 1); } /** @@ -290,3 +292,7 @@ void uart_buff_print() { puts("| |"); puts("'--'"); } + +u32 uart_buff_packets_processed() { + return packets_processed; +} diff --git a/quad/sw/modular_quad_pid/src/uart_buff.h b/quad/sw/modular_quad_pid/src/uart_buff.h index 184d7cb40..58dfa0040 100644 --- a/quad/sw/modular_quad_pid/src/uart_buff.h +++ b/quad/sw/modular_quad_pid/src/uart_buff.h @@ -15,7 +15,7 @@ u8 uart_buff_data_get_u8(size_t); u16 uart_buff_data_get_u16(size_t); u32 uart_buff_data_get_u32(size_t); float uart_buff_data_get_float(size_t); -void uart_buff_flush_packet(); +void uart_buff_consume_packet(); size_t uart_buff_size(); size_t uart_buff_data_length(); size_t uart_buff_packet_size(); @@ -25,5 +25,6 @@ int uart_buff_full(); size_t uart_buff_calc_index(int); char * uart_buff_get_packet(); void uart_buff_print(); +u32 uart_buff_packets_processed(); #endif -- GitLab