Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Orbital maneuvers #64

Open
wants to merge 10 commits into
base: master
Choose a base branch
from
Prev Previous commit
Next Next commit
Now only prints the fail reason or success message.
  • Loading branch information
akrasuski1 committed May 18, 2015
commit c34e39ac9cf8e70b480fd26db7bd98ec27f97067
151 changes: 68 additions & 83 deletions unit_tests/test_lib_orbital_maneuvers.ks
Original file line number Diff line number Diff line change
@@ -1,83 +1,79 @@
run lib_orbital_maneuvers.

// Run this test while being in an orbit, preferably eccentric to
// test many scenarios. Note that there should not be an encounter
// or escape on current orbit.
@lazyglobal off.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Needs // This file is distributed under the terms of the MIT license, (c) the KSLib team


function print_unit_test_result{
parameter result.
run lib_orbital_maneuvers.
run lib_exec.

local str is "FAIL".
if result{
set str to "PASS".
}
print "+============+".
print "|TEST "+str+"ED.|".
print "+============+".
}
// Please run this test while being in an orbit around Kerbin, which has
// Pe>30km (which is any stable orbit), Ap<30Mm (inside Mun's orbit is
// enough). Preferably, to test weird scenarios, make your orbit
// inclined and eccentric.

function om_unit_test1{
// This test tries to circularize at many different points of the orbit.
local i is 0.
local result is true.
until i>obt:period{
local nd is circularize_at_time(time:seconds+i).
add nd.
local deviation is round(nd:orbit:apoapsis-nd:orbit:periapsis,1).
print "Deviation="+deviation+"m".
if deviation>100{ // a reasonable margin
set result to false.
break.
return "Orbit was not circularized - (Ap-Pe)="+deviation+"m>100m".
}
wait 0.01.
remove nd.
set i to i+obt:period/100.
}
print_unit_test_result(result).
return result.
return "".
}

function om_unit_test2{
// This test will try to predict ETA:periapsis and ETA:apoapsis via
// time_to_true_anomaly
local result is true.
local deviation is ETA:periapsis-time_to_true_anomaly(obt,0).
print "Deviation: "+round(deviation,2)+"s".
if abs(deviation)>5{
set result to false.
return "Difference between ETA:periapsis and time predicted by"+
"library is "+deviation+"s>5s".
}
local deviation is ETA:apoapsis-time_to_true_anomaly(obt,180).
print "Deviation: "+round(deviation,2)+"s".
if abs(deviation)>5{
set result to false.
return "Difference between ETA:apoapsis and time predicted by"+
"library is "+deviation+"s>5s".
}
// Note that this test might fail even though it works correctly, if
// you are very close to Pe or Ap, so that ETA fluctuates between 0
// and obt:period.
print_unit_test_result(result).
return result.
return "".
}

function om_unit_test3{
// This test will try to set its periapsis at exactly 30km at different
// positions in orbit.
local i is 0.
local result is true.
until i>obt:period{
local nd is change_opposite_height(time:seconds+i,30000).
add nd.
local deviation is round(nd:orbit:periapsis-30000,1).
print "Deviation="+deviation+"m".
if abs(deviation)>5{ // a reasonable margin
set result to false.
break.
return "The script tried to set its periapsis at 30km, but"+
" instead set it at "+nd:orbit:periapsis/1000+"km".
}
wait 0.01.
remove nd.
set i to i+obt:period/100.
}
local i is 0.
until i>obt:period{
local nd is change_opposite_height(time:seconds+i,30000000).
add nd.
local deviation is round(nd:orbit:apoapsis-30000000,1).
if abs(deviation)>1000{ // a reasonable margin
return "The script tried to set its periapsis at 30Mm, but"+
" instead set it at "+nd:orbit:apoapsis/1000000+"Mm".
}
wait 0.01.
remove nd.
set i to i+obt:period/100.
}
print_unit_test_result(result).
return result.
return "".
}

function om_unit_test4{
@@ -94,24 +90,23 @@ function om_unit_test4{
local expected_normal is VCRS(expected_vel,expected_pos).

local vel_before_burn is velocityat(ship,nd:eta):orbit.

if abs(obt:period-nd:orbit:period)>10{ // 10s of margin
print "Maneuver changed orbital period.".
set result to false.

local deviation is abs(obt:period-nd:orbit:period).
if deviation>10{ // 10s of margin
return "Plane switch maneuver changed orbital period (deviation="+
deviation+"s)".
}
print "Angle between normals: "+VANG(expected_normal,minmus_normal).
if VANG(expected_normal,minmus_normal)>2{ // two degrees of margin
print "Maneuver did not place at the same plane.".
set result to false.
local ang is VANG(expected_normal,minmus_normal).
if ang>2{ // two degrees of margin
return "Plane switch maneuver did not place at the same plane - "+
"deviation of planes is "+ang+" degrees, which is more than 2.".
}
if nd:eta>obt:period/2+20{
print "Maneuver was set at wrong inclination node.".
set result to false.
return "Plane switch maneuver was set at wrong inclination node.".
}
wait 1.
remove nd.
print_unit_test_result(result).
return result.
return "".
}

function om_unit_test5{
@@ -120,69 +115,59 @@ function om_unit_test5{
local pe is obt:periapsis.
local res is time_to_pass_height(obt,pe-1000).
if res[0]<>1{
print "Found a moment when height=periapsis-1000".
set result to false.
return "Found a moment when height=periapsis-1000 (impossible)".
}
local res is time_to_pass_height(obt,ap+1000).
if res[0]<>-1{
print "Found a moment when height=apoapsis+1000".
set result to false.
return "Found a moment when height=apoapsis+1000 (impossible)".
}

local res is time_to_pass_height(obt,(ap+pe)/2).
if res[0]<>0{
print "Haven't found a moment when height=SMA".
set result to false.
return "Haven't found a moment when height=SMA".
}
local res is time_to_pass_height(obt,pe+1).
if res[0]<>0{
print "Haven't found a moment when height=pe+1m".
set result to false.
return "Haven't found a moment when height=Pe+1m".
}
else{
// This test may give false positive depending on eccentricity
// of your orbit. Try to test using eccentricity > 0.1
local deviation is abs(res[1]-res[2]).
print "Deviation="+deviation+"s".
if deviation>obt:period/100{
print "Two moments were too far separated".
set result to false.
return "Two moments when height=Pe+1m were too separated"+
" in time ("+deviation+"s)".
}
}
print_unit_test_result(result).
return result.
return "".
}

function om_unit_tests{
local overall is true.
if not om_unit_test1(){
set overall to false.
}
wait 1.
if not om_unit_test2(){
set overall to false.
}
wait 1.
if not om_unit_test3(){
set overall to false.
}
wait 1.
if not om_unit_test4(){
set overall to false.
}
wait 1.
if not om_unit_test5(){
set overall to false.
local fail_reason is "".
local res is "".
local i is 1.
until i>5{
set res to evaluate("om_unit_test"+i+"()").
if res<>""{
set fail_reason to res.
break.
}
wait 1.
set i to i+1.
}
wait 1.

print " ".
print "OVERALL RESULT:".
if overall{
print "ALL TESTS PASSED SUCCESSFULLY".
print "Overall result:".
if fail_reason=""{
print "All tests passed SUCCESSFULLY".
}
else{
print "SOME TESTS FAILED".
print "Some tests FAILED".
print "Reasone of the first FAIL:".
print "".
print fail_reason.
}
print " ".
}

om_unit_tests().