Debian Codegraph @

source/unix/odesrc/joints/transmission.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 "transmission.h"
27 #include "joint_internal.h"
28
29 namespace {
30 static inline dReal clamp(dReal x, dReal minX, dReal maxX)
31 {
32 return x < minX ? minX : (x > maxX ? maxX : x);
33 }
34 }
35
36 /*
37 * Transmission joint
38 */
39
40 dxJointTransmission::dxJointTransmission(dxWorld* w) :
41 dxJoint(w)
42 {
43 int i;
44
45 flags |= dJOINT_TWOBODIES;
46 mode = dTransmissionParallelAxes;
47
48 cfm = world->global_cfm;
49 erp = world->global_erp;
50
51 for (i = 0 ; i < 2 ; i += 1) {
52 dSetZero( anchors[i], 4 );
53 dSetZero( axes[i], 4 );
54 axes[i][0] = 1;
55
56 radii[i] = 0;
57 }
58
59 backlash = 0;
60 ratio = 1;
61 update = 1;
62 }
63
64 void
65 dxJointTransmission::getSureMaxInfo( SureMaxInfo* info )
66 {
67 info->max_m = 1;
68 }
69
70 void
71 dxJointTransmission::getInfo1( dxJoint::Info1* info )
72 {
73 // If there's backlash in the gears then constraint must be
74 // unilateral, that is the driving gear can only push the driven
75 // gear in one direction. In order to push it in the other it
76 // first needs to traverse the backlash gap.
77
78 info->m = 1;
79 info->nub = backlash > 0 ? 0 : 1;
80 }
81
82 void
83 dxJointTransmission::getInfo2( dReal worldFPS, dReal /*worldERP*/,
84 int rowskip, dReal *J1, dReal *J2,
85 int pairskip, dReal *pairRhsCfm, dReal *pairLoHi,
86 int *findex )
87 {
88 dVector3 a[2], n[2], l[2], r[2], c[2], s, t, O, d, z, u, v;
89 dReal theta, delta, nn, na_0, na_1, cosphi, sinphi, m;
90 const dReal *p[2], *omega[2];
91 int i;
92
93 // Transform all needed quantities to the global frame.
94
95 for (i = 0 ; i < 2 ; i += 1) {
96 dBodyGetRelPointPos(node[i].body,
97 anchors[i][0], anchors[i][1], anchors[i][2],
98 a[i]);
99
100 dBodyVectorToWorld(node[i].body, axes[i][0], axes[i][1], axes[i][2],
101 n[i]);
102
103 p[i] = dBodyGetPosition(node[i].body);
104 omega[i] = dBodyGetAngularVel(node[i].body);
105 }
106
107 if (update) {
108 // Make sure both gear reference frames end up with the same
109 // handedness.
110
111 if (dCalcVectorDot3(n[0], n[1]) < 0) {
112 dNegateVector3(axes[0]);
113 dNegateVector3(n[0]);
114 }
115 }
116
117 // Calculate the mesh geometry based on the current mode.
118
119 switch (mode) {
120 case dTransmissionParallelAxes:
121 // Simply calculate the contact point as the point on the
122 // baseline that will yield the correct ratio.
123
124 dIASSERT (ratio > 0);
125
126 dSubtractVectors3(d, a[1], a[0]);
127 dAddVectorScaledVector3(c[0], a[0], d, ratio / (1 + ratio));
128 dCopyVector3(c[1], c[0]);
129
130 dNormalize3(d);
131
132 for (i = 0 ; i < 2 ; i += 1) {
133 dCalcVectorCross3(l[i], d, n[i]);
134 }
135
136 break;
137 case dTransmissionIntersectingAxes:
138 // Calculate the line of intersection between the planes of the
139 // gears.
140
141 dCalcVectorCross3(l[0], n[0], n[1]);
142 dCopyVector3(l[1], l[0]);
143
144 nn = dCalcVectorDot3(n[0], n[1]);
145 dIASSERT(fabs(nn) != 1);
146
147 na_0 = dCalcVectorDot3(n[0], a[0]);
148 na_1 = dCalcVectorDot3(n[1], a[1]);
149
150 dAddScaledVectors3(O, n[0], n[1],
151 (na_0 - na_1 * nn) / (1 - nn * nn),
152 (na_1 - na_0 * nn) / (1 - nn * nn));
153
154 // Find the contact point as:
155 //
156 // c = ((r_a - O) . l) l + O
157 //
158 // where r_a the anchor point of either gear and l, O the tangent
159 // line direction and origin.
160
161 for (i = 0 ; i < 2 ; i += 1) {
162 dSubtractVectors3(d, a[i], O);
163 m = dCalcVectorDot3(d, l[i]);
164 dAddVectorScaledVector3(c[i], O, l[i], m);
165 }
166
167 break;
168 case dTransmissionChainDrive:
169 dSubtractVectors3(d, a[0], a[1]);
170 m = dCalcVectorLength3(d);
171
172 dIASSERT(m > 0);
173
174 // Caclulate the angle of the contact point relative to the
175 // baseline.
176
177 cosphi = clamp((radii[1] - radii[0]) / m, REAL(-1.0), REAL(1.0)); // Force into range to fix possible computation errors
178 sinphi = dSqrt (REAL(1.0) - cosphi * cosphi);
179
180 dNormalize3(d);
181
182 for (i = 0 ; i < 2 ; i += 1) {
183 // Calculate the contact radius in the local reference
184 // frame of the chain. This has axis x pointing along the
185 // baseline, axis y pointing along the sprocket axis and
186 // the remaining axis normal to both.
187
188 u[0] = radii[i] * cosphi;
189 u[1] = 0;
190 u[2] = radii[i] * sinphi;
191
192 // Transform the contact radius into the global frame.
193
194 dCalcVectorCross3(z, d, n[i]);
195
196 v[0] = dCalcVectorDot3(d, u);
197 v[1] = dCalcVectorDot3(n[i], u);
198 v[2] = dCalcVectorDot3(z, u);
199
200 // Finally calculate contact points and l.
201
202 dAddVectors3(c[i], a[i], v);
203 dCalcVectorCross3(l[i], v, n[i]);
204 dNormalize3(l[i]);
205
206 // printf ("%d: %f, %f, %f\n",
207 // i, l[i][0], l[i][1], l[i][2]);
208 }
209
210 break;
211 }
212
213 if (update) {
214 // We need to calculate an initial reference frame for each
215 // wheel which we can measure the current phase against. This
216 // frame will have the initial contact radius as the x axis,
217 // the wheel axis as the z axis and their cross product as the
218 // y axis.
219
220 for (i = 0 ; i < 2 ; i += 1) {
221 dSubtractVectors3 (r[i], c[i], a[i]);
222 radii[i] = dCalcVectorLength3(r[i]);
223 dIASSERT(radii[i] > 0);
224
225 dBodyVectorFromWorld(node[i].body, r[i][0], r[i][1], r[i][2],
226 reference[i]);
227 dNormalize3(reference[i]);
228 dCopyVector3(reference[i] + 8, axes[i]);
229 dCalcVectorCross3(reference[i] + 4, reference[i] + 8, reference[i]);
230
231 // printf ("%f\n", dDOT(r[i], n[i]));
232 // printf ("(%f, %f, %f,\n %f, %f, %f,\n %f, %f, %f)\n",
233 // reference[i][0],reference[i][1],reference[i][2],
234 // reference[i][4],reference[i][5],reference[i][6],
235 // reference[i][8],reference[i][9],reference[i][10]);
236
237 phase[i] = 0;
238 }
239
240 ratio = radii[0] / radii[1];
241 update = 0;
242 }
243
244 for (i = 0 ; i < 2 ; i += 1) {
245 dReal phase_hat;
246
247 dSubtractVectors3 (r[i], c[i], a[i]);
248
249 // Transform the (global) contact radius into the gear's
250 // reference frame.
251
252 dBodyVectorFromWorld (node[i].body, r[i][0], r[i][1], r[i][2], s);
253 dMultiply0_331(t, reference[i], s);
254
255 // Now simply calculate its angle on the plane relative to the
256 // x-axis which is the initial contact radius. This will be
257 // an angle between -pi and pi that is coterminal with the
258 // actual phase of the wheel. To find the real phase we
259 // estimate it by adding omega * dt to the old phase and then
260 // find the closest angle to that, that is coterminal to
261 // theta.
262
263 theta = atan2(t[1], t[0]);
264 phase_hat = phase[i] + dCalcVectorDot3(omega[i], n[i]) / worldFPS;
265
266 if (phase_hat > M_PI_2) {
267 if (theta < 0) {
268 theta += (dReal)(2 * M_PI);
269 }
270
271 theta += (dReal)(floor(phase_hat / (2 * M_PI)) * (2 * M_PI));
272 } else if (phase_hat < -M_PI_2) {
273 if (theta > 0) {
274 theta -= (dReal)(2 * M_PI);
275 }
276
277 theta += (dReal)(ceil(phase_hat / (2 * M_PI)) * (2 * M_PI));
278 }
279
280 if (phase_hat - theta > M_PI) {
281 phase[i] = theta + (dReal)(2 * M_PI);
282 } else if (phase_hat - theta < -M_PI) {
283 phase[i] = theta - (dReal)(2 * M_PI);
284 } else {
285 phase[i] = theta;
286 }
287
288 dIASSERT(fabs(phase_hat - phase[i]) < M_PI);
289 }
290
291 // Calculate the phase error. Depending on the mode the condition
292 // is that the distances traveled by each contact point must be
293 // either equal (chain and sprockets) or opposite (gears).
294
295 if (mode == dTransmissionChainDrive) {
296 delta = (dCalcVectorLength3(r[0]) * phase[0] -
297 dCalcVectorLength3(r[1]) * phase[1]);
298 } else {
299 delta = (dCalcVectorLength3(r[0]) * phase[0] +
300 dCalcVectorLength3(r[1]) * phase[1]);
301 }
302
303 // When in chain mode a torque reversal, signified by the change
304 // in sign of the wheel phase difference, has the added effect of
305 // switching the active chain branch. We must therefore reflect
306 // the contact points and tangents across the baseline.
307
308 if (mode == dTransmissionChainDrive && delta < 0) {
309 dVector3 d;
310
311 dSubtractVectors3(d, a[0], a[1]);
312
313 for (i = 0 ; i < 2 ; i += 1) {
314 dVector3 nn;
315 dReal a;
316
317 dCalcVectorCross3(nn, n[i], d);
318 a = dCalcVectorDot3(nn, nn);
319 dIASSERT(a > 0);
320
321 dAddScaledVectors3(c[i], c[i], nn,
322 1, -2 * dCalcVectorDot3(c[i], nn) / a);
323 dAddScaledVectors3(l[i], l[i], nn,
324 -1, 2 * dCalcVectorDot3(l[i], nn) / a);
325 }
326 }
327
328 // Do not add the constraint if there's backlash and we're in the
329 // backlash gap.
330
331 if (backlash == 0 || fabs(delta) > backlash) {
332 // The constraint is satisfied if the absolute velocity of the
333 // contact point projected onto the tangent of the wheels is equal
334 // for both gears. This velocity can be calculated as:
335 //
336 // u = v + omega x r_c
337 //
338 // The constraint therefore becomes:
339 // (v_1 + omega_1 x r_c1) . l = (v_2 + omega_2 x r_c2) . l <=>
340 // (v_1 . l + (r_c1 x l) . omega_1 = v_2 . l + (r_c2 x l) . omega_2
341
342 for (i = 0 ; i < 2 ; i += 1) {
343 dSubtractVectors3 (r[i], c[i], p[i]);
344 }
345
346 dCopyVector3(J1 + GI2__JL_MIN, l[0]);
347 dCalcVectorCross3(J1 + GI2__JA_MIN, r[0], l[0]);
348
349 dCopyNegatedVector3(J2 + GI2__JL_MIN, l[1]);
350 dCalcVectorCross3(J2 + GI2__JA_MIN, l[1], r[1]);
351
352 if (delta > 0) {
353 if (backlash > 0) {
354 pairLoHi[GI2_LO] = -dInfinity;
355 pairLoHi[GI2_HI] = 0;
356 }
357
358 pairRhsCfm[GI2_RHS] = -worldFPS * erp * (delta - backlash);
359 } else {
360 if (backlash > 0) {
361 pairLoHi[GI2_LO] = 0;
362 pairLoHi[GI2_HI] = dInfinity;
363 }
364
365 pairRhsCfm[GI2_RHS] = -worldFPS * erp * (delta + backlash);
366 }
367 }
368
369 pairRhsCfm[GI2_CFM] = cfm;
370
371 // printf ("%f, %f, %f, %f, %f\n", delta, phase[0], phase[1], -phase[1] / phase[0], ratio);
372
373 // Cache the contact point (in world coordinates) to avoid
374 // recalculation if requested by the user.
375
376 dCopyVector3(contacts[0], c[0]);
377 dCopyVector3(contacts[1], c[1]);
378 }
379
380 void dJointSetTransmissionAxis( dJointID j, dReal x, dReal y, dReal z )
381 {
382 dxJointTransmission* joint = static_cast<dxJointTransmission*>(j);
383 int i;
384
385 dUASSERT( joint, "bad joint argument" );
386 dUASSERT(joint->mode == dTransmissionParallelAxes ||
387 joint->mode == dTransmissionChainDrive ,
388 "axes must be set individualy in current mode" );
389
390 for (i = 0 ; i < 2 ; i += 1) {
391 if (joint->node[i].body) {
392 dBodyVectorFromWorld(joint->node[i].body, x, y, z, joint->axes[i]);
393 dNormalize3(joint->axes[i]);
394 }
395 }
396
397 joint->update = 1;
398 }
399
400 void dJointSetTransmissionAxis1( dJointID j, dReal x, dReal y, dReal z )
401 {
402 dxJointTransmission* joint = static_cast<dxJointTransmission*>(j);
403 dUASSERT( joint, "bad joint argument" );
404 dUASSERT(joint->mode == dTransmissionIntersectingAxes,
405 "can't set individual axes in current mode" );
406
407 if (joint->node[0].body) {
408 dBodyVectorFromWorld(joint->node[0].body, x, y, z, joint->axes[0]);
409 dNormalize3(joint->axes[0]);
410 }
411
412 joint->update = 1;
413 }
414
415 void dJointSetTransmissionAxis2( dJointID j, dReal x, dReal y, dReal z )
416 {
417 dxJointTransmission* joint = static_cast<dxJointTransmission*>(j);
418 dUASSERT( joint, "bad joint argument" );
419 dUASSERT(joint->mode == dTransmissionIntersectingAxes,
420 "can't set individual axes in current mode" );
421
422 if (joint->node[1].body) {
423 dBodyVectorFromWorld(joint->node[1].body, x, y, z, joint->axes[1]);
424 dNormalize3(joint->axes[1]);
425 }
426
427 joint->update = 1;
428 }
429
430 void dJointSetTransmissionAnchor1( dJointID j, dReal x, dReal y, dReal z )
431 {
432 dxJointTransmission* joint = static_cast<dxJointTransmission*>(j);
433 dUASSERT( joint, "bad joint argument" );
434
435 if (joint->node[0].body) {
436 dBodyGetPosRelPoint(joint->node[0].body, x, y, z, joint->anchors[0]);
437 }
438
439 joint->update = 1;
440 }
441
442 void dJointSetTransmissionAnchor2( dJointID j, dReal x, dReal y, dReal z )
443 {
444 dxJointTransmission* joint = static_cast<dxJointTransmission*>(j);
445 dUASSERT( joint, "bad joint argument" );
446
447 if (joint->node[1].body) {
448 dBodyGetPosRelPoint(joint->node[1].body, x, y, z, joint->anchors[1]);
449 }
450
451 joint->update = 1;
452 }
453
454 void dJointGetTransmissionContactPoint1( dJointID j, dVector3 result )
455 {
456 dxJointTransmission* joint = static_cast<dxJointTransmission*>(j);
457 dUASSERT( joint, "bad joint argument" );
458 dUASSERT( result, "bad result argument" );
459
460 dCopyVector3(result, joint->contacts[0]);
461 }
462
463 void dJointGetTransmissionContactPoint2( dJointID j, dVector3 result )
464 {
465 dxJointTransmission* joint = static_cast<dxJointTransmission*>(j);
466 dUASSERT( joint, "bad joint argument" );
467 dUASSERT( result, "bad result argument" );
468
469 dCopyVector3(result, joint->contacts[1]);
470 }
471
472 void dJointGetTransmissionAxis( dJointID j, dVector3 result )
473 {
474 dxJointTransmission* joint = static_cast<dxJointTransmission*>(j);
475 dUASSERT( joint, "bad joint argument" );
476 dUASSERT( result, "bad result argument" );
477 dUASSERT(joint->mode == dTransmissionParallelAxes,
478 "axes must be queried individualy in current mode" );
479
480 if (joint->node[0].body) {
481 dBodyVectorToWorld(joint->node[0].body,
482 joint->axes[0][0],
483 joint->axes[0][1],
484 joint->axes[0][2],
485 result);
486 }
487 }
488
489 void dJointGetTransmissionAxis1( dJointID j, dVector3 result )
490 {
491 dxJointTransmission* joint = static_cast<dxJointTransmission*>(j);
492 dUASSERT( joint, "bad joint argument" );
493 dUASSERT( result, "bad result argument" );
494
495 if (joint->node[0].body) {
496 dBodyVectorToWorld(joint->node[0].body,
497 joint->axes[0][0],
498 joint->axes[0][1],
499 joint->axes[0][2],
500 result);
501 }
502 }
503
504 void dJointGetTransmissionAxis2( dJointID j, dVector3 result )
505 {
506 dxJointTransmission* joint = static_cast<dxJointTransmission*>(j);
507 dUASSERT( joint, "bad joint argument" );
508 dUASSERT( result, "bad result argument" );
509
510 if (joint->node[1].body) {
511 dBodyVectorToWorld(joint->node[1].body,
512 joint->axes[1][0],
513 joint->axes[1][1],
514 joint->axes[1][2],
515 result);
516 }
517 }
518
519 void dJointGetTransmissionAnchor1( dJointID j, dVector3 result )
520 {
521 dxJointTransmission* joint = static_cast<dxJointTransmission*>(j);
522 dUASSERT( joint, "bad joint argument" );
523 dUASSERT( result, "bad result argument" );
524
525 if (joint->node[0].body) {
526 dBodyGetRelPointPos(joint->node[0].body,
527 joint->anchors[0][0],
528 joint->anchors[0][1],
529 joint->anchors[0][2],
530 result);
531 }
532 }
533
534 void dJointGetTransmissionAnchor2( dJointID j, dVector3 result )
535 {
536 dxJointTransmission* joint = static_cast<dxJointTransmission*>(j);
537 dUASSERT( joint, "bad joint argument" );
538 dUASSERT( result, "bad result argument" );
539
540 if (joint->node[1].body) {
541 dBodyGetRelPointPos(joint->node[1].body,
542 joint->anchors[1][0],
543 joint->anchors[1][1],
544 joint->anchors[1][2],
545 result);
546 }
547 }
548
549 void dJointSetTransmissionParam( dJointID j, int parameter, dReal value )
550 {
551 dxJointTransmission* joint = static_cast<dxJointTransmission*>(j);
552 dUASSERT( joint, "bad joint argument" );
553
554 switch ( parameter ) {
555 case dParamCFM:
556 joint->cfm = value;
557 break;
558 case dParamERP:
559 joint->erp = value;
560 break;
561 }
562 }
563
564
565 dReal dJointGetTransmissionParam( dJointID j, int parameter )
566 {
567 dxJointTransmission* joint = static_cast<dxJointTransmission*>(j);
568 dUASSERT( joint, "bad joint argument" );
569
570 switch ( parameter ) {
571 case dParamCFM:
572 return joint->cfm;
573 case dParamERP:
574 return joint->erp;
575 default:
576 return 0;
577 }
578 }
579
580 void dJointSetTransmissionMode( dJointID j, int mode )
581 {
582 dxJointTransmission* joint = static_cast<dxJointTransmission*>(j);
583 dUASSERT( joint, "bad joint argument" );
584 dUASSERT( mode == dTransmissionParallelAxes ||
585 mode == dTransmissionIntersectingAxes ||
586 mode == dTransmissionChainDrive, "invalid joint mode" );
587
588 joint->mode = mode;
589 }
590
591
592 int dJointGetTransmissionMode( dJointID j )
593 {
594 dxJointTransmission* joint = static_cast<dxJointTransmission*>(j);
595 dUASSERT( joint, "bad joint argument" );
596
597 return joint->mode;
598 }
599
600 void dJointSetTransmissionRatio( dJointID j, dReal ratio )
601 {
602 dxJointTransmission* joint = static_cast<dxJointTransmission*>(j);
603 dUASSERT( joint, "bad joint argument" );
604 dUASSERT( joint->mode == dTransmissionParallelAxes,
605 "can't set ratio explicitly in current mode" );
606 dUASSERT( ratio > 0, "ratio must be positive" );
607
608 joint->ratio = ratio;
609 }
610
611
612 dReal dJointGetTransmissionRatio( dJointID j )
613 {
614 dxJointTransmission* joint = static_cast<dxJointTransmission*>(j);
615 dUASSERT( joint, "bad joint argument" );
616
617 return joint->ratio;
618 }
619
620 dReal dJointGetTransmissionAngle1( dJointID j )
621 {
622 dxJointTransmission* joint = static_cast<dxJointTransmission*>(j);
623 dUASSERT( joint, "bad joint argument" );
624
625 return joint->phase[0];
626 }
627
628 dReal dJointGetTransmissionAngle2( dJointID j )
629 {
630 dxJointTransmission* joint = static_cast<dxJointTransmission*>(j);
631 dUASSERT( joint, "bad joint argument" );
632
633 return joint->phase[1];
634 }
635
636 dReal dJointGetTransmissionRadius1( dJointID j )
637 {
638 dxJointTransmission* joint = static_cast<dxJointTransmission*>(j);
639 dUASSERT( joint, "bad joint argument" );
640
641 return joint->radii[0];
642 }
643
644 dReal dJointGetTransmissionRadius2( dJointID j )
645 {
646 dxJointTransmission* joint = static_cast<dxJointTransmission*>(j);
647 dUASSERT( joint, "bad joint argument" );
648
649 return joint->radii[1];
650 }
651
652 void dJointSetTransmissionRadius1( dJointID j, dReal radius )
653 {
654 dxJointTransmission* joint = static_cast<dxJointTransmission*>(j);
655 dUASSERT( joint, "bad joint argument" );
656 dUASSERT( joint->mode == dTransmissionChainDrive,
657 "can't set wheel radius explicitly in current mode" );
658
659 joint->radii[0] = radius;
660 }
661
662 void dJointSetTransmissionRadius2( dJointID j, dReal radius )
663 {
664 dxJointTransmission* joint = static_cast<dxJointTransmission*>(j);
665 dUASSERT( joint, "bad joint argument" );
666 dUASSERT( joint->mode == dTransmissionChainDrive,
667 "can't set wheel radius explicitly in current mode" );
668
669 joint->radii[1] = radius;
670 }
671
672 dReal dJointGetTransmissionBacklash( dJointID j )
673 {
674 dxJointTransmission* joint = static_cast<dxJointTransmission*>(j);
675 dUASSERT( joint, "bad joint argument" );
676
677 return joint->backlash;
678 }
679
680 void dJointSetTransmissionBacklash( dJointID j, dReal backlash )
681 {
682 dxJointTransmission* joint = static_cast<dxJointTransmission*>(j);
683 dUASSERT( joint, "bad joint argument" );
684
685 joint->backlash = backlash;
686 }
687
688 dJointType
689 dxJointTransmission::type() const
690 {
691 return dJointTypeTransmission;
692 }
693
694 size_t
695 dxJointTransmission::size() const
696 {
697 return sizeof( *this );
698 }