My Kerbal RPC scripts

monitor.py 2.0KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  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("Stop 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. f = open(datafile, 'w')
  24. f.write('timepoint, current_altitude, v_speed, h_speed, mass, fuel, available_thrust, current_thrust, g_force\n')
  25. timepoint = 0
  26. vessel = conn.space_center.active_vessel
  27. body = vessel.orbit.body
  28. r_frame = body.reference_frame
  29. while button_clicked() == False:
  30. dry_mass = vessel.dry_mass
  31. vm = vessel.mass
  32. at = vessel.available_thrust
  33. current_altitude = vessel.flight(r_frame).surface_altitude
  34. v_speed = vessel.flight(r_frame).vertical_speed
  35. h_speed = vessel.flight(r_frame).horizontal_speed
  36. c_thrust = vessel.thrust
  37. g_force = vessel.flight(r_frame).g_force
  38. f.write('{0}, {1}, {2}, {3}, {4}, {5}, {6}, {7}, {8}\n'.format(timepoint,
  39. current_altitude,
  40. v_speed, h_speed,
  41. vm, vm -
  42. dry_mass, at,
  43. c_thrust, g_force))
  44. timepoint = timepoint + 1
  45. time.sleep(0.1)
  46. f.close()
  47. button.remove()
  48. panel.remove()
  49. conn.close()