Debian Codegraph @

source/unix/odesrc/joints/hinge2.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 "hinge2.h"
27 #include "joint_internal.h"
28
29
30
31
32 //****************************************************************************
33 // hinge 2. note that this joint must be attached to two bodies for it to work
34
35 dReal
36 dxJointHinge2::measureAngle1() const
37 {
38 // bring axis 2 into first body's reference frame
39 dVector3 p, q;
40 if (node[1].body)
41 dMultiply0_331( p, node[1].body->posr.R, axis2 );
42 else
43 dCopyVector3(p, axis2);
44
45 if (node[0].body)
46 dMultiply1_331( q, node[0].body->posr.R, p );
47 else
48 dCopyVector3(q, p);
49
50 dReal x = dCalcVectorDot3( v1, q );
51 dReal y = dCalcVectorDot3( v2, q );
52 return -dAtan2( y, x );
53 }
54
55 dReal
56 dxJointHinge2::measureAngle2() const
57 {
58 // bring axis 1 into second body's reference frame
59 dVector3 p, q;
60 if (node[0].body)
61 dMultiply0_331( p, node[0].body->posr.R, axis1 );
62 else
63 dCopyVector3(p, axis1);
64
65 if (node[1].body)
66 dMultiply1_331( q, node[1].body->posr.R, p );
67 else
68 dCopyVector3(q, p);
69
70 dReal x = dCalcVectorDot3( w1, q );
71 dReal y = dCalcVectorDot3( w2, q );
72 return -dAtan2( y, x );
73 }
74
75
76 dxJointHinge2::dxJointHinge2( dxWorld *w ) :
77 dxJoint( w )
78 {
79 dSetZero( anchor1, 4 );
80 dSetZero( anchor2, 4 );
81 dSetZero( axis1, 4 );
82 axis1[0] = 1;
83 dSetZero( axis2, 4 );
84 axis2[1] = 1;
85 c0 = 0;
86 s0 = 0;
87
88 dSetZero( v1, 4 );
89 v1[0] = 1;
90 dSetZero( v2, 4 );
91 v2[1] = 1;
92
93 limot1.init( world );
94 limot2.init( world );
95
96 susp_erp = world->global_erp;
97 susp_cfm = world->global_cfm;
98
99 flags |= dJOINT_TWOBODIES;
100 }
101
102
103 void
104 dxJointHinge2::getSureMaxInfo( SureMaxInfo* info )
105 {
106 info->max_m = 6;
107 }
108
109
110 void
111 dxJointHinge2::getInfo1( dxJoint::Info1 *info )
112 {
113 info->m = 4;
114 info->nub = 4;
115
116 // see if we're powered or at a joint limit for axis 1
117 limot1.limit = 0;
118 if (( limot1.lostop >= -M_PI || limot1.histop <= M_PI ) &&
119 limot1.lostop <= limot1.histop )
120 {
121 dReal angle = measureAngle1();
122 limot1.testRotationalLimit( angle );
123 }
124 if ( limot1.limit || limot1.fmax > 0 ) info->m++;
125
126 // see if we're powering axis 2 (we currently never limit this axis)
127 limot2.limit = 0;
128 if ( limot2.fmax > 0 ) info->m++;
129 }
130
131
132 ////////////////////////////////////////////////////////////////////////////////
133 /// Function that computes ax1,ax2 = axis 1 and 2 in global coordinates (they are
134 /// relative to body 1 and 2 initially) and then computes the constrained
135 /// rotational axis as the cross product of ax1 and ax2.
136 /// the sin and cos of the angle between axis 1 and 2 is computed, this comes
137 /// from dot and cross product rules.
138 ///
139 /// @param ax1 Will contain the joint axis1 in world frame
140 /// @param ax2 Will contain the joint axis2 in world frame
141 /// @param axis Will contain the cross product of ax1 x ax2
142 /// @param sin_angle
143 /// @param cos_angle
144 ////////////////////////////////////////////////////////////////////////////////
145 void
146 dxJointHinge2::getAxisInfo(dVector3 ax1, dVector3 ax2, dVector3 axCross,
147 dReal &sin_angle, dReal &cos_angle) const
148 {
149 dMultiply0_331 (ax1, node[0].body->posr.R, axis1);
150 dMultiply0_331 (ax2, node[1].body->posr.R, axis2);
151 dCalcVectorCross3(axCross,ax1,ax2);
152 sin_angle = dSqrt (axCross[0]*axCross[0] + axCross[1]*axCross[1] + axCross[2]*axCross[2]);
153 cos_angle = dCalcVectorDot3 (ax1,ax2);
154 }
155
156
157 void
158 dxJointHinge2::getInfo2( dReal worldFPS, dReal worldERP,
159 int rowskip, dReal *J1, dReal *J2,
160 int pairskip, dReal *pairRhsCfm, dReal *pairLoHi,
161 int *findex )
162 {
163 // get information we need to set the hinge row
164 dReal s, c;
165 dVector3 q;
166
167 dVector3 ax1, ax2;
168 getAxisInfo( ax1, ax2, q, s, c );
169 dNormalize3( q ); // @@@ quicker: divide q by s ?
170
171 // set the three ball-and-socket rows (aligned to the suspension axis ax1)
172 setBall2( this, worldFPS, worldERP, rowskip, J1, J2, pairskip, pairRhsCfm, anchor1, anchor2, ax1, susp_erp );
173 // set parameter for the suspension
174 pairRhsCfm[GI2_CFM] = susp_cfm;
175
176 // set the hinge row
177 int currRowSkip = 3 * rowskip;
178 dCopyVector3(J1 + currRowSkip + GI2__JA_MIN, q);
179 if ( node[1].body ) {
180 dCopyNegatedVector3(J2 + currRowSkip + GI2__JA_MIN, q);
181 }
182
183 // compute the right hand side for the constrained rotational DOF.
184 // axis 1 and axis 2 are separated by an angle `theta'. the desired
185 // separation angle is theta0. sin(theta0) and cos(theta0) are recorded
186 // in the joint structure. the correcting angular velocity is:
187 // |angular_velocity| = angle/time = erp*(theta0-theta) / stepsize
188 // = (erp*fps) * (theta0-theta)
189 // (theta0-theta) can be computed using the following small-angle-difference
190 // approximation:
191 // theta0-theta ~= tan(theta0-theta)
192 // = sin(theta0-theta)/cos(theta0-theta)
193 // = (c*s0 - s*c0) / (c*c0 + s*s0)
194 // = c*s0 - s*c0 assuming c*c0 + s*s0 ~= 1
195 // where c = cos(theta), s = sin(theta)
196 // c0 = cos(theta0), s0 = sin(theta0)
197
198 dReal k = worldFPS * worldERP;
199
200 int currPairSkip = 3 * pairskip;
201 pairRhsCfm[currPairSkip + GI2_RHS] = k * ( c0 * s - this->s0 * c );
202
203 currRowSkip += rowskip; currPairSkip += pairskip;
204 // if the axis1 hinge is powered, or has joint limits, add in more stuff
205 if (limot1.addLimot( this, worldFPS, J1 + currRowSkip, J2 + currRowSkip, pairRhsCfm + currPairSkip, pairLoHi + currPairSkip, ax1, 1 )) {
206 currRowSkip += rowskip; currPairSkip += pairskip;
207 }
208
209 // if the axis2 hinge is powered, add in more stuff
210 limot2.addLimot( this, worldFPS, J1 + currRowSkip, J2 + currRowSkip, pairRhsCfm + currPairSkip, pairLoHi + currPairSkip, ax2, 1 );
211 }
212
213
214 // compute vectors v1 and v2 (embedded in body1), used to measure angle
215 // between body 1 and body 2
216
217 void
218 dxJointHinge2::makeV1andV2()
219 {
220 if ( node[0].body )
221 {
222 // get axis 1 and 2 in global coords
223 dVector3 ax1, ax2, v;
224 dMultiply0_331( ax1, node[0].body->posr.R, axis1 );
225 dMultiply0_331( ax2, node[1].body->posr.R, axis2 );
226
227 // modify axis 2 so it's perpendicular to axis 1
228 dReal k = dCalcVectorDot3( ax1, ax2 );
229 dAddVectorScaledVector3(ax2, ax2, ax1, -k);
230
231 if (dxSafeNormalize3( ax2 )) {
232 // make v1 = modified axis2, v2 = axis1 x (modified axis2)
233 dCalcVectorCross3( v, ax1, ax2 );
234 dMultiply1_331( v1, node[0].body->posr.R, ax2 );
235 dMultiply1_331( v2, node[0].body->posr.R, v );
236 }
237 else {
238 dUASSERT(false, "Hinge2 axes must be chosen to be linearly independent");
239 }
240 }
241 }
242
243 // same as above, but for the second axis
244
245 void
246 dxJointHinge2::makeW1andW2()
247 {
248 if ( node[1].body )
249 {
250 // get axis 1 and 2 in global coords
251 dVector3 ax1, ax2, w;
252 dMultiply0_331( ax1, node[0].body->posr.R, axis1 );
253 dMultiply0_331( ax2, node[1].body->posr.R, axis2 );
254
255 // modify axis 1 so it's perpendicular to axis 2
256 dReal k = dCalcVectorDot3( ax2, ax1 );
257 dAddVectorScaledVector3(ax1, ax1, ax2, -k);
258
259 if (dxSafeNormalize3( ax1 )) {
260 // make w1 = modified axis1, w2 = axis2 x (modified axis1)
261 dCalcVectorCross3( w, ax2, ax1 );
262 dMultiply1_331( w1, node[1].body->posr.R, ax1 );
263 dMultiply1_331( w2, node[1].body->posr.R, w );
264 }
265 else {
266 dUASSERT(false, "Hinge2 axes must be chosen to be linearly independent");
267 }
268 }
269 }
270
271
272 /*ODE_API */
273 void dJointSetHinge2Anchor( dJointID j, dReal x, dReal y, dReal z )
274 {
275 dxJointHinge2* joint = ( dxJointHinge2* )j;
276 dUASSERT( joint, "bad joint argument" );
277 checktype( joint, Hinge2 );
278
279 setAnchors( joint, x, y, z, joint->anchor1, joint->anchor2 );
280
281 joint->makeV1andV2();
282 joint->makeW1andW2();
283 }
284
285
286 /*ODE_API */
287 void dJointSetHinge2Axes (dJointID j, const dReal *axis1/*=[dSA__MAX],=NULL*/, const dReal *axis2/*=[dSA__MAX],=NULL*/)
288 {
289 dxJointHinge2* joint = ( dxJointHinge2* )j;
290 dUASSERT( joint, "bad joint argument" );
291 checktype( joint, Hinge2 );
292
293 dAASSERT(axis1 != NULL || axis2 != NULL);
294 dAASSERT(joint->node[0].body != NULL || axis1 == NULL);
295 dAASSERT(joint->node[1].body != NULL || axis2 == NULL);
296
297 if ( axis1 != NULL )
298 {
299 setAxes(joint, axis1[dSA_X], axis1[dSA_Y], axis1[dSA_Z], joint->axis1, NULL);
300 }
301
302 if ( axis2 != NULL )
303 {
304 setAxes(joint, axis2[dSA_X], axis2[dSA_Y], axis2[dSA_Z], NULL, joint->axis2);
305 }
306
307 // compute the sin and cos of the angle between axis 1 and axis 2
308 dVector3 ax1, ax2, ax;
309 joint->getAxisInfo( ax1, ax2, ax, joint->s0, joint->c0 );
310
311 joint->makeV1andV2();
312 joint->makeW1andW2();
313 }
314
315
316 /*ODE_API_DEPRECATED ODE_API */
317 void dJointSetHinge2Axis1( dJointID j, dReal x, dReal y, dReal z )
318 {
319 dVector3 axis1;
320 axis1[dSA_X] = x; axis1[dSA_Y] = y; axis1[dSA_Z] = z;
321 dJointSetHinge2Axes(j, axis1, NULL);
322 }
323
324 /*ODE_API_DEPRECATED ODE_API */
325 void dJointSetHinge2Axis2( dJointID j, dReal x, dReal y, dReal z )
326 {
327 dVector3 axis2;
328 axis2[dSA_X] = x; axis2[dSA_Y] = y; axis2[dSA_Z] = z;
329 dJointSetHinge2Axes(j, NULL, axis2);
330 }
331
332
333 void dJointSetHinge2Param( dJointID j, int parameter, dReal value )
334 {
335 dxJointHinge2* joint = ( dxJointHinge2* )j;
336 dUASSERT( joint, "bad joint argument" );
337 checktype( joint, Hinge2 );
338 if (( parameter & 0xff00 ) == 0x100 )
339 {
340 joint->limot2.set( parameter & 0xff, value );
341 }
342 else
343 {
344 if ( parameter == dParamSuspensionERP ) joint->susp_erp = value;
345 else if ( parameter == dParamSuspensionCFM ) joint->susp_cfm = value;
346 else joint->limot1.set( parameter, value );
347 }
348 }
349
350
351 void dJointGetHinge2Anchor( dJointID j, dVector3 result )
352 {
353 dxJointHinge2* joint = ( dxJointHinge2* )j;
354 dUASSERT( joint, "bad joint argument" );
355 dUASSERT( result, "bad result argument" );
356 checktype( joint, Hinge2 );
357 if ( joint->flags & dJOINT_REVERSE )
358 getAnchor2( joint, result, joint->anchor2 );
359 else
360 getAnchor( joint, result, joint->anchor1 );
361 }
362
363
364 void dJointGetHinge2Anchor2( dJointID j, dVector3 result )
365 {
366 dxJointHinge2* joint = ( dxJointHinge2* )j;
367 dUASSERT( joint, "bad joint argument" );
368 dUASSERT( result, "bad result argument" );
369 checktype( joint, Hinge2 );
370 if ( joint->flags & dJOINT_REVERSE )
371 getAnchor( joint, result, joint->anchor1 );
372 else
373 getAnchor2( joint, result, joint->anchor2 );
374 }
375
376
377 void dJointGetHinge2Axis1( dJointID j, dVector3 result )
378 {
379 dxJointHinge2* joint = ( dxJointHinge2* )j;
380 dUASSERT( joint, "bad joint argument" );
381 dUASSERT( result, "bad result argument" );
382 checktype( joint, Hinge2 );
383 if ( joint->node[0].body )
384 {
385 dMultiply0_331( result, joint->node[0].body->posr.R, joint->axis1 );
386 }
387 else
388 {
389 dZeroVector3(result);
390 dUASSERT( false, "the joint does not have first body attached" );
391 }
392 }
393
394
395 void dJointGetHinge2Axis2( dJointID j, dVector3 result )
396 {
397 dxJointHinge2* joint = ( dxJointHinge2* )j;
398 dUASSERT( joint, "bad joint argument" );
399 dUASSERT( result, "bad result argument" );
400 checktype( joint, Hinge2 );
401 if ( joint->node[1].body )
402 {
403 dMultiply0_331( result, joint->node[1].body->posr.R, joint->axis2 );
404 }
405 else
406 {
407 dZeroVector3(result);
408 dUASSERT( false, "the joint does not have second body attached" );
409 }
410 }
411
412
413 dReal dJointGetHinge2Param( dJointID j, int parameter )
414 {
415 dxJointHinge2* joint = ( dxJointHinge2* )j;
416 dUASSERT( joint, "bad joint argument" );
417 checktype( joint, Hinge2 );
418 if (( parameter & 0xff00 ) == 0x100 )
419 {
420 return joint->limot2.get( parameter & 0xff );
421 }
422 else
423 {
424 if ( parameter == dParamSuspensionERP ) return joint->susp_erp;
425 else if ( parameter == dParamSuspensionCFM ) return joint->susp_cfm;
426 else return joint->limot1.get( parameter );
427 }
428 }
429
430
431 dReal dJointGetHinge2Angle1( dJointID j )
432 {
433 dxJointHinge2* joint = ( dxJointHinge2* )j;
434 dUASSERT( joint, "bad joint argument" );
435 checktype( joint, Hinge2 );
436 return joint->measureAngle1();
437 }
438
439
440 dReal dJointGetHinge2Angle2( dJointID j )
441 {
442 dxJointHinge2* joint = ( dxJointHinge2* )j;
443 dUASSERT( joint, "bad joint argument" );
444 checktype( joint, Hinge2 );
445 return joint->measureAngle2();
446 }
447
448
449
450 dReal dJointGetHinge2Angle1Rate( dJointID j )
451 {
452 dxJointHinge2* joint = ( dxJointHinge2* )j;
453 dUASSERT( joint, "bad joint argument" );
454 checktype( joint, Hinge2 );
455 if ( joint->node[0].body )
456 {
457 dVector3 axis;
458 dMultiply0_331( axis, joint->node[0].body->posr.R, joint->axis1 );
459 dReal rate = dCalcVectorDot3( axis, joint->node[0].body->avel );
460 if ( joint->node[1].body )
461 rate -= dCalcVectorDot3( axis, joint->node[1].body->avel );
462 return rate;
463 }
464 else return 0;
465 }
466
467
468 dReal dJointGetHinge2Angle2Rate( dJointID j )
469 {
470 dxJointHinge2* joint = ( dxJointHinge2* )j;
471 dUASSERT( joint, "bad joint argument" );
472 checktype( joint, Hinge2 );
473 if ( joint->node[0].body && joint->node[1].body )
474 {
475 dVector3 axis;
476 dMultiply0_331( axis, joint->node[1].body->posr.R, joint->axis2 );
477 dReal rate = dCalcVectorDot3( axis, joint->node[0].body->avel );
478 if ( joint->node[1].body )
479 rate -= dCalcVectorDot3( axis, joint->node[1].body->avel );
480 return rate;
481 }
482 else return 0;
483 }
484
485
486 void dJointAddHinge2Torques( dJointID j, dReal torque1, dReal torque2 )
487 {
488 dxJointHinge2* joint = ( dxJointHinge2* )j;
489 dVector3 axis1, axis2;
490 dUASSERT( joint, "bad joint argument" );
491 checktype( joint, Hinge2 );
492
493 if ( joint->node[0].body && joint->node[1].body )
494 {
495 dMultiply0_331( axis1, joint->node[0].body->posr.R, joint->axis1 );
496 dMultiply0_331( axis2, joint->node[1].body->posr.R, joint->axis2 );
497 axis1[0] = axis1[0] * torque1 + axis2[0] * torque2;
498 axis1[1] = axis1[1] * torque1 + axis2[1] * torque2;
499 axis1[2] = axis1[2] * torque1 + axis2[2] * torque2;
500 dBodyAddTorque( joint->node[0].body, axis1[0], axis1[1], axis1[2] );
501 dBodyAddTorque( joint->node[1].body, -axis1[0], -axis1[1], -axis1[2] );
502 }
503 }
504
505
506 dJointType
507 dxJointHinge2::type() const
508 {
509 return dJointTypeHinge2;
510 }
511
512
513 size_t
514 dxJointHinge2::size() const
515 {
516 return sizeof( *this );
517 }
518
519
520 void
521 dxJointHinge2::setRelativeValues()
522 {
523 dVector3 anchor;
524 dJointGetHinge2Anchor(this, anchor);
525 setAnchors( this, anchor[0], anchor[1], anchor[2], anchor1, anchor2 );
526
527 dVector3 axis;
528
529 if ( node[0].body )
530 {
531 dJointGetHinge2Axis1(this, axis);
532 setAxes( this, axis[0],axis[1],axis[2], axis1, NULL );
533 }
534
535 if ( node[0].body )
536 {
537 dJointGetHinge2Axis2(this, axis);
538 setAxes( this, axis[0],axis[1],axis[2], NULL, axis2 );
539 }
540
541 dVector3 ax1, ax2;
542 getAxisInfo( ax1, ax2, axis, s0, c0 );
543
544 makeV1andV2();
545 makeW1andW2();
546 }