《坎巴拉太空计划》krpc模组|0.5.4|文档(中译版)——教程与示例:对接引导与用户界面
本翻译基于 [kRPC 官方文档](https://krpc.github.io/krpc),遵循 LGPL v3 许可。 部分内容可能受 GPLv3 或 MIT 许可约束,详见原始仓库。 Copyright 2015-2023 kRPC Org.

本教程和示例脚本合集详细说明了如何使用 kRPC 的功能特性。

注意:由于篇幅简短,为提高文章质量,译者将两章合并翻译

对接引导

以下脚本输出对接引导信息。它会等待航天器从一个对接口进行控制,并且设置了当前目标对接口。然后它会打印出沿对接轴线方向的速度和距离信息。

它使用 numpy 对 kRPC 返回的向量进行线性代数运算——例如计算向量的点积或长度——并使用 curses 进行终端输出。

import curses import time import numpy as np import numpy.linalg as la import krpc # Set up curses stdscr = curses.initscr() curses.nocbreak() stdscr.keypad(1) curses.noecho() try: # Connect to kRPC conn = krpc.connect(name='Docking Guidance') vessel = conn.space_center.active_vessel current = None target = None while True: stdscr.clear() stdscr.addstr(0, 0, '-- Docking Guidance --') current = conn.space_center.active_vessel.parts.controlling.docking_port target = conn.space_center.target_docking_port if current is None: stdscr.addstr(2, 0, 'Awaiting control from docking port...') elif target is None: stdscr.addstr(2, 0, 'Awaiting target docking port...') else: # Get positions, distances, velocities and # speeds relative to the target docking port current_position = current.position(target.reference_frame) velocity = current.part.velocity(target.reference_frame) displacement = np.array(current_position) distance = la.norm(displacement) speed = la.norm(np.array(velocity)) # Get speeds and distances relative to the docking axis # (the direction the target docking port is facing in) # Axial = along the docking axis axial_displacement = np.copy(displacement) axial_displacement[0] = 0 axial_displacement[2] = 0 axial_distance = axial_displacement[1] axial_velocity = np.copy(velocity) axial_velocity[0] = 0 axial_velocity[2] = 0 axial_speed = axial_velocity[1] if axial_distance > 0: axial_speed *= -1 # Radial = perpendicular to the docking axis radial_displacement = np.copy(displacement) radial_displacement[1] = 0 radial_distance = la.norm(radial_displacement) radial_velocity = np.copy(velocity) radial_velocity[1] = 0 radial_speed = la.norm(radial_velocity) if np.dot(radial_velocity, radial_displacement) > 0: radial_speed *= -1 # Get the docking port state if current.state == conn.space_center.DockingPortState.ready: state = 'Ready to dock' elif current.state == conn.space_center.DockingPortState.docked: state = 'Docked' elif current.state == conn.space_center.DockingPortState.docking: state = 'Docking...' else: state = 'Unknown' # Output information stdscr.addstr(2, 0, 'Current ship: {:30}'.format(current.part.vessel.name[:30])) stdscr.addstr(3, 0, 'Current port: {:30}'.format(current.part.title[:30])) stdscr.addstr(5, 0, 'Target ship: {:30}'.format(target.part.vessel.name[:30])) stdscr.addstr(6, 0, 'Target port: {:30}'.format(target.part.title[:30])) stdscr.addstr(8, 0, 'Status: {:10}'.format(state)) stdscr.addstr(10, 0, ' +---------------------------+') stdscr.addstr(11, 0, ' | Distance | Speed |') stdscr.addstr(12, 0, '+---------+------------+--------------+') stdscr.addstr(13, 0, '| | {:>+6.2f} m | {:>+6.2f} m/s |' .format(distance, speed)) stdscr.addstr(14, 0, '| Axial | {:>+6.2f} m | {:>+6.2f} m/s |' .format(axial_distance, axial_speed)) stdscr.addstr(15, 0, '| Radial | {:>+6.2f} m | {:>+6.2f} m/s |' .format(radial_distance, radial_speed)) stdscr.addstr(16, 0, '+---------+------------+--------------+') stdscr.refresh() time.sleep(0.25) finally: # Shutdown curses curses.nocbreak() stdscr.keypad(0) curses.echo() curses.endwin()

用户界面

以下脚本演示了如何使用 UI 服务显示文本和处理基本用户输入。它在屏幕左侧添加一个面板,显示航天器当前产生的推力,以及一个将油门 (throttle) 设置到最大的按钮。

import time import krpc conn = krpc.connect(name='User Interface Example') canvas = conn.ui.stock_canvas # Get the size of the game window in pixels screen_size = canvas.rect_transform.size # Add a panel to contain the UI elements panel = canvas.add_panel() # Position the panel on the left of the screen rect = panel.rect_transform rect.size = (200, 100) rect.position = (110-(screen_size[0]/2), 0) # Add a button to set the throttle to maximum button = panel.add_button("Full Throttle") button.rect_transform.position = (0, 20) # Add some text displaying the total engine thrust text = panel.add_text("Thrust: 0 kN") text.rect_transform.position = (0, -20) text.color = (1, 1, 1) text.size = 18 # Set up a stream to monitor the throttle button button_clicked = conn.add_stream(getattr, button, 'clicked') vessel = conn.space_center.active_vessel while True: # Handle the throttle button being clicked if button_clicked(): vessel.control.throttle = 1 button.clicked = False # Update the thrust text text.content = 'Thrust: %d kN' % (vessel.thrust/1000) time.sleep(0.1)