Skip to content

Commit 2b29d17

Browse files
authored
modify manipulation states to add timeout and other parameters (#11)
* modify manipulation states * add most basic state import tests * add timeout outcome to constructor
1 parent 6e4e944 commit 2b29d17

16 files changed

+164
-33
lines changed

flexbe_manipulation_states/src/flexbe_manipulation_states/get_joint_values_dyn_state.py

Lines changed: 29 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -15,32 +15,45 @@
1515
class GetJointValuesDynState(EventState):
1616
'''
1717
Retrieves current values of specified joints.
18+
In this version, specified joint names are passed by user data
19+
20+
-- timeout double Timeout value (optional)
21+
22+
-- joint_states_topic string Optional name of joint states topic
23+
(default: /joint_states)
1824
1925
># joint_names string[] List of desired joint names.
2026
2127
#> joint_values float[] List of current joint values.
2228
2329
<= retrieved Joint values are available.
30+
<= timeout Joint values are not available.
2431
2532
'''
2633

27-
def __init__(self):
34+
def __init__(self, timeout=None, joint_states_topic='/joint_states'):
2835
'''
2936
Constructor
3037
'''
3138
super(GetJointValuesDynState, self).__init__(
32-
outcomes=['retrieved'],
39+
outcomes=['retrieved', 'timeout'],
3340
output_keys=['joint_values'],
3441
input_keys=['joint_names'])
35-
36-
self._topic = '/joint_states'
42+
43+
self._topic = joint_states_topic
3744
self._sub = ProxySubscriberCached({self._topic: JointState})
3845

3946
self._joints = None
4047
self._joint_values = list()
41-
42-
48+
self._return_code = None
49+
self._timeout = timeout
50+
51+
4352
def execute(self, userdata):
53+
if (self._return_code is not None):
54+
# Handle blocked transition or error during on_enter
55+
return self._return_code
56+
4457
while self._sub.has_buffered(self._topic):
4558
msg = self._sub.get_from_buffer(self._topic)
4659
for i in range(len(msg.name)):
@@ -50,14 +63,22 @@ def execute(self, userdata):
5063

5164
if all(v is not None for v in self._joint_values):
5265
userdata.joint_values = self._joint_values
66+
self._return_code = 'retrieved'
5367
return 'retrieved'
5468

55-
69+
if (self._timeout is not None and \
70+
(Time.now()-self._start_time) > rospy.Duration(self._timeout) ):
71+
self._return_code = 'timeout'
72+
return 'timeout'
73+
74+
5675
def on_enter(self, userdata):
5776
self._sub.enable_buffer(self._topic)
5877
self._joint_values = [None] * len(self._joints)
5978
self._joints = userdata.joint_names
60-
79+
self._return_code = None
80+
self._start_time = rospy.Time.now()
81+
userdata.joint_values = None
6182

6283
def on_exit(self, userdata):
6384
self._sub.disable_buffer(self._topic)

flexbe_manipulation_states/src/flexbe_manipulation_states/get_joint_values_state.py

Lines changed: 27 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -15,30 +15,40 @@
1515
class GetJointValuesState(EventState):
1616
'''
1717
Retrieves current values of specified joints.
18+
Joints are specified by parameter list.
1819
19-
-- joints string[] List of desired joint names.
20+
-- joints string[] List of desired joint names.
21+
-- timeout double Timeout value (optional)
22+
-- joint_states_topic string Optional name of joint states topic
23+
(default: /joint_states)
2024
2125
#> joint_values float[] List of current joint values.
2226
2327
<= retrieved Joint values are available.
28+
<= timeout Joint values are not available.
2429
2530
'''
2631

27-
def __init__(self, joints):
32+
def __init__(self, joints, timeout=None, joint_states_topic='/joint_states'):
2833
'''
2934
Constructor
3035
'''
31-
super(GetJointValuesState, self).__init__(outcomes=['retrieved'],
32-
output_keys=['joint_values'])
33-
34-
self._topic = '/joint_states'
36+
super(GetJointValuesState, self).__init__(outcomes=['retrieved', 'timeout'],
37+
output_keys=['joint_values'])
38+
39+
self._topic = joint_states_topic
3540
self._sub = ProxySubscriberCached({self._topic: JointState})
3641

3742
self._joints = joints
3843
self._joint_values = list()
39-
40-
44+
self._timeout = timeout
45+
46+
4147
def execute(self, userdata):
48+
if (self._return_code is not None):
49+
# Handle blocked transition or error during on_enter
50+
return self._return_code
51+
4252
while self._sub.has_buffered(self._topic):
4353
msg = self._sub.get_from_buffer(self._topic)
4454
for i in range(len(msg.name)):
@@ -48,12 +58,20 @@ def execute(self, userdata):
4858

4959
if all(v is not None for v in self._joint_values):
5060
userdata.joint_values = self._joint_values
61+
self._return_code = 'retrieved'
5162
return 'retrieved'
5263

53-
64+
if (self._timeout is not None and \
65+
(Time.now()-self._start_time) > rospy.Duration(self._timeout) ):
66+
self._return_code = 'timeout'
67+
return 'timeout'
68+
5469
def on_enter(self, userdata):
5570
self._sub.enable_buffer(self._topic)
5671
self._joint_values = [None] * len(self._joints)
72+
self._return_code = None
73+
self._start_time = rospy.Time.now()
74+
userdata.joint_values = None
5775

5876
def on_exit(self, userdata):
5977
self._sub.disable_buffer(self._topic)

flexbe_manipulation_states/src/flexbe_manipulation_states/get_joints_from_srdf_group.py

Lines changed: 28 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -14,14 +14,17 @@
1414

1515
class GetJointsFromSrdfGroup(EventState):
1616
'''
17-
Simple state to look up a pre-defined joint configuration from the given joint group in a SRDF file.
17+
Simple state to look up a pre-defined list of joint names from a given
18+
joint group defined in a SRDF file.
1819
This state is recommended if you only need these values without any unnecessary overhead.
1920
2021
-- move_group string Name of the move group of interest.
2122
e.g., "my_moveit_config/config/my_robot.srdf"
2223
-- robot_name string Optional name of the robot to be used.
2324
If left empty, the first one found will be used
2425
(only required if multiple robots are specified in the same file).
26+
-- srdf_name string Optional name of the srdf parameter
27+
(default: "/robot_description_semantic")
2528
2629
#> joint_names string[] List of joint values for the requested group.
2730
@@ -30,30 +33,32 @@ class GetJointsFromSrdfGroup(EventState):
3033
3134
'''
3235

33-
def __init__(self, move_group, robot_name=""):
36+
def __init__(self, move_group, robot_name="", srdf_name = "/robot_description_semantic"):
3437
'''
3538
Constructor
3639
'''
3740
super(GetJointsFromSrdfGroup, self).__init__(outcomes=['retrieved', 'param_error'],
38-
output_keys=['joint_names'])
41+
output_keys=['joint_names'])
3942

4043
self._move_group = move_group
4144
self._robot_name = robot_name
45+
self._srdf_name = srdf_name
4246

43-
# Check existence of SRDF parameter.
44-
# Anyways, values will only be read during runtime to allow modifications.
4547
self._srdf_param = None
46-
if rospy.has_param("/robot_description_semantic"):
47-
self._srdf_param = rospy.get_param("/robot_description_semantic")
48+
if rospy.has_param(self._srdf_name):
49+
self._srdf_param = rospy.get_param(self._srdf_name)
4850
else:
49-
Logger.logerr('Unable to get parameter: /robot_description_semantic')
51+
Logger.logerr('Unable to get parameter: ' + self._srdf_name)
5052

5153
self._file_error = False
5254
self._srdf = None
55+
self._return_code = None
5356

54-
5557
def execute(self, userdata):
5658

59+
if (self._return_code is not None):
60+
# Handle blocked transition or error during on_enter
61+
return self._return_code
5762

5863
robot = None
5964
for r in self._srdf.iter('robot'):
@@ -62,6 +67,7 @@ def execute(self, userdata):
6267
break
6368
if robot is None:
6469
Logger.logwarn('Did not find robot name in SRDF: %s' % self._robot_name)
70+
self._return_code = 'param_error'
6571
return 'param_error'
6672

6773
group = None
@@ -71,25 +77,33 @@ def execute(self, userdata):
7177
break
7278
if group is None:
7379
Logger.logwarn('Did not find group name in SRDF: %s' % self._move_group)
80+
self._return_code = 'param_error'
7481
return 'param_error'
7582

7683
try:
7784
userdata.joint_names = [str(j.attrib['name']) for j in group.iter('joint')]
7885
except Exception as e:
7986
Logger.logwarn('Unable to parse joint values from SRDF:\n%s' % str(e))
87+
self._return_code = 'param_error'
8088
return 'param_error'
8189

90+
self._return_code = 'retrieved'
8291
return 'retrieved'
8392

84-
93+
8594
def on_enter(self, userdata):
86-
# Parameter check
87-
if self._srdf_param is None:
88-
self._param_error = True
95+
# Check existence of SRDF parameter.
96+
# Values are read during runtime to allow modifications.
97+
if rospy.has_param(self._srdf_name):
98+
self._srdf_param = rospy.get_param(self._srdf_name)
99+
else:
100+
Logger.logerror("Unable to get parameter: "+self._srdf_name)
101+
self._return_code = 'param_error'
89102
return
90103

104+
# Parse the SRDF string
91105
try:
92106
self._srdf = ET.fromstring(self._srdf_param)
93107
except Exception as e:
94108
Logger.logwarn('Unable to parse given SRDF parameter: /robot_description_semantic')
95-
self._param_error = True
109+
self._return_code = 'param_error'
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
# Declare state to be tested
2+
path: flexbe_manipulation_states.get_joint_values_dyn_state
3+
class: GetJointValuesDynState
4+
5+
import_only: True
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
# Declare state to be tested
2+
path: flexbe_manipulation_states.get_joint_values_state
3+
class: GetJointValuesState
4+
5+
import_only: True
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
# Declare state to be tested
2+
path: flexbe_manipulation_states.get_joints_from_srdf_group
3+
class: GetJointsFromSrdfGroup
4+
5+
import_only: True
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
# Declare state to be tested
2+
path: flexbe_manipulation_states.get_joints_from_srdf_state
3+
class: GetJointsFromSrdfState
4+
5+
import_only: True
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
# Declare state to be tested
2+
path: flexbe_manipulation_states.joint_state_to_moveit
3+
class: JointStateToMoveit
4+
5+
import_only: True
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
# Declare state to be tested
2+
path: flexbe_manipulation_states.moveit_to_joints_dyn_state
3+
class: MoveitToJointsDynState
4+
5+
import_only: True
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
# Declare state to be tested
2+
path: flexbe_manipulation_states.moveit_to_joints_state
3+
class: MoveitToJointsState
4+
5+
import_only: True

0 commit comments

Comments
 (0)