Debian Codegraph @

source/unix/odesrc/joints/pr.cpp

raw
1 /*************************************************************************
2 * *
3 * Open Dynamics Engine, Copyright (C) 2001,2002 Russell L. Smith. *
4 * All rights reserved. Email: [email protected] Web: www.q12.org *
5 * *
6 * This library is free software; you can redistribute it and/or *
7 * modify it under the terms of EITHER: *
8 * (1) The GNU Lesser General Public License as published by the Free *
9 * Software Foundation; either version 2.1 of the License, or (at *
10 * your option) any later version. The text of the GNU Lesser *
11 * General Public License is included with this library in the *
12 * file LICENSE.TXT. *
13 * (2) The BSD-style license that is included with this library in *
14 * the file LICENSE-BSD.TXT. *
15 * *
16 * This library is distributed in the hope that it will be useful, *
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the files *
19 * LICENSE.TXT and LICENSE-BSD.TXT for more details. *
20 * *
21 *************************************************************************/
22
23
24 #include <ode/odeconfig.h>
25 #include "config.h"
26 #include "pr.h"
27 #include "joint_internal.h"
28
29
30
31 //****************************************************************************
32 // Prismatic and Rotoide
33
34 dxJointPR::dxJointPR( dxWorld *w ) :
35 dxJoint( w )
36 {
37 // Default Position
38 // Z^
39 // | Body 1 P R Body2
40 // |+---------+ _ _ +-----------+
41 // || |----|----(_)--------+ |
42 // |+---------+ - +-----------+
43 // |
44 // X.-----------------------------------------> Y
45 // N.B. X is comming out of the page
46 dSetZero( anchor2, 4 );
47
48 dSetZero( axisR1, 4 );
49 axisR1[0] = 1;
50 dSetZero( axisR2, 4 );
51 axisR2[0] = 1;
52
53 dSetZero( axisP1, 4 );
54 axisP1[1] = 1;
55 dSetZero( qrel, 4 );
56 dSetZero( offset, 4 );
57
58 limotR.init( world );
59 limotP.init( world );
60 }
61
62
63 dReal dJointGetPRPosition( dJointID j )
64 {
65 dxJointPR* joint = ( dxJointPR* ) j;
66 dUASSERT( joint, "bad joint argument" );
67 checktype( joint, PR );
68
69 dVector3 q;
70 // get the offset in global coordinates
71 dMultiply0_331( q, joint->node[0].body->posr.R, joint->offset );
72
73 if ( joint->node[1].body )
74 {
75 dVector3 anchor2;
76
77 // get the anchor2 in global coordinates
78 dMultiply0_331( anchor2, joint->node[1].body->posr.R, joint->anchor2 );
79
80 q[0] = (( joint->node[0].body->posr.pos[0] + q[0] ) -
81 ( joint->node[1].body->posr.pos[0] + anchor2[0] ) );
82 q[1] = (( joint->node[0].body->posr.pos[1] + q[1] ) -
83 ( joint->node[1].body->posr.pos[1] + anchor2[1] ) );
84 q[2] = (( joint->node[0].body->posr.pos[2] + q[2] ) -
85 ( joint->node[1].body->posr.pos[2] + anchor2[2] ) );
86
87 }
88 else
89 {
90 //N.B. When there is no body 2 the joint->anchor2 is already in
91 // global coordinates
92
93 q[0] = (( joint->node[0].body->posr.pos[0] + q[0] ) -
94 ( joint->anchor2[0] ) );
95 q[1] = (( joint->node[0].body->posr.pos[1] + q[1] ) -
96 ( joint->anchor2[1] ) );
97 q[2] = (( joint->node[0].body->posr.pos[2] + q[2] ) -
98 ( joint->anchor2[2] ) );
99
100 if ( joint->flags & dJOINT_REVERSE )
101 {
102 q[0] = -q[0];
103 q[1] = -q[1];
104 q[2] = -q[2];
105 }
106 }
107
108 dVector3 axP;
109 // get prismatic axis in global coordinates
110 dMultiply0_331( axP, joint->node[0].body->posr.R, joint->axisP1 );
111
112 return dCalcVectorDot3( axP, q );
113 }
114
115 dReal dJointGetPRPositionRate( dJointID j )
116 {
117 dxJointPR* joint = ( dxJointPR* ) j;
118 dUASSERT( joint, "bad joint argument" );
119 checktype( joint, PR );
120 // get axis1 in global coordinates
121 dVector3 ax1;
122 dMultiply0_331( ax1, joint->node[0].body->posr.R, joint->axisP1 );
123
124 if ( joint->node[1].body )
125 {
126 dVector3 lv2;
127 dBodyGetRelPointVel( joint->node[1].body, joint->anchor2[0], joint->anchor2[1], joint->anchor2[2], lv2 );
128 return dCalcVectorDot3( ax1, joint->node[0].body->lvel ) - dCalcVectorDot3( ax1, lv2 );
129 }
130 else
131 {
132 dReal rate = dCalcVectorDot3( ax1, joint->node[0].body->lvel );
133 return ( (joint->flags & dJOINT_REVERSE) ? -rate : rate);
134 }
135 }
136
137
138
139 dReal dJointGetPRAngle( dJointID j )
140 {
141 dxJointPR* joint = ( dxJointPR* )j;
142 dAASSERT( joint );
143 checktype( joint, PR );
144 if ( joint->node[0].body )
145 {
146 dReal ang = getHingeAngle( joint->node[0].body,
147 joint->node[1].body,
148 joint->axisR1,
149 joint->qrel );
150 if ( joint->flags & dJOINT_REVERSE )
151 return -ang;
152 else
153 return ang;
154 }
155 else return 0;
156 }
157
158
159
160 dReal dJointGetPRAngleRate( dJointID j )
161 {
162 dxJointPR* joint = ( dxJointPR* )j;
163 dAASSERT( joint );
164 checktype( joint, PR );
165 if ( joint->node[0].body )
166 {
167 dVector3 axis;
168 dMultiply0_331( axis, joint->node[0].body->posr.R, joint->axisR1 );
169 dReal rate = dCalcVectorDot3( axis, joint->node[0].body->avel );
170 if ( joint->node[1].body ) rate -= dCalcVectorDot3( axis, joint->node[1].body->avel );
171 if ( joint->flags & dJOINT_REVERSE ) rate = -rate;
172 return rate;
173 }
174 else return 0;
175 }
176
177
178
179
180 void
181 dxJointPR::getSureMaxInfo( SureMaxInfo* info )
182 {
183 info->max_m = 6;
184 }
185
186
187
188 void
189 dxJointPR::getInfo1( dxJoint::Info1 *info )
190 {
191 info->nub = 4;
192 info->m = 4;
193
194
195 // see if we're at a joint limit.
196 limotP.limit = 0;
197 if (( limotP.lostop > -dInfinity || limotP.histop < dInfinity ) &&
198 limotP.lostop <= limotP.histop )
199 {
200 // measure joint position
201 dReal pos = dJointGetPRPosition( this );
202 limotP.testRotationalLimit( pos ); // N.B. The function is ill named
203 }
204
205 // powered needs an extra constraint row
206 if ( limotP.limit || limotP.fmax > 0 ) info->m++;
207
208
209 // see if we're at a joint limit.
210 limotR.limit = 0;
211 if (( limotR.lostop >= -M_PI || limotR.histop <= M_PI ) &&
212 limotR.lostop <= limotR.histop )
213 {
214 dReal angle = getHingeAngle( node[0].body,
215 node[1].body,
216 axisR1, qrel );
217 limotR.testRotationalLimit( angle );
218 }
219
220 // powered morit or at limits needs an extra constraint row
221 if ( limotR.limit || limotR.fmax > 0 ) info->m++;
222
223 }
224
225
226
227 void
228 dxJointPR::getInfo2( dReal worldFPS, dReal worldERP,
229 int rowskip, dReal *J1, dReal *J2,
230 int pairskip, dReal *pairRhsCfm, dReal *pairLoHi,
231 int *findex )
232 {
233 dReal k = worldFPS * worldERP;
234
235
236 dVector3 q; // plane space of axP and after that axR
237
238 // pull out pos and R for both bodies. also get the `connection'
239 // vector pos2-pos1.
240
241 dReal *pos2 = NULL, *R2 = NULL;
242
243 dReal *pos1 = node[0].body->posr.pos;
244 dReal *R1 = node[0].body->posr.R;
245
246 dxBody *body1 = node[1].body;
247
248 if ( body1 )
249 {
250 pos2 = body1->posr.pos;
251 R2 = body1->posr.R;
252 }
253
254
255 dVector3 axP; // Axis of the prismatic joint in global frame
256 dMultiply0_331( axP, R1, axisP1 );
257
258 // distance between the body1 and the anchor2 in global frame
259 // Calculated in the same way as the offset
260 dVector3 wanchor2 = {0, 0, 0}, dist;
261
262 if ( body1 )
263 {
264 // Calculate anchor2 in world coordinate
265 dMultiply0_331( wanchor2, R2, anchor2 );
266 dist[0] = wanchor2[0] + pos2[0] - pos1[0];
267 dist[1] = wanchor2[1] + pos2[1] - pos1[1];
268 dist[2] = wanchor2[2] + pos2[2] - pos1[2];
269 }
270 else
271 {
272 if ( (flags & dJOINT_REVERSE) != 0 )
273 {
274 dSubtractVectors3(dist, pos1, anchor2); // Invert the value
275 }
276 else
277 {
278 dSubtractVectors3(dist, anchor2, pos1); // Invert the value
279 }
280 }
281
282
283 // ======================================================================
284 // Work on the Rotoide part (i.e. row 0, 1 and maybe 4 if rotoide powered
285
286 // Set the two rotoide rows. The rotoide axis should be the only unconstrained
287 // rotational axis, the angular velocity of the two bodies perpendicular to
288 // the rotoide axis should be equal. Thus the constraint equations are
289 // p*w1 - p*w2 = 0
290 // q*w1 - q*w2 = 0
291 // where p and q are unit vectors normal to the rotoide axis, and w1 and w2
292 // are the angular velocity vectors of the two bodies.
293 dVector3 ax2;
294 dVector3 ax1;
295 dMultiply0_331( ax1, R1, axisR1 );
296 dCalcVectorCross3( q , ax1, axP );
297
298 dCopyVector3(J1 + GI2__JA_MIN, axP);
299
300 if ( body1 )
301 {
302 dCopyNegatedVector3(J2 + GI2__JA_MIN, axP);
303 }
304
305 dCopyVector3(J1 + rowskip + GI2__JA_MIN, q);
306
307 if ( body1 )
308 {
309 dCopyNegatedVector3(J2 + rowskip + GI2__JA_MIN, q);
310 }
311
312 // Compute the right hand side of the constraint equation set. Relative
313 // body velocities along p and q to bring the rotoide back into alignment.
314 // ax1,ax2 are the unit length rotoide axes of body1 and body2 in world frame.
315 // We need to rotate both bodies along the axis u = (ax1 x ax2).
316 // if `theta' is the angle between ax1 and ax2, we need an angular velocity
317 // along u to cover angle erp*theta in one step :
318 // |angular_velocity| = angle/time = erp*theta / stepsize
319 // = (erp*fps) * theta
320 // angular_velocity = |angular_velocity| * (ax1 x ax2) / |ax1 x ax2|
321 // = (erp*fps) * theta * (ax1 x ax2) / sin(theta)
322 // ...as ax1 and ax2 are unit length. if theta is smallish,
323 // theta ~= sin(theta), so
324 // angular_velocity = (erp*fps) * (ax1 x ax2)
325 // ax1 x ax2 is in the plane space of ax1, so we project the angular
326 // velocity to p and q to find the right hand side.
327
328 if ( body1 )
329 {
330 dMultiply0_331( ax2, R2, axisR2 );
331 }
332 else
333 {
334 dCopyVector3(ax2, axisR2);
335 }
336
337 dVector3 b;
338 dCalcVectorCross3( b, ax1, ax2 );
339 pairRhsCfm[GI2_RHS] = k * dCalcVectorDot3( b, axP );
340 pairRhsCfm[pairskip + GI2_RHS] = k * dCalcVectorDot3( b, q );
341
342
343
344 // ==========================
345 // Work on the Prismatic part (i.e row 2,3 and 4 if only the prismatic is powered
346 // or 5 if rotoide and prismatic powered
347
348 // two rows. we want: vel2 = vel1 + w1 x c ... but this would
349 // result in three equations, so we project along the planespace vectors
350 // so that sliding along the prismatic axis is disregarded. for symmetry we
351 // also substitute (w1+w2)/2 for w1, as w1 is supposed to equal w2.
352
353 // p1 + R1 dist' = p2 + R2 anchor2' ## OLD ## p1 + R1 anchor1' = p2 + R2 dist'
354 // v1 + w1 x R1 dist' + v_p = v2 + w2 x R2 anchor2'## OLD v1 + w1 x R1 anchor1' = v2 + w2 x R2 dist' + v_p
355 // v_p is speed of prismatic joint (i.e. elongation rate)
356 // Since the constraints are perpendicular to v_p we have:
357 // p dot v_p = 0 and q dot v_p = 0
358 // ax1 dot ( v1 + w1 x dist = v2 + w2 x anchor2 )
359 // q dot ( v1 + w1 x dist = v2 + w2 x anchor2 )
360 // ==
361 // ax1 . v1 + ax1 . w1 x dist = ax1 . v2 + ax1 . w2 x anchor2 ## OLD ## ax1 . v1 + ax1 . w1 x anchor1 = ax1 . v2 + ax1 . w2 x dist
362 // since a . (b x c) = - b . (a x c) = - (a x c) . b
363 // and a x b = - b x a
364 // ax1 . v1 - ax1 x dist . w1 - ax1 . v2 - (- ax1 x anchor2 . w2) = 0
365 // ax1 . v1 + dist x ax1 . w1 - ax1 . v2 - anchor2 x ax1 . w2 = 0
366 // Coeff for 1er line of: J1l => ax1, J2l => -ax1
367 // Coeff for 2er line of: J1l => q, J2l => -q
368 // Coeff for 1er line of: J1a => dist x ax1, J2a => - anchor2 x ax1
369 // Coeff for 2er line of: J1a => dist x q, J2a => - anchor2 x q
370
371 int currRowSkip = 2 * rowskip;
372 {
373 dCopyVector3( J1 + currRowSkip + GI2__JL_MIN, ax1 );
374 dCalcVectorCross3( J1 + currRowSkip + GI2__JA_MIN, dist, ax1 );
375
376 if ( body1 )
377 {
378 dCopyNegatedVector3( J2 + currRowSkip + GI2__JL_MIN, ax1 );
379 // ax2 x anchor2 instead of anchor2 x ax2 since we want the negative value
380 dCalcVectorCross3( J2 + currRowSkip + GI2__JA_MIN, ax2, wanchor2 ); // since ax1 == ax2
381 }
382 }
383
384 currRowSkip += rowskip;
385 {
386 dCopyVector3( J1 + currRowSkip + GI2__JL_MIN, q );
387 dCalcVectorCross3(J1 + currRowSkip + GI2__JA_MIN, dist, q );
388
389 if ( body1 )
390 {
391 dCopyNegatedVector3( J2 + currRowSkip + GI2__JL_MIN, q);
392 // The cross product is in reverse order since we want the negative value
393 dCalcVectorCross3( J2 + currRowSkip + GI2__JA_MIN, q, wanchor2 );
394 }
395 }
396
397 // We want to make correction for motion not in the line of the axisP
398 // We calculate the displacement w.r.t. the anchor pt.
399 //
400 // compute the elements 2 and 3 of right hand side.
401 // we want to align the offset point (in body 2's frame) with the center of body 1.
402 // The position should be the same when we are not along the prismatic axis
403 dVector3 err;
404 dMultiply0_331( err, R1, offset );
405 dSubtractVectors3(err, dist, err);
406
407 int currPairSkip = 2 * pairskip;
408 {
409 pairRhsCfm[currPairSkip + GI2_RHS] = k * dCalcVectorDot3( ax1, err );
410 }
411
412 currPairSkip += pairskip;
413 {
414 pairRhsCfm[currPairSkip + GI2_RHS] = k * dCalcVectorDot3( q, err );
415 }
416
417 currRowSkip += rowskip; currPairSkip += pairskip;
418
419 if ( body1 || (flags & dJOINT_REVERSE) == 0 )
420 {
421 if (limotP.addLimot ( this, worldFPS, J1 + currRowSkip, J2 + currRowSkip, pairRhsCfm + currPairSkip, pairLoHi + currPairSkip, axP, 0 ))
422 {
423 currRowSkip += rowskip; currPairSkip += pairskip;
424 }
425 }
426 else
427 {
428 dVector3 rAxP;
429 dCopyNegatedVector3(rAxP, axP);
430
431 if (limotP.addLimot ( this, worldFPS, J1 + currRowSkip, J2 + currRowSkip, pairRhsCfm + currPairSkip, pairLoHi + currPairSkip, rAxP, 0 ))
432 {
433 currRowSkip += rowskip; currPairSkip += pairskip;
434 }
435 }
436
437 limotR.addLimot ( this, worldFPS, J1 + currRowSkip, J2 + currRowSkip, pairRhsCfm + currPairSkip, pairLoHi + currPairSkip, ax1, 1 );
438 }
439
440
441 // compute initial relative rotation body1 -> body2, or env -> body1
442 void
443 dxJointPR::computeInitialRelativeRotation()
444 {
445 if ( node[0].body )
446 {
447 if ( node[1].body )
448 {
449 dQMultiply1( qrel, node[0].body->q, node[1].body->q );
450 }
451 else
452 {
453 // set joint->qrel to the transpose of the first body q
454 qrel[0] = node[0].body->q[0];
455 for ( int i = 1; i < 4; i++ )
456 qrel[i] = -node[0].body->q[i];
457 // WARNING do we need the - in -joint->node[0].body->q[i]; or not
458 }
459 }
460 }
461
462 void dJointSetPRAnchor( dJointID j, dReal x, dReal y, dReal z )
463 {
464 dxJointPR* joint = ( dxJointPR* ) j;
465 dUASSERT( joint, "bad joint argument" );
466 checktype( joint, PR );
467 setAnchors( joint, x, y, z, joint->offset, joint->anchor2 );
468 }
469
470
471 void dJointSetPRAxis1( dJointID j, dReal x, dReal y, dReal z )
472 {
473 dxJointPR* joint = ( dxJointPR* ) j;
474 dUASSERT( joint, "bad joint argument" );
475 checktype( joint, PR );
476
477 setAxes( joint, x, y, z, joint->axisP1, 0 );
478
479 joint->computeInitialRelativeRotation();
480 }
481
482
483 void dJointSetPRAxis2( dJointID j, dReal x, dReal y, dReal z )
484 {
485 dxJointPR* joint = ( dxJointPR* ) j;
486 dUASSERT( joint, "bad joint argument" );
487 checktype( joint, PR );
488 setAxes( joint, x, y, z, joint->axisR1, joint->axisR2 );
489 joint->computeInitialRelativeRotation();
490 }
491
492
493 void dJointSetPRParam( dJointID j, int parameter, dReal value )
494 {
495 dxJointPR* joint = ( dxJointPR* ) j;
496 dUASSERT( joint, "bad joint argument" );
497 checktype( joint, PR );
498 if (( parameter & 0xff00 ) == 0x100 )
499 {
500 joint->limotR.set( parameter & 0xff, value ); // Take only lower part of the
501 } // parameter alue
502 else
503 {
504 joint->limotP.set( parameter, value );
505 }
506 }
507
508 void dJointGetPRAnchor( dJointID j, dVector3 result )
509 {
510 dxJointPR* joint = ( dxJointPR* ) j;
511 dUASSERT( joint, "bad joint argument" );
512 dUASSERT( result, "bad result argument" );
513 checktype( joint, PR );
514
515 if ( joint->node[1].body )
516 getAnchor2( joint, result, joint->anchor2 );
517 else
518 {
519 result[0] = joint->anchor2[0];
520 result[1] = joint->anchor2[1];
521 result[2] = joint->anchor2[2];
522 }
523 }
524
525 void dJointGetPRAxis1( dJointID j, dVector3 result )
526 {
527 dxJointPR* joint = ( dxJointPR* ) j;
528 dUASSERT( joint, "bad joint argument" );
529 dUASSERT( result, "bad result argument" );
530 checktype( joint, PR );
531 getAxis( joint, result, joint->axisP1 );
532 }
533
534 void dJointGetPRAxis2( dJointID j, dVector3 result )
535 {
536 dxJointPR* joint = ( dxJointPR* ) j;
537 dUASSERT( joint, "bad joint argument" );
538 dUASSERT( result, "bad result argument" );
539 checktype( joint, PR );
540 getAxis( joint, result, joint->axisR1 );
541 }
542
543 dReal dJointGetPRParam( dJointID j, int parameter )
544 {
545 dxJointPR* joint = ( dxJointPR* ) j;
546 dUASSERT( joint, "bad joint argument" );
547 checktype( joint, PR );
548 if (( parameter & 0xff00 ) == 0x100 )
549 {
550 return joint->limotR.get( parameter & 0xff );
551 }
552 else
553 {
554 return joint->limotP.get( parameter );
555 }
556 }
557
558 void dJointAddPRTorque( dJointID j, dReal torque )
559 {
560 dxJointPR* joint = ( dxJointPR* ) j;
561 dVector3 axis;
562 dAASSERT( joint );
563 checktype( joint, PR );
564
565 if ( joint->flags & dJOINT_REVERSE )
566 torque = -torque;
567
568 getAxis( joint, axis, joint->axisR1 );
569 axis[0] *= torque;
570 axis[1] *= torque;
571 axis[2] *= torque;
572
573 if ( joint->node[0].body != 0 )
574 dBodyAddTorque( joint->node[0].body, axis[0], axis[1], axis[2] );
575 if ( joint->node[1].body != 0 )
576 dBodyAddTorque( joint->node[1].body, -axis[0], -axis[1], -axis[2] );
577 }
578
579
580 dJointType
581 dxJointPR::type() const
582 {
583 return dJointTypePR;
584 }
585
586 size_t
587 dxJointPR::size() const
588 {
589 return sizeof( *this );
590 }
591
592
593 void
594 dxJointPR::setRelativeValues()
595 {
596 dVector3 anchor;
597 dJointGetPRAnchor(this, anchor);
598 setAnchors( this, anchor[0], anchor[1], anchor[2], offset, anchor2 );
599
600 dVector3 axis;
601 dJointGetPRAxis1(this, axis);
602 setAxes( this, axis[0], axis[1], axis[2], axisP1, 0 );
603
604 dJointGetPRAxis2(this, axis);
605 setAxes( this, axis[0], axis[1], axis[2], axisR1, axisR2 );
606
607 computeInitialRelativeRotation();
608 }
609
610
611
612
613