My Kerbal RPC scripts

monitor.py 2.2KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  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('timepoint, current_altitude, v_speed, h_speed, mass, fuel, available_thrust, current_thrust, g_force\n')
  33. timepoint = 0
  34. vessel = conn.space_center.active_vessel
  35. body = vessel.orbit.body
  36. r_frame = body.reference_frame
  37. while button_clicked() == False:
  38. dry_mass = vessel.dry_mass
  39. vm = vessel.mass
  40. at = vessel.available_thrust
  41. current_altitude = vessel.flight(r_frame).surface_altitude
  42. v_speed = vessel.flight(r_frame).vertical_speed
  43. h_speed = vessel.flight(r_frame).horizontal_speed
  44. c_thrust = vessel.thrust
  45. g_force = vessel.flight(r_frame).g_force
  46. f.write('{0}, {1}, {2}, {3}, {4}, {5}, {6}, {7}, {8}\n'.format(timepoint,
  47. current_altitude,
  48. v_speed, h_speed,
  49. vm, vm -
  50. dry_mass, at,
  51. c_thrust, g_force))
  52. timepoint = timepoint + 1
  53. time.sleep(0.1)
  54. f.close()
  55. button.remove()
  56. panel.remove()
  57. conn.close()