robot_motion_observer.py
Go to the documentation of this file.
1 #!/usr/bin/env python
2 # -*- coding: utf-8 -*-
3 
4 #
5 # Copyright © 2018 Pilz GmbH & Co. KG
6 #
7 # Licensed under the Apache License, Version 2.0 (the "License");
8 # you may not use this file except in compliance with the License.
9 # You may obtain a copy of the License at
10 #
11 # http://www.apache.org/licenses/LICENSE-2.0
12 #
13 # Unless required by applicable law or agreed to in writing, software
14 # distributed under the License is distributed on an "AS IS" BASIS,
15 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 # See the License for the specific language governing permissions and
17 # limitations under the License.
18 
19 import rospy
20 import numpy
21 from moveit_commander import RobotCommander, MoveItCommanderException
22 
23 
24 class RobotMotionObserver(object):
25  """Observes motions of the robot.
26 
27  The implementation is based on using the get_current_joint_values()) functionality of the RobotCommander().
28 
29  :param group_name: Name of the planning group, default value is 'manipulator'
30  """
31 
32  _DEFAULT_WAIT_TIME_FOR_MOTION_DETECTION_SEC = 3.0
33  _DEFAULT_TOLERANCE_FOR_MOTION_DETECTION_RAD = 0.01
34  _DEFAULT_SLEEP_INTERVAL_SEC = 0.01
35  _DEFAULT_GROUP_NAME = "manipulator"
36 
37  def __init__(self, group_name=_DEFAULT_GROUP_NAME):
38  self._robot_commander = RobotCommander()
39  self._group_name = group_name
40 
41  @staticmethod
42  def _detect_motion(joint_values_a, joint_values_b, tolerance=_DEFAULT_TOLERANCE_FOR_MOTION_DETECTION_RAD):
43  """TRUE if a significant motion was detected, otherwise FALSE."""
44  return not numpy.allclose(joint_values_a, joint_values_b,
45  atol=tolerance)
46 
48  """Returns the current joint state values of the robot.
49  :raises RobotCurrentStateError if given planning group does not exist.
50  """
51  try:
52  return self._robot_commander.get_group(self._group_name).get_current_joint_values()
53  except MoveItCommanderException as e:
54  rospy.logerr(e.message)
55  raise RobotCurrentStateError(e.message)
56 
57  def is_robot_moving(self,
58  sleep_interval=_DEFAULT_SLEEP_INTERVAL_SEC,
59  move_tolerance=_DEFAULT_TOLERANCE_FOR_MOTION_DETECTION_RAD):
60  """TRUE if the robot is currently moving, otherwise FLASE."""
61  start_position = self._get_current_joint_states()
62  rospy.sleep(sleep_interval)
63  return RobotMotionObserver._detect_motion(start_position, self._get_current_joint_states(), move_tolerance)
64 
65  def wait_motion_start(self,
66  wait_time_out=_DEFAULT_WAIT_TIME_FOR_MOTION_DETECTION_SEC,
67  move_tolerance=_DEFAULT_TOLERANCE_FOR_MOTION_DETECTION_RAD,
68  sleep_interval=_DEFAULT_SLEEP_INTERVAL_SEC):
69  """TRUE if the motion started in the given time interval, otherwise FALSE."""
70 
71  old_joint_values = self._get_current_joint_states()
72  rospy.loginfo("Start joint values: " + str(old_joint_values))
73 
74  motion_started = False
75  start_time = rospy.get_time()
76  rospy.loginfo("Wait until motion starts...")
77 
78  while True:
79  rospy.sleep(sleep_interval)
80  curr_joint_values = self._get_current_joint_states()
81 
82  if RobotMotionObserver._detect_motion(curr_joint_values, old_joint_values, move_tolerance):
83  motion_started = True
84  rospy.loginfo("Changed joint values detected: " + str(curr_joint_values))
85  rospy.loginfo("Motion started.")
86  break
87 
88  if (rospy.get_time() - start_time > wait_time_out):
89  rospy.loginfo("Reached timeout when waiting for motion start.")
90  break
91 
92  return motion_started
93 
94  def wait_motion_stop(self,
95  wait_time_out=_DEFAULT_WAIT_TIME_FOR_MOTION_DETECTION_SEC,
96  move_tolerance=_DEFAULT_TOLERANCE_FOR_MOTION_DETECTION_RAD,
97  sleep_interval=_DEFAULT_SLEEP_INTERVAL_SEC):
98  """TRUE if the motion stopped in the given time interval, otherwise FALSE."""
99 
100  motion_stopped = False
101  start_time = rospy.get_time()
102  rospy.loginfo("Wait until motion stops...")
103 
104  while True:
105 
106  if not self.is_robot_moving(sleep_interval, move_tolerance):
107  motion_stopped = True
108  rospy.loginfo("Motion stopped.")
109  break
110 
111  if (rospy.get_time() - start_time > wait_time_out):
112  rospy.loginfo("Reached timeout when waiting for motion stop.")
113  break
114 
115  return motion_stopped
def wait_motion_start(self, wait_time_out=_DEFAULT_WAIT_TIME_FOR_MOTION_DETECTION_SEC, move_tolerance=_DEFAULT_TOLERANCE_FOR_MOTION_DETECTION_RAD, sleep_interval=_DEFAULT_SLEEP_INTERVAL_SEC)
def wait_motion_stop(self, wait_time_out=_DEFAULT_WAIT_TIME_FOR_MOTION_DETECTION_SEC, move_tolerance=_DEFAULT_TOLERANCE_FOR_MOTION_DETECTION_RAD, sleep_interval=_DEFAULT_SLEEP_INTERVAL_SEC)
def _detect_motion(joint_values_a, joint_values_b, tolerance=_DEFAULT_TOLERANCE_FOR_MOTION_DETECTION_RAD)
def is_robot_moving(self, sleep_interval=_DEFAULT_SLEEP_INTERVAL_SEC, move_tolerance=_DEFAULT_TOLERANCE_FOR_MOTION_DETECTION_RAD)


pilz_industrial_motion_testutils
Author(s):
autogenerated on Mon Apr 6 2020 03:17:28