Gear constraint example (demo_IRR_gears.cpp)

Create a transmission made of pulleys, spur gears, bevel gears.

Custom simplified constraints are available, to avoid the simulation of contacts between gear teeth.

This tutorial shows how to:

  • use ChLinkGear constraints to represent simplified gear interaction in 3D space
  • the ChLinkPulley constraint is used to model simplified pulleys in 3D space.
  • manage rotations of references using ChCoordsys and ChFrame classes.
  • create a motor between two parts.
// =============================================================================
// PROJECT CHRONO - http://projectchrono.org
//
// Copyright (c) 2014 projectchrono.org
// All rights reserved.
//
// Use of this source code is governed by a BSD-style license that can be found
// in the LICENSE file at the top level of the distribution and at
// http://projectchrono.org/license-chrono.txt.
//
// =============================================================================
// Authors: Alessandro Tasora
// =============================================================================
//
// Demonstration of the gear constraint (ChLinkGear) as a method to impose a
// transmission ratio between two shafts as they were connected by gears,
// without the need of performing collision detection between gear teeth
// geometries (which would be inefficient)
//
// =============================================================================
#include "chrono/core/ChRealtimeStep.h"
#include "chrono/physics/ChLinkMotorRotationSpeed.h"
#include "chrono/physics/ChSystemNSC.h"
#include "chrono/physics/ChBodyEasy.h"
#include "chrono_irrlicht/ChVisualSystemIrrlicht.h"
// Use the namespaces of Chrono
using namespace chrono;
using namespace chrono::irrlicht;
int main(int argc, char* argv[]) {
GetLog() << "Copyright (c) 2017 projectchrono.org\nChrono version: " << CHRONO_VERSION << "\n\n";
// Create a Chrono physical system
// Contact material shared among all bodies
auto mat = chrono_types::make_shared<ChMaterialSurfaceNSC>();
// Shared visualization material
auto vis_mat = chrono_types::make_shared<ChVisualMaterial>();
vis_mat->SetKdTexture(GetChronoDataFile("textures/pinkwhite.png"));
// Create all the rigid bodies.
double radA = 2;
double radB = 4;
// ...the truss
auto mbody_truss = chrono_types::make_shared<ChBodyEasyBox>(20, 10, 2, 1000, true, false, mat);
sys.Add(mbody_truss);
mbody_truss->SetBodyFixed(true);
mbody_truss->SetPos(ChVector<>(0, 0, 3));
// ...the rotating bar support for the two epicycloidal wheels
auto mbody_train = chrono_types::make_shared<ChBodyEasyBox>(8, 1.5, 1.0, 1000, true, false, mat);
sys.Add(mbody_train);
mbody_train->SetPos(ChVector<>(3, 0, 0));
// ...which must rotate respect to truss along Z axis, in 0,0,0,
auto link_revoluteTT = chrono_types::make_shared<ChLinkLockRevolute>();
link_revoluteTT->Initialize(mbody_truss, mbody_train, ChCoordsys<>(ChVector<>(0, 0, 0), QUNIT));
sys.AddLink(link_revoluteTT);
// ...the first gear
auto mbody_gearA = chrono_types::make_shared<ChBodyEasyCylinder>(radA, 0.5, 1000, true, false, mat);
sys.Add(mbody_gearA);
mbody_gearA->SetPos(ChVector<>(0, 0, -1));
mbody_gearA->SetRot(Q_from_AngAxis(CH_C_PI / 2, VECT_X));
mbody_gearA->GetVisualShape(0)->SetMaterial(0, vis_mat);
// for aesthetic reasons, also add a thin cylinder only as a visualization:
auto mshaft_shape = chrono_types::make_shared<ChCylinderShape>();
mshaft_shape->GetCylinderGeometry().p1 = ChVector<>(0, -3, 0);
mshaft_shape->GetCylinderGeometry().p2 = ChVector<>(0, 10, 0);
mshaft_shape->GetCylinderGeometry().rad = radA * 0.4;
mbody_gearA->AddVisualShape(mshaft_shape);
// ...impose rotation speed between the first gear and the fixed truss
auto link_motor = chrono_types::make_shared<ChLinkMotorRotationSpeed>();
link_motor->Initialize(mbody_gearA, mbody_truss, ChFrame<>(ChVector<>(0, 0, 0), QUNIT));
link_motor->SetSpeedFunction(chrono_types::make_shared<ChFunction_Const>(6));
sys.AddLink(link_motor);
// ...the second gear
double interaxis12 = radA + radB;
auto mbody_gearB = chrono_types::make_shared<ChBodyEasyCylinder>(radB, 0.4, 1000, true, false, mat);
sys.Add(mbody_gearB);
mbody_gearB->SetPos(ChVector<>(interaxis12, 0, -1));
mbody_gearB->SetRot(Q_from_AngAxis(CH_C_PI / 2, VECT_X));
mbody_gearB->GetVisualShape(0)->SetMaterial(0, vis_mat);
// ... the second gear is fixed to the rotating bar
auto link_revolute = chrono_types::make_shared<ChLinkLockRevolute>();
link_revolute->Initialize(mbody_gearB, mbody_train, ChCoordsys<>(ChVector<>(interaxis12, 0, 0), QUNIT));
sys.AddLink(link_revolute);
// ...the gear constraint between the two wheels A and B.
// As transmission ratio (=speed of wheel B / speed of wheel A) to enter in Set_tau(), we
// could use whatever positive value we want: the ChLinkGear will compute the two radii of the
// wheels for its 'hidden' computations, given the distance between the two axes. However, since
// we already build two '3D cylinders' bodies -just for visualization reasons!- with radA and radB,
// we must enter Set_tau(radA/radB).
// Also, note that the initial position of the constraint has no importance (simply use CSYSNORM),
// but we must set where the two axes are placed in the local coordinates of the two wheels, so
// we use Set_local_shaft1() and pass some local ChFrame. Note that, since the Z axis of that frame
// will be considered the axis of the wheel, we must rotate the frame 90 with Q_from_AngAxis(), because
// we created the wheel with ChBodyEasyCylinder() which created a cylinder with Y as axis.
auto link_gearAB = chrono_types::make_shared<ChLinkGear>();
link_gearAB->Initialize(mbody_gearA, mbody_gearB, CSYSNORM);
link_gearAB->Set_local_shaft1(ChFrame<>(VNULL, chrono::Q_from_AngAxis(-CH_C_PI / 2, VECT_X)));
link_gearAB->Set_local_shaft2(ChFrame<>(VNULL, chrono::Q_from_AngAxis(-CH_C_PI / 2, VECT_X)));
link_gearAB->Set_tau(radA / radB);
link_gearAB->Set_checkphase(true);
sys.AddLink(link_gearAB);
// ...the gear constraint between the second wheel B and a large wheel C with inner teeth, that
// does not necessarily need to be created as a new body because it is the 'fixed' part of the
// epicycloidal reducer, so, as wheel C, we will simply use the ground object 'mbody_truss'.
double radC = 2 * radB + radA;
auto link_gearBC = chrono_types::make_shared<ChLinkGear>();
link_gearBC->Initialize(mbody_gearB, mbody_truss, CSYSNORM);
link_gearBC->Set_local_shaft1(ChFrame<>(VNULL, chrono::Q_from_AngAxis(-CH_C_PI / 2, VECT_X)));
link_gearBC->Set_local_shaft2(ChFrame<>(ChVector<>(0, 0, -4), QUNIT));
link_gearBC->Set_tau(radB / radC);
link_gearBC->Set_epicyclic(true); // <-- this means: use a wheel with internal teeth!
sys.AddLink(link_gearBC);
// ...the bevel gear at the side,
double radD = 5;
auto mbody_gearD = chrono_types::make_shared<ChBodyEasyCylinder>(radD, 0.8, 1000, true, false, mat);
sys.Add(mbody_gearD);
mbody_gearD->SetPos(ChVector<>(-10, 0, -9));
mbody_gearD->SetRot(Q_from_AngAxis(CH_C_PI / 2, VECT_Z));
mbody_gearD->GetVisualShape(0)->SetMaterial(0, vis_mat);
// ... it is fixed to the truss using a revolute joint with horizontal axis (must rotate
// default ChLink creation coordys 90 on the Y vertical, since the revolute axis is the Z axis).
auto link_revoluteD = chrono_types::make_shared<ChLinkLockRevolute>();
link_revoluteD->Initialize(mbody_gearD, mbody_truss,
ChCoordsys<>(ChVector<>(-10, 0, -9), Q_from_AngAxis(CH_C_PI / 2, VECT_Y)));
sys.AddLink(link_revoluteD);
// ... Let's make a 1:1 gear between wheel A and wheel D as a bevel gear: Chrono does not require
// special info for this case -the position of the two shafts and the transmission ratio are enough-
auto link_gearAD = chrono_types::make_shared<ChLinkGear>();
link_gearAD->Initialize(mbody_gearA, mbody_gearD, CSYSNORM);
link_gearAD->Set_local_shaft1(ChFrame<>(ChVector<>(0, -7, 0), chrono::Q_from_AngAxis(-CH_C_PI / 2, VECT_X)));
link_gearAD->Set_local_shaft2(ChFrame<>(ChVector<>(0, -7, 0), chrono::Q_from_AngAxis(-CH_C_PI / 2, VECT_X)));
link_gearAD->Set_tau(1);
sys.AddLink(link_gearAD);
// ...the pulley at the side,
double radE = 2;
auto mbody_pulleyE = chrono_types::make_shared<ChBodyEasyCylinder>(radE, 0.8, 1000, true, false, mat);
sys.Add(mbody_pulleyE);
mbody_pulleyE->SetPos(ChVector<>(-10, -11, -9));
mbody_pulleyE->SetRot(Q_from_AngAxis(CH_C_PI / 2, VECT_Z));
mbody_pulleyE->GetVisualShape(0)->SetMaterial(0, vis_mat);
// ... it is fixed to the truss using a revolute joint with horizontal axis (must rotate
// default ChLink creation coordys 90 on the Y vertical, since the revolute axis is the Z axis).
auto link_revoluteE = chrono_types::make_shared<ChLinkLockRevolute>();
link_revoluteE->Initialize(mbody_pulleyE, mbody_truss,
ChCoordsys<>(ChVector<>(-10, -11, -9), Q_from_AngAxis(CH_C_PI / 2, VECT_Y)));
sys.AddLink(link_revoluteE);
// ... Let's make a synchro belt constraint between pulley D and pulley E. The user must be
// sure that the two shafts are parallel in absolute space. Also, interaxial distance should not change.
auto link_pulleyDE = chrono_types::make_shared<ChLinkPulley>();
link_pulleyDE->Initialize(mbody_gearD, mbody_pulleyE, CSYSNORM);
link_pulleyDE->Set_local_shaft1(ChFrame<>(VNULL, chrono::Q_from_AngAxis(-CH_C_PI / 2, VECT_X)));
link_pulleyDE->Set_local_shaft2(ChFrame<>(VNULL, chrono::Q_from_AngAxis(-CH_C_PI / 2, VECT_X)));
link_pulleyDE->Set_r1(radD);
link_pulleyDE->Set_r2(radE);
link_pulleyDE->Set_checkphase(
true); // synchro belts don't tolerate slipping: this avoids it as numerical errors accumulate.
sys.AddLink(link_pulleyDE);
// Create the Irrlicht visualization system
auto vis = chrono_types::make_shared<ChVisualSystemIrrlicht>();
vis->AttachSystem(&sys);
vis->SetWindowSize(800, 600);
vis->SetWindowTitle("Gears and pulleys");
vis->Initialize();
vis->AddLogo();
vis->AddSkyBox();
vis->AddCamera(ChVector<>(12, 15, -20));
// Prepare the physical system for the simulation
sys.SetTimestepperType(ChTimestepper::Type::EULER_IMPLICIT_PROJECTED);
// Simulation loop
double timestep = 0.001;
ChRealtimeStepTimer realtime_timer;
while (vis->Run()) {
vis->BeginScene();
vis->Render();
// .. draw also some circle lines representing gears - just for aesthetical reasons
tools::drawCircle(vis.get(), link_gearBC->Get_r2(),
(link_gearBC->Get_local_shaft2() >> *link_gearBC->GetBody2()).GetCoord(), ChColor(1, 0, 0),
50, true);
tools::drawCircle(vis.get(), link_gearAD->Get_r1(),
(link_gearAD->Get_local_shaft1() >> *link_gearAD->GetBody1()).GetCoord(), ChColor(1, 0, 0),
30, true);
tools::drawCircle(vis.get(), link_gearAD->Get_r2(),
(link_gearAD->Get_local_shaft2() >> *link_gearAD->GetBody2()).GetCoord(), ChColor(1, 0, 0),
30, true);
tools::drawCircle(vis.get(), 0.1, ChCoordsys<>(link_gearAB->GetMarker2()->GetAbsCoord().pos, QUNIT));
tools::drawCircle(vis.get(), 0.1, ChCoordsys<>(link_gearAD->GetMarker2()->GetAbsCoord().pos, QUNIT));
tools::drawCircle(vis.get(), 0.1, ChCoordsys<>(link_gearBC->GetMarker2()->GetAbsCoord().pos, QUNIT));
// ..draw also some segments for a simplified representation of pulley
tools::drawSegment(vis.get(), link_pulleyDE->Get_belt_up1(), link_pulleyDE->Get_belt_up2(), ChColor(0, 1, 0),
true);
tools::drawSegment(vis.get(), link_pulleyDE->Get_belt_low1(), link_pulleyDE->Get_belt_low2(), ChColor(0, 1, 0),
true);
vis->EndScene();
sys.DoStepDynamics(timestep);
realtime_timer.Spin(timestep);
}
return 0;
}
void AddTypicalLights()
Simple shortcut to set two point lights in the scene.
Definition: ChVisualSystemIrrlicht.cpp:274
std::string GetChronoDataFile(const std::string &filename)
Obtain the complete path to the specified filename, given relative to the Chrono data directory (thre...
Definition: ChGlobal.cpp:95
void Add(std::shared_ptr< ChPhysicsItem > item)
Attach an arbitrary ChPhysicsItem (e.g.
Definition: ChSystem.cpp:183
void drawSegment(ChVisualSystemIrrlicht *vis, ChVector<> start, ChVector<> end, chrono::ChColor col, bool use_Zbuffer)
Draw line segments in 3D space with given color.
Definition: ChIrrTools.cpp:667
COORDSYS:
Definition: ChCoordsys.h:38
void AddSkyBox(const std::string &texture_dir=GetChronoDataFile("skybox/"))
Add a sky box in a 3D scene.
Definition: ChVisualSystemIrrlicht.cpp:287
virtual void Initialize()
Initialize the visualization system.
Definition: ChVisualSystemIrrlicht.cpp:151
void drawCircle(ChVisualSystemIrrlicht *vis, double radius, ChCoordsys<> pos, chrono::ChColor col, int resolution, bool use_Zbuffer)
Draw a circle line in 3D space with given color.
Definition: ChIrrTools.cpp:695
ChLog & GetLog()
Global function to get the current ChLog object.
Definition: ChLog.cpp:39
bool Run()
Run the Irrlicht device.
Definition: ChVisualSystemIrrlicht.cpp:205
virtual void AddLink(std::shared_ptr< ChLinkBase > link)
Attach a link to the underlying assembly.
Definition: ChSystem.cpp:168
void AddCamera(const ChVector<> &pos, ChVector<> targ=VNULL)
Add a camera in an Irrlicht 3D scene.
Definition: ChVisualSystemIrrlicht.cpp:256
Representation of a 3D transform.
Definition: ChFrame.h:34
ChQuaternion< double > Q_from_AngAxis(double angle, const ChVector< double > &axis)
Get the quaternion from an angle of rotation and an axis, defined in abs coords.
Definition: ChQuaternion.cpp:100
virtual void BeginScene(bool backBuffer=true, bool zBuffer=true, ChColor color=ChColor(0, 0, 0))
Clean the canvas at the beginning of each animation frame.
Definition: ChVisualSystemIrrlicht.cpp:489
const ChApi ChQuaternion< double > QUNIT
Constant unit quaternion: {1, 0, 0, 0} , corresponds to no rotation (diagonal rotation matrix)
Namespace with classes for the Irrlicht module.
Definition: ChApiIrr.h:48
Class for a timer which attempts to enforce soft real-time.
Definition: ChRealtimeStep.h:25
void Spin(double step)
Call this function INSIDE the simulation loop, just ONCE per loop (preferably as the last call in the...
Definition: ChRealtimeStep.h:34
Definition of a visual color.
Definition: ChColor.h:26
Definition of general purpose 3d vector variables, such as points in 3D.
Definition: ChVector.h:35
int DoStepDynamics(double step_size)
Advances the dynamical simulation for a single step, of length step_size.
Definition: ChSystem.cpp:1426
virtual void EndScene()
End the scene draw at the end of each animation frame.
Definition: ChVisualSystemIrrlicht.cpp:534
void SetWindowTitle(const std::string &win_title)
Set the windoiw title (default "").
Definition: ChVisualSystemIrrlicht.cpp:118
virtual void AttachSystem(ChSystem *sys) override
Attach another Chrono system to the run-time visualization system.
Definition: ChVisualSystemIrrlicht.cpp:135
virtual void Render()
Draw all 3D shapes and GUI elements at the current frame.
Definition: ChVisualSystemIrrlicht.cpp:544
Main namespace for the Chrono package.
Definition: ChBarrelShape.cpp:17
void SetTimestepperType(ChTimestepper::Type type)
Set the method for time integration (time stepper type).
Definition: ChSystem.cpp:459
void AddLogo(const std::string &logo_filename=GetChronoDataFile("logo_chronoengine_alpha.png"))
Add a logo in a 3D scene.
Definition: ChVisualSystemIrrlicht.cpp:248
Class for a physical system in which contact is modeled using a non-smooth (complementarity-based) me...
Definition: ChSystemNSC.h:29
void SetWindowSize(unsigned int width, unsigned int height)
Set the window size (default 640x480).
Definition: ChVisualSystemIrrlicht.cpp:115