1+ #include " bitrl/bitrl_config.h"
2+
3+ #ifdef BITRL_CHRONO
4+
15#include " bitrl/bitrl_types.h"
2- #include " bitrl/utils/geometry/geom_point.h"
3- #include " bitrl/utils/geometry/mesh/mesh.h"
4- #include " bitrl/utils/geometry/mesh/quad_mesh_generation.h"
6+
7+
8+ #ifdef BITRL_LOG
9+ #define BOOST_LOG_DYN_LINK
10+ #include < boost/log/trivial.hpp>
11+ #endif
12+
13+ #include " chrono/core/ChRealtimeStep.h"
14+ #include " chrono/physics/ChSystemNSC.h"
15+ #include " chrono/physics/ChLinkMotorRotationSpeed.h"
16+ #include < chrono_irrlicht/ChVisualSystemIrrlicht.h>
517
618#include < filesystem>
719#include < iostream>
1123namespace example_13
1224{
1325using namespace bitrl ;
14- using utils::geom::GeomPoint;
15- using utils::geom::Mesh;
26+ using namespace chrono ::irrlicht;
27+
28+ // constants we will be using further below
29+ const uint_t WINDOW_HEIGHT = 800 ;
30+ const uint_t WINDOW_WIDTH = 1024 ;
31+ const real_t DT = 0.01 ;
32+ const real_t SIM_TIME = 5.0 ;
33+ const std::string WINDOW_TITLE ( " Example 13" );
34+
35+ void prepare_visualization (chrono::irrlicht::ChVisualSystemIrrlicht& visual)
36+ {
37+ visual.SetWindowSize (WINDOW_WIDTH , WINDOW_WIDTH ); // WINDOW_HEIGHT);
38+ visual.SetWindowTitle (WINDOW_TITLE );
39+ visual.Initialize ();
40+
41+ visual.AddLogo ();
42+ visual.AddSkyBox ();
43+ visual.AddCamera ({0 , -2 , 1 }, {0 , 0 , 0 });
44+ visual.AddTypicalLights ();
45+ visual.BindAll ();
46+ }
1647
1748} // namespace example_13
1849
1950int main ()
2051{
21-
2252 using namespace example_13 ;
53+ chrono::ChSystemNSC sys;
54+ sys.SetGravityY ();
2355
24- try
25- {
56+ // 2- Create the rigid bodies of the slider-crank mechanical system
57+ // (a crank, a rod, a truss), maybe setting position/mass/inertias of
58+ // their center of mass (COG) etc.
2659
27- // build a square grid
28- Mesh<2 > mesh;
29- utils::geom::build_quad_mesh (mesh, 10 , 10 , GeomPoint<2 >({0.0 , 0.0 }),
30- GeomPoint<2 >({1.0 , 1.0 }));
60+ // ..the truss
61+ auto my_body_A = chrono_types::make_shared<chrono::ChBody>();
62+ sys.AddBody (my_body_A);
63+ my_body_A->SetFixed (true ); // truss does not move!
64+ my_body_A->SetName (" Ground-Truss" );
3165
32- std::cout << " Number of elements: " << mesh.n_elements () << std::endl;
33- std::cout << " Number of nodes: " << mesh.n_nodes () << std::endl;
34- }
35- catch (std::exception &e)
36- {
37- std::cout << e.what () << std::endl;
38- }
39- catch (...)
40- {
66+ // ..the crank
67+ auto my_body_B = chrono_types::make_shared<chrono::ChBody>();
68+ sys.AddBody (my_body_B);
69+ my_body_B->SetPos (chrono::ChVector3d (1 , 0 , 0 )); // position of COG of crank
70+ my_body_B->SetMass (2 );
71+ my_body_B->SetName (" Crank" );
72+
73+ // ..the rod
74+ auto my_body_C = chrono_types::make_shared<chrono::ChBody>();
75+ sys.AddBody (my_body_C);
76+ my_body_C->SetPos (chrono::ChVector3d (4 , 0 , 0 )); // position of COG of rod
77+ my_body_C->SetMass (3 );
78+ my_body_C->SetName (" Rod" );
79+
80+ // 3- Create constraints: the mechanical joints between the rigid bodies.
81+
82+ // .. a revolute joint between crank and rod
83+ auto my_link_BC = chrono_types::make_shared<chrono::ChLinkLockRevolute>();
84+ my_link_BC->SetName (" RevJointCrankRod" );
85+ my_link_BC->Initialize (my_body_B, my_body_C, chrono::ChFrame<>(chrono::ChVector3d (2 , 0 , 0 )));
86+ sys.AddLink (my_link_BC);
87+
88+ // .. a slider joint between rod and truss
89+ auto my_link_CA = chrono_types::make_shared<chrono::ChLinkLockPointLine>();
90+ my_link_CA->SetName (" TransJointRodGround" );
91+ my_link_CA->Initialize (my_body_C, my_body_A, chrono::ChFrame<>(chrono::ChVector3d (6 , 0 , 0 )));
92+ sys.AddLink (my_link_CA);
4193
42- std::cout << " Unknown exception occured" << std::endl;
94+ // .. a motor between crank and truss
95+ auto my_link_AB = chrono_types::make_shared<chrono::ChLinkMotorRotationSpeed>();
96+ my_link_AB->Initialize (my_body_A, my_body_B, chrono::ChFrame<>(chrono::ChVector3d (0 , 0 , 0 )));
97+ my_link_AB->SetName (" RotationalMotor" );
98+ sys.AddLink (my_link_AB);
99+ auto my_speed_function = chrono_types::make_shared<chrono::ChFunctionConst>(chrono::CH_PI ); // speed w=3.145 rad/sec
100+ my_link_AB->SetSpeedFunction (my_speed_function);
101+
102+ chrono::irrlicht::ChVisualSystemIrrlicht visual;
103+ prepare_visualization (visual);
104+ visual.AttachSystem (&sys);
105+
106+ // Simulation loop
107+
108+ // Timer for enforcing soft real-time
109+ chrono::ChRealtimeStepTimer realtime_timer;
110+
111+ // bool removed = false;
112+ while (visual.Run ()) {
113+ // Irrlicht must prepare frame to draw
114+ visual.BeginScene ();
115+
116+ // Irrlicht now draws simple lines in 3D world representing a
117+ // skeleton of the mechanism, in this instant:
118+ //
119+ // .. draw items belonging to Irrlicht scene, if any
120+ visual.Render ();
121+ // .. draw a grid
122+ tools::drawGrid (&visual, 0.5 , 0.5 );
123+ // .. draw GUI items belonging to Irrlicht screen, if any
124+ visual.GetGUIEnvironment ()->drawAll ();
125+
126+ // .. draw the rod (from joint BC to joint CA)
127+ tools::drawSegment (&visual, my_link_BC->GetMarker1 ()->GetAbsCoordsys ().pos ,
128+ my_link_CA->GetMarker1 ()->GetAbsCoordsys ().pos ,
129+ chrono::ChColor (0 , 1 , 0 ));
130+ // .. draw the crank (from joint AB to joint BC)
131+ tools::drawSegment (&visual, my_link_AB->GetFrame2Abs ().GetCoordsys ().pos ,
132+ my_link_BC->GetMarker1 ()->GetAbsCoordsys ().pos ,
133+ chrono::ChColor (1 , 0 , 0 ));
134+
135+ // .. draw a small circle at crank origin
136+ tools::drawCircle (&visual, 0.1 , chrono::ChCoordsys<>(chrono::ChVector3d (0 , 0 , 0 ), chrono::QUNIT ));
137+
138+ /* test: delete a link after 10 seconds
139+ if (sys.GetChTime() >10 && (!removed))
140+ {
141+ sys.RemoveLink(my_link_AB);
142+ removed = true;
143+ }*/
144+
145+ // ADVANCE SYSTEM STATE BY ONE STEP
146+ sys.DoStepDynamics (DT );
147+ // Enforce soft real-time
148+ realtime_timer.Spin (DT );
149+
150+ // Irrlicht must finish drawing the frame
151+ visual.EndScene ();
43152 }
44153
154+
45155 return 0 ;
46156}
157+ #else
158+ #include < iostream>
159+ int main ()
160+ {
161+ std::cerr<<" You need PROJECTCHRONO configured with "
162+ <<" bitrl in order to run this example "
163+ <<" Reconfigure bitrl and set ENABLE_CHRONO=ON" <<std::endl;
164+ return 1 ;
165+ }
166+
167+ #endif
0 commit comments