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

CopyPose.cpp « itasc « intern - git.blender.org/blender.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 8b177bb102de2fe611c2e514279a508c69937fdc (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
/** \file itasc/CopyPose.cpp
 * \ingroup itasc
 */
/*
 * CopyPose.cpp
 *
 *  Created on: Mar 17, 2009
 *      Author: benoit bolsee
 */

#include "CopyPose.hpp"
#include "kdl/kinfam_io.hpp"
#include <math.h>
#include <string.h>

namespace iTaSC
{

const unsigned int maxPoseCacheSize = (2*(3+3*2));
CopyPose::CopyPose(unsigned int control_output, unsigned int dynamic_output, double armlength, double accuracy, unsigned int maximum_iterations):
    ConstraintSet(),
	m_cache(NULL),
    m_poseCCh(-1),m_poseCTs(0)
{
	m_maxerror = armlength/2.0;
	m_outputControl = (control_output & CTL_ALL);
	unsigned int _nc = nBitsOn(m_outputControl);
	if (!_nc) 
		return;
	// reset the constraint set
	reset(_nc, accuracy, maximum_iterations);
	_nc = 0;
	m_nvalues = 0;
	int nrot = 0, npos = 0;
	int nposCache = 0, nrotCache = 0;
	m_outputDynamic = (dynamic_output & m_outputControl);
	memset(m_values, 0, sizeof(m_values));
	memset(m_posData, 0, sizeof(m_posData));
	memset(m_rotData, 0, sizeof(m_rotData));
	memset(&m_rot, 0, sizeof(m_rot));
	memset(&m_pos, 0, sizeof(m_pos));
	if (m_outputControl & CTL_POSITION) {
		m_pos.alpha = 1.0;		
		m_pos.K = 20.0;		
		m_pos.tolerance = 0.05;	
		m_values[m_nvalues].alpha = m_pos.alpha;
		m_values[m_nvalues].feedback = m_pos.K;
		m_values[m_nvalues].tolerance = m_pos.tolerance;
		m_values[m_nvalues].id = ID_POSITION;
		if (m_outputControl & CTL_POSITIONX) {
		    m_Wy(_nc) = m_pos.alpha/*/(m_pos.tolerance*m_pos.K)*/;
			m_Cf(_nc++,0)=1.0;
			m_posData[npos++].id = ID_POSITIONX;
			if (m_outputDynamic & CTL_POSITIONX)
				nposCache++;
		} 
		if (m_outputControl & CTL_POSITIONY) {
		    m_Wy(_nc) = m_pos.alpha/*/(m_pos.tolerance*m_pos.K)*/;
			m_Cf(_nc++,1)=1.0;
			m_posData[npos++].id = ID_POSITIONY;
			if (m_outputDynamic & CTL_POSITIONY)
				nposCache++;
		}
		if (m_outputControl & CTL_POSITIONZ) {
		    m_Wy(_nc) = m_pos.alpha/*/(m_pos.tolerance*m_pos.K)*/;
			m_Cf(_nc++,2)=1.0;
			m_posData[npos++].id = ID_POSITIONZ;
			if (m_outputDynamic & CTL_POSITIONZ)
				nposCache++;
		}
		m_values[m_nvalues].number = npos;
		m_values[m_nvalues++].values = m_posData;
		m_pos.firsty = 0;
		m_pos.ny = npos;
	}
	if (m_outputControl & CTL_ROTATION) {
		m_rot.alpha = 1.0;		
		m_rot.K = 20.0;		
		m_rot.tolerance = 0.05;	
		m_values[m_nvalues].alpha = m_rot.alpha;
		m_values[m_nvalues].feedback = m_rot.K;
		m_values[m_nvalues].tolerance = m_rot.tolerance;
		m_values[m_nvalues].id = ID_ROTATION;
		if (m_outputControl & CTL_ROTATIONX) {
		    m_Wy(_nc) = m_rot.alpha/*/(m_rot.tolerance*m_rot.K)*/;
			m_Cf(_nc++,3)=1.0;
			m_rotData[nrot++].id = ID_ROTATIONX;
			if (m_outputDynamic & CTL_ROTATIONX)
				nrotCache++;
		}
		if (m_outputControl & CTL_ROTATIONY) {
		    m_Wy(_nc) = m_rot.alpha/*/(m_rot.tolerance*m_rot.K)*/;
			m_Cf(_nc++,4)=1.0;
			m_rotData[nrot++].id = ID_ROTATIONY;
			if (m_outputDynamic & CTL_ROTATIONY)
				nrotCache++;
		}
		if (m_outputControl & CTL_ROTATIONZ) {
		    m_Wy(_nc) = m_rot.alpha/*/(m_rot.tolerance*m_rot.K)*/;
			m_Cf(_nc++,5)=1.0;
			m_rotData[nrot++].id = ID_ROTATIONZ;
			if (m_outputDynamic & CTL_ROTATIONZ)
				nrotCache++;
		}
		m_values[m_nvalues].number = nrot;
		m_values[m_nvalues++].values = m_rotData;
		m_rot.firsty = npos;
		m_rot.ny = nrot;
	}
	assert(_nc == m_nc);
    m_Jf=e_identity_matrix(6,6);
	m_poseCacheSize = ((nrotCache)?(3+nrotCache*2):0)+((nposCache)?(3+nposCache*2):0);
}

CopyPose::~CopyPose()
{
}

bool CopyPose::initialise(Frame& init_pose)
{
    m_externalPose = m_internalPose = init_pose;
	updateJacobian();
    return true;
}

void CopyPose::modelUpdate(Frame& _external_pose,const Timestamp& timestamp)
{
	m_internalPose = m_externalPose = _external_pose;
	updateJacobian();
}

void CopyPose::initCache(Cache *_cache)
{
    m_cache = _cache;
    m_poseCCh = -1;
    if (m_cache) {
        // create one channel for the coordinates
        m_poseCCh = m_cache->addChannel(this, "Xf", m_poseCacheSize*sizeof(double));
        // don't save initial value, it will be recomputed from external pose
        //pushPose(0);
    }
}

double* CopyPose::pushValues(double* item, ControlState* _state, unsigned int mask)
{
	ControlState::ControlValue* _yval;
	int i;

	*item++ = _state->alpha;
	*item++ = _state->K;
	*item++ = _state->tolerance;

	for (i=0, _yval=_state->output; i<_state->ny; mask<<=1) {
		if (m_outputControl & mask) {
			if (m_outputDynamic & mask) {
				*item++ = _yval->yd;
				*item++ = _yval->yddot;
			}
			_yval++;
			i++;
		}
	}
	return item;
}

void CopyPose::pushPose(CacheTS timestamp)
{
    if (m_poseCCh >= 0) {
		if (m_poseCacheSize) {
			double buf[maxPoseCacheSize];
			double *item = buf;
			if (m_outputDynamic & CTL_POSITION)
				item = pushValues(item, &m_pos, CTL_POSITIONX);
			if (m_outputDynamic & CTL_ROTATION)
				item = pushValues(item, &m_rot, CTL_ROTATIONX);
			m_cache->addCacheVectorIfDifferent(this, m_poseCCh, timestamp, buf, m_poseCacheSize, KDL::epsilon);
		} else
			m_cache->addCacheVectorIfDifferent(this, m_poseCCh, timestamp, NULL, 0, KDL::epsilon);
		m_poseCTs = timestamp;
    }
}

double* CopyPose::restoreValues(double* item, ConstraintValues* _values, ControlState* _state, unsigned int mask)
{
	ConstraintSingleValue* _data;
	ControlState::ControlValue* _yval;
	int i, j;

	_values->alpha = _state->alpha = *item++;
	_values->feedback = _state->K = *item++;
	_values->tolerance = _state->tolerance = *item++;

	for (i=_state->firsty, j=i+_state->ny, _yval=_state->output, _data=_values->values; i<j; mask<<=1) {
		if (m_outputControl & mask) {
			m_Wy(i) = _state->alpha/*/(_state->tolerance*_state->K)*/;
			if (m_outputDynamic & mask) {
				_data->yd = _yval->yd = *item++;
				_data->yddot = _yval->yddot = *item++;
			}
			_data++;
			_yval++;
			i++;
		}
	}
	return item;
}

bool CopyPose::popPose(CacheTS timestamp)
{
	bool found = false;
    if (m_poseCCh >= 0) {
        double *item = (double*)m_cache->getPreviousCacheItem(this, m_poseCCh, &timestamp);
		if (item) {
			found = true;
			if (timestamp != m_poseCTs) {
				int i=0;
				if (m_outputControl & CTL_POSITION) {
					if (m_outputDynamic & CTL_POSITION) {
						item = restoreValues(item, &m_values[i], &m_pos, CTL_POSITIONX);
					}
					i++;
				}
				if (m_outputControl & CTL_ROTATION) {
					if (m_outputDynamic & CTL_ROTATION) {
						item = restoreValues(item, &m_values[i], &m_rot, CTL_ROTATIONX);
					}
					i++;
				}
				m_poseCTs = timestamp;
				item = NULL;
			}
        }
    }
    return found;
}

void CopyPose::interpolateOutput(ControlState* _state, unsigned int mask, const Timestamp& timestamp)
{
	ControlState::ControlValue* _yval;
	int i;

	for (i=0, _yval=_state->output; i<_state->ny; mask <<= 1) {
		if (m_outputControl & mask) {
			if (m_outputDynamic & mask) {
				if (timestamp.substep && timestamp.interpolate) {
					_yval->yd += _yval->yddot*timestamp.realTimestep;
				} else {
					_yval->yd = _yval->nextyd;
					_yval->yddot = _yval->nextyddot;
				}
			}
			i++;
			_yval++;
		}
	}
}

void CopyPose::pushCache(const Timestamp& timestamp)
{
	if (!timestamp.substep && timestamp.cache) {
        pushPose(timestamp.cacheTimestamp);
	}
}

void CopyPose::updateKinematics(const Timestamp& timestamp)
{
	if (timestamp.interpolate) {
		if (m_outputDynamic & CTL_POSITION)
			interpolateOutput(&m_pos, CTL_POSITIONX, timestamp);
		if (m_outputDynamic & CTL_ROTATION)
			interpolateOutput(&m_rot, CTL_ROTATIONX, timestamp);
	}
	pushCache(timestamp);
}

void CopyPose::updateJacobian()
{
    //Jacobian is always identity at the start of the constraint chain
	//instead of going through complicated jacobian operation, implemented direct formula
	//m_Jf(1,3) = m_internalPose.p.z();
	//m_Jf(2,3) = -m_internalPose.p.y();
	//m_Jf(0,4) = -m_internalPose.p.z();
	//m_Jf(2,4) = m_internalPose.p.x();
	//m_Jf(0,5) = m_internalPose.p.y();
	//m_Jf(1,5) = -m_internalPose.p.x();
}

void CopyPose::updateState(ConstraintValues* _values, ControlState* _state, unsigned int mask, double timestep)
{
	unsigned int id = (mask == CTL_ROTATIONX) ? ID_ROTATIONX : ID_POSITIONX;
	ControlState::ControlValue* _yval;
	ConstraintSingleValue* _data;
	int i, j, k;
    int action = 0;

    if ((_values->action & ACT_ALPHA) && _values->alpha >= 0.0) {
        _state->alpha = _values->alpha;
        action |= ACT_ALPHA;
    }
    if ((_values->action & ACT_TOLERANCE) && _values->tolerance > KDL::epsilon) {
        _state->tolerance = _values->tolerance;
        action |= ACT_TOLERANCE;
    }
    if ((_values->action & ACT_FEEDBACK) && _values->feedback > KDL::epsilon) {
        _state->K = _values->feedback;
        action |= ACT_FEEDBACK;
    }
	for (i=_state->firsty, j=_state->firsty+_state->ny, _yval=_state->output; i<j; mask <<= 1, id++) {
		if (m_outputControl & mask) {
			if (action)
				m_Wy(i) = _state->alpha/*/(_state->tolerance*_state->K)*/;
			// check if this controlled output is provided
			for (k=0, _data=_values->values; k<_values->number; k++, _data++) {
				if (_data->id == id) {
					switch (_data->action & (ACT_VALUE|ACT_VELOCITY)) {
					case 0:
						// no indication, keep current values
						break;
					case ACT_VELOCITY:
						// only the velocity is given estimate the new value by integration
						_data->yd = _yval->yd+_data->yddot*timestep;
						// walkthrough
					case ACT_VALUE:
						_yval->nextyd = _data->yd;
						// if the user sets the value, we assume future velocity is zero
						// (until the user changes the value again)
						_yval->nextyddot = (_data->action & ACT_VALUE) ? 0.0 : _data->yddot;
						if (timestep>0.0) {
							_yval->yddot = (_data->yd-_yval->yd)/timestep;
						} else {
							// allow the user to change target instantenously when this function
							// if called from setControlParameter with timestep = 0
							_yval->yd = _yval->nextyd;
							_yval->yddot = _yval->nextyddot;
						}
						break;
					case (ACT_VALUE|ACT_VELOCITY):
						// the user should not set the value and velocity at the same time.
						// In this case, we will assume that he wants to set the future value
						// and we compute the current value to match the velocity
						_yval->yd = _data->yd - _data->yddot*timestep;
						_yval->nextyd = _data->yd;
						_yval->nextyddot = _data->yddot;
						if (timestep>0.0) {
							_yval->yddot = (_data->yd-_yval->yd)/timestep;
						} else {
							_yval->yd = _yval->nextyd;
							_yval->yddot = _yval->nextyddot;
						}
						break;
					}
				}
			}
			_yval++;
			i++;
		}
	}
}


bool CopyPose::setControlParameters(struct ConstraintValues* _values, unsigned int _nvalues, double timestep)
{
	while (_nvalues > 0) {
		if (_values->id >= ID_POSITION && _values->id <= ID_POSITIONZ && (m_outputControl & CTL_POSITION)) {
			updateState(_values, &m_pos, CTL_POSITIONX, timestep);
		} 
		if (_values->id >= ID_ROTATION && _values->id <= ID_ROTATIONZ && (m_outputControl & CTL_ROTATION)) {
			updateState(_values, &m_rot, CTL_ROTATIONX, timestep);
		}
		_values++;
		_nvalues--;
	}
    return true;
}

void CopyPose::updateValues(Vector& vel, ConstraintValues* _values, ControlState* _state, unsigned int mask)
{
	ConstraintSingleValue* _data;
	ControlState::ControlValue* _yval;
	int i, j;

	_values->action = 0;

	for (i=_state->firsty, j=0, _yval=_state->output, _data=_values->values; j<3; j++, mask<<=1) {
		if (m_outputControl & mask) {
			*(double*)&_data->y = vel(j);
			*(double*)&_data->ydot = m_ydot(i);
			_data->yd = _yval->yd;
			_data->yddot = _yval->yddot;
			_data->action = 0;
			i++;
			_data++;
			_yval++;
		}
	}
}

void CopyPose::updateOutput(Vector& vel, ControlState* _state, unsigned int mask)
{
	ControlState::ControlValue* _yval;
	int i, j;
	double coef=1.0;
	if (mask & CTL_POSITION) {
		// put a limit on position error
		double len=0.0;
		for (j=0, _yval=_state->output; j<3; j++) {
			if (m_outputControl & (mask<<j)) {
				len += KDL::sqr(_yval->yd-vel(j));
				_yval++;
			}
		}
		len = KDL::sqrt(len);
		if (len > m_maxerror)
			coef = m_maxerror/len;
	}
	for (i=_state->firsty, j=0, _yval=_state->output; j<3; j++) {
		if (m_outputControl & (mask<<j)) {
			m_ydot(i)=_yval->yddot+_state->K*coef*(_yval->yd-vel(j));
			_yval++;
			i++;
		}
	}
}

void CopyPose::updateControlOutput(const Timestamp& timestamp)
{
    //IMO this should be done, no idea if it is enough (wrt Distance impl)
	Twist y = diff(F_identity, m_internalPose);
	bool found = true;
	if (!timestamp.substep) {
		if (!timestamp.reiterate) {
			found = popPose(timestamp.cacheTimestamp);
		}
	}
	if (m_constraintCallback && (m_substep || (!timestamp.reiterate && !timestamp.substep))) {
		// initialize first callback the application to get the current values
		int i=0;
		if (m_outputControl & CTL_POSITION) {
			updateValues(y.vel, &m_values[i++], &m_pos, CTL_POSITIONX);
		}
		if (m_outputControl & CTL_ROTATION) {
			updateValues(y.rot, &m_values[i++], &m_rot, CTL_ROTATIONX);
		}
		if ((*m_constraintCallback)(timestamp, m_values, m_nvalues, m_constraintParam)) {
			setControlParameters(m_values, m_nvalues, (found && timestamp.interpolate)?timestamp.realTimestep:0.0);
		}
	}
	if (m_outputControl & CTL_POSITION) {
		updateOutput(y.vel, &m_pos, CTL_POSITIONX);
	}
	if (m_outputControl & CTL_ROTATION) {
		updateOutput(y.rot, &m_rot, CTL_ROTATIONX);
	}
}

const ConstraintValues* CopyPose::getControlParameters(unsigned int* _nvalues)
{
	Twist y = diff(m_internalPose,F_identity);
	int i=0;
	if (m_outputControl & CTL_POSITION) {
		updateValues(y.vel, &m_values[i++], &m_pos, CTL_POSITIONX);
	}
	if (m_outputControl & CTL_ROTATION) {
		updateValues(y.rot, &m_values[i++], &m_rot, CTL_ROTATIONX);
	}
	if (_nvalues)
		*_nvalues=m_nvalues; 
	return m_values; 
}

double CopyPose::getMaxTimestep(double& timestep)
{
	// CopyPose should not have any limit on linear velocity: 
	// in case the target is out of reach, this can be very high.
	// We will simply limit on rotation
	e_scalar maxChidot = m_chidot.block(3,0,3,1).array().abs().maxCoeff();
	if (timestep*maxChidot > m_maxDeltaChi) {
		timestep = m_maxDeltaChi/maxChidot;
	}
	return timestep;
}

}