Skip to content
Snippets Groups Projects
test_safety_checks.rb 2.45 KiB
Newer Older
#!/usr/bin/env ruby

# Safety Checks
#
# Startup the virtual quad and make sure it doesn't allow combinations of things
# that could hurt people.

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...")
  sleep 1

  # Start virtual quad
  quad_pid = Process.spawn("./virt-quad -q",
                           { :rlimit_as => 536870912, # 512 MiB total RAM
                             :rlimit_stack => 1048576}) # 1 MiB stack

  #################
  #  Begin Tests
  #################
    puts("Beginning tests...")
    # Set gravity
    File.write(I2C_MPU_ACCEL_Z, -1 * GRAVITY)
    puts("Check that motors are off at startup")
    check_motors_are_off

    puts("Check that LED is off at startup")
    check_led(0)

    puts("Check that increasing the throttle does nothing to motors")
    # (because gear is still off)
    for i in (THROTTLE_MIN..THROTTLE_MAX).step(1000)
      File.write(THROTTLE, i)
      check_motors_are_off
      sleep 0.005
    end

    puts("Check that flipping gear to 1 while throttle is high does nothing")
    # (motors should still be off, LED should still be off)
    File.write(GEAR, GEAR_ON)
    sleep 0.015
    check_motors_are_off
    i = THROTTLE_MAX
    while i > THROTTLE_MID
      i -= 1000
      File.write(THROTTLE, i)
      check_motors_are_off
      check_led 0
      sleep 0.005
    end

    # (swtich GEAR back to off and bring throttle off)
    File.write(GEAR, GEAR_OFF)
    File.write(THROTTLE, THROTTLE_MIN)

    puts("Check that the LED turns on when gear is flipped on")
    # (motors should still be off because our throttle is low)
    File.write(GEAR, GEAR_ON)
    check_motors_are_off
    check_led 1

    puts("Check that motors turn on")
    File.write(THROTTLE, THROTTLE_MID)
    averages = get_motor_averages
    average = (averages[0] + averages[1] + averages[2] + averages[3])/4
    puts averages, "(#{average})"
    assert average.between?(THROTTLE_EIGHTH, MOTOR_MAX)

    puts("Check that gear switch kills the motors")
    # (and that light goes off)
    File.write(GEAR, GEAR_OFF)
    check_motors_are_off
    # (Bring the RC throttle back down)
    File.write(THROTTLE, THROTTLE_MIN)
    sleep 1
    puts "All safety checks passed."
    Process.kill(9, quad_pid)
    Process.wait(quad_pid)

  end
}