Advertisement
space_is_hard

ascent_logger.ks

Sep 8th, 2015
120
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.56 KB | None | 0 0
  1. //Ascent Logger
  2.  
  3. //Logs ascent stats to aid in programming a vehicle's optimal ascent profile
  4. //Engage abort action group to finish logging
  5.  
  6. @LAZYGLOBAL OFF.
  7.  
  8. PARAMETER log_rate. //How many times per second to log parameters
  9.  
  10. CLEARSCREEN.
  11.  
  12. //Logs to archive. RT connection required throughout ascent
  13. SWITCH TO 0.
  14.  
  15. //Creates ascent log if it doesn't already exist to keep the following delete command
  16. //from barfing if it didn't exist
  17. LOG "" TO ascent_log.csv.
  18.  
  19. DELETE ascent_log.csv.
  20.  
  21. //The header for our data
  22. LOG "MET, ALT, PIT, HDG, TWR" TO ascent_log.csv.
  23.  
  24. //Used to track when to log parameters
  25. LOCAL log_timer TO TIME:SECONDS.
  26.  
  27. //Used to print out how many lines of data have been logged
  28. LOCAL log_lines TO 0.
  29.  
  30. //Time of launch, used to track MET
  31. LOCAL launch_time TO -1.
  32.  
  33. //Mission Elapsed Time
  34. LOCAL MET TO -1.
  35.  
  36. //Thrust to weight ratio
  37. LOCAL TWR TO SHIP:MAXTHRUST / (SHIP:MASS * 9.82).
  38.  
  39. //Pitch from vertical
  40. LOCAL vertical_pitch TO VANG(SHIP:FACING:FOREVECTOR, SHIP:UP:FOREVECTOR).
  41.  
  42. //Compass heading
  43. LOCAL ship_heading TO 0.
  44.  
  45. //Borrowed from KSLib
  46. function compass_for {
  47.   parameter ves.
  48.  
  49.   local pointing is ves:facing:forevector.
  50.   local east is vcrs(ves:up:vector, ves:north:vector).
  51.  
  52.   local trig_x is vdot(ves:north:vector, pointing).
  53.   local trig_y is vdot(east, pointing).
  54.  
  55.   local result is arctan2(trig_y, trig_x).
  56.  
  57.   if result < 0 {
  58.     return 360 + result.
  59.   } else {
  60.     return result.
  61.   }
  62. }
  63.  
  64. WAIT UNTIL SHIP:STATUS <> ("PreLaunch").
  65.  
  66. SET launch_time TO TIME:SECONDS.
  67.  
  68. ABORT OFF.
  69. UNTIL ABORT {
  70.    
  71.     SET MET TO TIME:SECONDS - launch_time.
  72.     SET vertical_pitch TO VANG(SHIP:FACING:FOREVECTOR, SHIP:UP:FOREVECTOR).
  73.     SET ship_heading TO compass_for(SHIP).
  74.     SET TWR TO SHIP:MAXTHRUST / (SHIP:MASS * 9.82).
  75.    
  76.     IF TIME:SECONDS > log_timer {
  77.         LOG
  78.             MET + ", " +
  79.             SHIP:ALTITUDE + ", " +
  80.             vertical_pitch + ", " +
  81.             ship_heading + ", " +
  82.             TWR
  83.         TO ascent_log.csv.
  84.        
  85.         SET log_lines TO log_lines + 1.
  86.         SET log_timer TO log_timer + (1 / log_rate).
  87.     }.
  88.    
  89.     //Readout
  90.     PRINT "LOG: " + log_lines + "     " AT(0,0).
  91.     PRINT "MET: " + ROUND(MET, 2) + "     " AT(0,2).
  92.     PRINT "ALT: " + ROUND(SHIP:ALTITUDE) + "     " AT(0,3).
  93.     PRINT "PIT: " + ROUND(vertical_pitch, 2) + "     " AT(0,4).
  94.     PRINT "HDG: " + ROUND(ship_heading, 2) + "     " AT(0,5).
  95.     PRINT "TWR: " + ROUND(TWR, 2) + "     " AT(0,6).
  96.    
  97.     //Prevents loop from cycling faster than game physics does
  98.     WAIT UNTIL TRUE.
  99.    
  100. }.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement