From 20156e34e8abf1df849dfb28c4bba7841dda9733 Mon Sep 17 00:00:00 2001
From: "ucart@co3050-12" <dawehr@iastate.edu>
Date: Tue, 24 Jan 2017 18:28:44 -0600
Subject: [PATCH] Debugging communication

Fixed issue with printf() hanging. Removed all printfs(). Don't use them!
Fixed issue with mismatched checksums not getting consumed
---
 quad/scripts/stress_tests.py                  | 101 ++++++++++++++++++
 quad/scripts/test_uart_comm.py                |   7 +-
 quad/sw/modular_quad_pid/.cproject            |   6 ++
 quad/sw/modular_quad_pid/src/callbacks.c      |   3 -
 quad/sw/modular_quad_pid/src/communication.c  |   4 +-
 .../modular_quad_pid/src/iic_mpu9150_utils.c  |   4 -
 quad/sw/modular_quad_pid/src/main.c           |   3 -
 quad/sw/modular_quad_pid/src/util.c           |   9 --
 8 files changed, 113 insertions(+), 24 deletions(-)
 create mode 100755 quad/scripts/stress_tests.py

diff --git a/quad/scripts/stress_tests.py b/quad/scripts/stress_tests.py
new file mode 100755
index 000000000..06f45fd6d
--- /dev/null
+++ b/quad/scripts/stress_tests.py
@@ -0,0 +1,101 @@
+#!/usr/local/bin/python3.6
+
+import sys
+from time import sleep
+
+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, 1, 0, data)
+
+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, 2, 0, b'')
+    ser.write(query_msg)
+    ser.flush()
+    
+    resp = read_packet(ser)
+    received_str = resp[:-1].decode('ascii')
+    if len(received_str) == 0:
+        print("Timed out")
+        return (-1,-1)
+    return tuple(map(int, received_str.split(',')))
+
+def check_test(ser, n_sent, size_sent, old_status):
+    new_n, new_size = query_received(ser)
+    valid = True
+    if old_status[0] + n_sent != new_n:
+        print("Failure: Expected " + str(old_status[0] + n_sent) + " messages, responded with " + str(new_n))
+        valid = False
+    if old_status[1] + size_sent != new_size:
+        print("Failure: Expected " + str(old_status[1] + size_sent) + " data, responded with " + str(new_size))
+        valid = False
+    return valid, (new_n, new_size)
+
+def test_partial_packet(ser, cur_status, size=50):
+    print("Checking partial packet...")
+    to_send = create_test_packet(size)
+    cutpoint = int(len(to_send) / 2)
+    ser.write(to_send[:cutpoint])
+    ser.flush()
+    sleep(0.2)
+    ser.write(to_send[cutpoint:])
+    sleep(0.2)
+    return check_test(ser, 1, size, cur_status)
+
+    return success, (n_received, tot_size_received)
+
+def test_blast(ser, cur_status, N=50, size=32):
+    print("Checking data blast... of size " + str(N) + " with size " + str(size))
+    to_send = create_test_packet(size)
+    for i in range(N):
+        ser.write(to_send)
+    ser.flush()
+    sleep(0.5)
+    return check_test(ser, N, N*size, cur_status)
+
+def test_bad_checksum(ser, cur_status, size=30):
+    print("Checking bad checksum...")
+    to_send = create_test_packet(size)
+    l_to_send = list(to_send)
+    l_to_send[-1] = (l_to_send[-1] + 1) % 256
+    to_send = bytes(l_to_send)
+    ser.write(to_send)
+    ser.flush()
+    return check_test(ser, 0, 0, cur_status)
+
+if __name__ == '__main__':
+    with serial.Serial('/dev/ttyUSB0', 921600, timeout=5) as ser:
+        ser.reset_input_buffer()
+        while (ser.in_waiting != 0):
+            ser.read()
+        status = query_received(ser)
+        
+        passed, status = test_partial_packet(ser, status)
+        passed, status = test_blast(ser, status)
+        passed, status = test_blast(ser, status, 150, 80)
+        passed, status = test_bad_checksum(ser, status)
+
diff --git a/quad/scripts/test_uart_comm.py b/quad/scripts/test_uart_comm.py
index 061d8303e..bd4f36c0c 100755
--- a/quad/scripts/test_uart_comm.py
+++ b/quad/scripts/test_uart_comm.py
@@ -22,7 +22,7 @@ def create_msg(main_type, subtype, msg_id, data):
 
 def create_test_packet(size=8):
     data = bytes((i % 256 for i in range(size)))
-    return create_msg(0, 2, 0, data)
+    return create_msg(0, 1, 0, data)
 
 def read_packet(ser):
     header = ser.read(7)
@@ -33,12 +33,13 @@ def read_packet(ser):
 
 def query_received(ser):
     # Send request
-    query_msg = create_msg(0, 3, 0, b'')
+    query_msg = create_msg(0, 2, 0, b'')
     ser.write(query_msg)
     ser.flush()
     
     resp = read_packet(ser)
-    received_str = resp[:-1].decode()
+    print(resp.hex())
+    received_str = resp[:-1].decode('ascii')
 
     return tuple(map(int, received_str.split(',')))
 
diff --git a/quad/sw/modular_quad_pid/.cproject b/quad/sw/modular_quad_pid/.cproject
index 86d1f2550..c203f31db 100644
--- a/quad/sw/modular_quad_pid/.cproject
+++ b/quad/sw/modular_quad_pid/.cproject
@@ -70,6 +70,9 @@
 							<tool id="xilinx.gnu.arm.size.debug.923967150" name="ARM Print Size" superClass="xilinx.gnu.arm.size.debug"/>
 						</toolChain>
 					</folderInfo>
+					<sourceEntries>
+						<entry excluding="test" flags="VALUE_WORKSPACE_PATH|RESOLVED" kind="sourcePath" name=""/>
+					</sourceEntries>
 				</configuration>
 			</storageModule>
 			<storageModule moduleId="org.eclipse.cdt.core.externalSettings"/>
@@ -143,6 +146,9 @@
 							<tool id="xilinx.gnu.arm.size.release.362029751" name="ARM Print Size" superClass="xilinx.gnu.arm.size.release"/>
 						</toolChain>
 					</folderInfo>
+					<sourceEntries>
+						<entry excluding="test" flags="VALUE_WORKSPACE_PATH|RESOLVED" kind="sourcePath" name=""/>
+					</sourceEntries>
 				</configuration>
 			</storageModule>
 			<storageModule moduleId="org.eclipse.cdt.core.externalSettings"/>
diff --git a/quad/sw/modular_quad_pid/src/callbacks.c b/quad/sw/modular_quad_pid/src/callbacks.c
index 766e3c389..a81f93792 100644
--- a/quad/sw/modular_quad_pid/src/callbacks.c
+++ b/quad/sw/modular_quad_pid/src/callbacks.c
@@ -5,7 +5,6 @@
 
 int debug(modular_structs_t *structs)
 {
-	printf("function for debug\n");
 	return 0;
 }
 
@@ -74,7 +73,6 @@ int cb_log(modular_structs_t *structs)
 {
 	size_t length;
 	unsigned char *packet = uart_buff_get_raw(&length);
-	printf("Logging: %s\n", packet);
 	return 0;
 }
 
@@ -82,7 +80,6 @@ int cb_response(modular_structs_t *structs)
 {
 	size_t length;
 	char *packet = uart_buff_get_raw(&length);
-	printf("This is the response: %s\n", packet);
 	return 0;
 }
 
diff --git a/quad/sw/modular_quad_pid/src/communication.c b/quad/sw/modular_quad_pid/src/communication.c
index 46102505d..04a5705b1 100644
--- a/quad/sw/modular_quad_pid/src/communication.c
+++ b/quad/sw/modular_quad_pid/src/communication.c
@@ -14,7 +14,7 @@
 #define MAX_PACKET_SIZE 256
 #define UART_BUF_SIZE (((BAUD_RATE * (DESIRED_USEC_PER_LOOP / 1000) / 1000) / 10) + MAX_PACKET_SIZE + 128)
 
-#define INTERRUPT_BENCHMARK
+//#define INTERRUPT_BENCHMARK
 
 // Declaration of interrupt handler
 void Handler(void *CallBackRef, u32 Event, unsigned int EventData);
@@ -88,7 +88,7 @@ int process_packet(modular_structs_t *structs) {
 	}
 	// Discard if checksum didn't match
 	if(packet_checksum != calculated_checksum) {
-		printf("Checksums did not match: 0x%02x\t0x%02x\n", packet_checksum, calculated_checksum);
+		uart_buff_consume_packet();
 		return -1;
 	}
 
diff --git a/quad/sw/modular_quad_pid/src/iic_mpu9150_utils.c b/quad/sw/modular_quad_pid/src/iic_mpu9150_utils.c
index a0611197e..d3773f271 100644
--- a/quad/sw/modular_quad_pid/src/iic_mpu9150_utils.c
+++ b/quad/sw/modular_quad_pid/src/iic_mpu9150_utils.c
@@ -45,7 +45,6 @@ int initI2C0(){
 
 	// Check if initialization was successful
 	if(status != XST_SUCCESS){
-		printf("ERROR (initI2C0): Initializing I2C0\r\n");
 		return -1;
 	}
 
@@ -86,7 +85,6 @@ int startMPU9150(){
 	// Do about 20 reads to warm up the device
 	for(i=0; i < 20; ++i){
 		if(get_gam_reading(&temp_gam) == -1){
-			printf("ERROR (startMPU9150): error occured while getting GAM data\r\n");
 			return -1;
 		}
 		usleep(1000);
@@ -108,7 +106,6 @@ void iic0Write(u8 register_addr, u8 data){
 
 	// Check if within register range
 	if(register_addr < 0 || register_addr > 0x75){
-		printf("ERROR (iic0Write) : Cannot write to register address, 0x%x: out of bounds\r\n", register_addr);
 		return;
 	}
 
@@ -127,7 +124,6 @@ void iic0Read(u8* recv_buffer, u8 register_addr, int size){
 
 	// Check if within register range
 	if(register_addr < 0 || register_addr > 0x75){
-		printf("ERROR (iic0Read): Cannot read register address, 0x%x: out of bounds\r\n", register_addr);
 	}
 
 	// Set device address to the if 0x00 <= register address <= 0x12
diff --git a/quad/sw/modular_quad_pid/src/main.c b/quad/sw/modular_quad_pid/src/main.c
index d7c232418..3dbd52a20 100644
--- a/quad/sw/modular_quad_pid/src/main.c
+++ b/quad/sw/modular_quad_pid/src/main.c
@@ -33,7 +33,6 @@ int main()
 			&(structs.user_defined_struct), &(structs.raw_actuator_struct), &(structs.actuator_command_struct));
 
 	if (init_error != 0) {
-		printf("ERROR (main): Problem initializing...Goodbye\r\n");
 		return -1;
 	}
 
@@ -42,8 +41,6 @@ int main()
 	protection_loops(&structs);
 #endif
 
-	printf("The quad loop is now beginning.\n");
-
 	// Main control loop
 	do
 	{
diff --git a/quad/sw/modular_quad_pid/src/util.c b/quad/sw/modular_quad_pid/src/util.c
index f01c044f1..c7dd33a1c 100644
--- a/quad/sw/modular_quad_pid/src/util.c
+++ b/quad/sw/modular_quad_pid/src/util.c
@@ -17,7 +17,6 @@ int pulseW = pulse_throttle_low;
  * Default period length = 2.33 ms
  */
 void pwm_init() {
-	printf("Period initialization starting\r\n");
 
 	int* pwm_0 = (int*) PWM_0_ADDR + PWM_PERIOD;
 	int* pwm_1 = (int*) PWM_1_ADDR + PWM_PERIOD;
@@ -29,19 +28,12 @@ void pwm_init() {
 	*pwm_1 = period_width;
 	*pwm_2 = period_width;
 	*pwm_3 = period_width;
-	printf("Period initialization successful %d\n", period_width);
 	// Initializes the PWM pulse lengths to be 1 ms
 	*(int*) (PWM_0_ADDR + PWM_PULSE) = pulse_throttle_low;
 	*(int*) (PWM_1_ADDR + PWM_PULSE) = pulse_throttle_low;
 	*(int*) (PWM_2_ADDR + PWM_PULSE) = pulse_throttle_low;
 	*(int*) (PWM_3_ADDR + PWM_PULSE) = pulse_throttle_low;
-	printf("Pulse initialization successful %d\n", pulse_throttle_low);
 
-#ifdef X_CONFIG
-	printf("In x config mode\n");
-#else
-	printf("In + config mode\n");
-#endif
 	usleep(1000000);
 }
 
@@ -297,7 +289,6 @@ void pwm_kill(){
 	*(int*) (PWM_1_ADDR + PWM_PULSE) = pulse_throttle_low;
 	*(int*) (PWM_2_ADDR + PWM_PULSE) = pulse_throttle_low;
 	*(int*) (PWM_3_ADDR + PWM_PULSE) = pulse_throttle_low;
-	xil_printf("Kill switch was touched, shutting off the motors and ending the program\n");
 }
 
 /**
-- 
GitLab