@@ -42,6 +42,84 @@ void TopShellPlugin::PreUpdate(
4242 const gz::sim::UpdateInfo &_info,
4343 gz::sim::EntityComponentManager &_ecm)
4444{
45+ if (teleport_state != TopShellTeleportState::FINISHED){
46+ switch (teleport_state){
47+ case TopShellTeleportState::IDLE:
48+ break ;
49+ case TopShellTeleportState::READY:
50+ {
51+ if (bottom_shell_entity == gz::sim::kNullEntity ){
52+ break ;
53+ }
54+
55+ gz::sim::Entity bottom_shell_has_parent = false ;
56+ _ecm.Each <gz::sim::components::DetachableJoint>(
57+ [&](
58+ const gz::sim::Entity &entity,
59+ const gz::sim::components::DetachableJoint *detachable_joint
60+ ) -> bool {
61+ if (detachable_joint->Data ().childLink == gz::sim::Model (bottom_shell_entity).LinkByName (_ecm, " base_link" )){
62+ bottom_shell_has_parent = true ;
63+ return false ;
64+ }
65+ return true ;
66+ }
67+ );
68+
69+ if (bottom_shell_has_parent){
70+ break ;
71+ }
72+
73+ std::optional<gz::math::Pose3d> bottom_shell_pose = gz::sim::Link (gz::sim::Model (bottom_shell_entity).LinkByName (_ecm, " base_link" )).WorldPose (_ecm);
74+
75+ if (!bottom_shell_pose.has_value ()){
76+ break ;
77+ }
78+
79+ gz::math::Pose3d target_pose = flipped_bottom_shell_pose;
80+
81+ target_pose.SetX (bottom_shell_pose.value ().X ());
82+ target_pose.SetY (bottom_shell_pose.value ().Y ());
83+ target_pose.SetZ (bottom_shell_pose.value ().Z ());
84+ gz::sim::Model (bottom_shell_entity).SetWorldPoseCmd (_ecm, target_pose);
85+ teleport_state = TopShellTeleportState::JOINT_NEEDED;
86+
87+ teleport_step = _info.iterations ;
88+
89+ break ;
90+ }
91+ case TopShellTeleportState::JOINT_NEEDED:
92+
93+ lock_joint = _ecm.CreateEntity ();
94+
95+ _ecm.CreateComponent (lock_joint, gz::sim::components::DetachableJoint ({
96+ section_3_link_entity,
97+ gz::sim::Model (bottom_shell_entity).LinkByName (_ecm, " base_link" ),
98+ " fixed" }));
99+
100+ teleport_state = TopShellTeleportState::JOINT_REMOVAL;
101+
102+ break ;
103+ case TopShellTeleportState::JOINT_REMOVAL:
104+ if (_info.iterations - teleport_step < 100 ){
105+ break ;
106+ }
107+
108+ _ecm.RequestRemoveEntity (lock_joint);
109+
110+ lock_joint = gz::sim::kNullEntity ;
111+
112+ teleport_state = TopShellTeleportState::FINISHED;
113+ break ;
114+
115+ case TopShellTeleportState::FINISHED:
116+ break ;
117+
118+ default :
119+ break ;
120+ }
121+ }
122+
45123 if (lock_state == TopShellLockState::LOCKED){
46124 return ;
47125 }
@@ -79,82 +157,6 @@ void TopShellPlugin::PreUpdate(
79157
80158 lock_state = TopShellLockState::LOCKED;
81159 }
82-
83- switch (teleport_state){
84- case TopShellTeleportState::IDLE:
85- break ;
86- case TopShellTeleportState::READY:
87- {
88- if (bottom_shell_entity == gz::sim::kNullEntity ){
89- break ;
90- }
91-
92- gz::sim::Entity bottom_shell_has_parent = false ;
93- _ecm.Each <gz::sim::components::DetachableJoint>(
94- [&](
95- const gz::sim::Entity &entity,
96- const gz::sim::components::DetachableJoint *detachable_joint
97- ) -> bool {
98- if (detachable_joint->Data ().childLink == gz::sim::Model (bottom_shell_entity).LinkByName (_ecm, " base_link" )){
99- bottom_shell_has_parent = true ;
100- return false ;
101- }
102- return true ;
103- }
104- );
105-
106- if (bottom_shell_has_parent){
107- break ;
108- }
109-
110- std::optional<gz::math::Pose3d> bottom_shell_pose = gz::sim::Link (gz::sim::Model (bottom_shell_entity).LinkByName (_ecm, " base_link" )).WorldPose (_ecm);
111-
112- if (!bottom_shell_pose.has_value ()){
113- break ;
114- }
115-
116- gz::math::Pose3d target_pose = flipped_bottom_shell_pose;
117-
118- target_pose.SetX (bottom_shell_pose.value ().X ());
119- target_pose.SetY (bottom_shell_pose.value ().Y ());
120- target_pose.SetZ (bottom_shell_pose.value ().Z ());
121- gz::sim::Model (bottom_shell_entity).SetWorldPoseCmd (_ecm, target_pose);
122- teleport_state = TopShellTeleportState::JOINT_NEEDED;
123-
124- teleport_step = _info.iterations ;
125-
126- break ;
127- }
128- case TopShellTeleportState::JOINT_NEEDED:
129-
130- lock_joint = _ecm.CreateEntity ();
131-
132- _ecm.CreateComponent (lock_joint, gz::sim::components::DetachableJoint ({
133- section_3_link_entity,
134- gz::sim::Model (bottom_shell_entity).LinkByName (_ecm, " base_link" ),
135- " fixed" }));
136-
137- teleport_state = TopShellTeleportState::JOINT_REMOVAL;
138-
139- break ;
140- case TopShellTeleportState::JOINT_REMOVAL:
141- if (_info.iterations - teleport_step < 100 ){
142- break ;
143- }
144-
145- _ecm.RequestRemoveEntity (lock_joint);
146-
147- lock_joint = gz::sim::kNullEntity ;
148-
149- teleport_state = TopShellTeleportState::FINISHED;
150- break ;
151-
152- case TopShellTeleportState::FINISHED:
153- break ;
154-
155- default :
156- break ;
157- }
158160}
159161
160162gz::sim::Entity TopShellPlugin::get_base_module_shell (
0 commit comments