summaryrefslogtreecommitdiff
path: root/libs/ode-0.16.1/ode/demo/demo_transmission.cpp
blob: 50cb820da890fc487475a5a7a93af42be0888c5d (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
/*************************************************************************
 *                                                                       *
 * Open Dynamics Engine, Copyright (C) 2001,2002 Russell L. Smith.       *
 * All rights reserved.  Email: russ@q12.org   Web: www.q12.org          *
 *                                                                       *
 * This library is free software; you can redistribute it and/or         *
 * modify it under the terms of EITHER:                                  *
 *   (1) The GNU Lesser General Public License as published by the Free  *
 *       Software Foundation; either version 2.1 of the License, or (at  *
 *       your option) any later version. The text of the GNU Lesser      *
 *       General Public License is included with this library in the     *
 *       file LICENSE.TXT.                                               *
 *   (2) The BSD-style license that is included with this library in     *
 *       the file LICENSE-BSD.TXT.                                       *
 *                                                                       *
 * This library is distributed in the hope that it will be useful,       *
 * but WITHOUT ANY WARRANTY; without even the implied warranty of        *
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the files    *
 * LICENSE.TXT and LICENSE-BSD.TXT for more details.                     *
 *                                                                       *
 *************************************************************************/

#include <ode/ode.h>
#include <drawstuff/drawstuff.h>
#include "texturepath.h"

#ifdef dDOUBLE
#define dsDrawCylinder dsDrawCylinderD
#define dsDrawLine dsDrawLineD
#define dsDrawSphere dsDrawSphereD
#endif

dReal theta = M_PI / 4;
dReal ratio = 1, speed = 5, rho_1 = 1, rho_2 = 1, backlash = 0.1;
int mode = 0;

dWorldID world;
dSpaceID space;
dBodyID body1, body2;
dGeomID geom1, geom2;
dJointID hinge1, hinge2, transmission;
dJointFeedback feedback;

void setup() {
    dMatrix3 R;
    
    switch (mode) {
    case 0:
	// Parallel axes.

	dBodySetPosition(body1, 1, 0, 1);
	dBodySetPosition(body2, -1, 0, 1);

	dRSetIdentity (R);
	dBodySetRotation (body1, R);
	dBodySetRotation (body2, R);

	dJointSetHingeAnchor(hinge2, -1, 0, 1);
	dJointSetHingeAxis(hinge2, 0, 0, 1);

	dJointSetHingeAnchor(hinge1, 1, 0, 1);
	dJointSetHingeAxis(hinge1, 0, 0, 1);

	dJointSetTransmissionMode(transmission, dTransmissionParallelAxes);
	dJointSetTransmissionRatio(transmission, ratio);
	dJointSetTransmissionAnchor1(transmission, 1, 0, 1);
	dJointSetTransmissionAnchor2(transmission, -1, 0, 1);
	dJointSetTransmissionAxis(transmission, 0, 0, 1);

	break;
    case 1:
	// Intersecting axes.

	dBodySetPosition(body1, 1, 0, 1);
	dBodySetPosition(body2, -1, 0, 2);

	dRSetIdentity (R);
	dBodySetRotation (body1, R);

	dRFromZAxis (R, cos(theta), 0, sin(theta));
	dBodySetRotation (body2, R);

	dJointSetHingeAnchor(hinge2, -1, 0, 2);
	dJointSetHingeAxis(hinge2, cos(theta), 0, sin(theta));

	dJointSetHingeAnchor(hinge1, 1, 0, 1);
	dJointSetHingeAxis(hinge1, 0, 0, 1);

	dJointSetTransmissionMode(transmission, dTransmissionIntersectingAxes);
	dJointSetTransmissionAnchor1(transmission, 1, 0, 1);
	dJointSetTransmissionAnchor2(transmission, -1, 0, 2);
	dJointSetTransmissionAxis1(transmission, 0, 0, -1);
	dJointSetTransmissionAxis2(transmission, cos(theta), 0, sin(theta));

	break;
    case 2:
	// Chain.
	
	dBodySetPosition(body1, 2, 0, 1);
	dBodySetPosition(body2, -2, 0, 1);

	dRSetIdentity (R);
	dBodySetRotation (body1, R);
	dBodySetRotation (body2, R);

	dJointSetHingeAnchor(hinge2, -2, 0, 1);
	dJointSetHingeAxis(hinge2, 0, 0, 1);

	dJointSetHingeAnchor(hinge1, 2, 0, 1);
	dJointSetHingeAxis(hinge1, 0, 0, 1);

	dJointSetTransmissionMode(transmission, dTransmissionChainDrive);
	dJointSetTransmissionAnchor1(transmission, 2, 0, 1);
	dJointSetTransmissionAnchor2(transmission, -2, 0, 1);
	dJointSetTransmissionRadius1(transmission, rho_1);
	dJointSetTransmissionRadius2(transmission, rho_2);
	dJointSetTransmissionAxis(transmission, 0, 0, 1);

	break;
    }

    dJointSetTransmissionBacklash(transmission, backlash);

    dJointSetHingeParam(hinge2, dParamVel, speed);
    dJointSetHingeParam(hinge2, dParamFMax, 50);

    dJointSetHingeParam(hinge1, dParamVel, 0);
    dJointSetHingeParam(hinge1, dParamFMax, 2);

    dBodySetLinearVel(body1, 0, 0, 0);
    dBodySetLinearVel(body2, 0, 0, 0);
    dBodySetAngularVel(body1, 0, 0, 0);
    dBodySetAngularVel(body2, 0, 0, 0);
}

void start()
{
    dMass mass;
    
    world = dWorldCreate();
    dWorldSetGravity (world,0,0,-9.8);

    dWorldSetERP(world, 0.2);

    space = dSimpleSpaceCreate (0);
    
    body1 = dBodyCreate(world);
    body2 = dBodyCreate(world);

    dBodySetFiniteRotationMode(body1, 1);
    dBodySetFiniteRotationMode(body2, 1);

    geom1 = dCreateCylinder(space, 0.2, 0.5);
    dGeomSetBody(geom1, body1);
    dMassSetCylinder(&mass, 100, 3, 0.2, 0.5);
    dBodySetMass(body1, &mass);
    
    geom2 = dCreateCylinder(space, 0.2, 0.5);
    dGeomSetBody(geom2, body2);
    dMassSetCylinder(&mass, 100, 3, 0.2, 0.5);
    dBodySetMass(body2, &mass);

    hinge1 = dJointCreateHinge(world, 0);
    dJointAttach(hinge1, body1, 0);
    
    hinge2 = dJointCreateHinge(world, 0);
    dJointAttach(hinge2, body2, 0);

    transmission = dJointCreateTransmission(world, 0);
    dJointAttach(transmission, body1, body2);
    dJointSetFeedback(transmission, &feedback);

    setup();

    // initial camera position
    static float xyz[3] = {1.15,-2.78,4.1};
    static float hpr[3] = {105,-45.5,0};
    dsSetViewpoint (xyz,hpr);

    fprintf (stderr,
	     "The green wheel is driving the red one. To control it use the following:\n"
	     "   '[' : decrease wheel ratio\n"
	     "   ']' : increase wheel ratio\n"
	     "   ',' : decrease driving wheel speed\n"
	     "   '.' : increase driving wheel speed\n"
	     "   '-' : decrease backlash\n"
	     "   '=' : increase backlash\n"
	     "   '1' : switch to parallel axes gears mode\n"
	     "   '2' : switch to intersecting axes gears mode\n"
	     "   '3' : switch to chain (or belt) mode\n"
);
}

void stop()
{
    dSpaceDestroy(space);

    dWorldDestroy(world);
}

void drawGeom(dGeomID g)
{
    int gclass = dGeomGetClass(g);
    const dReal *pos = dGeomGetPosition(g);
    const dReal *rot = dGeomGetRotation(g);

    switch (gclass) {
        case dCylinderClass:
        {
            dReal length, radius;

	    if (g == geom1) {
		dsSetColorAlpha(1, 0, 0, 1);
	    } else {
		dsSetColorAlpha(0, 1, 0, 1);
	    }

            dsSetTexture (DS_WOOD);
            dGeomCylinderGetParams(g, &radius, &length);
            dsDrawCylinder(pos, rot, length, radius);
            break;
        }
        
        default:
	{
	    abort();
	}
    }
}

void simLoop(int pause)
{
    if (!pause) {

        const dReal step = 0.003;
        const unsigned nsteps = 4;

        for (unsigned i=0; i<nsteps; ++i) {
            dWorldQuickStep(world, step);
        }
    }

#if 0
    {
	const dReal *omega_1, *omega_2;

	omega_1 = dBodyGetAngularVel(body1);
	omega_2 = dBodyGetAngularVel(body2);

	printf ("T1: %f, %f, %f\n",
		feedback.t1[0], feedback.t1[1], feedback.t1[2]);

	printf ("T2: %f, %f, %f\n",
		feedback.t2[0], feedback.t2[1], feedback.t2[2]);

	printf ("F1: %f, %f, %f\n",
		feedback.f1[0], feedback.f1[1], feedback.f1[2]);

	printf ("F2: %f, %f, %f\n",
		feedback.f2[0], feedback.f2[1], feedback.f2[2]);
    }
#endif

    // now we draw everything
    unsigned ngeoms = dSpaceGetNumGeoms(space);
    for (unsigned i=0; i<ngeoms; ++i) {
        dGeomID g = dSpaceGetGeom(space, i);

        drawGeom(g);
    }

    const dReal *R_1 = dGeomGetRotation(geom1);
    const dReal *R_2 = dGeomGetRotation(geom2);
    dVector3 c_1, c_2, a_1, a_2;

    dJointGetTransmissionContactPoint1(transmission, c_1);
    dJointGetTransmissionContactPoint2(transmission, c_2);
    dJointGetTransmissionAnchor1(transmission, a_1);
    dJointGetTransmissionAnchor2(transmission, a_2);

    dsSetColorAlpha(1, 0, 0, 0.5);
    dsDrawCylinder(a_1, R_1, 0.05, dCalcPointsDistance3(c_1, a_1));
    dsSetColorAlpha(0, 1, 0, 0.5);
    dsDrawCylinder(a_2, R_2, 0.05, dCalcPointsDistance3(c_2, a_2));

    dsSetColorAlpha(1, 0, 0, 0.5);
    dsDrawSphere (c_1, R_1, 0.05);
    dsDrawSphere (c_2, R_1, 0.05);

    dsSetColorAlpha(1, 1, 0, 0.5);
    if (mode == dTransmissionChainDrive) {
        dsDrawLine(c_1, c_2);
    }
}

static void command (int cmd)
{
    if (cmd == '[') {
	switch(mode) {
	case dTransmissionParallelAxes:
	    if (ratio > 0.125) {
		ratio *= 0.5;

		fprintf (stderr, "Gear ratio set to %.3f.\n", ratio);
	    }
	    break;
	case dTransmissionIntersectingAxes:
	    if (theta > 0.1) {
		theta -= 0.1;

		fprintf (stderr, "Gear angle set to %.3f deg.\n",
			 theta / M_PI * 180);
	    }
	    break;
	case dTransmissionChainDrive:
	    if (rho_2 > 0.125) {
		rho_2 /= 2;

		fprintf (stderr, "Sprocket ratio set to %.3f.\n", rho_2 / rho_1);
	    }
	    break;
	}

	setup();
    } else if (cmd == ']') {
	switch(mode) {
	case dTransmissionParallelAxes:
	    if (ratio < 8) {
		ratio *= 2;

		fprintf (stderr, "Gear ratio set to %.3f.\n", ratio);
	    }
	    break;
	case dTransmissionIntersectingAxes:
	    if (theta < 0.9) {
		theta += 0.1;

		fprintf (stderr, "Gear angle set to %.3f deg.\n",
			 theta / M_PI * 180);
	    }
	    break;
	case dTransmissionChainDrive:
	    if (rho_2 < 2) {
		rho_2 *= 2;

		fprintf (stderr, "Sprocket ratio set to %.3f.\n", rho_2 / rho_1);
	    }
	    break;
	}

	setup();
    } else if (cmd == '.') {
	speed += 5;

	fprintf (stderr, "Driving wheel speed set to %g rad/s.\n", speed);

	dJointSetHingeParam(hinge2, dParamVel, speed);
    } else if (cmd == ',') {
	speed -= 5;

	fprintf (stderr, "Driving wheel speed set to %g rad/s.\n", speed);

	dJointSetHingeParam(hinge2, dParamVel, speed);
    } else if (cmd == '/') {
	if (dJointGetHingeParam(hinge2, dParamFMax) > 0) {
	    dJointSetHingeParam(hinge2, dParamFMax, 0);
	} else {
	    dJointSetHingeParam(hinge2, dParamFMax, 50);
	}

    } else if (cmd == '-') {
	backlash -= 0.1;

	fprintf (stderr, "Backlash set to %g m.\n", backlash);

        dJointSetTransmissionBacklash(transmission, backlash);
    } else if (cmd == '=') {
	backlash += 0.1;

	fprintf (stderr, "Backlash set to %g m.\n", backlash);

        dJointSetTransmissionBacklash(transmission, backlash);
    } else if (cmd == '1') {
	mode = dTransmissionParallelAxes;
	setup();
    } else if (cmd == '2') {
	mode = dTransmissionIntersectingAxes;
	setup();
    } else if (cmd == '3') {
	mode = dTransmissionChainDrive;
	setup();
    }
}

int main(int argc, char **argv)
{
    // setup pointers to drawstuff callback functions
    dsFunctions fn;
    fn.version = DS_VERSION;
    fn.start = &start;
    fn.step = &simLoop;
    fn.command = &command;
    fn.stop = stop;
    fn.path_to_textures = DRAWSTUFF_TEXTURE_PATH;
    
    // create world
    dInitODE();

    // run demo
    dsSimulationLoop (argc, argv, 800, 600, &fn);

    dCloseODE();
    return 0;
}