18 import numpy,copy, pickle
20 import general_python
as genpy
23 from packages.matej.artificial_intelligence.data_mining.kdtree
import kdtree
29 FILE_DATA = open(path_and_file_name,
"r")
30 work_space = pickle.load(FILE_DATA)
35 'NA' : 'Not Applicable',
39 'JSG' : 'Joint Space Grid',
40 'PCL' : 'Predefined Config List',
41 'NTP' : 'Nearest to Target Pose',
42 'NCC' : 'Nearest to Current Configuration',
43 'CLO' : 'Config List Order',
47 'Nearest to Target Pose' : 'NTP',
48 'Nearest to Current Configuration' : 'NCC',
58 def __init__( self, creation_method = 'JSG', grid_resolution = 2, representation_of_orientation = 'angular_spherical', number_of_search_items = 1, number_of_configs = 10, search_criteria = 'Nearest to Target Pose'):
61 creation_method: Configuration Generation Method
65 'Joint Space Grid' (key = 'JSG'): Initial Configurations generated by FK computations of configs of a grid of jointspace
66 'Random' (key = 'RND'): Initial Configurations generated randomly (Additional time required only for generating random configurations)
67 'Constant Set' (key = 'CST'): Initial Configurations read from a list of Configurations corresponding to a "Constant Set of Poses" (No additional time required)
109 s +=
"Workspace Settings: " +
"\n" +
"\n"
122 This class inherits DH parameters from class: "Manipulator_Geometry"
123 It contains pre-computed kinematic properties of a manipulator in a discretized grid of the jointspace.
124 These properties are in two equivalent lists named "config_list" and "pose_list"
127 def __init__( self, config_settings, geo_settings, end_settings, ws_settings):
130 super(Workspace, self).
__init__(config_settings, geo_settings, end_settings)
150 Generate a number of random configs in the feasible range of the jointspace and computes the forward kinematic of each configuration.
151 The number of random points generated are determined by property: "self.ws_settings.number_of_configs"
152 The configurations are stored in property "self.config_list" and the corresponding poses are stored in property "self.pose_list"
153 A pose contains 3 elements for each position (reference_position) and 3 elements for each orientation (reference_orientation)
155 A nun-redundant parametrization for orientation should be introduced by property "self.representation_of_orientation"
158 p = self.ws_settings.number_of_configs
160 for i
in range(0, p):
161 print 'Creating Workspace .... ' + str(i) +
' out of: ' + str (p)
163 assert self.set_config(self.random_config())
166 for tp in self.task_point:
167 H = self.transfer_matrices()
172 ef_pose = self.pose_to_tuple(
'actual', self.ws_settings.representation_of_orientation)
174 self.pose_list.append(ef_pose)
175 self.config_list.append(self.free_config(self.q))
178 config_tuple_list = [ tuple(c)
for c
in self.
config_list ]
179 config_set = set(config_tuple_list)
181 self.
pose_tree = kdtree.KDTree.construct_from_data(list(pose_set))
182 self.
config_tree = kdtree.KDTree.construct_from_data(list(config_set))
186 Generate a lattice in the jointspace and computes the forward kinematic of each configuration.
187 The feasible range of each jach joint is divided by "self.ws_settings.grid_resolution" + 1 intervals.
188 The configurations are sstored in property "self.config_list" and the corresponding poses are stored in property "self.pose_list"
189 A pose contains 3 elements for each position (reference_position) and 3 elements for each orientation (reference_orientation)
191 A nun-redundant parametrization for orientation should be introduced by property "self.representation_of_orientation"
195 p = self.ws_settings.grid_resolution ** self.config_settings.DOF
196 self.ws_settings.number_of_configs = p
198 fwd_kin.take_to_grid_configuration(0, self.ws_settings.grid_resolution)
199 endeff.update(fwd_kin)
201 for tp in endeff.reference_positions:
202 tp.ru = numpy.copy(tp.ra)
203 tp.rl = numpy.copy(tp.ra)
205 for i
in range(0, p):
206 print 'Creating Workspace .... ' + str(i) +
' out of: ' + str (p)
208 assert self.set_config(self.grid_config(i, self.ws_settings.grid_resolution))
210 for tp in self.task_point:
212 if (tp.ra[j] > tp.ru[j]):
215 if (tp.ra[j] < tp.rl[j]):
218 ef_pose = self.pose_to_tuple(
'actual', self.ws_settings.representation_of_orientation)
220 self.pose_list.append(ef_pose)
221 self.config_list.append(self.free_config(self.q))
225 for tp in endeff.reference_positions:
227 self.lower_pose += tp.rl[j],
228 self.upper_pose += tp.ru[j],
232 config_tuple_list = [ tuple(c)
for c
in self.
config_list ]
233 config_set = set(config_tuple_list)
235 self.
pose_tree = kdtree.KDTree.construct_from_data(list(pose_set))
236 self.
config_tree = kdtree.KDTree.construct_from_data(list(config_set))
238 self.
file_name = self.ws_settings.gen_file_name(self.geo_settings.name)
241 print "Workspace Creation Started. Creation Method: ",self.ws_settings.creation_method
242 if self.ws_settings.creation_method ==
'JSG':
244 elif self.ws_settings.creation_method ==
'RND':
246 elif self.ws_settings.creation_method ==
'PCL':
247 if not self.ws_settings.predefined_config_list ==
None:
249 p = len(self.ws_settings.predefined_config_list)
250 for q
in self.ws_settings.predefined_config_list:
251 print 'Creating Workspace .... ' + str(i) +
' out of: ' + str (p)
252 qf = self.free_config(q)
253 assert self.set_config(qf)
254 self.config_list.append(qf)
255 ef_pose = self.pose_to_tuple(
'actual', self.ws_settings.representation_of_orientation)
256 self.pose_list.append(ef_pose)
257 self.ws_settings.number_of_configs = len(self.
config_list)
260 config_tuple_list = [ tuple(c)
for c
in self.
config_list ]
261 config_set = set(config_tuple_list)
263 self.
pose_tree = kdtree.KDTree.construct_from_data(list(pose_set))
264 self.
config_tree = kdtree.KDTree.construct_from_data(list(config_set))
266 assert False, genpy.err_str(__name__, self.__class__.__name__,
'create',
'predefined_config_list has not been set. Workspace can NOT be created !')
268 assert False, genpy.err_str(__name__, self.__class__.__name__,
'create', self.ws_settings.creation_method+
' is not a valid value for creation_method')
272 endeff contains the target pose
274 Return a list of configurations which:
275 Their corresponding poses are closest to the target pose
277 QUESTION : configs_nearest_to_target( EE, distance ? ) ??
279 Ns = self.ws_settings.number_of_search_items
280 Nc = self.ws_settings.number_of_configs
284 target_pose = endeff.pose_to_tuple(
'desired', self.ws_settings.representation_of_orientation)
285 nearest_pose = self.pose_tree.query(query_point = target_pose, t = Ns)
286 nearest_config_list = []
288 for pose
in nearest_pose:
289 nearest_config_list.append(numpy.copy(self.
config_list[self.pose_list.index(pose)]))
291 return nearest_config_list
296 Return a list of configurations which:
297 Are closest to the starting configuration
299 (second / online usecase : time coherence ... )
301 if self.ws_settings.number_of_search_items == 0:
305 nearest_config_list = self.config_tree.query(query_point = current_q, t = self.ws_settings.number_of_search_items)
307 return nearest_config_list
310 if self.ws_settings.search_criteria ==
'NTP':
312 elif self.ws_settings.search_criteria ==
'NCC':
314 elif self.ws_settings.search_criteria ==
'CLO':
315 nearest_config_list = []
316 for i
in range(self.ws_settings.number_of_search_items):
319 assert False, genpy.err_str(__name__, self.__class__.__name__,
'nearest_configs', self.ws_settings.search_criteria +
' is not a valid value for search_criteria')
321 return nearest_config_list
325 Save the class in file specified by "self.file_name"
327 FILE_DATA = open(path_and_file_name,
"w")
328 pickle.dump(self, FILE_DATA)
def read_from_file
Use this function to load the workspace from a file.
def configs_nearest_to_pose
representation_of_orientation
This class contains all the required settings of a workspace.