My Kerbal RPC scripts

monitor.py 2.3KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. #! /bin/python3
  2. # Modified from the script at
  3. # http://krpc.github.io/krpc/tutorials/user-interface.html
  4. datafile = 'flight.csv'
  5. remote = '192.168.1.10'
  6. import time
  7. import krpc
  8. conn = krpc.connect(name='Flight monitor', address=remote)
  9. canvas = conn.ui.stock_canvas
  10. # Get the size of the game window in pixels
  11. screen_size = canvas.rect_transform.size
  12. # Add a panel to contain the UI elements
  13. panel = canvas.add_panel()
  14. # Position the panel on the left of the screen
  15. rect = panel.rect_transform
  16. rect.size = (200, 50)
  17. rect.position = (110-(screen_size[0]/2), screen_size[1]/2-75)
  18. # Add a button to set the throttle to maximum
  19. button = panel.add_button("Start monitor")
  20. button.rect_transform.position = (0, 0)
  21. # Set up a stream to monitor the throttle button
  22. button_clicked = conn.add_stream(getattr, button, 'clicked')
  23. # Wait for button
  24. while button_clicked() == False:
  25. time.sleep(0.1)
  26. button.remove()
  27. # replace button
  28. button = panel.add_button("Stop monitor")
  29. button.rect_transform.position = (0, 0)
  30. button_clicked = conn.add_stream(getattr, button, 'clicked')
  31. f = open(datafile, 'w')
  32. f.write('mission_time, current_altitude, v_speed, h_speed, mass, electric_charge, liquid_fuel, oxidizer, available_thrust, current_thrust, g_force, apoapsis, periapsis, orbital_radius, orbit_speed, orbit_period\n')
  33. vessel = conn.space_center.active_vessel
  34. body = vessel.orbit.body
  35. r_frame = body.reference_frame
  36. while button_clicked() == False:
  37. dry_mass = vessel.dry_mass
  38. vm = vessel.mass
  39. at = vessel.available_thrust
  40. current_altitude = vessel.flight(r_frame).surface_altitude
  41. v_speed = vessel.flight(r_frame).vertical_speed
  42. h_speed = vessel.flight(r_frame).horizontal_speed
  43. c_thrust = vessel.thrust
  44. e_charge = vessel.resources.amount('ElectricCharge')
  45. lf = vessel.resources.amount('LiquidFuel')
  46. ox = vessel.resources.amount('Oxidizer')
  47. g_force = vessel.flight(r_frame).g_force
  48. timept = vessel.met
  49. vo = vessel.orbit
  50. ap = vo.apoapsis_altitude
  51. pa = vo.periapsis_altitude
  52. orad = vo.radius
  53. os = vo.speed
  54. op = vo.period
  55. f.write('{0}, {1}, {2}, {3}, {4}, {5}, {6}, {7}, {8}, {9}, {10}, {11}, {12}, {13}, {14}, {15}\n'.format(timept, current_altitude, v_speed, h_speed, vm, e_charge, lf, ox, at, c_thrust, g_force, ap, pa, orad, os, op))
  56. time.sleep(0.1)
  57. f.close()
  58. button.remove()
  59. panel.remove()
  60. conn.close()