5
5
import json
6
6
import rospy
7
7
from jsk_2015_05_baxter_apc .msg import WorkOrder , WorkOrderArray
8
- from jsk_apc2016_common import get_bin_contents , get_work_order
8
+ import jsk_apc2016_common
9
+ from jsk_topic_tools .log_utils import jsk_logwarn
9
10
11
+ import numpy as np
10
12
11
- def get_sorted_work_order (json_file ):
13
+
14
+ def get_sorted_work_order (json_file , gripper , object_data ):
12
15
"""Sort work order to maximize the score"""
13
- bin_contents = get_bin_contents (json_file = json_file )
14
- bin_n_contents = dict (map (lambda (bin_ , objects ): (bin_ , len (objects )), bin_contents .iteritems ()))
15
- sorted_work_order = []
16
- work_order = get_work_order (json_file = json_file )
17
- for bin_ , n_contents in sorted (bin_n_contents .items (), key = lambda x : x [1 ]):
18
- if n_contents > 5 : # Level3
19
- continue
20
- sorted_work_order .append ((bin_ , work_order [bin_ ]))
16
+ bin_contents = jsk_apc2016_common .get_bin_contents (json_file = json_file )
17
+ work_order = jsk_apc2016_common .get_work_order (json_file = json_file )
18
+ sorted_bin_list = bin_contents .keys ()
19
+
20
+ if object_data is not None :
21
+ if all (gripper in x for x in [d ['graspability' ].keys () for d in object_data ]):
22
+ def get_graspability (bin_ ):
23
+ target_object = work_order [bin_ ]
24
+ target_object_data = [data for data in object_data
25
+ if data ['name' ] == target_object ][0 ]
26
+ graspability = target_object_data ['graspability' ][gripper ]
27
+ return graspability
28
+ sorted_bin_list = sorted (sorted_bin_list , key = get_graspability )
29
+ else :
30
+ jsk_logwarn ('Not sorted by graspability' )
31
+ jsk_logwarn ('Not all object_data have graspability key: {gripper}'
32
+ .format (gripper = gripper ))
33
+ sorted_bin_list = sorted (sorted_bin_list ,
34
+ key = lambda bin_ : len (bin_contents [bin_ ]))
35
+ sorted_work_order = [(bin_ , work_order [bin_ ]) for bin_ in sorted_bin_list ]
21
36
return sorted_work_order
22
37
23
38
24
- def get_work_order_msg (json_file ):
25
- work_order = get_sorted_work_order (json_file = json_file )
39
+ def get_work_order_msg (json_file , gripper , max_weight , object_data = None ):
40
+ work_order = get_sorted_work_order (json_file , gripper , object_data )
41
+ bin_contents = jsk_apc2016_common .get_bin_contents (json_file = json_file )
42
+ if max_weight == - 1 :
43
+ max_weight = np .inf
26
44
msg = dict (left = WorkOrderArray (), right = WorkOrderArray ())
27
45
abandon_target_objects = [
28
46
'genuine_joe_plastic_stir_sticks' ,
@@ -35,30 +53,47 @@ def get_work_order_msg(json_file):
35
53
'rolodex_jumbo_pencil_cup' ,
36
54
'oreo_mega_stuf'
37
55
]
38
- bin_contents = get_bin_contents (json_file = json_file )
39
56
for bin_ , target_object in work_order :
40
- if target_object in abandon_target_objects :
41
- continue
42
- bin_contents_bool = False
43
- for bin_object in bin_contents [bin_ ]:
44
- if bin_object in abandon_bin_objects :
45
- bin_contents_bool = True
46
- if bin_contents_bool :
57
+ if object_data is not None :
58
+ target_object_data = [data for data in object_data
59
+ if data ['name' ] == target_object ][0 ]
60
+ if target_object_data ['weight' ] > max_weight :
61
+ jsk_logwarn ('Skipping target {obj} in {bin_}: it exceeds max weight {weight} > {max_weight}'
62
+ .format (obj = target_object_data ['name' ], bin_ = bin_ ,
63
+ weight = target_object_data ['weight' ], max_weight = max_weight ))
64
+ continue
65
+ else :
66
+ if target_object in abandon_target_objects :
67
+ jsk_logwarn ('Skipping target {obj} in {bin_}: it is listed as abandon target'
68
+ .format (obj = target_object ['name' ], bin_ = bin_ ))
69
+ continue
70
+ if any (bin_object in abandon_bin_objects for bin_object in bin_contents [bin_ ]):
71
+ jsk_logwarn ('Skipping {bin_}: this bin contains abandon objects' .format (bin_ = bin_ ))
72
+ continue
73
+ if len (bin_contents [bin_ ]) > 5 : # Level3
74
+ jsk_logwarn ('Skipping {bin_}: this bin contains more than 5 objects' .format (bin_ = bin_ ))
47
75
continue
76
+ order = WorkOrder (bin = bin_ , object = target_object )
48
77
if bin_ in 'abdegj' :
49
- msg ['left' ].array .append (WorkOrder ( bin = bin_ , object = target_object ) )
78
+ msg ['left' ].array .append (order )
50
79
elif bin_ in 'cfhikl' :
51
- msg ['right' ].array .append (WorkOrder ( bin = bin_ , object = target_object ) )
80
+ msg ['right' ].array .append (order )
52
81
return msg
53
82
54
83
55
84
def main ():
56
85
json_file = rospy .get_param ('~json' , None )
86
+ is_apc2016 = rospy .get_param ('~is_apc2016' , True )
87
+ gripper = rospy .get_param ('~gripper' , 'gripper2016' )
88
+ max_weight = rospy .get_param ('~max_weight' , - 1 )
57
89
if json_file is None :
58
90
rospy .logerr ('must set json file path to ~json' )
59
91
return
92
+ object_data = None
93
+ if is_apc2016 :
94
+ object_data = jsk_apc2016_common .get_object_data ()
60
95
61
- msg = get_work_order_msg (json_file )
96
+ msg = get_work_order_msg (json_file , gripper , max_weight , object_data )
62
97
63
98
pub_left = rospy .Publisher ('~left_hand' ,
64
99
WorkOrderArray ,
0 commit comments