Skip to content

Commit 9769226

Browse files
committed
Update example 13
1 parent afa9027 commit 9769226

6 files changed

Lines changed: 164 additions & 47 deletions

File tree

examples/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ ADD_SUBDIRECTORY(example_9)
3535
ADD_SUBDIRECTORY(example_10)
3636
ADD_SUBDIRECTORY(example_11)
3737
ADD_SUBDIRECTORY(example_12)
38+
ADD_SUBDIRECTORY(example_13)
3839
ADD_SUBDIRECTORY(example_14)
3940
ADD_SUBDIRECTORY(example_15)
4041
ADD_SUBDIRECTORY(example_16)

examples/example_11/example_11.cpp

Lines changed: 0 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,3 @@
1-
/*
2-
* This example shows how to create
3-
* a waypoint trajectory consisting of straight
4-
* lines
5-
*
6-
*
7-
*
8-
*/
9-
101
#include "bitrl/bitrl_config.h"
112

123
#ifdef BITRL_CHRONO
@@ -22,20 +13,6 @@
2213
#include <chrono/physics/ChBodyEasy.h>
2314
#include <chrono_irrlicht/ChVisualSystemIrrlicht.h>
2415

25-
26-
#include "bitrl/utils/geometry/geom_point.h"
27-
#include "bitrl/utils/trajectory/line_segment_link.h"
28-
#include "bitrl/utils/trajectory/waypoint.h"
29-
#include "bitrl/utils/trajectory/waypoint_trajectory.h"
30-
#include "bitrl/utils/unit_converter.h"
31-
32-
#include <any>
33-
#include <array>
34-
#include <iostream>
35-
#include <map>
36-
#include <string>
37-
#include <vector>
38-
3916
namespace example_11
4017
{
4118

examples/example_11/example_11.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ Note that you should have compiled Chrono with <a href="https://irrlicht.sourcef
99
The main interface for creating rigid bodies in Chrono is the <a href="https://api.projectchrono.org/9.0.0/classchrono_1_1_ch_body.html">ChBody</a>
1010
class. You can also find this <a href="https://api.projectchrono.org/9.0.0/rigid_bodies.html"> Rigid Bodies</a> helpful.
1111
_ChBody_ is an abstract class, and therefore we cannot instantiate it directly. Chrono provides various classes however we can
12-
immediately use. The one we will use in this example is the _ChBodyEasyBox_ class. This is defined in the _<chrono/physics/ChBodyEasy.h>_ header.
12+
immediately use. The one we will use in this example is the _ChBodyEasyBox_ class. This is defined in the _chrono/physics/ChBodyEasy.h_ header.
1313
Let's first create a box and try to visualize it. Below is the function that constructs a box.
1414

1515
@code{.cpp}

examples/example_13/CMakeLists.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ ADD_EXECUTABLE(${EXECUTABLE} ${SOURCE})
88
TARGET_LINK_LIBRARIES(${EXECUTABLE} PRIVATE bitrllib pthread boost_log)
99

1010
IF(ENABLE_CHRONO)
11-
TARGET_LINK_LIBRARIES(${EXECUTABLE} PRIVATE Chrono::Chrono_core)
11+
TARGET_LINK_LIBRARIES(${EXECUTABLE} PRIVATE Chrono::Chrono_core Chrono::Chrono_irrlicht)
1212
ENDIF ()
1313

1414
IF(BITRL_WEBOTS)

examples/example_13/example_13.cpp

Lines changed: 143 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,19 @@
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>
@@ -11,36 +23,145 @@
1123
namespace example_13
1224
{
1325
using 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

1950
int 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

examples/example_13/example_13.md

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
\page bitrl_example_13 BitRL Example 13 Using chrono::ChSystemSMC
2+
3+
Example \ref bitrl_example_11 discussed _ChBody_. Specifically, how to create a _ChBodyEasyBox_.
4+
Understanding rigid bodies is fundamental to robotics. In this example we will discuss _ChSystem_.
5+
This is the cornerstone of a Chrono simulation. Most information in this example
6+
can be found in the official doumentation here: <a href="https://api.projectchrono.org/9.0.0/simulation_system.html">Simulation system</a>.
7+
A _ChSystem_ is an abstract class. The Chrono library provides the following subclasses:
8+
9+
- _ChSystemNSC_ for Non Smooth Contacts (NSC): in case of contacts a complementarity solver will take care of them using non-smooth dynamics; this is very efficient even with large time steps.
10+
- _ChSystemSMC_ for SMooth Contacts (SMC): contacts are handled using penalty methods, i.e. contacts are deformable
11+
12+
Note that if there are no contacts or collisions in your system, it is indifferent to use _ChSystemNSC_ or _ChSystemSMC_.
13+
In this example we will create and simulate a differential drive system using Chrono
14+
15+
@code{.cpp}
16+
@endcode
17+
18+

0 commit comments

Comments
 (0)