MoveMasterModel.cpp
Go to the documentation of this file.
1 // *****************************************************************
2 // Filename: SearchModule.cpp
3 // Copyright: Kai Welke, Chair Prof. Dillmann (IAIM),
4 // Institute for Computer Science and Engineering (CSE),
5 // University of Karlsruhe. All rights reserved.
6 // Author: Kai Welke
7 // Date: 25.09.2008
8 // *****************************************************************
9 
10 // *****************************************************************
11 // includes
12 // *****************************************************************
13 #include "MoveMasterModel.h"
14 
15 
16 namespace visionx
17 {
18  // *****************************************************************
19  // initialization of static members
20  // *****************************************************************
21  const std::string CMoveMasterModel::keypoint[NO_KEYPOINTS] = {"PalmCenter", "IndexTip", "MiddleTip", "ThumbTip"};
22  const std::string CMoveMasterModel::jointname[NO_JOINTS] = {"Body", "Shoulder", "Elbow", "Wrist_pan", "Wrist_rotate",
23  "Palm", "Index_0", "Index_1", "Middle_0", "Middle_1", "Ring_0", "Ring_1",
24  "Pinky_0", "Pinky_1", "Thumb_0", "Thumb_1",
25  "Actuator_Thumb_1", "Actuator_Thumb_2", "Actuator_Index_1", "Actuator_Index_2",
26  "Actuator_Middle_1", "Actuator_Middle_2", "Actuator_Ring_1", "Actuator_Ring_2",
27  "Actuator_Pinky_1", "Actuator_Pinky_2"
28  };
29 
30  // *****************************************************************
31  // implementation of CMoveMasterModel
32  // *****************************************************************
33  // *****************************************************************
34  // construction / destruction
35  // *****************************************************************
37  {
39  }
40 
42  {
43  if (m_pOIFwdKinematicsInterface != NULL)
44  {
46  }
47  }
48 
49  // *****************************************************************
50  // init and update
51  // *****************************************************************
52  bool CMoveMasterModel::init(std::string sFilename, float fFocalLengthY)
53  {
54  if (m_pOIFwdKinematicsInterface != NULL)
55  {
56  printf("already initialized\n");
57  return false;
58  }
59 
62  {
65  }
66 
67  // *** Extract keypoint paths initially for fast access later
68  for (int i = 0; i < NO_KEYPOINTS; i++)
69  {
70  if (! m_pOIFwdKinematicsInterface->getPath(keypoint[i], keypath[i]))
71  {
72  printf("Keypoint node %s not found in path!\n", keypoint[i].c_str());
73  }
74  }
75 
76  // *** Extract joint paths initially for fast access later
77  for (int i = 0; i < NO_JOINTS; i++)
78  {
79  if (! m_pOIFwdKinematicsInterface->getPath(jointname[i], jointpath[i]))
80  {
81  printf("joint node %s not found in path!\n", jointname[i].c_str());
82  }
83  }
84 
85 
86  // hand (David)
87  SoPath* temp_path;
88  m_pOIFwdKinematicsInterface->getPath("HandPos", temp_path);
89  hand_pos = static_cast<SoTranslation*>(temp_path->getNodeFromTail(0));
90  m_pOIFwdKinematicsInterface->getPath("HandRot", temp_path);
91  hand_rot = static_cast<SoTransform*>(temp_path->getNodeFromTail(0));
92  // object cylinder (David)
93  m_pOIFwdKinematicsInterface->getPath("ObjCylinderPos", temp_path);
94  m_pObjCylPos = static_cast<SoTranslation*>(temp_path->getNodeFromTail(0));
95  m_pOIFwdKinematicsInterface->getPath("ObjCylinderRot", temp_path);
96  m_pObjCylRot = static_cast<SoTransform*>(temp_path->getNodeFromTail(0));
97  m_pOIFwdKinematicsInterface->getPath("ObjCylinder", temp_path);
98  m_pObjectCylinder = static_cast<SoCylinder*>(temp_path->getNodeFromTail(0));
99  // test sphere (David)
100  m_pOIFwdKinematicsInterface->getPath("TestSphere", temp_path);
101  m_pTestSpherePos = static_cast<SoTranslation*>(temp_path->getNodeFromTail(0));
102 
103  return true;
104  }
105 
107  {
109  {
110  printf("call init first\n");
111  return;
112  }
113 
115  }
116 
117  // *****************************************************************
118  // kinematics
119  // *****************************************************************
120  void CMoveMasterModel::setJointAngle(int nJoint, float fAngleRad)
121  {
122  if (nJoint >= NO_JOINTS)
123  {
124  printf("Illegal joint: %d\n", nJoint);
125  return;
126  }
127 
128  if (!m_pOIFwdKinematicsInterface->setJointAngle(jointpath[nJoint], fAngleRad))
129  {
130  printf("Could not set joint angle for node %s!\n", jointname[nJoint].c_str());
131  }
132  //else
133  //m_pOIFwdKinematicsInterface->update();
134  }
135 
137  {
138  Vec3d result;
139 
140  if (nKeyPoint >= NO_KEYPOINTS)
141  {
142  printf("Illegal keypoint: %d\n", nKeyPoint);
143  return result;
144  }
145 
146 
147  if (m_pOIFwdKinematicsInterface->getTranslation(iposition[nKeyPoint], keypath[nKeyPoint]))
148  {
149  float x, y, z;
150  iposition[nKeyPoint].getValue(x, y, z);
151 
152  // transform to robot coordinate system
153  result.x = -x;
154  result.y = y;
155  result.z = -z;
156  }
157  else
158  {
159  printf("Could not retrieve position of keypath %s!\n", keypoint[nKeyPoint].c_str());
160  }
161 
162  return result;
163  }
164 
165 
166 
167 
168 
169  void CMoveMasterModel::setHandRotation(double alpha, double beta, double gamma) // <- added (David)
170  {
171  Mat3d tempmat;
172  Vec3d axis;
173  float angle;
174  SbRotation rot;
175  SbVec3f vec;
176 
177  Math3d::SetRotationMat(tempmat, alpha, beta, gamma);
178  Math3d::GetAxisAndAngle(tempmat, axis, angle);
179  if (Math3d::Length(axis) == 0)
180  {
181  axis.z = 1;
182  }
183  //vec.setValue(-axis.x, axis.y, -axis.z);// transform to robot coordinate system
184  vec.setValue(axis.x, axis.y, axis.z);
185 
186  //*** Keep angle in interval [-2Pi,2Pi] (required for coin!)
187  if (fabs(angle) > 2 * M_PI)
188  {
189  angle = angle - (floor(angle / (2 * M_PI)) * 2 * M_PI);
190  }
191  //*** Keep angle positive (required for coin!)
192  if (angle <= 0.0f) // This is mandatory: Coin uses quaternions internally and can thus not
193  {
194  angle = 2 * M_PI + angle; // reflect angles of 0deg without change of axis. Therefore set to 2Pi instead.
195  }
196 
197  hand_rot->rotation.setValue(vec, angle);
198  }
199 
200 
201  void CMoveMasterModel::setHandPosition(Vec3d pos) // <- added (David)
202  {
203  SbVec3f temp;
204  temp.setValue(pos.x, pos.y, pos.z); // transform to robot coordinate system?
205  hand_pos->translation.setValue(temp);
206  }
207 
208 
209 
210  void CMoveMasterModel::setObjCylConfig(Vec3d pos, double alpha, double beta, double gamma, float height, float radius) // (David)
211  {
212  SbVec3f temp;
213  temp.setValue(pos.x, pos.y, pos.z);
214  m_pObjCylPos->translation.setValue(temp);
215 
216 
217  Mat3d tempmat;
218  Vec3d axis;
219  float angle;
220  SbRotation rot;
221  SbVec3f vec;
222 
223  Math3d::SetRotationMat(tempmat, alpha, beta, gamma);
224  Math3d::GetAxisAndAngle(tempmat, axis, angle);
225  if (Math3d::Length(axis) == 0)
226  {
227  axis.z = 1;
228  }
229  //vec.setValue(-axis.x, axis.y, -axis.z);// transform to robot coordinate system
230  vec.setValue(axis.x, axis.y, axis.z);
231 
232  //*** Keep angle in interval [-2Pi,2Pi] (required for coin!)
233  if (std::fabs(angle) > 2 * M_PI)
234  {
235  angle = angle - (std::floor(angle / (2 * M_PI)) * 2 * M_PI);
236  }
237  //*** Keep angle positive (required for coin!)
238  if (angle <= 0.0f) // This is mandatory: Coin uses quaternions internally and can thus not
239  {
240  angle = 2 * M_PI + angle; // reflect angles of 0deg without change of axis. Therefore set to 2Pi instead.
241  }
242 
243  m_pObjCylRot->rotation.setValue(vec, angle);
244 
245  m_pObjectCylinder->height.setValue(height);
246  m_pObjectCylinder->radius.setValue(radius);
247  }
248 
249 
251  {
252  SbVec3f temp;
253  temp.setValue(pos.x, pos.y, pos.z); // transform to robot coordinate system?
254  m_pTestSpherePos->translation.setValue(temp);
255  }
256 }
visionx::CMoveMasterModel::m_pOIFwdKinematicsInterface
OIFwdKinematicsInterface * m_pOIFwdKinematicsInterface
Definition: MoveMasterModel.h:48
MoveMasterModel.h
visionx::CMoveMasterModel::~CMoveMasterModel
~CMoveMasterModel()
Definition: MoveMasterModel.cpp:41
visionx::OIFwdKinematicsInterface::m_fFocalLengthY
float m_fFocalLengthY
Definition: OIFwdKinematicsInterface.h:80
visionx
ArmarX headers.
Definition: OpenPoseStressTest.h:38
visionx::CMoveMasterModel::m_pObjCylPos
SoTranslation * m_pObjCylPos
Definition: MoveMasterModel.h:59
visionx::CMoveMasterModel::setObjCylConfig
void setObjCylConfig(Vec3d pos, double alpha, double beta, double gamma, float height, float radius)
Definition: MoveMasterModel.cpp:210
visionx::OIFwdKinematicsInterface::getPath
bool getPath(std::string name, SoPath *&p, SoNode *r=NULL)
Definition: OIFwdKinematicsInterface.cpp:292
visionx::CMoveMasterModel::setHandPosition
void setHandPosition(Vec3d pos)
Definition: MoveMasterModel.cpp:201
visionx::CMoveMasterModel::CMoveMasterModel
CMoveMasterModel()
Definition: MoveMasterModel.cpp:36
visionx::OIFwdKinematicsInterface::getTranslation
bool getTranslation(SbVec3f &translation, SoPath *p)
Definition: OIFwdKinematicsInterface.cpp:319
visionx::CMoveMasterModel::m_pTestSpherePos
SoTranslation * m_pTestSpherePos
Definition: MoveMasterModel.h:64
visionx::CMoveMasterModel::hand_pos
SoTranslation * hand_pos
Definition: MoveMasterModel.h:53
M_PI
#define M_PI
Definition: MathTools.h:17
visionx::OIFwdKinematicsInterface::update
void update()
Definition: OIFwdKinematicsInterface.cpp:165
visionx::CMoveMasterModel::getPositionOfKeypoint
Vec3d getPositionOfKeypoint(int nKeyPoint)
Definition: MoveMasterModel.cpp:136
GfxTL::Vec3d
VectorXD< 3, double > Vec3d
Definition: VectorXD.h:695
visionx::OIFwdKinematicsInterface::initviewer
void initviewer()
Definition: OIFwdKinematicsInterface.cpp:190
NO_KEYPOINTS
#define NO_KEYPOINTS
Definition: MoveMasterModel.h:23
visionx::CMoveMasterModel::update
void update()
Definition: MoveMasterModel.cpp:106
visionx::CMoveMasterModel::hand_rot
SoTransform * hand_rot
Definition: MoveMasterModel.h:52
visionx::CMoveMasterModel::init
bool init(std::string sFilename, float fFocalLengthY)
Definition: MoveMasterModel.cpp:52
angle
double angle(const Point &a, const Point &b, const Point &c)
Definition: point.hpp:100
visionx::OIFwdKinematicsInterface
Definition: OIFwdKinematicsInterface.h:59
visionx::CMoveMasterModel::setJointAngle
void setJointAngle(int nJoint, float fAngleRad)
Definition: MoveMasterModel.cpp:120
visionx::OIFwdKinematicsInterface::isInitialized
bool isInitialized()
Definition: OIFwdKinematicsInterface.cpp:525
visionx::OIFwdKinematicsInterface::setJointAngle
bool setJointAngle(SoPath *p, float angle)
Definition: OIFwdKinematicsInterface.cpp:349
visionx::CMoveMasterModel::setTestSpherePosition
void setTestSpherePosition(Vec3d pos)
Definition: MoveMasterModel.cpp:250
visionx::CMoveMasterModel::m_pObjCylRot
SoTransform * m_pObjCylRot
Definition: MoveMasterModel.h:58
visionx::CMoveMasterModel::setHandRotation
void setHandRotation(double alpha, double beta, double gamma)
Definition: MoveMasterModel.cpp:169
visionx::CMoveMasterModel::m_pObjectCylinder
SoCylinder * m_pObjectCylinder
Definition: MoveMasterModel.h:60
NO_JOINTS
#define NO_JOINTS
Definition: MoveMasterModel.h:24