MAGIKS  1.1
Manipulator General Inverse Kinematic Solver
 All Classes Namespaces Files Functions Variables Pages
general_math.py
Go to the documentation of this file.
1 ## @file general.py
2 # @brief This module provides general mathematical functions
3 # @author Nima Ramezani Taghiabadi
4 #
5 # PhD Researcher
6 # Faculty of Engineering and Information Technology
7 # University of Technology Sydney (UTS)
8 # Broadway, Ultimo, NSW 2007, Australia
9 # Phone No. : 04 5027 4611
10 # Email(1) : nima.ramezani@gmail.com
11 # Email(2) : Nima.RamezaniTaghiabadi@uts.edu.au
12 # @version 1.0
13 #
14 # Last Revision: 03 January 2015
15 
16 import math, numpy
17 from interval import interval, inf
18 
19 # global variables:
20 two_pi = 2*math.pi
21 pi = 1*math.pi
22 deg_to_rad = (math.pi/180.00) #convert an angle from degree to radian by multiplying it by this variable
23 rad_to_deg = (180.00/math.pi) #convert an angle from radian to degree by multiplying it by this variable
24 
25 epsilon = 0.00001 # a very small number
26 f0 = float(0) # constant number zero as a float
27 f1 = float(1) # constant number one as a float
28 err_code = 0
29 infinity = float("inf")
30 
31 def sign_choice(x, y, z):
32  if z > 0:
33  return x
34  elif z < 0:
35  return y
36  else:
37  print "Error from sign_choice(): value z can not be zero"
38 
39 def inv(x):
40  if abs(x) < epsilon:
41  if x > 0:
42  return infinity
43  else:
44  return - infinity
45  else:
46  return 1.0/x
47 
48 def round(x):
49 
50  if abs(x) < epsilon:
51  y = 0
52  elif abs(x - 1) < epsilon:
53  y = 1
54  elif abs(x + 1) < epsilon:
55  y = -1
56  else:
57  y = x
58  return y
59 
60 def round_mat(A):
61  n = A.shape[0]
62  m = A.shape[1]
63  B = numpy.zeros((n,m))
64 
65  for i in range(n):
66  for j in range(m):
67  B[i,j] = round(A[i,j])
68  return(B)
69 
70 def equal(v1,v2, epsilon = epsilon):
71  '''
72  Returns 1 if two values v1 and v2 are equal otherwise returns 0
73  '''
74  return (abs(v1-v2) < epsilon)
75 
76 def ensured_in_range(x, xl, xh):
77  if x < xl:
78  x = xl
79  if x > xh:
80  x = xh
81  return x
82 
83 def sign(x):
84  '''
85  Returns 1 if x is positive, -1 if negative and 0 if abs(x) is smaller than epsilon
86  '''
87 
88  if (abs(x) < epsilon):
89  return 0.0
90  elif x > 0:
91  return 1.0
92  else:
93  return -1.0
94 
96  '''
97  Sometimes the interval includes two descrite intervals so that
98  the upper bound of interval i equals the lower bound of interval i + 1
99  In this case it is better to connect the two intervals.
100  This functions does this and returns an interval in which all sequential continuous intervals are replaced by one interval.
101  '''
102  nC = len(C)
103  if nC == 0:
104  return(C)
105  else:
106  (a, b) = C[0]
107  R = interval()
108  for i in range(1, nC):
109  (aa, bb) = C[i]
110  if equal(aa,b):
111  b = bb
112  else:
113  R = R | interval([a,b])
114  a = aa
115  b = bb
116 
117  R = R | interval([a,b])
118  return R
119 
120 def binary_choice(a,b,z):
121  '''
122  This function returns "a" or "b" depending on the sign of "z",
123  if "z" is positive or zero, "a" is returned otherwise "b" is returned
124  '''
125  if z>=0:
126  return a
127  else:
128  return b
129 
131  '''
132  solves the quadratic inequality "a*x^2 + b*x + c > 0" for "x"
133  and returns a feasibility set for "x" so that the inequality holds
134  '''
135  Delta = b**2 - 4*a*c
136  if Delta > 0:
137  sqrt_delta = math.sqrt(Delta)
138  x_l = (- b - sqrt_delta)/(2*a)
139  x_h = (- b + sqrt_delta)/(2*a)
140  if a > 0:
141  return interval([-inf, x_l] , [x_h, inf])
142  else:
143  return interval([x_l, x_h])
144  else:
145  if a > 0:
146  return interval([-inf, inf])
147  else:
148  return interval()
149 
150 def closest_border(x, S, k = 0.01):
151  '''
152  Returns the closest border of set S to value x. S is an interval variable from package interval
153  '''
154  d_min = infinity
155  for intrvl in S:
156  (xl, xh) = intrvl
157  d = abs(x - xl)
158  if d < d_min:
159  d_min = d
160  clx = xl + k*(xh-xl)
161 
162  d = abs(x - xh)
163  if d < d_min:
164  d_min = d
165  clx = xh - k*(xh-xl)
166  return clx
167 
169  '''
170  This function returns the interval in which x is located(accommodated)
171  if x is not in the interval, then None is returned
172  '''
173  for intrvl in S:
174  (xl,xh) = intrvl
175  if (x >= xl) and (x <= xh):
176  return intrvl
177 
178  return None
179 
180 def gauss_rec(phi, W_arr, r, c, h, N_w):
181  '''
182  This function is translated from simulink model created by Gabriel
183  The .mdl file can be found in:
184  /home/nimasoft/Dropbox/software/matlab/packages/gabriell/Periodic_learning_v5_fafo_simulink/output_dyn_system_v4_simple.m
185  '''
186 
187  '''
188  phi, r, h must be scalar values
189  c, W_arr must be arrays of minimum length N_w
190  N_w = length(W_arr)
191  '''
192 
193  # put input to the output dynamical system
194  num_in = 0
195  den_in = 0
196  for i in range(N_w):
197  (psi,arg) = gauss_kernel_cosine(phi, c[i], h)
198  '''
199  print "W_arr[i] = ", W_arr[i]
200  print "r = ", r
201  print "psi = ", psi
202  print "num_in = ", num_in
203  '''
204  num_in = num_in + psi*W_arr[i]*r
205  den_in = den_in + psi
206 
207  return(num_in/den_in)
208 
209 def gauss_kernel_cosine(phi, c, h):
210  '''
211  This function is translated from Matlab code written by Gabriel.
212  The Matlab code can be found in:
213  /home/nimasoft/Dropbox/software/matlab/packages/gabriell/Periodic_learning_v5_fafo_simulink/gauss_kernel_cosine.m
214  '''
215  # psi: Gaussian kernel function
216  # phi: phase
217  # c: value between 0 and 2*pi
218  # h: 'width' of the kernel function
219 
220  arg = math.cos(phi - c) - 1.0
221  psi = math.exp(h*arg)
222  output = (psi, arg)
223  return output
224 
225