Slider crank example (demo_MBS_crank.cpp)
The simpliest way to integrate Chrono::Engine in the Irrlicht 3D visualization library: in fact the coordinates of the joints are simply used as end-points of simple polygonal lines which are drawn in the 3D space for each frame redraw, to show a very simplified 'skeleton' of a slider-crank mechanism. The demo also teaches how to:
- create constraints and 'engine' objects
- make a real-time application, where Chrono::Engine adapts the integration step to the speed of the CPU.
// =============================================================================
// 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
// =============================================================================
//
// Demo code about
//
// - constraints and 'motor' objects
// - using IRRLICHT as a realtime 3D viewer of a slider-crank mechanism
// simulated with Chrono::Engine.
// - using the real-time step.
//
// This is just a possible method of integration of Chrono::Engine + Irrlicht;
// many others are possible.
//
// =============================================================================
#include "chrono/core/ChRealtimeStep.h"
#include "chrono/physics/ChSystemNSC.h"
#include "chrono/physics/ChLinkMotorRotationSpeed.h"
#include "chrono_irrlicht/ChVisualSystemIrrlicht.h"
// Use the namespaces of Chrono
using namespace chrono;
using namespace chrono::irrlicht;
int main(int argc, char* argv[]) {
std::cout << "Copyright (c) 2017 projectchrono.org\nChrono version: " << CHRONO_VERSION << std::endl;
//
// HERE YOU CREATE THE MECHANICAL SYSTEM OF CHRONO...
//
// 1- Create a Chrono physical system: all bodies and constraints
// will be handled by this ChSystemNSC object.
ChSystemNSC sys;
// 2- Create the rigid bodies of the slider-crank mechanical system
// (a crank, a rod, a truss), maybe setting position/mass/inertias of
// their center of mass (COG) etc.
// ..the truss
auto my_body_A = chrono_types::make_shared<ChBody>();
sys.AddBody(my_body_A);
my_body_A->SetFixed(true); // truss does not move!
my_body_A->SetName("Ground-Truss");
// ..the crank
auto my_body_B = chrono_types::make_shared<ChBody>();
sys.AddBody(my_body_B);
my_body_B->SetPos(ChVector3d(1, 0, 0)); // position of COG of crank
my_body_B->SetMass(2);
my_body_B->SetName("Crank");
// ..the rod
auto my_body_C = chrono_types::make_shared<ChBody>();
sys.AddBody(my_body_C);
my_body_C->SetPos(ChVector3d(4, 0, 0)); // position of COG of rod
my_body_C->SetMass(3);
my_body_C->SetName("Rod");
// 3- Create constraints: the mechanical joints between the rigid bodies.
// .. a revolute joint between crank and rod
auto my_link_BC = chrono_types::make_shared<ChLinkLockRevolute>();
my_link_BC->SetName("RevJointCrankRod");
my_link_BC->Initialize(my_body_B, my_body_C, ChFrame<>(ChVector3d(2, 0, 0)));
sys.AddLink(my_link_BC);
// .. a slider joint between rod and truss
auto my_link_CA = chrono_types::make_shared<ChLinkLockPointLine>();
my_link_CA->SetName("TransJointRodGround");
my_link_CA->Initialize(my_body_C, my_body_A, ChFrame<>(ChVector3d(6, 0, 0)));
sys.AddLink(my_link_CA);
// .. a motor between crank and truss
auto my_link_AB = chrono_types::make_shared<ChLinkMotorRotationSpeed>();
my_link_AB->Initialize(my_body_A, my_body_B, ChFrame<>(ChVector3d(0, 0, 0)));
my_link_AB->SetName("RotationalMotor");
sys.AddLink(my_link_AB);
auto my_speed_function = chrono_types::make_shared<ChFunctionConst>(CH_PI); // speed w=3.145 rad/sec
my_link_AB->SetSpeedFunction(my_speed_function);
// 4- Create the Irrlicht visualization system
auto vis = chrono_types::make_shared<ChVisualSystemIrrlicht>();
vis->AttachSystem(&sys);
vis->SetWindowSize(800, 600);
vis->SetWindowTitle("Simple slider-crank example");
vis->Initialize();
vis->AddLogo();
vis->AddSkyBox();
vis->AddCamera(ChVector3d(0, 0, -6));
vis->AddTypicalLights();
// Simulation loop
// Timer for enforcing soft real-time
ChRealtimeStepTimer realtime_timer;
double time_step = 0.01;
// bool removed = false;
while (vis->Run()) {
// Irrlicht must prepare frame to draw
vis->BeginScene();
// Irrlicht now draws simple lines in 3D world representing a
// skeleton of the mechanism, in this instant:
//
// .. draw items belonging to Irrlicht scene, if any
vis->Render();
// .. draw a grid
tools::drawGrid(vis.get(), 0.5, 0.5);
// .. draw GUI items belonging to Irrlicht screen, if any
vis->GetGUIEnvironment()->drawAll();
// .. draw the rod (from joint BC to joint CA)
tools::drawSegment(vis.get(), my_link_BC->GetMarker1()->GetAbsCoordsys().pos,
my_link_CA->GetMarker1()->GetAbsCoordsys().pos, ChColor(0, 1, 0));
// .. draw the crank (from joint AB to joint BC)
tools::drawSegment(vis.get(), my_link_AB->GetFrame2Abs().GetCoordsys().pos,
my_link_BC->GetMarker1()->GetAbsCoordsys().pos, ChColor(1, 0, 0));
// .. draw a small circle at crank origin
/* test: delete a link after 10 seconds
if (sys.GetChTime() >10 && (!removed))
{
sys.RemoveLink(my_link_AB);
removed = true;
}*/
// ADVANCE SYSTEM STATE BY ONE STEP
sys.DoStepDynamics(time_step);
// Enforce soft real-time
realtime_timer.Spin(time_step);
// Irrlicht must finish drawing the frame
vis->EndScene();
}
return 0;
}
void AddTypicalLights()
Simple shortcut to set two point lights in the scene.
Definition: ChVisualSystemIrrlicht.cpp:344
virtual void Render() override
Draw all 3D shapes and GUI elements at the current frame.
Definition: ChVisualSystemIrrlicht.cpp:570
virtual void Initialize() override
Initialize the visualization system.
Definition: ChVisualSystemIrrlicht.cpp:181
Representation of a transform with translation and rotation.
Definition: ChCoordsys.h:28
void AddSkyBox(const std::string &texture_dir=GetChronoDataFile("skybox/"))
Add a sky box in a 3D scene.
Definition: ChVisualSystemIrrlicht.cpp:357
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:782
const ChQuaterniond QUNIT(1., 0., 0., 0.)
Constant unit quaternion: {1, 0, 0, 0} , corresponds to no rotation (diagonal rotation matrix)
Definition: ChQuaternion.h:431
void drawSegment(ChVisualSystemIrrlicht *vis, ChVector3d start, ChVector3d end, chrono::ChColor col, bool use_Zbuffer)
Draw line segments in 3D space with given color.
Definition: ChIrrTools.cpp:757
virtual void AddLink(std::shared_ptr< ChLinkBase > link)
Attach a link to the underlying assembly.
Definition: ChSystem.cpp:144
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
virtual void EndScene() override
End the scene draw at the end of each animation frame.
Definition: ChVisualSystemIrrlicht.cpp:560
virtual void BeginScene() override
Perform any necessary operations at the beginning of each rendering frame.
Definition: ChVisualSystemIrrlicht.cpp:543
virtual int AddCamera(const ChVector3d &pos, ChVector3d targ=VNULL) override
Add a camera in an Irrlicht 3D scene.
Definition: ChVisualSystemIrrlicht.cpp:290
void drawGrid(ChVisualSystemIrrlicht *vis, double ustep, double vstep, int nu, int nv, ChCoordsys<> pos, chrono::ChColor col, bool use_Zbuffer)
Draw grids in 3D space with given orientation, color, and spacing.
Definition: ChIrrTools.cpp:879
int DoStepDynamics(double step_size)
Advance the dynamics simulation by a single time step of given length.
Definition: ChSystem.cpp:1635
ChVector3< double > ChVector3d
Alias for double-precision vectors.
Definition: ChVector3.h:283
void SetWindowTitle(const std::string &win_title)
Set the windoiw title (default "").
Definition: ChVisualSystemIrrlicht.cpp:138
virtual void AttachSystem(ChSystem *sys) override
Attach another Chrono system to the run-time visualization system.
Definition: ChVisualSystemIrrlicht.cpp:165
virtual bool Run() override
Run the Irrlicht device.
Definition: ChVisualSystemIrrlicht.cpp:243
void AddLogo(const std::string &logo_filename=GetChronoDataFile("logo_chronoengine_alpha.png"))
Add a logo in a 3D scene.
Definition: ChVisualSystemIrrlicht.cpp:336
virtual void AddBody(std::shared_ptr< ChBody > body)
Attach a body to the underlying assembly.
Definition: ChSystem.cpp:132
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:134