diff --git a/quad/scripts/test_uart_comm.py b/quad/scripts/test_uart_comm.py index 324f73b1f7dde43b0701ee57ab19c759751cf41a..c3e2fef063aa5926dd63708e1dc7d338f322cd3f 100755 --- a/quad/scripts/test_uart_comm.py +++ b/quad/scripts/test_uart_comm.py @@ -1,9 +1,8 @@ -#!/usr/bin/python +#!/usr/local/bin/python3.6 import sys import time -print(sys.version_info) import serial def create_msg(main_type, subtype, msg_id, data): @@ -46,9 +45,9 @@ def query_received(ser): if __name__ == '__main__': with serial.Serial('/dev/ttyUSB0', 921600, timeout=5) as ser: ser.reset_input_buffer() - ser.write(create_test_packet(40)) - time.sleep(0.05) - #while ser.in_waiting != 0: + ser.write(create_test_packet(8)) + # time.sleep(0.05) + # while ser.in_waiting != 0: # resp = read_packet(ser) # elapsed = int.from_bytes(resp, byteorder='little') # print(f"Took {elapsed} cycles, {elapsed / 100} us") diff --git a/quad/scripts/uart_comm_listen.py b/quad/scripts/uart_comm_listen.py new file mode 100755 index 0000000000000000000000000000000000000000..c41209f442caab84c26c7aefb55410022a158588 --- /dev/null +++ b/quad/scripts/uart_comm_listen.py @@ -0,0 +1,38 @@ +#!/usr/local/bin/python3.6 + +import sys +import time + +import serial + +def read_packet(ser): + header = ser.read(7) + length = int.from_bytes(header[5:7], byteorder='little') + data = ser.read(length) + 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 + while True: + ser.reset_input_buffer() + time.sleep(0.05) + while ser.in_waiting != 0: + resp = read_packet(ser) + elapsed = int.from_bytes(resp, byteorder='little') + print("{} {}".format(i, elapsed)) + i += 1 + ser.flush() + diff --git a/quad/scripts/uart_comm_send.py b/quad/scripts/uart_comm_send.py new file mode 100755 index 0000000000000000000000000000000000000000..c6528f041694d8f7fcdb7a38217bd2331bac727a --- /dev/null +++ b/quad/scripts/uart_comm_send.py @@ -0,0 +1,33 @@ +#!/usr/local/bin/python3.6 + +import sys +import time + +import serial + +def create_msg(main_type, subtype, msg_id, data): + msg = bytes() + msg += b'\xBE' + msg += main_type.to_bytes(1, 'little') + msg += subtype.to_bytes(1, 'little') + msg += msg_id.to_bytes(2, 'little') + msg += len(data).to_bytes(2, 'little') + msg += data + + checksum = 0 + for b in msg: + checksum ^= b + msg += checksum.to_bytes(1, 'little') + return msg + +def create_test_packet(size=8): + data = bytes((i % 256 for i in range(size))) + return create_msg(0, 2, 0, data) + +if __name__ == '__main__': + with serial.Serial('/dev/ttyUSB0', 921600, timeout=5) as ser: + for i in range(0, 1): + time.sleep(0.5) + ser.reset_input_buffer() + ser.write(create_test_packet(8)) + ser.flush() diff --git a/quad/sw/modular_quad_pid/.gitignore b/quad/sw/modular_quad_pid/.gitignore index 06a1484bb54b84d2fb2054a167cd183be528bfa7..e09df2b7841f50ade27e462a4a8c87ecd4b230f0 100644 --- a/quad/sw/modular_quad_pid/.gitignore +++ b/quad/sw/modular_quad_pid/.gitignore @@ -1,2 +1,3 @@ Debug/ +Release/ bootimage/ diff --git a/quad/sw/modular_quad_pid/src/communication.c b/quad/sw/modular_quad_pid/src/communication.c index 2a428909adfd53c1ccad7564b9a6b7ebbb3cb7d7..ea86526aa05632dd408ab32fc32749c8a9cdc7e2 100644 --- a/quad/sw/modular_quad_pid/src/communication.c +++ b/quad/sw/modular_quad_pid/src/communication.c @@ -119,7 +119,11 @@ 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 6b6d9e0735425e1ee8e37888f9d30c2765dc2644..712afda89d621ea65b1c3d2ecc05a3954f7e1b44 100644 --- a/quad/sw/modular_quad_pid/src/main.c +++ b/quad/sw/modular_quad_pid/src/main.c @@ -52,6 +52,7 @@ int main() // Process all received data process_received(&structs); + #ifndef BENCH_TEST // Get the user input and put it into user_input_struct get_user_input(&(structs.log_struct), &(structs.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 fab7e464f6a7f49d4edff46c6678d3e2b2ff5f55..c2b1579021cdc3369cac3984fa82fe0935c1ea38 100644 --- a/quad/sw/modular_quad_pid/src/uart_buff.c +++ b/quad/sw/modular_quad_pid/src/uart_buff.c @@ -2,10 +2,10 @@ #include "uart_buff.h" #include <stdlib.h> -#define UART_MAX_BUFF_SIZE 2048 -#define UART_MAX_PACKET_SIZE 256 +#define UART_MAX_BUFF_SIZE 20 +#define UART_MAX_PACKET_SIZE 10 -static volatile unsigned char buff[UART_MAX_BUFF_SIZE]; +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; @@ -97,12 +97,12 @@ u8 uart_buff_get_u8(size_t offset) { } /** - * Retrieve a 16-bit from the buffer according to the given offset with respect - * to the start index of the buffer. + * Retrieve a 16-bit unsigned integer from the buffer according to the given + * offset with respect to the start index of the buffer. */ u16 uart_buff_get_u16(size_t offset) { - return uart_buff_data_get_u8(offset + 1) << 8 | - uart_buff_data_get_u8(offset); + return uart_buff_get_u8(offset + 1) << 8 | + uart_buff_get_u8(offset); } /** @@ -110,10 +110,10 @@ u16 uart_buff_get_u16(size_t offset) { * to the start index of the buffer. */ u32 uart_buff_get_u32(size_t offset) { - return uart_buff_data_get_u8(offset + 3) << 24 | - uart_buff_data_get_u8(offset + 2) << 16 | - uart_buff_data_get_u8(offset + 1) << 8 | - uart_buff_data_get_u8(offset); + return uart_buff_get_u8(offset + 3) << 24 | + uart_buff_get_u8(offset + 2) << 16 | + uart_buff_get_u8(offset + 1) << 8 | + uart_buff_get_u8(offset); } /** @@ -151,7 +151,7 @@ u32 uart_buff_data_get_u32(size_t offset) { /** * Retrieve a 32-bit floating point number from the buffer according to the - * given offset with respect to the start of the data portion of the current + * given offset with respect to the start of the data portion of the current * packet. * * This has undefined behavior if a packet is not ready. diff --git a/quad/sw/modular_quad_pid/src/uart_buff.h b/quad/sw/modular_quad_pid/src/uart_buff.h index cdf4298f2018bb80de6a169d131338cf3baca13b..184d7cb40edd5d0ccc2102e514d8384396a3b82b 100644 --- a/quad/sw/modular_quad_pid/src/uart_buff.h +++ b/quad/sw/modular_quad_pid/src/uart_buff.h @@ -26,5 +26,4 @@ size_t uart_buff_calc_index(int); char * uart_buff_get_packet(); void uart_buff_print(); - #endif