This guide writes the runtime: a loop that steps the model through four phases: approach, capture, berth, and release. It calls the arm's methods from the last guide and puts the Proximity, Equality Constraints and Video Recording guides to work.
classMissionInput(mojo.UserData):"""Distances, speeds and times that steer the berthing."""PRE_STANDOFF:float=1.5*US.m"""How far in front of the grapple fixture the arm parks."""PARKED_TOLERANCE:float=0.01*US.m"""How close the tip must get to its parking spot to count as parked."""COLINEAR_DISTANCE:float=0.01*US.m"""How far off the fixture's axis the tip may be and still count as lined up."""COLINEAR_ANGLE_DEG:float=1.0"""How far, in degrees, the tip's axis may turn from the fixture's and still count as lined up."""CLEARANCE_RANGE:float=3.0*US.m"""Range of the clearance measurements: when two parts are further apart than this, the distance is only estimated."""GAP_RANGE:float=10.0*US.m"""Range of the gap measurement: when the end effector and the grapple fixture are further apart than this, the distance is only estimated."""RECORD_RANGE:float=2.0*US.m"""The arm camera films only while the gap is below this."""ARM_CAMERA_FRAME_RATE:float=20"""Frame rate of the arm camera's video, in frames per unit of simulation time."""OVERVIEW_FRAME_RATE:float=10"""Frame rate of the overview camera's video, in frames per unit of simulation time."""APPROACH_SPEED:float=0.15*US["m/s"]"""Speed the end effector closes in on the fixture at."""CAPTURE_GAP:float=0.03*US.m"""Gap at which the capture weld is switched on."""OVERSHOOT:float=0.05*US.m"""How far past the fixture the end effector is aimed, so the servos' lag cannot stall it short of the capture gap."""BERTH_SPEED:float=0.2*US["m/s"]"""Speed the arm carries the visitor at."""HOLD_TIME:float=3.0*US.s"""How long the arm holds the visitor at the berthing point before letting go."""SETTLE_TIME:float=2.0*US.s"""How long the run goes on after the release."""END_TIME:float=120.0*US.s"""Longest the run may last."""MISSION=MissionInput()
classPhase(StrEnum):"""The phases of the berthing."""APPROACH="approach""""Move to a spot in front of the grapple fixture and line up with it."""CAPTURE="capture""""Move in slowly while lined up, and switch the weld on."""BERTH="berth""""Carry the visitor to the berthing point and hold it there."""RELEASE="release""""Wait for the visitor to settle once the weld is off."""
Each step, the loop works out how well the end effector lines up with the fixture, acts on the current phase, and steps the model. The full runtime is here:
defruntime(mojo_model:mojo.MojoModel,runtime_manager:rt.RuntimeManager,state:mojo.MjState,*args,**kwargs,)->mojo.MojoModel:"""Runs the berthing through its phases: approach, capture, berth and release. Each part that needs a control has a method for it, and this only calls them."""withruntime_managerasrm:ifrm.signal_manager:rm.signal_manager.record_decimation=10berthing=mojo_model.get_user_data(Berthing)arm,visitor,capture=berthing.arm,berthing.visitor,berthing.capturesite=visitor.fixture.sitearm.register_servos()berthing.keep_arm_clear()berthing.register_proximities()berthing.register_videos(mojo_model,state)dt=state.model.opt.timestepberth=berthing.station.berthphase=Phase.APPROACHstandoff=MISSION.PRE_STANDOFFarrived_at:float|None=Nonereleased_at:float|None=Nonewhilestate.data.time<MISSION.END_TIME:# the end effector must be on the fixture's axis and pointing along it before it goes inoff_axis,angle=arm.misalignment(state,site)lined_up=berthing.lined_up(state)matchphase:casePhase.APPROACH:# the goal follows the fixture, so the arm keeps up if the visitor driftsremaining=arm.approach(state,site,standoff)ifremaining<MISSION.PARKED_TOLERANCEandlined_up:logger.info(f"t={state.data.time:.1f}s: parked, gap {berthing.gap(state):.3f} m, "f"{1000*off_axis:.1f} mm off the fixture's axis and {np.degrees(angle):.2f} degrees "f"out of line, {berthing.clearance(state):.2f} m of clearance")phase=Phase.CAPTUREcasePhase.CAPTURE:# move in only while lined up, aiming a little past the fixture so the servos' lag cannot stall the end effector short of the capture gapiflined_up:standoff=max(standoff-MISSION.APPROACH_SPEED*dt,-MISSION.OVERSHOOT)arm.approach(state,site,standoff)gap=berthing.gap(state)ifgap<MISSION.CAPTURE_GAPandlined_up:capture.engage(state)logger.info(f"t={state.data.time:.1f}s: captured, gap {gap*1000:.1f} mm, "f"{1000*off_axis:.1f} mm off the fixture's axis and {np.degrees(angle):.2f} degrees out of line")# the visitor is held, so its gap and distance to the arm cannot changeberthing.stop_proximities()phase=Phase.BERTHcasePhase.BERTH:# the weld ties the tip to the visitor's grapple site, so putting the tip on the berthing point puts the visitor thereremaining=arm.carry_to(state,berth.rt_pos(state),berth.rt_xmat(state),MISSION.BERTH_SPEED,)ifremaining<MISSION.PARKED_TOLERANCE:ifarrived_atisNone:arrived_at=state.data.timelogger.info(f"t={arrived_at:.1f}s: at the berthing point, holding")ifstate.data.time-arrived_at>MISSION.HOLD_TIME:capture.release(state)released_at=state.data.timelogger.info(f"t={released_at:.1f}s: released")phase=Phase.RELEASEcasePhase.RELEASE:# nothing holds the visitor now, so it should stay where it was putassertreleased_atisnotNoneifstate.data.time-released_at>MISSION.SETTLE_TIME:breakrm.step(state)logger.info(f"finished at t={state.data.time:.1f}s with the weld "f"{'on'ifcapture.is_engaged(state)else'off'}, the visitor's grapple site "f"{1000*np.linalg.norm(site.rt_pos(state)-berth.rt_pos(state)):.1f} mm from the berthing point")returnmojo_model
casePhase.APPROACH:# the goal follows the fixture, so the arm keeps up if the visitor driftsremaining=arm.approach(state,site,standoff)ifremaining<MISSION.PARKED_TOLERANCEandlined_up:logger.info(f"t={state.data.time:.1f}s: parked, gap {berthing.gap(state):.3f} m, "f"{1000*off_axis:.1f} mm off the fixture's axis and {np.degrees(angle):.2f} degrees "f"out of line, {berthing.clearance(state):.2f} m of clearance")phase=Phase.CAPTURE
In the approach, the goal follows the fixture, so the arm keeps up if the visitor drifts. The arm counts as parked when it is close to its parking spot and lined up, which means on the fixture's axis and pointing along it, within the mission's tolerances.
casePhase.CAPTURE:# move in only while lined up, aiming a little past the fixture so the servos' lag cannot stall the end effector short of the capture gapiflined_up:standoff=max(standoff-MISSION.APPROACH_SPEED*dt,-MISSION.OVERSHOOT)arm.approach(state,site,standoff)gap=berthing.gap(state)ifgap<MISSION.CAPTURE_GAPandlined_up:capture.engage(state)logger.info(f"t={state.data.time:.1f}s: captured, gap {gap*1000:.1f} mm, "f"{1000*off_axis:.1f} mm off the fixture's axis and {np.degrees(angle):.2f} degrees out of line")# the visitor is held, so its gap and distance to the arm cannot changeberthing.stop_proximities()phase=Phase.BERTH
The end effector moves in only while it is lined up.
It aims a little past the fixture, so the servos' lag cannot stall it short of the capture gap.
When the gap is small enough and the arm is lined up, capture.engage switches the weld on.
The visitor is then held, so the gap is zero and its distance to the arm cannot change, and stop_proximities stops measuring those.
casePhase.BERTH:# the weld ties the tip to the visitor's grapple site, so putting the tip on the berthing point puts the visitor thereremaining=arm.carry_to(state,berth.rt_pos(state),berth.rt_xmat(state),MISSION.BERTH_SPEED,)ifremaining<MISSION.PARKED_TOLERANCE:ifarrived_atisNone:arrived_at=state.data.timelogger.info(f"t={arrived_at:.1f}s: at the berthing point, holding")ifstate.data.time-arrived_at>MISSION.HOLD_TIME:capture.release(state)released_at=state.data.timelogger.info(f"t={released_at:.1f}s: released")phase=Phase.RELEASEcasePhase.RELEASE:# nothing holds the visitor now, so it should stay where it was putassertreleased_atisnotNoneifstate.data.time-released_at>MISSION.SETTLE_TIME:break
The weld ties the end effector to the visitor's grapple site, so putting the end effector on the berthing point puts the visitor there.
carry_to moves the goal to the berthing point in a straight line at a set speed.
After a hold, capture.release switches the weld off.
The release phase waits for the visitor to settle, and then the loop ends.
Proximity measures ten pairs of mesh volumes, nine for the clearance and one for the gap.
The clearance pairs are everything the arm must keep clear of:
the first boom, the second boom and the free end effector, each against the visitor's hull
the same three parts, each against the station's cylinder
the arm against itself: the free end effector against the first boom and the base end effector, and the second boom against the base end effector. Parts that meet at a joint are left out, since they would always read zero.
The gap pair is the free end effector against the visitor's grapple fixture. clearance is the smallest of the nine clearance pairs, and a value of zero means two parts touch.
Once the weld is on, the visitor moves with the arm, so the gap and the hull cannot change any more. stop_proximities then sets the active flag of those pairs to False. The station and arm pairs keep measuring, since the arm still has to clear the station while it carries the visitor.
The housings at the joints are primitive shapes, so these pairs do not see them. mink does, since it works from the whole model, which is why the arm also steers clear of the station and the hull on its own.
_hull_clearances:list[mojo.utils.Proximity]=PrivateAttr(default_factory=list)"""Distances from each boom and the free end effector to the hull, which stop mattering once the visitor is held."""_other_clearances:list[mojo.utils.Proximity]=PrivateAttr(default_factory=list)"""Distances from the arm to the station and to itself, which matter until the end."""_gap:mojo.utils.Proximity|None=PrivateAttr(default=None)"""Distance from the free end effector to the visitor's grapple fixture."""defregister_proximities(self)->None:"""Starts measuring and logging the clearance of the arm from the hull, the station and itself, and the gap to the grapple fixture. Call it inside the runtime manager's `with` block."""arm,visitor,station=self.arm,self.visitor,self.station# `Proximity` works on mesh volumes, which is why these parts are meshesdefmeasure(one:mojo.GeomMesh,other:mojo.GeomMesh,visualize:bool=False)->mojo.utils.Proximity:returnmojo.utils.Proximity(volume_1=one,volume_2=other,dist_max=MISSION.CLEARANCE_RANGE,visualize=visualize,).register_to_rm()moving=(arm.boom_1,arm.boom_2,arm.free_effector.geom)self._hull_clearances=[measure(part,visitor.hull,visualize=True)forpartinmoving]self._other_clearances=[*(measure(part,station.cylinder)forpartinmoving),# parts that meet at a joint are left out, since they would always read zeromeasure(arm.free_effector.geom,arm.boom_1),measure(arm.free_effector.geom,arm.base_effector.geom),measure(arm.boom_2,arm.base_effector.geom),]self._gap=mojo.utils.Proximity(volume_1=arm.free_effector.geom,volume_2=visitor.fixture.geom,dist_max=MISSION.GAP_RANGE,).register_to_rm()forproximityin(*self._hull_clearances,*self._other_clearances,self._gap):proximity.request(channels=["dist","fromto","prox_type"])defgap(self,state:mojo.MjState)->float:"""Distance from the free end effector to the visitor's grapple fixture."""assertself._gap# made by register_proximitiesreturnself._gap.get_proximity(state)[0]defclearance(self,state:mojo.MjState)->float:"""Distance from the arm to the closest thing it must keep clear of, which is zero if two of them touch."""returnmin(proximity.get_proximity(state)[0]forproximityin(*self._hull_clearances,*self._other_clearances)ifproximity.active)@propertydeftracking(self)->bool:"""Whether the gap and the hull distances are still being measured."""returnself._gapisnotNoneandself._gap.activedefstop_proximities(self)->None:"""Stops measuring, drawing and logging the gap and the hull distances. Call it once the weld is on: the visitor is held, so there is nothing left to watch there."""assertself._gap# made by register_proximitiesforproximityin(*self._hull_clearances,self._gap):proximity.active=False
The capture weld joins the end effector's site to the visitor's grapple site. It is off until the capture, as in the Equality Constraints guide, and a small class wraps its three actions:
classCapture(mojo.UserData):"""A weld between the end effector's tip and the visitor's grapple site, switched off until the capture."""weld:mojo.EqualityWeldSite"""The weld, inactive at the start."""defengage(self,state:mojo.MjState)->None:"""Switches the weld on, so the visitor goes wherever the arm goes."""self.weld.enable(state)defrelease(self,state:mojo.MjState)->None:"""Switches the weld off, which lets the visitor go."""self.weld.disable(state)defis_engaged(self,state:mojo.MjState)->bool:"""Whether the weld is on."""returnself.weld.is_active(state)@classmethoddefnew(cls,mojo_model:mojo.MojoModel,arm:Arm,visitor:Visitor)->Self:"""Makes the weld and adds it to the model."""tip=arm.free_effector.tip.namegrapple=visitor.fixture.site.nameasserttipassertgrappleweld=mojo.EqualityWeldSite(name=mojo.EqualityName("capture"),site1=tip,site2=grapple,active=False,)mojo_model.mjcf.equalities.append(mojo.Equality(welds=[weld]))returncls(weld=weld)
Video Recording films two cameras, the arm camera and the overview camera, into the trial's folder. Only the nominal trial films. Each frame carries a label with the time, the gap, the clearance and whether the weld is on. After the capture the gap is no longer measured, so the label drops it.
The overview camera records the whole run. The arm camera starts the first time the end effector lines up with the fixture and keeps going from there, since the arm can drift out of tolerance for a moment as it starts to move.
defregister_videos(self,mojo_model:mojo.MojoModel,state:mojo.MjState)->None:"""Films both cameras into the trial's folder, with a label on every frame. Only the nominal trial films. Call it inside the runtime manager's `with` block, after `register_proximities`."""ifnotmojo_model.is_nominal:returnarm_camera,overview_camera=self.arm.camera.name,self.overview.camera.nameassertarm_cameraassertoverview_cameradeflabel(state:mojo.MjState)->rt.LabelConfig:weld="on"ifself.capture.is_engaged(state)else"off"gap=f"gap = {self.gap(state):5.2f} m "ifself.trackingelse""return{"text":f"t = {state.data.time:6.1f} s {gap}clearance = {self.clearance(state):4.2f} m weld {weld}","background_color":mojo.utils.Color.SLATE_900.with_alpha(0.7),}# the clip starts when first lined up and then carries on, since the arm can drift out of tolerance for a moment as it starts to movestarted=Falsedefarm_camera_trigger(state:mojo.MjState)->bool:nonlocalstartedstarted=startedorself.lined_up(state)returnstartedand(notself.trackingorself.gap(state)<MISSION.RECORD_RANGE)rt.VideoRecorder(path=mojo_model.trial_dir/"arm_camera.mp4",camera_name=arm_camera,frame_rate=MISSION.ARM_CAMERA_FRAME_RATE,show_proximities=True,recording_trigger=arm_camera_trigger,frame_label=label,).setup(state).register_to_rm()rt.VideoRecorder(path=mojo_model.trial_dir/"overview.mp4",camera_name=overview_camera,frame_rate=MISSION.OVERVIEW_FRAME_RATE,show_proximities=True,frame_label=label,).setup(state).register_to_rm()
Run the Reloaded script from the example's folder:
Bash
./reloaded.sh
The runtime logs each milestone: parked, captured, at the berthing point, released and finished. The last line says how far the visitor's grapple site ended up from the berthing point.
Success
That completes the berthing walkthrough. Change the inputs in mission.py and see how the run changes.