Skip to content

Commit ec9229f

Browse files
committed
Update examples and documentation (#176)
1 parent 191174d commit ec9229f

4 files changed

Lines changed: 211 additions & 86 deletions

File tree

config.h.in

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
/*Use Webots*/
1717
#cmakedefine BITRL_WEBOTS
1818

19-
/*Use Chron*/
19+
/*Use Chrono*/
2020
#cmakedefine BITRL_CHRONO
2121

2222
/*Use OpenCV*/

examples/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ ADD_SUBDIRECTORY(example_2)
2929
ADD_SUBDIRECTORY(example_3)
3030
ADD_SUBDIRECTORY(example_4)
3131
ADD_SUBDIRECTORY(example_5)
32+
ADD_SUBDIRECTORY(example_6)
3233
ADD_SUBDIRECTORY(example_7)
3334
ADD_SUBDIRECTORY(example_8)
3435
ADD_SUBDIRECTORY(example_9)

examples/example_11/example_11.md

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,14 @@
33
The <a href="https://github.com/projectchrono/chrono">Chrono</a> library is the main library that _bitrl_ is using in order
44
to simulate robots and create environments for reinforcement learning agents. As such, knowing your way around
55
Chrono is essential. However, Chrono is a relatively large library with many components and therefore not necessarily easy
6-
to grasp. In a series of examples, we will see main components of the library that _bitrl_ utilizes.
6+
to grasp. In a series of examples, we will see the main components of the library that _bitrl_ utilizes.
77
Note that you should have compiled Chrono with <a href="https://irrlicht.sourceforge.io/">Irrlicht</a> support in order to be able to run this example.
88

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.
11-
_ChBody_ is an abstract class, and therefore we cannot instantiate it directly. Chrono provides various classes however we can
11+
_ChBody_ is an abstract class, and therefore we cannot instantiate it directly. Chrono provides various classes however, we can
1212
immediately use. The one we will use in this example is the _ChBodyEasyBox_ class. This is defined in the _chrono/physics/ChBodyEasy.h_ header.
13-
Let's first create a box and try to visualize it. Below is the function that constructs a box.
13+
Let's first create a box and try to visualize it. The function below does exactly that.
1414

1515
@code{.cpp}
1616
std::shared_ptr<chrono::ChBody> create_box(real_t xlength, real_t ylength, real_t zlength,
@@ -30,12 +30,14 @@ real_t density, bool create_visualization)
3030
@endcode
3131

3232

33-
The following is a helper for setting up the visualization
33+
The following is a helper for setting up the visualization. We need to build Chrono with Irrlicht support in order to be able to
34+
use it as a visualization engine. You can find more information about the
35+
visualisation system in Chrono at <a href="https://api.projectchrono.org/9.0.0/visualization_system.html">Visualization System</a>.
3436

3537
@code{.cpp}
3638
void prepare_visualization(chrono::irrlicht::ChVisualSystemIrrlicht& visual)
3739
{
38-
visual.SetWindowSize(WINDOW_WIDTH, WINDOW_WIDTH); //WINDOW_HEIGHT);
40+
visual.SetWindowSize(WINDOW_WIDTH, WINDOW_WIDTH);
3941
visual.SetWindowTitle(WINDOW_TITLE);
4042
visual.Initialize();
4143

@@ -47,8 +49,9 @@ visual.Initialize();
4749
}
4850
@endcode
4951

50-
Below is the main function:
51-
52+
Below is the main driver for this example.
53+
It creates a box and attaches it to a _ChSystem_ (see <a href="https://api.projectchrono.org/9.0.0/simulation_system.html">Simulation system</a> for more information).
54+
It also sets up a minimal visualization obejct _ChVisualSystemIrrlicht_ for the simulation and finally runs the simulation loop.
5255
@code{.cpp}
5356
int main()
5457
{

examples/example_13/example_13.cpp

Lines changed: 199 additions & 78 deletions
Original file line numberDiff line numberDiff line change
@@ -11,101 +11,222 @@
1111
#endif
1212

1313
#include "bitrl/bitrl_consts.h"
14+
#include "bitrl/rigid_bodies/chrono_robots/diff_drive_robot.h"
15+
1416
#include "chrono/core/ChRealtimeStep.h"
1517
#include "chrono/physics/ChSystemNSC.h"
18+
#include <chrono/physics/ChSystemSMC.h>
1619
#include <chrono/physics/ChBodyEasy.h>
20+
#include "chrono/assets/ChVisualSystem.h"
1721
#include <chrono_irrlicht/ChVisualSystemIrrlicht.h>
18-
#include "bitrl/rigid_bodies/chrono_robots/diff_drive_robot.h"
22+
// #include "chrono/assets/ChColorAsset.h"
23+
// #include "chrono/assets/ChLineShape.h"
24+
1925

2026
#include <filesystem>
2127
#include <string>
2228

23-
namespace example_13
24-
{
25-
using namespace bitrl;
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-
}
47-
48-
29+
#include "chrono/physics/ChSystemNSC.h"
30+
#include "chrono/physics/ChBodyEasy.h"
31+
#include "chrono/physics/ChLinkMotorRotationSpeed.h"
32+
#include "chrono/functions/ChFunctionConst.h"
4933

34+
#include "chrono_irrlicht/ChVisualSystemIrrlicht.h"
5035

51-
} // namespace example_13
36+
using namespace chrono;
37+
using namespace chrono::irrlicht;
5238

53-
int main()
39+
namespace
5440
{
55-
using namespace example_13;
56-
chrono::ChSystemNSC sys;
57-
sys.SetGravitationalAcceleration(chrono::ChVector3d(0, 0, -9.81));
58-
59-
sys.SetCollisionSystemType(chrono::ChCollisionSystem::Type::BULLET);
60-
chrono::ChCollisionModel::SetDefaultSuggestedEnvelope(0.0025);
61-
chrono::ChCollisionModel::SetDefaultSuggestedMargin(0.0025);
62-
63-
auto floor_mat = chrono_types::make_shared<chrono::ChContactMaterialNSC>();
64-
auto mfloor = chrono_types::make_shared<chrono::ChBodyEasyBox>(20, 20, 1, 1000, true, true, floor_mat);
65-
mfloor->SetPos(chrono::ChVector3d(0, 0, -1));
66-
mfloor->SetFixed(true);
67-
mfloor->GetVisualShape(0)->SetTexture(chrono::GetChronoDataFile("textures/concrete.jpg"));
68-
sys.Add(mfloor);
41+
void draw_world_axes(chrono::irrlicht::ChVisualSystemIrrlicht& vis,
42+
double scale = 1.0) {
43+
auto* driver = vis.GetVideoDriver();
44+
45+
// X axis (red)
46+
driver->draw3DLine(
47+
{0, 0, 0},
48+
{static_cast<float>(scale), 0, 0},
49+
irr::video::SColor(255, 255, 0, 0));
50+
51+
// Y axis (green)
52+
driver->draw3DLine(
53+
{0, 0, 0},
54+
{0, static_cast<float>(scale), 0},
55+
irr::video::SColor(255, 0, 255, 0));
56+
57+
// Z axis (blue)
58+
driver->draw3DLine(
59+
{0, 0, 0},
60+
{0, 0, static_cast<float>(scale)},
61+
irr::video::SColor(255, 0, 0, 255));
62+
}
63+
}
6964

70-
bitrl::rb::bitrl_chrono::CHRONO_DiffDriveRobot robot(sys,
71-
chrono::ChVector3d(0, 0, -0.45), chrono::QUNIT);
65+
int main() {
66+
67+
// -----------------------------
68+
// Create Chrono physical system
69+
// -----------------------------
70+
ChSystemNSC system;
71+
//ChSystemSMC system;
72+
system.SetGravitationalAcceleration(chrono::ChVector3d(0,0,-9.81));
73+
system.SetCollisionSystemType(chrono::ChCollisionSystem::Type::BULLET);
74+
75+
// -----------------------------
76+
// Create ground
77+
// -----------------------------
78+
auto material = chrono_types::make_shared<ChContactMaterialNSC>();
79+
80+
material->SetFriction(0.8f);
81+
material->SetRestitution(0.1f);
82+
auto floor = chrono_types::make_shared<chrono::ChBodyEasyBox>(
83+
5, 5, 0.1, // size
84+
1000, // density
85+
true, true, material); // visual + collision
86+
87+
floor->SetPos(chrono::ChVector3d(0,0,-0.05));
88+
floor->SetFixed(true);
89+
floor->EnableCollision(true);
90+
91+
system.Add(floor);
92+
93+
// -----------------------------
94+
// Robot chassis
95+
// -----------------------------
96+
double wheel_radius = 0.1;
97+
double chassis_height = 0.1;
98+
auto chassis = chrono_types::make_shared<ChBodyEasyBox>(
99+
0.5, 0.3, chassis_height,
100+
1000,
101+
true, true, material);
102+
103+
// 0,0,wheel_radius + chassis_height/2.0
104+
//chrono::ChVector3d pos(0,0,0.2);
105+
chrono::ChVector3d pos(0.0, 0.0, wheel_radius + chassis_height/2.0 + 0.01);
106+
chassis->SetPos(pos);
107+
108+
chrono::ChVector3d vel(0.5, 0.0, 0.0);
109+
chassis->SetPosDt(vel);
110+
system.Add(chassis);
111+
112+
// -----------------------------
113+
// Wheels
114+
// -----------------------------
115+
116+
double wheel_width = 0.05;
117+
//auto material = chrono_types::make_shared<chrono::ChContactMaterialNSC>();
118+
auto left_wheel = chrono_types::make_shared<chrono::ChBodyEasyCylinder>(
119+
chrono::ChAxis::Y,
120+
wheel_radius,
121+
wheel_width,
122+
1000,
123+
true, true, material);
124+
125+
auto right_wheel = chrono_types::make_shared<chrono::ChBodyEasyCylinder>(
126+
chrono::ChAxis::Y,
127+
wheel_radius,
128+
wheel_width,
129+
1000,
130+
true, true, material);
131+
132+
left_wheel->SetPos(chrono::ChVector3d(0, 0.2, 0.1));
133+
right_wheel->SetPos(chrono::ChVector3d(0,-0.2, 0.1));
134+
135+
system.Add(left_wheel);
136+
system.Add(right_wheel);
137+
138+
// -----------------------------
139+
// Motors (differential drive)
140+
// -----------------------------
141+
auto motor_left = chrono_types::make_shared<chrono::ChLinkMotorRotationSpeed>();
142+
auto motor_right = chrono_types::make_shared<chrono::ChLinkMotorRotationSpeed>();
143+
144+
chrono::ChQuaternion<> rot = chrono::QuatFromAngleX(chrono::CH_PI_2);
145+
motor_left->Initialize(
146+
left_wheel,
147+
chassis,
148+
chrono::ChFrame<>(chrono::ChVector3d(0,0.2,0.1), rot)
149+
);
150+
151+
motor_right->Initialize(
152+
right_wheel,
153+
chassis,
154+
chrono::ChFrame<>(chrono::ChVector3d(0,-0.2,0.1), rot)
155+
);
156+
157+
system.Add(motor_left);
158+
system.Add(motor_right);
159+
160+
// -----------------------------
161+
// Wheel speeds
162+
// -----------------------------
163+
auto speed_left = chrono_types::make_shared<chrono::ChFunctionConst>(-4.5);
164+
auto speed_right = chrono_types::make_shared<chrono::ChFunctionConst>(-4.5);
165+
166+
motor_left->SetSpeedFunction(speed_left);
167+
motor_right->SetSpeedFunction(speed_right);
168+
169+
double caster_radius = 0.05;
170+
auto caster = chrono_types::make_shared<ChBodyEasySphere>(
171+
caster_radius, // radius
172+
1000, // density
173+
true, // visualization
174+
true, // collision
175+
material);
176+
177+
caster->SetPos(chrono::ChVector3d(0.2, 0, 0.05));
178+
caster->EnableCollision(true);
179+
180+
system.Add(caster);
181+
182+
auto caster_joint = chrono_types::make_shared<ChLinkLockRevolute>();
183+
184+
caster_joint->Initialize(
185+
caster,
186+
chassis,
187+
ChFrame<>(chrono::ChVector3d(-0.25, 0, caster_radius), QUNIT));
188+
189+
system.Add(caster_joint);
190+
191+
floor->EnableCollision(true);
192+
chassis->EnableCollision(true);
193+
left_wheel->EnableCollision(true);
194+
right_wheel->EnableCollision(true);
195+
196+
// -----------------------------
197+
// Irrlicht visualization
198+
// -----------------------------
199+
chrono::irrlicht::ChVisualSystemIrrlicht vis;
200+
201+
vis.AttachSystem(&system);
202+
203+
vis.SetWindowSize(1280,720);
204+
vis.SetWindowTitle("Chrono Differential Drive Robot");
205+
206+
vis.Initialize();
207+
vis.AddSkyBox();
208+
vis.AddLogo();
209+
vis.AddSkyBox();
210+
vis.AddCamera({0, -2, 1}, {0, 0, 0});
211+
vis.AddTypicalLights();
212+
vis.BindAll();
213+
// 1 meter length
214+
// -----------------------------
215+
// Simulation loop
216+
// -----------------------------
217+
double step_size = 0.002;
72218

73-
robot.init();
74-
robot.set_motor_speed(bitrl::consts::maths::PI, 0);
75-
robot.set_motor_speed(bitrl::consts::maths::PI, 1);
219+
while (vis.Run()) {
76220

77-
chrono::irrlicht::ChVisualSystemIrrlicht visual;
78-
prepare_visualization(visual);
79-
visual.AttachSystem(&sys);
221+
vis.BeginScene();
222+
vis.Render();
223+
draw_world_axes(vis, 2.0);
80224

81-
// Simulation loop
225+
auto position = chassis -> GetPos();
226+
std::cout << position <<std::endl;
82227

83-
// Timer for enforcing soft real-time
84-
chrono::ChRealtimeStepTimer realtime_timer;
85-
86-
// bool removed = false;
87-
while (visual.Run()) {
88-
// Irrlicht must prepare frame to draw
89-
visual.BeginScene();
90-
91-
// Irrlicht now draws simple lines in 3D world representing a
92-
// skeleton of the mechanism, in this instant:
93-
//
94-
// .. draw items belonging to Irrlicht scene, if any
95-
visual.Render();
96-
// .. draw a grid
97-
tools::drawGrid(&visual, 0.5, 0.5);
98-
// .. draw GUI items belonging to Irrlicht screen, if any
99-
visual.GetGUIEnvironment()->drawAll();
100-
101-
// ADVANCE SYSTEM STATE BY ONE STEP
102-
sys.DoStepDynamics(DT);
103-
104-
// Enforce soft real-time
105-
realtime_timer.Spin(DT);
106-
107-
// Irrlicht must finish drawing the frame
108-
visual.EndScene();
228+
system.DoStepDynamics(step_size);
229+
vis.EndScene();
109230
}
110231

111232
return 0;

0 commit comments

Comments
 (0)