1111# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
1212# See the License for the specific language governing permissions and
1313# limitations under the License.
14-
1514"""Implementation of PyReach Gym Arm Device."""
1615
1716import copy
@@ -41,17 +40,17 @@ class ReachDeviceArm(reach_device.ReachDevice):
4140
4241 Attributes:
4342 action_space: A Gym action space represented as a Gym Dict Space with
44- "command", "joint_angles", "pose", "synchronous", "id", "controller"
45- and "command" should be 0 for do nothing, 1 for set joint angles and 2
46- for set pose. "joint_angles" should be the desired joint angles in
47- radians. "pose" should be the desired arm pose. "synchronous" is only
48- valid when the arm is configured as asynchronous. When set to 1, a
49- synchronous move is performed. When set to 0 (or not present), an
50- asynchronous move occurs. "id" is used to keep track of asynchronous
51- move status. When id is present and positive, each asynchronous move
52- can be given a unique id (simple counter bumping adequate) to keep
53- track of the returned status for the move. The most recent returned
54- statuses are put into the "responses" portion of the arm observation.
43+ "command", "joint_angles", "pose", "synchronous", "id", "controller" and
44+ "command" should be 0 for do nothing, 1 for set joint angles and 2 for set
45+ pose. "joint_angles" should be the desired joint angles in radians. "pose"
46+ should be the desired arm pose. "synchronous" is only valid when the arm
47+ is configured as asynchronous. When set to 1, a synchronous move is
48+ performed. When set to 0 (or not present), an asynchronous move occurs.
49+ "id" is used to keep track of asynchronous move status. When id is present
50+ and positive, each asynchronous move can be given a unique id (simple
51+ counter bumping adequate) to keep track of the returned status for the
52+ move. The most recent returned statuses are put into the "responses"
53+ portion of the arm observation.
5554 observation_space: A Gym observation space represented as a Gym Dict Space
5655 with "ts", "joint_angles", and "pose" fields.
5756 """
@@ -294,6 +293,7 @@ def get_observation(self,
294293 pyreach_status ))
295294 status : str = pyreach_status .status
296295 error : str = pyreach_status .error
296+ message : str = pyreach_status .message
297297 if status == "done" :
298298 if error == "timeout" :
299299 response = arm_element .ReachResponse .RESPONSE_TIMEOUT
@@ -304,8 +304,8 @@ def get_observation(self,
304304 elif status == "aborted" :
305305 response = arm_element .ReachResponse .RESPONSE_ABORTED
306306 else :
307- logging .warning ("Internal Error: Unexpected response '%s' '%s'" ,
308- status , error )
307+ logging .warning ("Internal Error: Unexpected response '%s' '%s' ' %s' " ,
308+ status , error , message )
309309 response = arm_element .ReachResponse .RESPONSE_FAILED
310310 if not 0 <= response <= arm_element .ReachResponse .RESPONSE_MAX :
311311 raise pyreach .PyReachError (
@@ -361,6 +361,11 @@ def get_observation(self,
361361 ts , arm_state .sequence ),)
362362 return observation , snapshot_reference , tuple (responses )
363363
364+ def synchronize (self ) -> None :
365+ """Synchronously update the arm state."""
366+ if self ._arm :
367+ _ = self ._arm .fetch_state ()
368+
364369 def _get_response_queue (
365370 self
366371 ) -> Tuple [gyms_core .Observation , List [lib_snapshot .SnapshotResponse ]]:
@@ -594,19 +599,24 @@ def do_action(
594599 timeout = timeout ))
595600 return cmd_tuple
596601
597- # Keep track of the callbacks:
598- count = self ._arm_state_capturer .start (action_id )
602+ callback = None
603+ finished_callback = None
604+
605+ if self ._response_queue_length :
606+ # Keep track of the callbacks:
607+ count = self ._arm_state_capturer .start (action_id )
599608
600- def arm_state_callback (arm_state : pyreach .PyReachStatus ) -> None :
601- """Track asynchronous arm state callbacks."""
602- self ._arm_state_capturer .callback (count , arm_state )
609+ def arm_state_callback (arm_state : pyreach .PyReachStatus ) -> None :
610+ """Track asynchronous arm state callbacks."""
611+ self ._arm_state_capturer .callback (count , arm_state )
603612
604- def arm_state_finished_callback () -> None :
605- """Track finished arm state callback."""
606- self ._arm_state_capturer .finished_callback (count )
613+ def arm_state_finished_callback () -> None :
614+ """Track finished arm state callback."""
615+ self ._arm_state_capturer .finished_callback (count )
616+
617+ callback = arm_state_callback
618+ finished_callback = arm_state_finished_callback
607619
608- callback = arm_state_callback
609- finished_callback = arm_state_finished_callback
610620 with self ._timers .select ({"!agent*" , "!gym*" , "host.arm.to_joints" }):
611621 arm .async_to_joints (
612622 joints ,
@@ -662,19 +672,23 @@ def arm_state_finished_callback() -> None:
662672 timeout = timeout )
663673 return cmd_tuple
664674
665- # Keep track of the callbacks:
666- count = self ._arm_state_capturer .start (action_id )
675+ callback = None
676+ finished_callback = None
677+
678+ if self ._response_queue_length :
679+ # Keep track of the callbacks:
680+ count = self ._arm_state_capturer .start (action_id )
667681
668- def arm_state_callback2 (arm_state : pyreach .PyReachStatus ) -> None :
669- """Track asynchronous arm state callbacks."""
670- self ._arm_state_capturer .callback (count , arm_state )
682+ def arm_state_callback2 (arm_state : pyreach .PyReachStatus ) -> None :
683+ """Track asynchronous arm state callbacks."""
684+ self ._arm_state_capturer .callback (count , arm_state )
671685
672- def arm_state_finished_callback2 () -> None :
673- """Track finished arm state callback."""
674- self ._arm_state_capturer .finished_callback (count )
686+ def arm_state_finished_callback2 () -> None :
687+ """Track finished arm state callback."""
688+ self ._arm_state_capturer .finished_callback (count )
675689
676- callback = arm_state_callback2
677- finished_callback = arm_state_finished_callback2
690+ callback = arm_state_callback2
691+ finished_callback = arm_state_finished_callback2
678692
679693 with self ._timers .select ({"!agent*" , "!gym*" , "host.arm.to_pose" }):
680694 arm .async_to_pose (
0 commit comments