Docking with a Target
This script will dock any rocket with a targetted rocket. The script assumes the target is a ship and it is next to the target.
The complete program is available in a variety of languages:
First of all, we start with connecting to the server, initializing service instances and getting the active vessel.
15 public static void main(String[] args) throws IOException, RPCException, StreamException {
16 //This script assumes the vessel is next to the target and the target is a ship.
17 try(Connection conn = Connection.newInstance("Docking with a target")) {
18 SpaceCenter sc = SpaceCenter.newInstance(conn);
19 MechJeb mj = MechJeb.newInstance(conn);
20 Vessel active = sc.getActiveVessel();
1import krpc
2
3#This script assumes the vessel is next to the target and the target is a ship.
4conn = krpc.connect(name="Docking with target")
5sc = conn.space_center
6mj = conn.mech_jeb
7active = sc.active_vessel
Then, we set the first docking port as the controlling part, find a free docking port attached to the target vessel and set it as the target.
23 Parts parts = active.getParts();
24 parts.setControlling(parts.getDockingPorts().get(0).getPart());
25
26 System.out.println("Looking for a free docking port attached to the target vessel");
27 for(DockingPort dp : sc.getTargetVessel().getParts().getDockingPorts())
28 if(dp.getDockedPart() == null) {
29 sc.setTargetDockingPort(dp);
30 break;
31 }
9print("Setting the first docking port as the controlling part")
10parts = active.parts
11parts.controlling = parts.docking_ports[0].part
12
13print("Looking for a free docking port attached to the target vessel")
14for dp in sc.target_vessel.parts.docking_ports:
15 if not dp.docked_part:
16 sc.target_docking_port = dp
17 break
Finally, we engage Docking Autopilot and close the connection when it finishes.
34 DockingAutopilot docking = mj.getDockingAutopilot();
35 docking.setEnabled(true);
36
37 Stream<Boolean> enabled = conn.addStream(docking, "getEnabled");
38 enabled.setRate(1); //we don't need a high throughput rate, 1 second is more than enough
39 synchronized(enabled.getCondition()) {
40 while(enabled.get())
41 enabled.waitForUpdate();
42 }
43 enabled.remove();
44
45 System.out.println("Docking complete!");
46 }
47 }
48}
19print("Starting the docking process")
20docking = mj.docking_autopilot
21docking.enabled = True
22
23with conn.stream(getattr, docking, "enabled") as enabled:
24 enabled.rate = 1 #we don't need a high throughput rate, 1 second is more than enough
25 with enabled.condition:
26 while enabled():
27 enabled.wait()
28
29print("Docking complete!")
30conn.close()
The vessels should now be docked.