-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathksp_interface.py
356 lines (307 loc) · 16.9 KB
/
ksp_interface.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
from datetime import datetime, timedelta
import krpc, math
import numpy as np
from ksp_types import VesselAttitude, VesselFlightControl, VesselFlightState, VesselOrbitalParameters, VesselResources
from pyquaternion import Quaternion
from util import project_a_onto_b, project_vector_a_onto_plane_b, vector_normalize
class KspInterface:
#
# Constants
#
MAX_RETRY_INTERVAL_MS = 5000
#
# Constructor
#
def __init__(self, ip_address, rpc_port, stream_port):
self.ip_address = ip_address
self.rpc_port = rpc_port
self.stream_port = stream_port
self.is_connected = False
self.is_data_streaming = False
self.last_connect_time = datetime.now()
self.last_data_setup_time = datetime.now()
self.retry_interval_ms = 100
#
# Public Methods
#
def init_connection(self) -> bool:
if self.is_connected:
return True
try:
print("Attempting to connect KRPC...")
self.last_connect_time = datetime.now()
self.krpc_connection = krpc.connect(
name="Kockpit",
address=self.ip_address,
rpc_port=self.rpc_port,
stream_port=self.stream_port)
self.is_connected = True
self.retry_interval_ms = 100
# self.__execute_debugging_tools()
except Exception as e:
self.is_connected = False
print("Failed to connect KRPC interface")
print("Exception type : ", type(e).__name__)
print("Exception message : ", str(e))
if not self.is_connected:
return self.is_connected
return self.is_connected
def deinit_connection(self) -> None:
if not self.is_connected:
return
self.krpc_connection.close()
def setup_connection_if_needed(self) -> None:
if self.is_connected:
return
current_timestamp = datetime.now()
time_since_last_connect = current_timestamp - self.last_connect_time
if time_since_last_connect > timedelta(milliseconds=self.retry_interval_ms):
is_success = self.init_connection()
if not is_success:
self.__increase_retry_interval()
def setup_data_streams_if_needed(self) -> None:
if self.is_data_streaming or (not self.is_connected):
return
current_timestamp = datetime.now()
time_since_last_data_setup = current_timestamp - self.last_data_setup_time
if time_since_last_data_setup < timedelta(milliseconds=self.retry_interval_ms):
return
try:
print("Setting up KRPC data streams...")
self.last_data_setup_time = datetime.now()
self.gravitational_constant = self.krpc_connection.space_center.g
vessel = self.krpc_connection.space_center.active_vessel
self.stream_vessel_orbit = self.krpc_connection.add_stream(getattr, vessel, 'orbit')
vessel_flight = vessel.flight() # surface reference frame
# Vessel attitude
self.stream_heading = self.krpc_connection.add_stream(getattr, vessel_flight, 'heading')
self.stream_pitch = self.krpc_connection.add_stream(getattr, vessel_flight, 'pitch')
self.stream_roll = self.krpc_connection.add_stream(getattr, vessel_flight, 'roll')
# Vessel flight state
self.stream_situation = self.krpc_connection.add_stream(getattr, vessel, 'situation')
self.stream_max_thrust = self.krpc_connection.add_stream(getattr, vessel, 'max_thrust')
self.stream_mass = self.krpc_connection.add_stream(getattr, vessel, 'mass')
self.stream_max_torque = self.krpc_connection.add_stream(getattr, vessel, 'available_torque')
self.stream_moi = self.krpc_connection.add_stream(getattr, vessel, 'moment_of_inertia')
# Vessel's orbital parameters
# self.stream_orbital_period = self.krpc_connection.add_stream(getattr, vessel.orbit, 'period')
# self.stream_time_to_apoapsis = self.krpc_connection.add_stream(getattr, vessel.orbit, 'time_to_apoapsis')
# self.stream_time_to_periapsis = self.krpc_connection.add_stream(getattr, vessel.orbit, 'time_to_periapsis')
# Visual debugging markers
self.draw_vessel_pos_unit = self.krpc_connection.drawing.add_line((0.0, 0.0, 0.0), (0.0, 0.0, 0.0), self.stream_vessel_orbit().body.reference_frame)
self.draw_vessel_pos_unit.color = (0.0, 1.0, 0.0)
self.draw_vessel_vel_unit = self.krpc_connection.drawing.add_line((0.0, 0.0, 0.0), (0.0, 0.0, 0.0), self.stream_vessel_orbit().body.reference_frame)
self.draw_vessel_vel_unit.color = (0.0, 0.0, 1.0)
self.draw_vessel_srfvel_unit = self.krpc_connection.drawing.add_line((0.0, 0.0, 0.0), (0.0, 0.0, 0.0), self.stream_vessel_orbit().body.reference_frame)
self.draw_vessel_srfvel_unit.color = (1.0, 0.0, 0.0)
self.draw_vessel_fwd_unit = self.krpc_connection.drawing.add_line((0.0, 0.0, 0.0), (0.0, 0.0, 0.0), self.stream_vessel_orbit().body.reference_frame)
self.draw_vessel_fwd_unit.color = (1.0, 0.6, 0.1)
self.is_data_streaming = True
self.retry_interval_ms = 100
except Exception as e:
print("Failed to setup KRPC data streams")
print("Exception type : ", type(e).__name__)
print("Exception message : ", str(e))
self.is_data_streaming = False
self.__increase_retry_interval()
def get_krpc_status(self) -> str:
krpc_status = "no connection"
if not self.is_connected:
return krpc_status
try:
krpc_status = self.krpc_connection.krpc.get_status().version
except ConnectionAbortedError as e:
# Client connection has failed, reset connection status
krpc_status = "no connection"
self.is_connected = False
self.is_data_streaming = False
print("KRPC connection failure")
print("Exception type : ", type(e).__name__)
print("Exception message : ", str(e))
except Exception as e:
print("Failed to get KRPC status")
print("Exception type : ", type(e).__name__)
print("Exception message : ", str(e))
return krpc_status
def get_vessel_attitude(self) -> VesselAttitude:
is_valid = False
heading = 0
pitch = 0
roll = 0
if self.is_connected and self.is_data_streaming:
try:
heading = self.stream_heading()
pitch = self.stream_pitch()
roll = self.stream_roll()
is_valid = True
except Exception as e:
print("Failed to get KRPC vessel attitude")
print("Exception type : ", type(e).__name__)
print("Exception message : ", str(e))
self.is_data_streaming = False
return VesselAttitude(is_valid, heading, pitch, roll)
def get_vessel_flight_state(self) -> VesselFlightState:
data = VesselFlightState(False, 0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0)
if self.is_connected and self.is_data_streaming:
try:
vessel = self.krpc_connection.space_center.active_vessel
vessel_mass = self.stream_mass()
vessel_cbody_refframe = self.stream_vessel_orbit().body.reference_frame
vessel_pos = vessel.position(vessel_cbody_refframe)
vessel_pos_vec = np.array([vessel_pos[0], vessel_pos[1], vessel_pos[2]])
vessel_pos_mag2 = np.dot(vessel_pos_vec, vessel_pos_vec)
vessel_pos_mag = math.sqrt(vessel_pos_mag2)
vessel_pos_unit = vessel_pos_vec / vessel_pos_mag
vessel_vel = vessel.velocity(vessel_cbody_refframe)
vessel_vel_vec = np.array([vessel_vel[0], vessel_vel[1], vessel_vel[2]])
vessel_vel_mag2 = np.dot(vessel_vel_vec, vessel_vel_vec)
vessel_vel_mag = math.sqrt(vessel_vel_mag2)
vessel_vel_unit = np.array([vessel_vel[0] / vessel_vel_mag, vessel_vel[1] / vessel_vel_mag, vessel_vel[2] / vessel_vel_mag])
cbody_gravity = self.gravitational_constant * vessel.orbit.body.mass / vessel_pos_mag2
vessel_srfvel_vec = project_vector_a_onto_plane_b(vessel_vel_vec, vessel_pos_unit)
vessel_srfvel_mag2 = np.dot(vessel_srfvel_vec, vessel_srfvel_vec)
vessel_srfvel_mag = math.sqrt(vessel_srfvel_mag2)
vessel_srfvel_unit = vessel_srfvel_vec / vessel_srfvel_mag
vessel_rot = vessel.rotation(vessel_cbody_refframe)
vessel_rot_q = Quaternion(vessel_rot[3], vessel_rot[0], vessel_rot[1], vessel_rot[2])
vessel_fwd = vessel_rot_q.rotate(np.array([0.0, 0.0, 1.0]))
vessel_lat = vessel_rot_q.rotate(np.array([1.0, 0.0, 0.0]))
surface_vessel_fwd = project_vector_a_onto_plane_b(vessel_fwd, vessel_pos_unit)
surface_vessel_fwd = vector_normalize(surface_vessel_fwd)
surface_vessel_lat = project_vector_a_onto_plane_b(vessel_lat, vessel_pos_unit)
surface_vessel_lat = vector_normalize(surface_vessel_lat)
surface_vessel_vel_fwd = project_a_onto_b(vessel_vel_vec, surface_vessel_fwd)
surface_vessel_vel_lat = project_a_onto_b(vessel_vel_vec, surface_vessel_lat)
vessel_ang_vel = vessel.angular_velocity(vessel_cbody_refframe)
vessel_ang_vel_vec = np.array([vessel_ang_vel[0], vessel_ang_vel[1], vessel_ang_vel[2]])
vessel_ang_vel_pitch = project_a_onto_b(vessel_ang_vel_vec, vessel_lat)
vessel_ang_vel_yaw = project_a_onto_b(vessel_ang_vel_vec, vessel_fwd)
self.draw_vessel_pos_unit.start = vessel_pos
self.draw_vessel_pos_unit.end = vessel_pos_vec + (vessel_pos_unit * 10.0)
self.draw_vessel_vel_unit.start = vessel_pos
self.draw_vessel_vel_unit.end = vessel_pos + (vessel_vel_unit * 10.0)
self.draw_vessel_srfvel_unit.start = vessel_pos
self.draw_vessel_srfvel_unit.end = vessel_pos + (vessel_srfvel_unit * 10.0)
self.draw_vessel_fwd_unit.start = vessel_pos
self.draw_vessel_fwd_unit.end = vessel_pos + (surface_vessel_vel_lat * 10.0)
data.iSituation = self.stream_situation()
data.fWeight = cbody_gravity * vessel_mass
data.fThrustMax = self.stream_max_thrust()
data.fVerticalSpeed = vessel.flight(self.stream_vessel_orbit().body.reference_frame).vertical_speed
data.fForwardSpeed = math.sqrt(np.dot(surface_vessel_vel_fwd, surface_vessel_vel_fwd)) * np.sign(np.dot(surface_vessel_vel_fwd, surface_vessel_fwd))
data.fLateralSpeed = math.sqrt(np.dot(surface_vessel_vel_lat, surface_vessel_vel_lat)) * np.sign(np.dot(surface_vessel_vel_lat, surface_vessel_lat))
data.fPitchSpeed = math.sqrt(np.dot(vessel_ang_vel_pitch, vessel_ang_vel_pitch)) * np.sign(np.dot(vessel_ang_vel_pitch, surface_vessel_lat))
data.fPitchTorqueMax = self.stream_max_torque()[0][0]
data.fPitchMomentOfInertia = self.stream_moi()[0]
data.fYawSpeed = math.sqrt(np.dot(vessel_ang_vel_yaw, vessel_ang_vel_yaw)) * np.sign(np.dot(vessel_ang_vel_yaw, surface_vessel_fwd))
data.fYawTorqueMax = self.stream_max_torque()[0][1]
data.fYawMomentOfInertia = self.stream_moi()[1]
data.bIsDataValid = True
except Exception as e:
print("Failed to get KRPC vessel flight state")
print("Exception type : ", type(e).__name__)
print("Exception message : ", str(e))
self.is_data_streaming = False
return data
def get_vessel_orbital_parameters(self) -> VesselOrbitalParameters:
data = VesselOrbitalParameters(False, "", 0.0, 0.0, 0.0, 0.0)
if self.is_connected and self.is_data_streaming:
try:
data.sCelestialBodyName = self.stream_vessel_orbit().body.name
data.fCelestialBodyMass = self.stream_vessel_orbit().body.mass
data.fPeriod = self.stream_vessel_orbit().period
data.fTimeToApoapsis = self.stream_vessel_orbit().time_to_apoapsis
data.fTimeToPeriapsis = self.stream_vessel_orbit().time_to_periapsis
data.bIsDataValid = True
except Exception as e:
print("Failed to get KRPC vessel orbit")
print("Exception type : ", type(e).__name__)
print("Exception message : ", str(e))
self.is_data_streaming = False
return data
def get_vessel_resources(self) -> VesselResources:
data = VesselResources(False, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0)
if self.is_connected:
try:
(data.fWater, data.fWaterMax) = self.get_total_resource("Water")
(data.fFood, data.fFoodMax) = self.get_total_resource("Food")
(data.fOxygen, data.fOxygenMax) = self.get_total_resource("Oxygen")
(data.fAtmo, data.fAtmoMax) = self.get_total_resource("Atmosphere")
(data.fWasteAtmo, data.fWasteAtmoMax) = self.get_total_resource("WasteAtmosphere")
data.bIsDataValid = True
except Exception as e:
print("Failed to get KRPC vessel resources")
print("Exception type : ", type(e).__name__)
print("Exception message : ", str(e))
return data
def get_total_resource(self, resource_name: str) -> tuple:
""" Returns the total amount of resource in the active vessel as a tuple: (amount, max)"""
amount = 0
max = 0
all_resources = self.krpc_connection.space_center.active_vessel.resources.with_resource(resource_name)
for r in all_resources:
amount += r.amount
max += r.max
return (amount, max)
def set_flight_controls(self, control: VesselFlightControl) -> None:
if control.bIsInputValid:
self.krpc_connection.space_center.active_vessel.control.throttle = control.fThrottle
self.krpc_connection.space_center.active_vessel.control.pitch = control.fPitch
self.krpc_connection.space_center.active_vessel.control.yaw = control.fYaw
#
# Private Methods
#
def __increase_retry_interval(self) -> None:
self.retry_interval_ms = self.retry_interval_ms * 2
if self.retry_interval_ms > self.MAX_RETRY_INTERVAL_MS:
self.retry_interval_ms = self.MAX_RETRY_INTERVAL_MS
def __execute_debugging_tools(self):
parts_to_log = []
vessel = self.krpc_connection.space_center.active_vessel
root = vessel.parts.root
stack = [(root, 0)]
while stack:
part, depth = stack.pop()
print(' ' * depth, part.title)
if 'ECLSS' in part.title:
parts_to_log.append(part)
elif 'Chemical' in part.title:
parts_to_log.append(part)
elif 'Geiger' in part.title:
parts_to_log.append(part)
elif 'Fuel Cell' in part.title:
parts_to_log.append(part)
for child in part.children:
stack.append((child, depth+1))
for part in parts_to_log:
self.__print_part_info(part)
def __print_part_info(self, krpc_part):
print("\n\n\n\n=== {0} ===".format(krpc_part.title))
for module in krpc_part.modules:
info_str = "Module: {0}\n".format(module.name)
idx = 0
for field in module.fields_by_id:
if idx == 0:
info_str += "Fields: "
else:
info_str += " "
info_str += field + " = "
info_str += module.fields_by_id[field] + "\n"
num_events = len(module.events_by_id)
for idx in range(0, num_events):
# if ("ECLSS" in krpc_part.title) and ("Scrubber" in module.events[idx]):
# module.trigger_event_by_id(module.events_by_id[idx])
# print("\n\nTRIGGERED\n\n")
if idx == 0:
info_str += "Events: "
else:
info_str += " "
info_str += module.events[idx] + " (ID=" + module.events_by_id[idx] + ")\n"
num_actions = len(module.actions_by_id)
for idx in range(0, num_actions):
if idx == 0:
info_str += "Action: "
else:
info_str += " "
info_str += module.actions[idx] + " (ID=" + module.actions_by_id[idx] + ")\n"
print(info_str)