ocra-recipes
Doxygen documentation for the ocra-recipes repository
LinearizedCoulombFunction.cpp
Go to the documentation of this file.
2 #include <math.h>
3 
4 #include <stdexcept>
5 
6 
7 namespace ocra
8 {
10 
11  LinearizedCoulombFunction::LinearizedCoulombFunction(Variable& f, double frictionCoeff,
12  int numberOfFaces, double margin)
13  :NamedInstance("linearized coulomb function")
16  ,LinearFunction(f,numberOfFaces)
17  , _mu(frictionCoeff)
18  , _margin(margin)
19  , _R_cone(Matrix3d::Identity())
20  {
21  if (f.getSize() != 3)
22  {
23  std::stringstream ss;
24  ss << "[ocra::LinearizedCoulombFunction::LinearizedCoulombFunction] Size of the variable is not 3 but " << f.getSize();
25  throw std::runtime_error(ss.str());
26  }
27 
28  if (numberOfFaces < 3)
29  throw std::runtime_error("[ocra::LinearizedCoulombFunction::LinearizedCoulombFunction] Number of faces is less than 3");
30 
31  checkCoeff(frictionCoeff);
32 
33  buildA();
34  buildb();
35  }
36 
37 
39  {
40  return _mu;
41  }
42 
44  {
45  return _margin;
46  }
47 
49  {
50  checkCoeff(coeff);
51  _mu = coeff;
52  buildA();
53  }
54 
56  {
57  _margin = margin;
58  buildb();
59  }
60 
62  {
63  _R_cone = R;
64  buildA();
65  }
66 
68  {
69  return _R_cone;
70  }
71 
72 
73  // ------------------------ private methods ---------------------------------
74  void LinearizedCoulombFunction::buildA()
75  {
76  double angle = ANGLE_OFFSET;
77  double angleIncr = 2*M_PI/_dim;
78  Vector3d v1, v2, n;
79  v1[0] = _mu * cos(angle); //ray of the discreatized cone
80  v1[1] = _mu * sin(angle);
81  v1[2] = 1;
82  for (int i=0; i<_dim; ++i)
83  {
84  angle += angleIncr;
85  v2[0] = _mu * cos(angle); //ray of the discretized cone
86  v2[1] = _mu * sin(angle);
87  v2[2] = 1;
88 
89  n = v2.cross(v1); //normal vector to a face
90  n.normalize();
91 
92  _jacobian.row(i) = n.transpose();
93 
94  v1 = v2;
95  }
96 
98  _jacobian = _J_cache * _R_cone.transpose();
99  }
100 
101  void LinearizedCoulombFunction::buildb()
102  {
103  _b.setConstant(_margin);
104  }
105 
106  void LinearizedCoulombFunction::checkCoeff(real mu)
107  {
108  if (mu<=0)
109  throw std::runtime_error("[ocra::LinearizedCoulombFunction::] Invalid (negative) input for a friction coefficient");
110  }
111 }
112 
113 
114 //test
115 namespace ocra
116 {
118  {
119  BaseVariable f("f", 3);
120  LinearizedCoulombFunction coul(f, 1, 4);
121 
122  Vector3d v;
123 
124  std::cout << coul.getA() << std::endl;
125  std::cout << coul.getb() << std::endl;
126 
127  bool b=true;
128  while (b)
129  {
130  std::cin >> v[0] >> v[1] >> v[2];
131  f.setValue(v);
132  std::cout << coul.getValue() << std::endl;
133  if (v[0] == -2.71828)
134  b=false;
135  }
136  }
137 }
138 
139 // cmake:sourcegroup=Function
140 
#define M_PI
Definition: MathTypes.h:43
const Matrix3d & getConeOrientation() const
void setValue(const VectorXd &value)
Definition: Variable.cpp:99
LinearizedCoulombFunction class.
void testLinearCoulomb(void)
LinearFunction class.
Optimization-based Robot Controller namespace. a library of classes to write and solve optimization p...
MatrixXd & _jacobian
Definition: Function.h:313
int getSize() const
Definition: Variable.cpp:81
const VectorXd & getb() const
This class represents a variable in a mathematical sense.
Definition: Variable.h:105
const VectorXd & getValue() const
Definition: Function.h:365
Declaration file of the LinearizedCoulombFunction class.
const MatrixXd & getA() const
double real
Definition: MathTypes.h:27
const int & _dim
Definition: Function.h:320
Implements a basic variable.
Definition: Variable.h:304