Main MRPT website > C++ reference for MRPT 1.9.9
CKinematicChain.cpp
Go to the documentation of this file.
1 /* +------------------------------------------------------------------------+
2  | Mobile Robot Programming Toolkit (MRPT) |
3  | http://www.mrpt.org/ |
4  | |
5  | Copyright (c) 2005-2017, Individual contributors, see AUTHORS file |
6  | See: http://www.mrpt.org/Authors - All rights reserved. |
7  | Released under BSD License. See details in http://www.mrpt.org/License |
8  +------------------------------------------------------------------------+ */
9 
10 #include "kinematics-precomp.h" // Precompiled headers
11 
13 #include <mrpt/utils/CStream.h>
16 #include <mrpt/opengl/CCylinder.h>
18 
19 using namespace mrpt;
20 using namespace mrpt::math;
21 using namespace mrpt::utils;
22 using namespace mrpt::poses;
23 using namespace mrpt::kinematics;
24 using namespace std;
25 
27 
28 /** Appends a new link to the robotic arm, with the given Denavit-Hartenberg
29  * parameters (see TKinematicLink for further details) */
31  double theta, double d, double a, double alpha, bool is_prismatic)
32 {
33  m_links.push_back(TKinematicLink(theta, d, a, alpha, is_prismatic));
34 }
35 
36 /** Removes one link from the kinematic chain (0<=idx<N) */
37 void CKinematicChain::removeLink(const size_t idx)
38 {
39  ASSERT_BELOW_(idx, m_links.size())
40  m_links.erase(m_links.begin() + idx);
41 }
42 
43 const TKinematicLink& CKinematicChain::getLink(const size_t idx) const
44 {
45  ASSERT_BELOW_(idx, m_links.size())
46  return m_links[idx];
47 }
48 
49 TKinematicLink& CKinematicChain::getLinkRef(const size_t idx)
50 {
51  ASSERT_BELOW_(idx, m_links.size())
52  return m_links[idx];
53 }
54 
55 void CKinematicChain::setOriginPose(const mrpt::poses::CPose3D& new_pose)
56 {
57  m_origin = new_pose;
58 }
59 
60 const mrpt::poses::CPose3D& CKinematicChain::getOriginPose() const
61 {
62  return m_origin;
63 }
64 
65 /*---------------------------------------------------------------
66  Implements the writing to a CStream capability of CSerializable objects
67  ---------------------------------------------------------------*/
68 void CKinematicChain::writeToStream(
69  mrpt::utils::CStream& out, int* version) const
70 {
71  if (version)
72  *version = 1;
73  else
74  {
75  out << m_links << m_origin;
76  }
77 }
78 
79 /*---------------------------------------------------------------
80  Implements the reading from a CStream capability of CSerializable objects
81  ---------------------------------------------------------------*/
82 void CKinematicChain::readFromStream(mrpt::utils::CStream& in, int version)
83 {
84  switch (version)
85  {
86  case 0:
87  case 1:
88  {
89  in >> m_links;
90  if (version >= 1)
91  {
92  in >> m_origin;
93  }
94  else
95  m_origin = mrpt::poses::CPose3D();
96  }
97  break;
98  default:
100  };
101 }
102 
103 /** Go thru all the links of the chain and compute the global pose of each link.
104  * The "ground" link pose "pose0" defaults to the origin of coordinates,
105  * but anything else can be passed as the optional argument. */
106 void CKinematicChain::recomputeAllPoses(
108  const mrpt::poses::CPose3D& pose0) const
109 {
110  MRPT_UNUSED_PARAM(pose0);
111  const size_t N = m_links.size();
112 
113  poses.resize(N + 1);
114 
115  CPose3D p = m_origin; // Cummulative pose
116 
117  poses[0] = p;
118 
119  for (size_t i = 0; i < N; i++)
120  {
121  // Build the 3D pose change of the i'th link:
122  const double th = m_links[i].theta;
123  const double alpha = m_links[i].alpha;
124  const double d = m_links[i].d;
125  const double a = m_links[i].a;
126 
127  const double t_vals[3] = {a * cos(th), a * sin(th), d};
128  const double r_vals[3 * 3] = {cos(th),
129  -sin(th) * cos(alpha),
130  sin(th) * sin(alpha),
131  sin(th),
132  cos(th) * cos(alpha),
133  -cos(th) * sin(alpha),
134  0,
135  sin(alpha),
136  cos(alpha)};
137 
138  const CMatrixDouble33 R(r_vals);
139  const CArrayDouble<3> t(t_vals);
140 
141  CPose3D link(R, t);
142 
143  p.composeFrom(p, link);
144 
145  poses[i + 1] = p;
146  }
147 }
148 
149 const float R = 0.01f;
150 
151 void addBar_D(mrpt::opengl::CSetOfObjects::Ptr& objs, const double d)
152 {
154  mrpt::make_aligned_shared<mrpt::opengl::CCylinder>(R, R, d);
155  gl_cyl->setColor_u8(mrpt::utils::TColor(0x00, 0x00, 0xff));
156  gl_cyl->setName("cyl.d");
157 
158  objs->insert(gl_cyl);
159 }
160 
161 void addBar_A(mrpt::opengl::CSetOfObjects::Ptr& objs, const double a)
162 {
164  mrpt::make_aligned_shared<mrpt::opengl::CCylinder>(R, R, -a);
165  gl_cyl2->setColor_u8(mrpt::utils::TColor(0xff, 0x00, 0x00));
166  gl_cyl2->setPose(mrpt::poses::CPose3D(0, 0, 0, 0, DEG2RAD(90), 0));
167  gl_cyl2->setName("cyl.a");
168 
169  objs->insert(gl_cyl2);
170 }
171 
172 void CKinematicChain::getAs3DObject(
175  const
176 {
177  ASSERT_(obj)
178  const size_t N = m_links.size();
179 
180  // Recompute current poses:
182  recomputeAllPoses(all_poses);
183 
184  m_last_gl_objects.resize(N + 1);
185 
186  // Ground [0] and Links [1-N]:
187  for (size_t i = 0; i <= N; i++)
188  {
191  gl_corner->setPose(all_poses[i]);
192 
193  gl_corner->setName(mrpt::format("%u", static_cast<unsigned int>(i)));
194  gl_corner->enableShowName();
195 
196  if (i < N) addBar_D(gl_corner, m_links[i].d);
197  if (i > 0) addBar_A(gl_corner, m_links[i - 1].a);
198 
199  obj->insert(gl_corner);
200  m_last_gl_objects[i] = gl_corner;
201  }
202 
203  if (out_all_poses) out_all_poses->swap(all_poses);
204 }
205 
206 void CKinematicChain::update3DObject(
208  const
209 {
210  ASSERTMSG_(
211  (m_links.size() + 1) == m_last_gl_objects.size(),
212  "The kinematic chain has changed since the last call to "
213  "getAs3DObject()")
214 
215  const size_t N = m_links.size();
216 
217  // Recompute current poses:
219  recomputeAllPoses(all_poses);
220 
221  for (size_t i = 0; i <= N; i++)
222  {
224  std::dynamic_pointer_cast<mrpt::opengl::CSetOfObjects>(
225  m_last_gl_objects[i]);
226  gl_objs->setPose(all_poses[i]);
227 
228  if (i < N)
229  {
231  std::dynamic_pointer_cast<mrpt::opengl::CCylinder>(
232  gl_objs->getByName("cyl.d"));
233  const double d = m_links[i].d;
234  glCyl->setHeight(d);
235  }
236 
237  if (i > 0)
238  {
240  std::dynamic_pointer_cast<mrpt::opengl::CCylinder>(
241  gl_objs->getByName("cyl.a"));
242  const double a = m_links[i - 1].a;
243  // glCyl2->setPose( mrpt::poses::CPose3D(0,0,d, 0, DEG2RAD(90),0) );
244  glCyl2->setHeight(-a);
245  }
246  }
247 
248  if (out_all_poses) out_all_poses->swap(all_poses);
249 }
250 
251 /** Erases all links and leave the robot arm empty. */
253 {
254  m_links.clear();
255  m_last_gl_objects.clear();
256 }
257 
260 {
261  uint32_t version;
262  in >> version;
263  switch (version)
264  {
265  case 0:
266  in >> o.theta >> o.d >> o.a >> o.alpha >> o.is_prismatic;
267  break;
268  default:
270  }
271  return in;
272 }
274  mrpt::utils::CStream& out, const TKinematicLink& o)
275 {
276  const uint32_t version = 0;
277  out << version;
278  out << o.theta << o.d << o.a << o.alpha << o.is_prismatic;
279  return out;
280 }
GLclampf GLclampf GLclampf alpha
Definition: glext.h:3525
Classes for serialization, sockets, ini-file manipulation, streams, list of properties-values, timewatch, extensions to STL.
GLdouble GLdouble t
Definition: glext.h:3689
std::string format(const char *fmt,...) MRPT_printf_format_check(1
A std::string version of C sprintf.
A set of object, which are referenced to the coordinates framework established in this object...
Definition: CSetOfObjects.h:30
The virtual base class which provides a unified interface for all persistent objects in MRPT...
Definition: CSerializable.h:44
#define IMPLEMENTS_SERIALIZABLE(class_name, base, NameSpace)
This must be inserted in all CSerializable classes implementation files.
void addBar_A(mrpt::opengl::CSetOfObjects::Ptr &objs, const double a)
#define ASSERT_BELOW_(__A, __B)
STL namespace.
GLsizei GLsizei GLuint * obj
Definition: glext.h:4070
mrpt::utils::CStream & operator>>(mrpt::utils::CStream &in, TKinematicLink &o)
void clear()
Clear the contents of this container.
Definition: ts_hash_map.h:189
This base class is used to provide a unified interface to files,memory buffers,..Please see the deriv...
Definition: CStream.h:41
This base provides a set of functions for maths stuff.
Definition: CArrayNumeric.h:19
#define MRPT_UNUSED_PARAM(a)
Can be used to avoid "not used parameters" warnings from the compiler.
#define MRPT_THROW_UNKNOWN_SERIALIZATION_VERSION(__V)
For use in CSerializable implementations.
std::shared_ptr< CCylinder > Ptr
Definition: CCylinder.h:34
A cylinder or cone whose base lies in the XY plane.
Definition: CCylinder.h:32
std::shared_ptr< CSetOfObjects > Ptr
Definition: CSetOfObjects.h:32
A RGB color - 8bit.
Definition: TColor.h:25
#define DEG2RAD
Classes for 2D/3D geometry representation, both of single values and probability density distribution...
Definition: CPoint.h:17
void addBar_D(mrpt::opengl::CSetOfObjects::Ptr &objs, const double d)
This is the global namespace for all Mobile Robot Programming Toolkit (MRPT) libraries.
const float R
A class used to store a 3D pose (a 3D translation + a rotation in 3D).
Definition: CPose3D.h:88
CSetOfObjects::Ptr CornerXYZSimple(float scale=1.0, float lineWidth=1.0)
Returns three arrows representing a X,Y,Z 3D corner (just thick lines instead of complex arrows for f...
GLuint in
Definition: glext.h:7274
#define ASSERT_(f)
CRenderizable & setPose(const mrpt::poses::CPose3D &o)
Set the 3D pose from a mrpt::poses::CPose3D object (return a ref to this)
typedef void(APIENTRYP PFNGLBLENDCOLORPROC)(GLclampf red
void setHeight(float height)
Chenges cylinder&#39;s height.
Definition: CCylinder.h:110
unsigned __int32 uint32_t
Definition: rptypes.h:47
A open-loop kinematic chain model, suitable to robotic manipulators.
#define ASSERTMSG_(f, __ERROR_MSG)
GLubyte GLubyte GLubyte a
Definition: glext.h:6279
GLfloat GLfloat p
Definition: glext.h:6305
std::vector< TYPE1, Eigen::aligned_allocator< TYPE1 > > vector_t
mrpt::utils::CStream & operator<<(mrpt::utils::CStream &out, const TKinematicLink &o)



Page generated by Doxygen 1.8.14 for MRPT 1.9.9 Git: ae4571287 Thu Nov 23 00:06:53 2017 +0100 at dom oct 27 23:51:55 CET 2019