From 6cf99d182d9cbd8516ffd47ef451573ae3b5402b Mon Sep 17 00:00:00 2001
From: Brendan Bartels <bbartels@iastate.edu>
Date: Thu, 20 Apr 2017 22:54:13 -0500
Subject: [PATCH] quad: add test script for logging

---
 quad/scripts/tests/test_communication.rb    |  23 +++-
 quad/scripts/tests/test_logging.rb          | 117 ++++++++++++++++++++
 quad/scripts/tests/test_memory_integrity.rb |   9 +-
 quad/scripts/tests/testing_library.rb       |  11 +-
 quad/src/virt_quad/hw_impl_unix_uart.c      |   2 +-
 5 files changed, 153 insertions(+), 9 deletions(-)
 create mode 100644 quad/scripts/tests/test_logging.rb

diff --git a/quad/scripts/tests/test_communication.rb b/quad/scripts/tests/test_communication.rb
index 5f562c48f..4887a29bc 100644
--- a/quad/scripts/tests/test_communication.rb
+++ b/quad/scripts/tests/test_communication.rb
@@ -53,7 +53,28 @@ Timeout::timeout(30) {
         send_packet [0xBE, 1, 0, 0, 0, 0, 0, 0xBF]
       }
 
-      msg = recv_packet
+      fifo = File.open(UART_TX)
+
+      # Receive the header
+      msg = []
+      for i in 1..7
+        sleep 0.0001
+        c = fifo.read(1)
+        msg.push(c)
+      end
+
+      # Receive the remaining data, according to the header specified length
+      length = msg[5..7].join().unpack("S")[0]
+
+      msg = []
+      for i in 1..length
+        sleep 0.0001
+        c = fifo.read(1)
+        msg.push(c)
+      end
+      fifo.close
+
+      msg = msg.join()
       puts msg
       assert_equal(msg.force_encoding("UTF-8"), "Packets received: #{j}")
     end
diff --git a/quad/scripts/tests/test_logging.rb b/quad/scripts/tests/test_logging.rb
new file mode 100644
index 000000000..a0e86e716
--- /dev/null
+++ b/quad/scripts/tests/test_logging.rb
@@ -0,0 +1,117 @@
+#!/usr/bin/env ruby
+
+# Logging test
+#
+# Let the quad fly for a bit, and then ensure we can
+# get its logs
+
+script_dir = File.expand_path(File.dirname(__FILE__))
+require script_dir + "/testing_library"
+
+bin_dir = script_dir + "/../../bin/"
+Dir.chdir(bin_dir)
+
+Timeout::timeout(30) {
+
+  puts("Setting up...")
+
+  # Start virtual quad
+  quad_pid = Process.spawn("./virt-quad -q",
+                           { :rlimit_as => 536870912, # 512 MiB total RAM
+                             :rlimit_stack => 1048576}) # 1 MiB stack
+
+  sleep 0.5
+
+  # Set RC switches
+  set_gear GEAR_OFF
+  set_flap FLAP_OFF
+
+  # Set initial quad orientation (flat on the ground, facing forward)
+  `./virt-quad set i2c_imu_x 0`
+  `./virt-quad set i2c_imu_y 0`
+  `./virt-quad set i2c_imu_z -1`
+  `./virt-quad set rc_roll 0.498`
+  `./virt-quad set rc_pitch 0.497`
+  `./virt-quad set rc_yaw 0.498`
+
+
+  #################
+  #  Begin Tests
+  #################
+
+  begin
+
+    puts "------------------------------------------"
+    puts "-- Beginning logging test..."
+    puts "------------------------------------------"
+
+    # Set initial quad orientation (flat on ground, facing forward)
+    `./virt-quad set i2c_imu_x 0`
+    `./virt-quad set i2c_imu_y 0`
+    `./virt-quad set i2c_imu_z -1`
+    `./virt-quad set rc_roll 0.498`
+    `./virt-quad set rc_pitch 0.497`
+    `./virt-quad set rc_yaw 0.498`
+
+    puts("Turning on GEAR...")
+    set_gear GEAR_ON
+    set_flap FLAP_ON
+    sleep 0.015
+
+    puts("Increasing Thrust to half maximum...")
+    for i in (THROTTLE_MIN..THROTTLE_MID).step(0.01)
+      set_throttle(i)
+      sleep 0.005
+    end
+
+    puts("Hovering for 3 seconds")
+    sleep 3
+
+    puts("Switching to autonomous and hovering for 3 seconds")
+    set_flap FLAP_OFF
+    sleep 3
+
+    puts("Switch back to manual, relaxing thrust to zero")
+    set_flap FLAP_ON
+    i = THROTTLE_MID
+    while i > THROTTLE_MIN
+      i -= 0.01
+      set_throttle(i)
+      sleep 0.005
+    end
+
+    # Get logs
+
+    Thread.new {
+      sleep 0.5
+      puts("Swiching off GEAR...")
+      set_gear GEAR_OFF
+    }
+
+    logs = []
+
+    begin
+      while true
+        logs.push(recv_packet)
+      end
+    rescue Timeout::Error
+      puts "No logs left"
+    end
+
+    if logs.length == 2
+      log_data = logs[1].split("\n")
+      for data in log_data
+        puts data
+      end
+      p log_data.length
+    end
+
+    puts "------------------------------------------"
+
+  ensure
+
+    Process.kill(9, quad_pid)
+    Process.wait(quad_pid)
+
+  end
+}
diff --git a/quad/scripts/tests/test_memory_integrity.rb b/quad/scripts/tests/test_memory_integrity.rb
index 31de3a763..d7116c1d4 100644
--- a/quad/scripts/tests/test_memory_integrity.rb
+++ b/quad/scripts/tests/test_memory_integrity.rb
@@ -52,11 +52,11 @@ begin
   sleep 3
 
   puts("Switching to autonomous and hovering for 3 seconds")
-  set_flap FLAP_ON
+  set_flap FLAP_OFF
   sleep 3
 
   puts("Switch back to manual, relaxing thrust to zero")
-  set_flap FLAP_OFF
+  set_flap FLAP_ON
   i = THROTTLE_MID
   while i > THROTTLE_MIN
     i -= 0.01
@@ -67,11 +67,14 @@ begin
   puts("Swiching off GEAR...")
   set_gear GEAR_OFF
 
+  sleep 3
+
+  set_throttle THROTTLE_MIN
+
   puts("Flight ended successfully.");
 ensure
 
   Process.kill(2, quad)
-
   sleep 1 # wait for valgrind to write to file
 
   # Process the valgrind file
diff --git a/quad/scripts/tests/testing_library.rb b/quad/scripts/tests/testing_library.rb
index 524ef76ca..aad1ba856 100644
--- a/quad/scripts/tests/testing_library.rb
+++ b/quad/scripts/tests/testing_library.rb
@@ -72,22 +72,25 @@ def send_packet(bytes)
 end
 
 def recv_packet
-  fifo = File.open(UART_TX)
+  fifo = nil
+  Timeout::timeout(5) {
+    fifo = File.open(UART_TX)
+  }
 
   # Receive the header
   msg = []
   for i in 1..7
-    sleep 0.010
     c = fifo.read(1)
     msg.push(c)
   end
 
   # Receive the remaining data, according to the header specified length
   length = msg[5..7].join().unpack("S")[0]
+
   msg = []
   for i in 1..length
-    sleep 0.010
-    msg.push(fifo.read(1))
+    c = fifo.read(1)
+    msg.push(c)
   end
   fifo.close
 
diff --git a/quad/src/virt_quad/hw_impl_unix_uart.c b/quad/src/virt_quad/hw_impl_unix_uart.c
index e39fbedfc..e750b2972 100644
--- a/quad/src/virt_quad/hw_impl_unix_uart.c
+++ b/quad/src/virt_quad/hw_impl_unix_uart.c
@@ -30,8 +30,8 @@ int unix_uart_write(struct UARTDriver *self, unsigned char c) {
   if (fifo >= 0) {
     printf("%s: %x\n", "uart-tx", c);
     write(fifo, &c, 1);
+    close(fifo);
   }
-  close(fifo);
   return 0;
 }
 
-- 
GitLab