20 static const unsigned int constraintCacheSize = 5;
22 std::string Armature::m_root =
"root";
59 for (JointConstraintList::iterator it=m_constraints.begin(); it != m_constraints.end(); it++) {
65 m_constraints.clear();
69 segment(_segment), value(), values(), function(_function), y_nr(_y_nr), param(_param), freeParam(_freeParam), substep(_substep)
85 switch (
segment->second.segment.getJoint().getType()) {
134 if (freeParam && param)
146 m_qCCh = m_cache->
addChannel(
this,
"q", m_qKdl.
rows()*
sizeof(
double));
151 m_yCCh = m_cache->
addChannel(
this,
"y", m_nconstraint*constraintCacheSize*
sizeof(
double));
152 m_buf =
new double[m_nconstraint*constraintCacheSize];
161 void Armature::pushQ(
CacheTS timestamp)
171 bool Armature::popQ(
CacheTS timestamp)
176 if (item && m_qCTs != timestamp) {
177 double* q = m_qKdl(0);
178 memcpy(q, item, m_qKdl.
rows()*
sizeof(
double));
183 return (item) ? true :
false;
188 void Armature::pushConstraints(
CacheTS timestamp)
193 double *item = m_buf;
194 for (
unsigned int i=0; i<m_nconstraint; i++) {
195 JointConstraint_struct* pConstraint = m_constraints[i];
196 *item++ = pConstraint->values.feedback;
197 *item++ = pConstraint->values.tolerance;
198 *item++ = pConstraint->value.yd;
199 *item++ = pConstraint->value.yddot;
200 *item++ = pConstraint->values.alpha;
209 bool Armature::popConstraints(
CacheTS timestamp)
213 if (item && m_yCTs != timestamp) {
214 for (
unsigned int i=0; i<m_nconstraint; i++) {
215 JointConstraint_struct* pConstraint = m_constraints[i];
216 if (pConstraint->function != Joint1DOFLimitCallback) {
217 pConstraint->values.feedback = *item++;
218 pConstraint->values.tolerance = *item++;
219 pConstraint->value.yd = *item++;
220 pConstraint->value.yddot = *item++;
221 pConstraint->values.alpha = *item++;
223 item += constraintCacheSize;
228 return (item) ? true :
false;
243 for (
int dof=0; dof<ndof; dof++) {
245 m_joints.push_back(js);
253 SegmentMap::const_iterator sit = m_tree.
getSegment(name);
256 p_joint = &sit->second.segment.getJoint();
257 if (q_size < p_joint->getNDof())
259 p_tip = &sit->second.segment.getFrameToTip();
260 for (
unsigned int dof=0; dof<p_joint->
getNDof(); dof++) {
261 (&q_rest)[dof] = m_joints[sit->second.q_nr+dof].rest;
262 (&q)[dof] = m_qKdl[sit->second.q_nr+dof];
271 double maxJoint = 0.0;
272 for (
unsigned int i=0; i<m_njoint; i++) {
274 double joint =
fabs(m_oldqKdl[i]-m_qKdl[i]);
275 if (maxJoint < joint)
285 double maxDelta = 0.0;
288 for (
unsigned int i = 0; i<m_neffector; i++) {
289 twist =
diff(m_effectors[i].pose, m_effectors[i].oldpose);
291 if (delta > maxDelta)
294 if (delta > maxDelta)
302 SegmentMap::const_iterator segment_it = m_tree.
getSegment(segment_name);
305 if (_freeParam && _param)
309 JointConstraintList::iterator constraint_it;
312 for (iConstraint=0, constraint_it=m_constraints.begin(); constraint_it != m_constraints.end(); constraint_it++, iConstraint++) {
313 pConstraint = *constraint_it;
314 if (pConstraint->
segment == segment_it) {
320 pConstraint->
param = _param;
322 pConstraint->
substep = _substep;
327 if (_freeParam && _param)
333 m_constraints.push_back(pConstraint);
334 m_noutput += pConstraint->
v_nr;
335 return m_nconstraint++;
340 SegmentMap::const_iterator segment_it = m_tree.
getSegment(segment_name);
343 const Joint& joint = segment_it->second.segment.getJoint();
348 if ((joint.
getNDof() == 1 && dof > 0) || (joint.
getNDof() == 2 && dof > 1))
350 Joint_struct& p_joint = m_joints[segment_it->second.q_nr+dof];
360 if (segments.find(name) == segments.end())
363 EffectorList::const_iterator it;
365 for (it=m_effectors.begin(), ee=0; it!=m_effectors.end(); it++, ee++) {
366 if (it->name == name)
372 m_effectors.push_back(effector);
373 return m_neffector++;
378 unsigned int i, j,
c;
384 for (i=
c=0; i<m_nconstraint; i++) {
386 for (j=0; j<pConstraint->
v_nr; j++,
c++) {
395 m_oldqKdl.
resize(m_njoint);
396 m_newqKdl.
resize(m_njoint);
397 m_qdotKdl.
resize(m_njoint);
398 for (i=0; i<m_njoint; i++) {
399 m_newqKdl[i] = m_oldqKdl[i] = m_qKdl[i] = m_joints[i].rest;
405 for (i=0; i<m_neffector; i++) {
407 KDL::SegmentMap::value_type
const *sit = m_tree.
getSegmentPtr(m_effectors[i].name);
408 while (sit->first !=
"root") {
409 Frame tip = sit->second.segment.pose(m_qKdl(sit->second.q_nr));
411 sit = sit->second.parent;
454 double* qdot=m_qdotKdl(0);
456 double* newq=m_newqKdl(0);
457 double norm, qx, qz, CX, CZ, sx, sz;
461 for (q_nr=0; q_nr<
m_nq; ++q_nr)
464 for (q_nr=0; q_nr<
m_nq; ) {
467 switch (joint->
type) {
472 if (joint[0].useLimit) {
473 if (joint[1].useLimit) {
479 if (qx > 0.0 && qz > 0.0) {
482 }
else if (qx <= 0.0 && qz > 0.0) {
487 }
else if (qx <= 0.0 && qz <= 0.0) {
516 newq[0] = qx*
norm*CX*sx;
517 newq[1] = qz*
norm*CZ*sz;
524 if (qx > joint[0].
max) {
525 newq[0] = joint[0].
max;
527 }
else if (qx < joint[0].
min) {
528 newq[0] = joint[0].
min;
532 }
else if (joint[1].useLimit) {
535 if (qz > joint[1].
max) {
536 newq[1] = joint[1].
max;
538 }
else if (qz < joint[1].
min) {
539 newq[1] = joint[1].
min;
543 if (joint[0].locked) {
556 callback.lockJoint(q_nr, 2, deltaq);
572 for (
unsigned int i=0; i<joint->
ndof; i++) {
574 if (joint[i].useLimit) {
575 if (newq[i] > joint[i].
max) {
576 newq[i] = joint[i].
max;
578 }
else if (newq[i] < joint[i].
min) {
579 newq[i] = joint[i].
min;
584 if (joint[0].locked) {
588 for (
unsigned int i=0; i<joint->
ndof; i++) {
589 qdot[i] = newq[i] - q[i];
590 norm += qdot[i]*qdot[i];
597 for (
unsigned int i=0; i<joint->
ndof; i++) {
614 for ( ; q_nr<
m_nq; ) {
621 return (unlocked) ? locked :
false;
631 memcpy(m_qKdl(0), m_newqKdl(0),
sizeof(
double)*m_qKdl.
rows());
641 for (
unsigned int ee=0; ee<
m_nee; ee++) {
642 m_fksolver->
JntToCart(m_qKdl,m_effectors[ee].pose,m_effectors[ee].name,m_root);
643 m_jacsolver->
JntToJac(m_qKdl,*m_jac,m_effectors[ee].name);
648 for(
unsigned int i=0;i<6;i++) {
649 for(
unsigned int j=0;j<
m_nq;j++)
650 Jq(i,j)=(*m_jac)(i,j);
668 return (m_fksolver->
JntToCart(m_qKdl,
result,segment_name,base_name) < 0) ? false :
true;
684 memcpy(m_oldqKdl(0), m_qKdl(0),
sizeof(
double)*m_qKdl.
rows());
685 for (
unsigned int i=0; i<m_neffector; i++) {
686 m_effectors[i].oldpose = m_effectors[i].pose;
691 for (JointList::iterator
jit=m_joints.begin();
jit!=m_joints.end(); ++
jit) {
692 (*jit).locked =
false;
695 JointConstraintList::iterator it;
696 unsigned int iConstraint;
699 for (iConstraint=0, it=m_constraints.begin(); it!=m_constraints.end(); it++, iConstraint++) {
702 for (i=0, nr = pConstraint->
segment->second.q_nr; i<pConstraint->v_nr; i++, nr++) {
703 *(
double *)&pConstraint->
value[i].
y = m_qKdl[nr];
704 *(
double *)&pConstraint->
value[i].
ydot = m_qdotKdl[nr];
710 for (i=0, nr=pConstraint->
y_nr; i<pConstraint->v_nr; i++, nr++) {
719 unsigned int lastid, i;
722 lastid = m_nconstraint;
723 }
else if (constraintId < m_nconstraint) {
724 lastid = constraintId+1;
728 for ( ; constraintId<lastid; ++constraintId) {
731 for (i=0; i<pConstraint->
v_nr; i++) {
747 for (i=0; i<pConstraint->
v_nr; i++) {
748 if (valueId == pConstraint->
value[i].
id) {
751 pConstraint->
value[i].
yd = value;
772 for (i=0; i<pConstraint->
v_nr; i++)
void BLI_kdtree_nd_() free(KDTree *tree)
#define CONSTRAINT_ID_ALL
SIMD_FORCE_INLINE btScalar length(const btQuaternion &q)
Return the length of a quaternion.
SIMD_FORCE_INLINE btScalar norm() const
Return the norm (length) of the vector.
represents a frame transformation in 3D space (rotation + translation)
Vector p
origine of the Frame
unsigned int rows() const
void resize(unsigned int newSize)
This class encapsulates a simple joint, that is with one parameterized degree of freedom and with sca...
unsigned int getNDof() const
const JointType & getType() const
represents rotations in 3 dimensional space.
Rotation Inverse() const
Gives back the inverse rotation matrix of *this.
This class encapsulates a simple segment, that is a "rigid body" (i.e., a frame and an inertia) with...
virtual int JntToCart(const JntArray &q_in, Frame &p_out, const std::string &segmentName, const std::string &baseName)
int JntToJac(const JntArray &q_in, Jacobian &jac, const std::string &segmentname)
const SegmentMap & getSegments() const
SegmentMap::const_iterator getSegment(const std::string &segment_name) const
SegmentMap::value_type const * getSegmentPtr(const std::string &segment_name) const
bool addSegment(const Segment &segment, const std::string &segment_name, const std::string &hook_name)
represents both translational and rotational velocities.
Vector rot
The rotational velocity of that point.
Vector vel
The velocity of that point.
A concrete implementation of a 3 dimensional vector class.
virtual void initCache(Cache *_cache)
bool addSegment(const std::string &segment_name, const std::string &hook_name, const Joint &joint, const double &q_rest, const Frame &f_tip=F_identity, const Inertia &M=Inertia::Zero())
int addLimitConstraint(const std::string &segment_name, unsigned int dof, double _min, double _max)
virtual void updateControlOutput(const Timestamp ×tamp)
virtual void pushCache(const Timestamp ×tamp)
virtual bool updateJoint(const Timestamp ×tamp, JointLockCallback &callback)
virtual void updateKinematics(const Timestamp ×tamp)
bool getRelativeFrame(Frame &result, const std::string &segment_name, const std::string &base_name=m_root)
virtual const Frame & getPose(const unsigned int end_effector)
int addConstraint(const std::string &segment_name, ConstraintCallback _function, void *_param=NULL, bool _freeParam=false, bool _substep=false)
virtual int addEndEffector(const std::string &name)
bool getSegment(const std::string &segment_name, const unsigned int q_size, const Joint *&p_joint, double &q_rest, double &q, const Frame *&p_tip)
virtual void updateJacobian()
virtual bool setControlParameter(unsigned int constraintId, unsigned int valueId, ConstraintAction action, double value, double timestep=0.0)
double getMaxEndEffectorChange()
virtual bool setJointArray(const KDL::JntArray &joints)
double getMaxJointChange()
virtual const KDL::JntArray & getJointArray()
int addChannel(const void *device, const char *name, unsigned int maxItemSize)
double * addCacheVectorIfDifferent(const void *device, int channel, CacheTS timestamp, double *data, unsigned int length, double threshold)
const void * getPreviousCacheItem(const void *device, int channel, CacheTS *timestamp)
std::vector< e_matrix > m_JqArray
virtual void initialize(unsigned int _nq, unsigned int _nc, unsigned int _nee)
DEGForeachIDComponentCallback callback
IMETHOD Rotation Rot(const Vector &axis_a_b)
Segment< FEdge *, Vec3r > segment
IMETHOD Vector diff(const Vector &a, const Vector &b, double dt=1)
double epsilon2
power or 2 of epsilon
INLINE Rall1d< T, V, S > sqrt(const Rall1d< T, V, S > &arg)
std::map< std::string, TreeElement, std::less< std::string >, Eigen::aligned_allocator< std::pair< const std::string, TreeElement > > > SegmentMap
void changeRefPoint(const Jacobian &src1, const Vector &base_AB, Jacobian &dest)
double epsilon
default precision while comparing with Equal(..,..) functions. Initialized at 0.0000001.
INLINE Rall1d< T, V, S > sqr(const Rall1d< T, V, S > &arg)
bool(* ConstraintCallback)(const Timestamp ×tamp, struct ConstraintValues *const _values, unsigned int _nvalues, void *_param)
JointConstraint_struct(SegmentMap::const_iterator _segment, unsigned int _y_nr, ConstraintCallback _function, void *_param, bool _freeParam, bool _substep)
ConstraintSingleValue value[3]
SegmentMap::const_iterator segment
ConstraintCallback function
ConstraintValues values[3]
~JointConstraint_struct()
KDL::Joint::JointType type
struct ConstraintSingleValue * values
ccl_device_inline float2 fabs(const float2 &a)