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:
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);
1import krpc
13#This script assumes the vessel is in orbit, planes match and the target is set.
14conn = krpc.connect(name="Rendezvous with target")
15mj = conn.mech_jeb
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);
17print("Planning Hohmann transfer")
18planner = mj.maneuver_planner
19hohmann = planner.operation_transfer
20hohmann.make_nodes()
21
22#check for warnings
23warning = hohmann.error_message
24if warning:
25 print(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 }
27#execute the nodes
28executor = mj.node_executor
29execute_nodes()
3def execute_nodes():
4 print("Executing maneuver nodes")
5 executor.execute_all_nodes()
6
7 with conn.stream(getattr, executor, "enabled") as enabled:
8 enabled.rate = 1 #we don't need a high throughput rate, 1 second is more than enough
9 with enabled.condition:
10 while enabled():
11 enabled.wait()
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);
31#fine tune closest approach to the target
32print("Correcting course")
33fineTuneClosestApproach = planner.operation_course_correction
34fineTuneClosestApproach.intercept_distance = 50 #50 meters seems to be optimal distance; if you use 0, you can hit the target
35fineTuneClosestApproach.make_nodes()
36executor.tolerance = 0.01 #do a high-precision maneuver (0.01 dV tolerance)
37execute_nodes()
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 }
39print("Matching speed with the target")
40matchSpeed = planner.operation_kill_rel_vel
41matchSpeed.time_selector.time_reference = mj.TimeReference.closest_approach #match speed at the closest approach
42matchSpeed.make_nodes()
43executor.tolerance = 0.1 #return the precision back to normal
44execute_nodes()
45
46print("Rendezvous complete!")
47conn.close()
The vessels should now be next to each other (50m distance), so we can start Docking with a Target.