Rendezvous with a Target

This script will rendezvous any rocket with a targetted rocket. The script assumes the rocket is in orbit, planes match and the target is set.

The complete program is available in a variety of languages:

Java, Python

First of all, we start with connecting to the server and initializing MechJeb service instance.

16    public static void main(String[] args) throws IOException, RPCException, StreamException {
17        //This script assumes the vessel is in orbit, planes match and the target is set.
18        try(Connection conn = Connection.newInstance("Rendezvous with a target")) {
19            MechJeb mj = MechJeb.newInstance(conn);

Then, we plan a Hohmann transfer to the target. If anything has gone wrong with the maneuver, we display a warning - the same as MechJeb would. If we don’t select a target, the program throws OperationException.

21            System.out.println("Planning Hohmann transfer");
22            ManeuverPlanner planner = mj.getManeuverPlanner();
23            OperationTransfer hohmann = planner.getOperationTransfer();
24            hohmann.makeNodes();
25
26            //check for warnings
27            String warning = hohmann.getErrorMessage();
28            if(!warning.isEmpty())
29                System.out.println(warning);

After that, we execute the maneuver node we just created. Since we are going to create multiple maneuvers, we create a method to execute maneuver nodes.

31            //execute the nodes
32            NodeExecutor nodeExecutor = mj.getNodeExecutor();
33            RendezvousWithTarget.executeNodes(conn, nodeExecutor);
54    private static void executeNodes(Connection conn, NodeExecutor ne) throws StreamException, RPCException {
55        System.out.println("Executing maneuver nodes");
56        ne.executeAllNodes();
57
58        Stream<Boolean> enabled = conn.addStream(ne, "getEnabled");
59        enabled.setRate(1); //we don't need a high throughput rate, 1 second is more than enough
60        synchronized(enabled.getCondition()) {
61            while(enabled.get())
62                enabled.waitForUpdate();
63        }
64        enabled.remove();
65    }

Although we now have an intercept, the between vessels distance may be too large, so we use OperationCourseCorrection (in Maneuver Planner window known as fine tune closest approach).

35            //fine tune closest approach to the target
36            System.out.println("Correcting course");
37            OperationCourseCorrection fineTuneClosestApproach = planner.getOperationCourseCorrection();
38            fineTuneClosestApproach.setInterceptDistance(50); //50 meters seems to be optimal distance; if you use 0, you can hit the target
39            fineTuneClosestApproach.makeNodes();
40            nodeExecutor.setTolerance(0.01); //do a high-precision maneuver (0.01 dV tolerance)
41            RendezvousWithTarget.executeNodes(conn, nodeExecutor);

Finally, we match speed with the target and close connection when it’s done.

43            System.out.println("Matching speed with the target");
44            OperationKillRelVel matchSpeed = planner.getOperationKillRelVel();
45            matchSpeed.getTimeSelector().setTimeReference(TimeReference.CLOSEST_APPROACH); //match speed at the closest approach
46            matchSpeed.makeNodes();
47            nodeExecutor.setTolerance(0.1); //return the precision back to normal
48            RendezvousWithTarget.executeNodes(conn, nodeExecutor);
49
50            System.out.println("Rendezvous complete!");
51        }
52    }

The vessels should now be next to each other (50m distance), so we can start Docking with a Target.