Interacting with Parts¶
The following examples demonstrate use of the Parts functionality to achieve various tasks. More details on specific topics can also be found in the API documentation:
Deploying all Parachutes¶
Sometimes things go horribly wrong. The following script does its best to save your Kerbals by deploying all the parachutes:
using KRPC.Client;
using KRPC.Client.Services.SpaceCenter;
class DeployParachutes
{
public static void Main ()
{
using (var connection = new Connection ()) {
var vessel = connection.SpaceCenter ().ActiveVessel;
foreach (var parachute in vessel.Parts.Parachutes)
parachute.Deploy ();
}
}
}
#include <krpc.hpp>
#include <krpc/services/space_center.hpp>
int main() {
krpc::Client conn = krpc::connect();
krpc::services::SpaceCenter space_center(&conn);
auto vessel = space_center.active_vessel();
for (auto parachute : vessel.parts().parachutes())
parachute.deploy();
}
#include <krpc_cnano.h>
#include <krpc_cnano/services/space_center.h>
int main() {
krpc_connection_t conn;
krpc_open(&conn, "COM0");
krpc_connect(conn, "DeployParachutes");
krpc_SpaceCenter_Vessel_t vessel;
krpc_SpaceCenter_ActiveVessel(conn, &vessel);
krpc_SpaceCenter_Parts_t parts;
krpc_SpaceCenter_Vessel_Parts(conn, &parts, vessel);
krpc_list_object_t parachutes;
krpc_SpaceCenter_Parts_Parachutes(conn, ¶chutes, parts);
for (size_t i = 0; i < parachutes.size; i++) {
krpc_SpaceCenter_Parachute_t parachute = parachutes.items[i];
krpc_SpaceCenter_Parachute_Deploy(conn, parachute);
}
}
import krpc.client.Connection;
import krpc.client.RPCException;
import krpc.client.services.SpaceCenter;
import krpc.client.services.SpaceCenter.Parachute;
import krpc.client.services.SpaceCenter.Vessel;
import java.io.IOException;
public class DeployParachutes {
public static void main(String[] args) throws IOException, RPCException {
Connection connection = Connection.newInstance();
SpaceCenter spaceCenter = SpaceCenter.newInstance(connection);
Vessel vessel = spaceCenter.getActiveVessel();
for (Parachute parachute : vessel.getParts().getParachutes()) {
parachute.deploy();
}
connection.close();
}
}
local krpc = require 'krpc'
local conn = krpc.connect('Example')
local vessel = conn.space_center.active_vessel
for _,parachute in ipairs(vessel.parts.parachutes) do
parachute:deploy()
end
import krpc
conn = krpc.connect()
vessel = conn.space_center.active_vessel
for parachute in vessel.parts.parachutes:
parachute.deploy()
‘Control From Here’ for Docking Ports¶
The following example will find a standard sized Clamp-O-Tron docking port, and control the vessel from it:
using System;
using System.Linq;
using KRPC.Client;
using KRPC.Client.Services.SpaceCenter;
class ControlFromHere
{
public static void Main ()
{
using (var conn = new Connection ()) {
var vessel = conn.SpaceCenter ().ActiveVessel;
var part = vessel.Parts.WithTitle ("Clamp-O-Tron Docking Port") [0];
vessel.Parts.Controlling = part;
}
}
}
#include <iostream>
#include <krpc.hpp>
#include <krpc/services/space_center.hpp>
int main() {
krpc::Client conn = krpc::connect();
krpc::services::SpaceCenter space_center(&conn);
auto vessel = space_center.active_vessel();
auto part = vessel.parts().with_title("Clamp-O-Tron Docking Port").front();
vessel.parts().set_controlling(part);
}
#include <krpc_cnano.h>
#include <krpc_cnano/services/space_center.h>
int main() {
krpc_connection_t conn;
krpc_open(&conn, "COM0");
krpc_connect(conn, "ControlFromHere");
krpc_SpaceCenter_Vessel_t vessel;
krpc_SpaceCenter_ActiveVessel(conn, &vessel);
krpc_SpaceCenter_Parts_t parts;
krpc_SpaceCenter_Vessel_Parts(conn, &parts, vessel);
krpc_list_object_t docking_port_parts;
krpc_SpaceCenter_Parts_WithTitle(conn, &docking_port_parts, parts, "Clamp-O-Tron Docking Port");
krpc_object_t part = docking_port_parts.items[0];
krpc_SpaceCenter_Parts_set_Controlling(conn, parts, part);
}
import krpc.client.Connection;
import krpc.client.RPCException;
import krpc.client.services.SpaceCenter;
import krpc.client.services.SpaceCenter.Part;
import krpc.client.services.SpaceCenter.Vessel;
import java.io.IOException;
public class ControlFromHere {
public static void main(String[] args) throws IOException, RPCException {
Connection connection = Connection.newInstance();
Vessel vessel = SpaceCenter.newInstance(connection).getActiveVessel();
Part part = vessel.getParts().withTitle("Clamp-O-Tron Docking Port").get(0);
vessel.getParts().setControlling(part);
connection.close();
}
}
local krpc = require 'krpc'
local conn = krpc.connect()
local vessel = conn.space_center.active_vessel
local part = vessel.parts:with_title('Clamp-O-Tron Docking Port')[1]
vessel.parts.controlling = part
import krpc
conn = krpc.connect()
vessel = conn.space_center.active_vessel
part = vessel.parts.with_title('Clamp-O-Tron Docking Port')[0]
vessel.parts.controlling = part
Combined Specific Impulse¶
The following script calculates the combined specific impulse of all currently active and fueled engines on a rocket. See here for a description of the maths: https://wiki.kerbalspaceprogram.com/wiki/Specific_impulse#Multiple_engines
using System;
using System.Linq;
using KRPC.Client;
using KRPC.Client.Services.SpaceCenter;
class CombinedIsp
{
public static void Main ()
{
using (var connection = new Connection ()) {
var vessel = connection.SpaceCenter ().ActiveVessel;
var activeEngines = vessel.Parts.Engines
.Where (e => e.Active && e.HasFuel).ToList ();
Console.WriteLine ("Active engines:");
foreach (var engine in activeEngines)
Console.WriteLine (" " + engine.Part.Title +
" in stage " + engine.Part.Stage);
double thrust = activeEngines.Sum (e => e.Thrust);
double fuel_consumption =
activeEngines.Sum (e => e.Thrust / e.SpecificImpulse);
double isp = thrust / fuel_consumption;
Console.WriteLine ("Combined vacuum Isp = {0:F0} seconds", isp);
}
}
}
#include <iostream>
#include <vector>
#include <krpc.hpp>
#include <krpc/services/space_center.hpp>
using SpaceCenter = krpc::services::SpaceCenter;
int main() {
auto conn = krpc::connect();
SpaceCenter sc(&conn);
auto vessel = sc.active_vessel();
auto engines = vessel.parts().engines();
std::vector<SpaceCenter::Engine> active_engines;
for (auto engine : engines)
if (engine.active() && engine.has_fuel())
active_engines.push_back(engine);
std::cout << "Active engines:" << std::endl;
for (auto engine : active_engines)
std::cout << " " << engine.part().title() << " in stage "
<< engine.part().stage() << std::endl;
double thrust = 0;
double fuel_consumption = 0;
for (auto engine : active_engines) {
thrust += engine.thrust();
fuel_consumption += engine.thrust() / engine.specific_impulse();
}
double isp = thrust / fuel_consumption;
std::cout << "Combined vacuum Isp = " << isp << " seconds" << std::endl;
}
#include <krpc_cnano.h>
#include <krpc_cnano/services/space_center.h>
int main() {
krpc_connection_t conn;
krpc_open(&conn, "COM0");
krpc_connect(conn, "CombinedISP");
krpc_SpaceCenter_Vessel_t vessel;
krpc_SpaceCenter_ActiveVessel(conn, &vessel);
krpc_SpaceCenter_Parts_t parts;
krpc_SpaceCenter_Vessel_Parts(conn, &parts, vessel);
krpc_list_object_t engines = KRPC_NULL_LIST;
krpc_SpaceCenter_Parts_Engines(conn, &engines, parts);
krpc_list_object_t active_engines = KRPC_NULL_LIST;
active_engines.size = 0;
active_engines.items = krpc_calloc(engines.size, sizeof(krpc_object_t));
for (size_t i = 0; i < engines.size; i++) {
krpc_SpaceCenter_Engine_t engine = engines.items[i];
bool active;
bool has_fuel;
krpc_SpaceCenter_Engine_Active(conn, &active, engine);
krpc_SpaceCenter_Engine_HasFuel(conn, &has_fuel, engine);
if (active && has_fuel) {
active_engines.items[active_engines.size] = engine;
active_engines.size++;
}
}
printf("Active engines:\n");
for (size_t i = 0; i < active_engines.size; i++) {
krpc_SpaceCenter_Engine_t engine = active_engines.items[i];
krpc_SpaceCenter_Part_t part;
krpc_SpaceCenter_Engine_Part(conn, &part, engine);
char * title = NULL;
int stage;
krpc_SpaceCenter_Part_Title(conn, &title, part);
krpc_SpaceCenter_Part_Stage(conn, &stage, part);
printf(" %s in stage %d\n", title, stage);
}
double thrust = 0;
double fuel_consumption = 0;
for (size_t i = 0; i < active_engines.size; i++) {
krpc_SpaceCenter_Engine_t engine = active_engines.items[i];
float engine_thrust;
float engine_isp;
krpc_SpaceCenter_Engine_Thrust(conn, &engine_thrust, engine);
krpc_SpaceCenter_Engine_SpecificImpulse(conn, &engine_isp, engine);
thrust += engine_thrust;
fuel_consumption += engine_thrust / engine_isp;
}
double isp = thrust / fuel_consumption;
printf("Combined vacuum Isp = %.2f seconds\n", isp);
KRPC_FREE_LIST(engines);
KRPC_FREE_LIST(active_engines);
}
import krpc.client.Connection;
import krpc.client.RPCException;
import krpc.client.services.SpaceCenter;
import krpc.client.services.SpaceCenter.Engine;
import krpc.client.services.SpaceCenter.Vessel;
import java.io.IOException;
import java.util.LinkedList;
import java.util.List;
public class CombinedIsp {
public static void main(String[] args) throws IOException, RPCException {
Connection connection = Connection.newInstance();
Vessel vessel = SpaceCenter.newInstance(connection).getActiveVessel();
List<Engine> engines = vessel.getParts().getEngines();
List<Engine> activeEngines = new LinkedList<Engine>();
for (Engine engine : engines) {
if (engine.getActive() && engine.getHasFuel()) {
activeEngines.add(engine);
}
}
System.out.println("Active engines:");
for (Engine engine : activeEngines) {
System.out.println(" " + engine.getPart().getTitle() +
" in stage " + engine.getPart().getStage());
}
double thrust = 0;
double fuelConsumption = 0;
for (Engine engine : activeEngines) {
thrust += engine.getThrust();
fuelConsumption += engine.getThrust() / engine.getSpecificImpulse();
}
double isp = thrust / fuelConsumption;
System.out.printf("Combined vacuum Isp = %.0f\n", isp);
connection.close();
}
}
local krpc = require 'krpc'
local math = require 'math'
local conn = krpc.connect()
local vessel = conn.space_center.active_vessel
local active_engines = {}
for _,engine in ipairs(vessel.parts.engines) do
if engine.active and engine.has_fuel then
table.insert(active_engines, engine)
end
end
print('Active engines:')
for _,engine in ipairs(active_engines) do
print(' ' .. engine.part.title .. ' in stage ' .. engine.part.stage)
end
thrust = 0
fuel_consumption = 0
for _,engine in ipairs(active_engines) do
thrust = thrust + engine.thrust
fuel_consumption = fuel_consumption + engine.thrust / engine.specific_impulse
end
isp = thrust / fuel_consumption
print(string.format('Combined vacuum Isp = %.1f seconds', isp))
import krpc
conn = krpc.connect()
vessel = conn.space_center.active_vessel
active_engines = [e for e in vessel.parts.engines if e.active and e.has_fuel]
print('Active engines:')
for engine in active_engines:
print(' %s in stage %d' % (engine.part.title, engine.part.stage))
thrust = sum(engine.thrust for engine in active_engines)
fuel_consumption = sum(engine.thrust / engine.specific_impulse
for engine in active_engines)
isp = thrust / fuel_consumption
print('Combined vacuum Isp = %d seconds' % isp)