Welcome to mirror list, hosted at ThFree Co, Russian Federation.

Armature.cpp « itasc « intern - git.blender.org/blender.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: f365d40e9f143b261e438cd343397d4241ddf148 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
/** \file itasc/Armature.cpp
 * \ingroup intern_itasc
 */
/*
 * Armature.cpp
 *
 *  Created on: Feb 3, 2009
 *      Author: benoitbolsee
 */

#include "Armature.hpp"
#include <algorithm>
#include <string.h>
#include <stdlib.h>

namespace iTaSC {

#if 0
// a joint constraint is characterized by 5 values: tolerance, K, alpha, yd, yddot
static const unsigned int constraintCacheSize = 5;
#endif
std::string Armature::m_root = "root";

Armature::Armature():
	ControlledObject(),
	m_tree(),
	m_njoint(0),
	m_nconstraint(0),
	m_noutput(0),
	m_neffector(0),
	m_finalized(false),
	m_cache(NULL),
	m_buf(NULL),
	m_qCCh(-1),
	m_qCTs(0),
	m_yCCh(-1),
#if 0
	m_yCTs(0),
#endif
	m_qKdl(),
	m_oldqKdl(),
	m_newqKdl(),
	m_qdotKdl(),
	m_jac(NULL),
	m_armlength(0.0),
	m_jacsolver(NULL),
	m_fksolver(NULL)
{
}

Armature::~Armature()
{
	if (m_jac)
		delete m_jac;
	if (m_jacsolver)
		delete m_jacsolver;
	if (m_fksolver)
		delete m_fksolver;
	for (JointConstraintList::iterator it=m_constraints.begin(); it != m_constraints.end(); it++) {
		if (*it != NULL)
			delete (*it);
	}
	if (m_buf)
		delete [] m_buf;
	m_constraints.clear();
}

Armature::JointConstraint_struct::JointConstraint_struct(SegmentMap::const_iterator _segment, unsigned int _y_nr, ConstraintCallback _function, void* _param, bool _freeParam, bool _substep):
	segment(_segment), value(), values(), function(_function), y_nr(_y_nr), param(_param), freeParam(_freeParam), substep(_substep)
{
	memset(values, 0, sizeof(values));
	memset(value, 0, sizeof(value));
	values[0].feedback = 20.0;
	values[1].feedback = 20.0;
	values[2].feedback = 20.0;
	values[0].tolerance = 1.0;
	values[1].tolerance = 1.0;
	values[2].tolerance = 1.0;
	values[0].values = &value[0];
	values[1].values = &value[1];
	values[2].values = &value[2];
	values[0].number = 1;
	values[1].number = 1;
	values[2].number = 1;
	switch (segment->second.segment.getJoint().getType()) {
	case Joint::RotX:
		value[0].id = ID_JOINT_RX;		
		values[0].id = ID_JOINT_RX;		
		v_nr = 1;
		break;
	case Joint::RotY:
		value[0].id = ID_JOINT_RY;		
		values[0].id = ID_JOINT_RY;		
		v_nr = 1;
		break;
	case Joint::RotZ:
		value[0].id = ID_JOINT_RZ;		
		values[0].id = ID_JOINT_RZ;		
		v_nr = 1;
		break;
	case Joint::TransX:
		value[0].id = ID_JOINT_TX;		
		values[0].id = ID_JOINT_TX;		
		v_nr = 1;
		break;
	case Joint::TransY:
		value[0].id = ID_JOINT_TY;		
		values[0].id = ID_JOINT_TY;		
		v_nr = 1;
		break;
	case Joint::TransZ:
		value[0].id = ID_JOINT_TZ;		
		values[0].id = ID_JOINT_TZ;		
		v_nr = 1;
		break;
	case Joint::Sphere:
		values[0].id = value[0].id = ID_JOINT_RX;		
		values[1].id = value[1].id = ID_JOINT_RY;
		values[2].id = value[2].id = ID_JOINT_RZ;		
		v_nr = 3;
		break;
	case Joint::Swing:
		values[0].id = value[0].id = ID_JOINT_RX;		
		values[1].id = value[1].id = ID_JOINT_RZ;		
		v_nr = 2;
		break;
	case Joint::None:
		break;
	}
}

Armature::JointConstraint_struct::~JointConstraint_struct()
{
	if (freeParam && param)
		free(param);
}

void Armature::initCache(Cache *_cache)
{
	m_cache = _cache;
	m_qCCh = -1;
	m_yCCh = -1;
	m_buf = NULL;
	if (m_cache) {
		// add a special channel for the joint
		m_qCCh = m_cache->addChannel(this, "q", m_qKdl.rows()*sizeof(double));
#if 0
		// for the constraints, instead of creating many different channels, we will
		// create a single channel for all the constraints
		if (m_nconstraint) {
			m_yCCh = m_cache->addChannel(this, "y", m_nconstraint*constraintCacheSize*sizeof(double));
			m_buf = new double[m_nconstraint*constraintCacheSize];
		}
		// store the initial cache position at timestamp 0
		pushConstraints(0);
#endif
		pushQ(0);
	}
}

void Armature::pushQ(CacheTS timestamp)
{
	if (m_qCCh >= 0) {
		// try to keep the cache if the joints are the same
		m_cache->addCacheVectorIfDifferent(this, m_qCCh, timestamp, m_qKdl(0), m_qKdl.rows(), KDL::epsilon);
		m_qCTs = timestamp;
	}
}

/* return true if a m_cache position was loaded */
bool Armature::popQ(CacheTS timestamp)
{
	if (m_qCCh >= 0) {
		double* item;
		item = (double *)m_cache->getPreviousCacheItem(this, m_qCCh, &timestamp);
		if (item && m_qCTs != timestamp) {
			double* q = m_qKdl(0);
			memcpy(q, item, m_qKdl.rows()*sizeof(double));
			m_qCTs = timestamp;
			// changing the joint => recompute the jacobian
			updateJacobian();
		}
		return (item) ? true : false;
	}
	return true;
}
#if 0
void Armature::pushConstraints(CacheTS timestamp)
{
	if (m_yCCh >= 0) {
		double *buf = NULL;
		if (m_nconstraint) {
			double *item = m_buf;
			for (unsigned int i=0; i<m_nconstraint; i++) {
				JointConstraint_struct* pConstraint = m_constraints[i];
				*item++ = pConstraint->values.feedback;
				*item++ = pConstraint->values.tolerance;
				*item++ = pConstraint->value.yd;
				*item++ = pConstraint->value.yddot;
				*item++ = pConstraint->values.alpha;
			}
		}
		m_cache->addCacheVectorIfDifferent(this, m_yCCh, timestamp, m_buf, m_nconstraint*constraintCacheSize, KDL::epsilon);
		m_yCTs = timestamp;
	}
}

/* return true if a cache position was loaded */
bool Armature::popConstraints(CacheTS timestamp)
{
	if (m_yCCh >= 0) {
		double *item = (double*)m_cache->getPreviousCacheItem(this, m_yCCh, &timestamp);
		if (item && m_yCTs != timestamp) {
			for (unsigned int i=0; i<m_nconstraint; i++) {
				JointConstraint_struct* pConstraint = m_constraints[i];
				if (pConstraint->function != Joint1DOFLimitCallback) {
					pConstraint->values.feedback = *item++;
					pConstraint->values.tolerance = *item++;
					pConstraint->value.yd = *item++;
					pConstraint->value.yddot = *item++;
					pConstraint->values.alpha = *item++;
				} else {
					item += constraintCacheSize;
				}
			}
			m_yCTs = timestamp;
		}
		return (item) ? true : false;
	}
	return true;
}
#endif

bool Armature::addSegment(const std::string& segment_name, const std::string& hook_name, const Joint& joint, const double& q_rest, const Frame& f_tip, const Inertia& M)
{
	if (m_finalized)
		return false;

	Segment segment(joint, f_tip, M);
	if (!m_tree.addSegment(segment, segment_name, hook_name))
		return false;
	int ndof = joint.getNDof();
	for (int dof=0; dof<ndof; dof++) {
		Joint_struct js(joint.getType(), ndof, (&q_rest)[dof]);
		m_joints.push_back(js);
	}
	m_njoint+=ndof;
	return true;
}

bool Armature::getSegment(const std::string& name, const unsigned int q_size, const Joint* &p_joint, double &q_rest, double &q, const Frame* &p_tip)
{
	SegmentMap::const_iterator sit = m_tree.getSegment(name);
	if (sit == m_tree.getSegments().end())
		return false;
	p_joint = &sit->second.segment.getJoint();
	if (q_size < p_joint->getNDof())
		return false;
	p_tip = &sit->second.segment.getFrameToTip();
	for (unsigned int dof=0; dof<p_joint->getNDof(); dof++) {
		(&q_rest)[dof] = m_joints[sit->second.q_nr+dof].rest;
		(&q)[dof] = m_qKdl[sit->second.q_nr+dof];
	}
	return true;
}

double Armature::getMaxJointChange()
{
	if (!m_finalized)
		return 0.0;
	double maxJoint = 0.0;
	for (unsigned int i=0; i<m_njoint; i++) {
		// this is a very rough calculation, it doesn't work well for spherical joint
		double joint = fabs(m_oldqKdl[i]-m_qKdl[i]);
		if (maxJoint < joint)
			maxJoint = joint;
	}
	return maxJoint;
}

double Armature::getMaxEndEffectorChange()
{
	if (!m_finalized)
		return 0.0;
	double maxDelta = 0.0;
	double delta;
	Twist twist;
	for (unsigned int i = 0; i<m_neffector; i++) {
		twist = diff(m_effectors[i].pose, m_effectors[i].oldpose);
		delta = twist.rot.Norm();
		if (delta > maxDelta)
			maxDelta = delta;
		delta = twist.vel.Norm();
		if (delta > maxDelta)
			maxDelta = delta;
	}
	return maxDelta;
}

int Armature::addConstraint(const std::string& segment_name, ConstraintCallback _function, void* _param, bool _freeParam, bool _substep)
{
	SegmentMap::const_iterator segment_it = m_tree.getSegment(segment_name);
	// not suitable for NDof joints
	if (segment_it == m_tree.getSegments().end()) {
		if (_freeParam && _param)
			free(_param);
		return -1;
	}
	JointConstraintList::iterator constraint_it;
	JointConstraint_struct* pConstraint;
	int iConstraint;
	for (iConstraint=0, constraint_it=m_constraints.begin(); constraint_it != m_constraints.end(); constraint_it++, iConstraint++) {
		pConstraint = *constraint_it;
		if (pConstraint->segment == segment_it) {
			// redefining a constraint
			if (pConstraint->freeParam && pConstraint->param) {
				free(pConstraint->param);
			}
			pConstraint->function = _function;
			pConstraint->param = _param;
			pConstraint->freeParam = _freeParam;
			pConstraint->substep = _substep;
			return iConstraint;
		}
	}
	if (m_finalized) {
		if (_freeParam && _param)
			free(_param);
		return -1;
	}
	// new constraint, append
	pConstraint = new JointConstraint_struct(segment_it, m_noutput, _function, _param, _freeParam, _substep);
	m_constraints.push_back(pConstraint);
	m_noutput += pConstraint->v_nr;
	return m_nconstraint++;
}

int Armature::addLimitConstraint(const std::string& segment_name, unsigned int dof, double _min, double _max)
{
	SegmentMap::const_iterator segment_it = m_tree.getSegment(segment_name);
	if (segment_it == m_tree.getSegments().end())
		return -1;
	const Joint& joint = segment_it->second.segment.getJoint();
	if (joint.getNDof() != 1 && joint.getType() != Joint::Swing) {
		// not suitable for Sphere joints
		return -1;
	}
	if ((joint.getNDof() == 1 && dof > 0) || (joint.getNDof() == 2 && dof > 1))
		return -1;
	Joint_struct& p_joint = m_joints[segment_it->second.q_nr+dof];
	p_joint.min = _min;
	p_joint.max = _max;
	p_joint.useLimit = true;
	return 0;
}

int Armature::addEndEffector(const std::string& name)
{
	const SegmentMap& segments = m_tree.getSegments();
	if (segments.find(name) == segments.end())
		return -1;

	EffectorList::const_iterator it;
	int ee;
	for (it=m_effectors.begin(), ee=0; it!=m_effectors.end(); it++, ee++) {
		if (it->name == name)
			return ee;
	}
	if (m_finalized)
		return -1;
	Effector_struct effector(name);
	m_effectors.push_back(effector);
	return m_neffector++;
}

bool Armature::finalize()
{
	unsigned int i, j, c;
	if (m_finalized)
		return true;
	if (m_njoint == 0)
		return false;
	initialize(m_njoint, m_noutput, m_neffector);
	for (i=c=0; i<m_nconstraint; i++) {
		JointConstraint_struct* pConstraint = m_constraints[i];
		for (j=0; j<pConstraint->v_nr; j++, c++) {
			m_Cq(c,pConstraint->segment->second.q_nr+j) = 1.0;
			m_Wy(c) = pConstraint->values[j].alpha/*/(pConstraint->values.tolerance*pConstraint->values.feedback)*/;
		}
	}
	m_jacsolver= new KDL::TreeJntToJacSolver(m_tree);
	m_fksolver = new KDL::TreeFkSolverPos_recursive(m_tree);
	m_jac = new Jacobian(m_njoint);
	m_qKdl.resize(m_njoint);
	m_oldqKdl.resize(m_njoint);
	m_newqKdl.resize(m_njoint);
	m_qdotKdl.resize(m_njoint);
	for (i=0; i<m_njoint; i++) {
		m_newqKdl[i] = m_oldqKdl[i] = m_qKdl[i] = m_joints[i].rest;
	}
	updateJacobian();
	// estimate the maximum size of the robot arms
	double length;
	m_armlength = 0.0;
	for (i=0; i<m_neffector; i++) {
		length = 0.0;
		KDL::SegmentMap::value_type const *sit = m_tree.getSegmentPtr(m_effectors[i].name);
		while (sit->first != "root") {
			Frame tip = sit->second.segment.pose(m_qKdl(sit->second.q_nr));
			length += tip.p.Norm();
			sit = sit->second.parent;
		}
		if (length > m_armlength)
			m_armlength = length;
	}
	if (m_armlength < KDL::epsilon)
		m_armlength = KDL::epsilon;
	m_finalized = true;
	return true;
}

void Armature::pushCache(const Timestamp& timestamp)
{
	if (!timestamp.substep && timestamp.cache) {
		pushQ(timestamp.cacheTimestamp);
		//pushConstraints(timestamp.cacheTimestamp);
	}
}

bool Armature::setJointArray(const KDL::JntArray& joints)
{
	if (!m_finalized)
		return false;
	if (joints.rows() != m_qKdl.rows())
		return false;
	m_qKdl = joints;
	updateJacobian();
	return true;
}

const KDL::JntArray& Armature::getJointArray()
{
	return m_qKdl;
}

bool Armature::updateJoint(const Timestamp& timestamp, JointLockCallback& callback)
{
	if (!m_finalized)
		return false;
	
	// integration and joint limit
	// for spherical joint we must use a more sophisticated method
	unsigned int q_nr;
	double* qdot=m_qdotKdl(0);
	double* q=m_qKdl(0);
	double* newq=m_newqKdl(0);
	double norm, qx, qz, CX, CZ, sx, sz;
	bool locked = false;
	int unlocked = 0;

	for (q_nr=0; q_nr<m_nq; ++q_nr)
		qdot[q_nr]=m_qdot[q_nr];

	for (q_nr=0; q_nr<m_nq; ) {
		Joint_struct* joint = &m_joints[q_nr];
		if (!joint->locked) {
			switch (joint->type) {
			case KDL::Joint::Swing:
			{
				KDL::Rotation base = KDL::Rot(KDL::Vector(q[0],0.0,q[1]));
				(base*KDL::Rot(KDL::Vector(qdot[0],0.0,qdot[1])*timestamp.realTimestep)).GetXZRot().GetValue(newq);
				if (joint[0].useLimit) {
					if (joint[1].useLimit) {
						// elliptical limit
						sx = sz = 1.0;
						qx = newq[0];
						qz = newq[1];
						// determine in which quadrant we are
						if (qx > 0.0 && qz > 0.0) {
							CX = joint[0].max;
							CZ = joint[1].max;
						} else if (qx <= 0.0 && qz > 0.0) {
							CX = -joint[0].min;
							CZ = joint[1].max;
							qx = -qx;
							sx = -1.0;
						} else if (qx <= 0.0 && qz <= 0.0) {
							CX = -joint[0].min;
							CZ = -joint[1].min;
							qx = -qx;
							qz = -qz;
							sx = sz = -1.0;
						} else {
							CX = joint[0].max;
							CZ = -joint[0].min;
							qz = -qz;
							sz = -1.0;
						}
						if (CX < KDL::epsilon || CZ < KDL::epsilon) {
							// quadrant is degenerated
							if (qx > CX) {
								newq[0] = CX*sx;
								joint[0].locked = true;
							}
							if (qz > CZ) {
								newq[1] = CZ*sz;
								joint[0].locked = true;
							}
						} else {
							// general case
							qx /= CX;
							qz /= CZ;
							norm = KDL::sqrt(KDL::sqr(qx)+KDL::sqr(qz));
							if (norm > 1.0) {
								norm = 1.0/norm;
								newq[0] = qx*norm*CX*sx;
								newq[1] = qz*norm*CZ*sz;
								joint[0].locked = true;
							}
						}
					} else {
						// limit on X only
						qx = newq[0];
						if (qx > joint[0].max) {
							newq[0] = joint[0].max;
							joint[0].locked = true;
						} else if (qx < joint[0].min) {
							newq[0] = joint[0].min;
							joint[0].locked = true;
						}
					}
				} else if (joint[1].useLimit) {
					// limit on Z only
					qz = newq[1];
					if (qz > joint[1].max) {
						newq[1] = joint[1].max;
						joint[0].locked = true;
					} else if (qz < joint[1].min) {
						newq[1] = joint[1].min;
						joint[0].locked = true;
					}
				}
				if (joint[0].locked) {
					// check the difference from previous position
					locked = true;
					norm = KDL::sqr(newq[0]-q[0])+KDL::sqr(newq[1]-q[1]);
					if (norm < KDL::epsilon2) {
						// joint didn't move, no need to update the jacobian
						callback.lockJoint(q_nr, 2);
					} else {
						// joint moved, compute the corresponding velocity
						double deltaq[2];
						(base.Inverse()*KDL::Rot(KDL::Vector(newq[0],0.0,newq[1]))).GetXZRot().GetValue(deltaq);
						deltaq[0] /= timestamp.realTimestep;
						deltaq[1] /= timestamp.realTimestep;
						callback.lockJoint(q_nr, 2, deltaq);
						// no need to update the other joints, it will be done after next rerun
						goto end_loop;
					}
				} else
					unlocked++;
				break;
			}
			case KDL::Joint::Sphere:
			{
				(KDL::Rot(KDL::Vector(q))*KDL::Rot(KDL::Vector(qdot)*timestamp.realTimestep)).GetRot().GetValue(newq);
				// no limit on this joint
				unlocked++;
				break;
			}
			default:
				for (unsigned int i=0; i<joint->ndof; i++) {
					newq[i] = q[i]+qdot[i]*timestamp.realTimestep;
					if (joint[i].useLimit) {
						if (newq[i] > joint[i].max) {
							newq[i] = joint[i].max;
							joint[0].locked = true;
						} else if (newq[i] < joint[i].min) {
							newq[i] = joint[i].min;
							joint[0].locked = true;
						}
					}
				}
				if (joint[0].locked) {
					locked = true;
					norm = 0.0;
					// compute delta to locked position
					for (unsigned int i=0; i<joint->ndof; i++) {
						qdot[i] = newq[i] - q[i];
						norm += qdot[i]*qdot[i];
					}
					if (norm < KDL::epsilon2) {
						// joint didn't move, no need to update the jacobian
						callback.lockJoint(q_nr, joint->ndof);
					} else {
						// solver needs velocity, compute equivalent velocity
						for (unsigned int i=0; i<joint->ndof; i++) {
							qdot[i] /= timestamp.realTimestep;
						}
						callback.lockJoint(q_nr, joint->ndof, qdot);
						goto end_loop;
					}
				} else 
					unlocked++;
			}
		}
		qdot += joint->ndof;
		q    += joint->ndof;
		newq += joint->ndof;
		q_nr += joint->ndof;
	}
end_loop:
	// check if there any other unlocked joint
	for ( ; q_nr<m_nq; ) {
		Joint_struct* joint = &m_joints[q_nr];
		if (!joint->locked)
			unlocked++;
		q_nr += joint->ndof;
	}
	// if all joints have been locked no need to run the solver again
	return (unlocked) ? locked : false;
}

void Armature::updateKinematics(const Timestamp& timestamp){

    //Integrate m_qdot
	if (!m_finalized)
		return;

	// the new joint value have been computed already, just copy
	memcpy(m_qKdl(0), m_newqKdl(0), sizeof(double)*m_qKdl.rows());
	pushCache(timestamp);
	updateJacobian();
	// here update the desired output.
	// We assume constant desired output for the joint limit constraint, no need to update it.
}

void Armature::updateJacobian()
{
    //calculate pose and jacobian
	for (unsigned int ee=0; ee<m_nee; ee++) {
		m_fksolver->JntToCart(m_qKdl,m_effectors[ee].pose,m_effectors[ee].name,m_root);
		m_jacsolver->JntToJac(m_qKdl,*m_jac,m_effectors[ee].name);
		// get the jacobian for the base point, to prepare transformation to world reference
		changeRefPoint(*m_jac,-m_effectors[ee].pose.p,*m_jac);
		//copy to Jq:
		e_matrix& Jq = m_JqArray[ee];
		for(unsigned int i=0;i<6;i++) {
			for(unsigned int j=0;j<m_nq;j++)
				Jq(i,j)=(*m_jac)(i,j);
		}
	}
	// remember that this object has moved 
	m_updated = true;
}

const Frame& Armature::getPose(const unsigned int ee)
{
	if (!m_finalized)
		return F_identity;
	return (ee >= m_nee) ? F_identity : m_effectors[ee].pose;
}

bool Armature::getRelativeFrame(Frame& result, const std::string& segment_name, const std::string& base_name)
{
	if (!m_finalized)
		return false;
	return (m_fksolver->JntToCart(m_qKdl,result,segment_name,base_name) < 0) ? false : true;
}

void Armature::updateControlOutput(const Timestamp& timestamp)
{
	if (!m_finalized)
		return;


	if (!timestamp.substep && !timestamp.reiterate && timestamp.interpolate) {
		popQ(timestamp.cacheTimestamp);
		//popConstraints(timestamp.cacheTimestamp);
	}

	if (!timestamp.substep) {
		// save previous joint state for getMaxJointChange()
		memcpy(m_oldqKdl(0), m_qKdl(0), sizeof(double)*m_qKdl.rows());
		for (unsigned int i=0; i<m_neffector; i++) {
			m_effectors[i].oldpose = m_effectors[i].pose;
		}
	}

	// remove all joint lock
	for (JointList::iterator jit=m_joints.begin(); jit!=m_joints.end(); ++jit) {
		(*jit).locked = false;
	}

	JointConstraintList::iterator it;
	unsigned int iConstraint;

	// scan through the constraints and call the callback functions
	for (iConstraint=0, it=m_constraints.begin(); it!=m_constraints.end(); it++, iConstraint++) {
		JointConstraint_struct* pConstraint = *it;
		unsigned int nr, i;
		for (i=0, nr = pConstraint->segment->second.q_nr; i<pConstraint->v_nr; i++, nr++) {
			*(double *)&pConstraint->value[i].y = m_qKdl[nr];
			*(double *)&pConstraint->value[i].ydot = m_qdotKdl[nr];
		}
		if (pConstraint->function && (pConstraint->substep || (!timestamp.reiterate && !timestamp.substep))) {
			(*pConstraint->function)(timestamp, pConstraint->values, pConstraint->v_nr, pConstraint->param);
		}
		// recompute the weight in any case, that's the most likely modification
		for (i=0, nr=pConstraint->y_nr; i<pConstraint->v_nr; i++, nr++) {
			m_Wy(nr) = pConstraint->values[i].alpha/*/(pConstraint->values.tolerance*pConstraint->values.feedback)*/;
			m_ydot(nr)=pConstraint->value[i].yddot+pConstraint->values[i].feedback*(pConstraint->value[i].yd-pConstraint->value[i].y);
		}
	}
}

bool Armature::setControlParameter(unsigned int constraintId, unsigned int valueId, ConstraintAction action, double value, double timestep)
{
	unsigned int lastid, i;
	if (constraintId == CONSTRAINT_ID_ALL) {
		constraintId = 0;
		lastid = m_nconstraint;
	} else if (constraintId < m_nconstraint) {
		lastid = constraintId+1;
	} else {
		return false;
	}
	for ( ; constraintId<lastid; ++constraintId) {
		JointConstraint_struct* pConstraint = m_constraints[constraintId];
		if (valueId == ID_JOINT) {
			for (i=0; i<pConstraint->v_nr; i++) {
				switch (action) {
				case ACT_TOLERANCE:
					pConstraint->values[i].tolerance = value;
					break;
				case ACT_FEEDBACK:
					pConstraint->values[i].feedback = value;
					break;
				case ACT_ALPHA:
					pConstraint->values[i].alpha = value;
					break;
				default:
					break;
				}
			}
		} else {
			for (i=0; i<pConstraint->v_nr; i++) {
				if (valueId == pConstraint->value[i].id) {
					switch (action) {
					case ACT_VALUE:
						pConstraint->value[i].yd = value;
						break;
					case ACT_VELOCITY:
						pConstraint->value[i].yddot = value;
						break;
					case ACT_TOLERANCE:
						pConstraint->values[i].tolerance = value;
						break;
					case ACT_FEEDBACK:
						pConstraint->values[i].feedback = value;
						break;
					case ACT_ALPHA:
						pConstraint->values[i].alpha = value;
						break;
					case ACT_NONE:
						break;
					}
				}
			}
		}
		if (m_finalized) {
			for (i=0; i<pConstraint->v_nr; i++) 
				m_Wy(pConstraint->y_nr+i) = pConstraint->values[i].alpha/*/(pConstraint->values.tolerance*pConstraint->values.feedback)*/;
		}
	}
	return true;
}

}