MAGIKS  1.1
Manipulator General Inverse Kinematic Solver
 All Classes Namespaces Files Functions Variables Pages
cost_function.py
Go to the documentation of this file.
1 
2 # HEADER
3 '''
4 @file: cost_function.py
5 @brief: This module provides a class representing a cost function which can be used for a criterion in the jointspace or taskspace
6 @author: Nima Ramezani Taghiabadi
7  PhD Researcher
8  Faculty of Engineering and Information Technology
9  University of Technology Sydney
10  Broadway, Ultimo, NSW 2007
11  Room No.: CB10.03.512
12  Phone: 02 9514 4621
13  Mobile: 04 5027 4611
14  Email(1): Nima.RamezaniTaghiabadi@student.uts.edu.au
15  Email(2): nima.ramezani@gmail.com
16  Email(3): nima_ramezani@yahoo.com
17  Email(4): ramezanitn@alum.sharif.edu
18 @version: 1.0
19 Last Revision: 09 August 2015
20 '''
21 # BODY
22 
23 all_purposes = [None, 'Main Task', 'Gradient Projection', 'Joint Damping']
24 all_input_refs = ['Joint Values', 'Taskpoint Error', 'Taskframe Error']
25 
26 import numpy
27 
28 class Cost_Function(object):
29  '''
30  '''
31  def __init__(self, input_ref, purpose = None, weight = 1.0):
32  '''
33  '''
34  assert input_ref in all_input_refs
35  assert purpose in all_purposes
36  self.input_ref = input_ref
37  self.purpose = purpose
38  self.weight = weight
39  self.function = None
40  self.ref_num = 0
41 
42  self.abs_grad = None
43 
44  def value(self, endeff):
45  if self.input_ref == 'Joint_Values':
46  x = endeff.free_config(endeff.q)
47  elif self.input_ref == 'Taskpoint Error':
48  tp = endeff.task_point[self.ref_num]
49  H = endeff.transfer_matrices()
50  x = tp.error.value(tp.position(H), tp.rd)
51  else:
52  assert False, "Not Supported!"
53  return self.function.value(x)
54 
55  def gradient(self, endeff):
56  if self.input_ref == 'Joint Values':
57  x = endeff.free_config(endeff.q)
58  return self.function.gradient(x)
59  elif self.input_ref == 'Taskpoint Error':
60  tp = endeff.task_point[self.ref_num]
61  H = endeff.transfer_matrices()
62  x = tp.error.value(tp.position(H), tp.rd)
63  EJ = tp.error_jacobian(H, endeff.ajac)
64  return numpy.dot(EJ.T, self.function.gradient(x))
65  elif self.input_ref == 'Taskframe Error':
66  tf = endeff.task_frame[self.ref_num]
67  H = endeff.transfer_matrices()
68  x = tf.error.value(tf.position(H), tf.rd)
69  EJ = tf.error_jacobian(H, endeff.ajac)
70  return numpy.dot(EJ.T, self.function.gradient(x))
71  else:
72  assert False, "Not Supported!"
73