Skip to content

Commit 0715852

Browse files
committed
Update example 11 notes
1 parent 23fb822 commit 0715852

6 files changed

Lines changed: 258 additions & 65 deletions

File tree

examples/example_11/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_11/box_1.png

42.6 KB
Loading

examples/example_11/example_11.cpp

Lines changed: 100 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,22 @@
77
*
88
*/
99

10+
#include "bitrl/bitrl_config.h"
11+
12+
#ifdef BITRL_CHRONO
13+
1014
#include "bitrl/bitrl_types.h"
15+
16+
#ifdef BITRL_LOG
17+
#define BOOST_LOG_DYN_LINK
18+
#include <boost/log/trivial.hpp>
19+
#endif
20+
21+
#include <chrono/physics/ChSystemSMC.h>
22+
#include <chrono/physics/ChBodyEasy.h>
23+
#include <chrono_irrlicht/ChVisualSystemIrrlicht.h>
24+
25+
1126
#include "bitrl/utils/geometry/geom_point.h"
1227
#include "bitrl/utils/trajectory/line_segment_link.h"
1328
#include "bitrl/utils/trajectory/waypoint.h"
@@ -24,76 +39,110 @@
2439
namespace example_11
2540
{
2641

27-
using bitrl::Null;
28-
using bitrl::real_t;
29-
using bitrl::uint_t;
30-
using bitrl::utils::geom::GeomPoint;
31-
using bitrl::utils::trajectory::LineSegmentLink;
32-
using bitrl::utils::trajectory::WayPoint;
33-
using bitrl::utils::trajectory::WaypointTrajectory;
42+
using namespace bitrl;
43+
using namespace chrono::irrlicht;
44+
45+
// constants we will be using further below
46+
const uint_t WINDOW_HEIGHT = 800;
47+
const uint_t WINDOW_WIDTH = 1024;
48+
const real_t DT = 0.0001;
49+
const real_t SIM_TIME = 5.0;
50+
const std::string WINDOW_TITLE( "Example 11");
3451

35-
struct LineSegmentData
52+
void prepare_visualization(chrono::irrlicht::ChVisualSystemIrrlicht& visual)
53+
{
54+
visual.SetWindowSize(WINDOW_WIDTH, WINDOW_WIDTH); //WINDOW_HEIGHT);
55+
visual.SetWindowTitle(WINDOW_TITLE);
56+
visual.Initialize();
57+
58+
visual.AddLogo();
59+
visual.AddSkyBox();
60+
visual.AddCamera({0, -2, 1}, {0, 0, 0});
61+
visual.AddTypicalLights();
62+
visual.BindAll();
63+
}
64+
65+
std::shared_ptr<chrono::ChBody> create_box(real_t xlength, real_t ylength, real_t zlength,
66+
real_t density, bool create_visualization)
3667
{
37-
/// \brief The maximum velocity
38-
/// allowed on the edge
39-
real_t Vmax{0.0};
4068

41-
/// \brief The orientation of the
42-
/// segment with respect to the global coordinate
43-
/// frame. This may also dictate the orientation
44-
/// that a reference vehicle may have on the segment
45-
real_t theta{0.0};
69+
// build the chassis of the robot
70+
auto box = chrono_types::make_shared<chrono::ChBodyEasyBox>(xlength, ylength, zlength,
71+
density, create_visualization);
72+
box -> SetMass(1.0);
73+
box -> SetPos(chrono::ChVector3d(0.0, 0.0, 0.22));
74+
75+
// allow the chassis to move
76+
box -> SetFixed(true);
77+
return box;
78+
79+
}
4680

47-
/// \brief The angular velocity on the segment
48-
real_t w{0.0};
4981

50-
/// \brief The linear velocity on the segement
51-
real_t v{0.0};
52-
};
5382

54-
typedef LineSegmentLink<2, Null, LineSegmentData> link_type;
55-
typedef link_type::w_point_type w_point_type;
5683

5784
} // namespace example_11
5885

86+
/*
5987
int main()
6088
{
6189
6290
using namespace example_11;
91+
chrono::ChSystemSMC sys;
6392
64-
// create a trajector of 4 links
65-
// the waypoints carry not data
66-
// but the the links
67-
// carry objects of type LineSegmentData
68-
WaypointTrajectory<link_type> trajectory(3);
93+
// build the body and add it to the system
94+
// we want to simulate
95+
auto box = create_box(1.0, 1.0, 1.0, 1.0, true);
96+
sys.Add(box);
6997
70-
// start adding the links
71-
// that form the trajectory
72-
w_point_type p0(GeomPoint<2>({0.0, 0.0}), static_cast<uint_t>(0));
73-
w_point_type p1(GeomPoint<2>({1.0, 0.0}), static_cast<uint_t>(1));
74-
LineSegmentData data{1.0, 0.0, 0.0, 0.95};
98+
// create the object that handles the visualization
99+
chrono::irrlicht::ChVisualSystemIrrlicht visual;
100+
prepare_visualization(visual);
101+
visual.AttachSystem(&sys);
75102
76-
link_type l0(p0, p1, static_cast<uint_t>(0), data);
77-
78-
w_point_type p2(GeomPoint<2>({1.0, 0.0}), static_cast<uint_t>(2));
79-
w_point_type p3(GeomPoint<2>({2.0, 2.0}), static_cast<uint_t>(3));
80-
data.theta = bitrl::utils::unit_converter::degrees_to_rad(45.0);
81-
link_type l1(p2, p3, static_cast<uint_t>(1), data);
103+
while (visual.Run())
104+
{
82105
83-
w_point_type p4(GeomPoint<2>({2.0, 2.0}), static_cast<uint_t>(4));
84-
w_point_type p5(GeomPoint<2>({2.0, 3.0}), static_cast<uint_t>(5));
85-
data.theta = bitrl::utils::unit_converter::degrees_to_rad(90.0);
86-
link_type l2(p2, p3, static_cast<uint_t>(2), data);
106+
// Irrlicht must prepare frame to draw
107+
visual.BeginScene();
87108
88-
trajectory[0] = l0;
89-
trajectory[1] = l1;
90-
trajectory[2] = l2;
109+
// .. draw items belonging to Irrlicht scene, if any
110+
visual.Render();
91111
92-
std::cout << "Trajectory number of links: " << trajectory.size() << std::endl;
112+
// .. draw a grid
113+
tools::drawGrid(&visual, 0.5, 0.5);
93114
94-
for (const auto &link : trajectory)
95-
{
96-
std::cout << link.get_id() << std::endl;
115+
// Irrlicht must finish drawing the frame
116+
visual.EndScene();
97117
}
98-
return 0;
118+
119+
}*/
120+
121+
int main()
122+
{
123+
124+
using namespace example_11;
125+
chrono::ChSystemSMC sys;
126+
127+
// build the body and add it to the system
128+
// we want to simulate
129+
auto box = create_box(1.0, 1.0, 1.0, 1.0, true);
130+
sys.Add(box);
131+
132+
auto position = box -> GetPos();
133+
BOOST_LOG_TRIVIAL(info)<<"Position of CoM: "<<position;
134+
BOOST_LOG_TRIVIAL(info)<<"Linear velocity of CoM: "<<box -> GetLinVel();
135+
auto pos_local = box->TransformPointParentToLocal(position);
136+
BOOST_LOG_TRIVIAL(info)<<"local position of CoM: "<<pos_local;
99137
}
138+
#else
139+
#include <iostream>
140+
int main()
141+
{
142+
std::cerr<<"You need PROJECTCHRONO configured with "
143+
<<"bitrl in order to run this example "
144+
<<"Reconfigure bitrl and set ENABLE_CHRONO=ON"<<std::endl;
145+
return 1;
146+
}
147+
148+
#endif

examples/example_11/example_11.md

Lines changed: 137 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,137 @@
1+
\page bitrl_example_11 BitRL Example 11 Create a rigid body in Chrono
2+
3+
The <a href="https://github.com/projectchrono/chrono">Chrono</a> library is the main library that _bitrl_ is using in order
4+
to simulate robots and create environments for reinforcement learning agents. As such, knowing your way around
5+
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.
7+
8+
You should have compiled Chrono with <a href="https://irrlicht.sourceforge.io/">Irrlicht</a> support.
9+
10+
11+
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>
12+
class. You can also find this <a href="https://api.projectchrono.org/9.0.0/rigid_bodies.html"> Rigid Bodies</a> helpful.
13+
_ChBody_ is an abstract class, and therefore we cannot instantiate it directly. Chrono provides various classes however we can
14+
immediately use. The one we will use in this example is the _ChBodyEasyBox_ class. This is defined in the _<chrono/physics/ChBodyEasy.h>_ header.
15+
Let's first create a box and try to visualize it. Below is the function that constructs a box.
16+
17+
@code{.cpp}
18+
std::shared_ptr<chrono::ChBody> create_box(real_t xlength, real_t ylength, real_t zlength,
19+
real_t density, bool create_visualization)
20+
{
21+
22+
// build the chassis of the robot
23+
auto box = chrono_types::make_shared<chrono::ChBodyEasyBox>(xlength, ylength, zlength,
24+
density, create_visualization);
25+
box -> SetMass(1.0);
26+
box -> SetPos(chrono::ChVector3d(0.0, 0.0, 0.22));
27+
28+
// allow the chassis to move
29+
box -> SetFixed(true);
30+
return box;
31+
32+
}
33+
@endcode
34+
35+
36+
The following is a helper for setting up the visualization
37+
38+
@code{.cpp}
39+
void prepare_visualization(chrono::irrlicht::ChVisualSystemIrrlicht& visual)
40+
{
41+
visual.SetWindowSize(WINDOW_WIDTH, WINDOW_WIDTH); //WINDOW_HEIGHT);
42+
visual.SetWindowTitle(WINDOW_TITLE);
43+
visual.Initialize();
44+
45+
visual.AddLogo();
46+
visual.AddSkyBox();
47+
visual.AddCamera({0, -2, 1}, {0, 0, 0});
48+
visual.AddTypicalLights();
49+
visual.BindAll();
50+
}
51+
@endcode
52+
53+
Below is the main function:
54+
55+
@code{.cpp}
56+
int main()
57+
{
58+
59+
using namespace example_11;
60+
61+
62+
chrono::ChSystemSMC sys;
63+
64+
// build the body and add it to the system
65+
// we want to simulate
66+
auto box = create_box(1.0, 1.0, 1.0, 1.0, true);
67+
sys.Add(box);
68+
69+
// create the object that handles the visualization
70+
chrono::irrlicht::ChVisualSystemIrrlicht visual;
71+
prepare_visualization(visual);
72+
visual.AttachSystem(&sys);
73+
74+
while (visual.Run())
75+
{
76+
77+
// Irrlicht must prepare frame to draw
78+
visual.BeginScene();
79+
80+
// .. draw items belonging to Irrlicht scene, if any
81+
visual.Render();
82+
83+
// .. draw a grid
84+
tools::drawGrid(&visual, 0.5, 0.5);
85+
86+
// Irrlicht must finish drawing the frame
87+
visual.EndScene();
88+
}
89+
}
90+
@endcode
91+
92+
Compiling and running the code will not do anything exciting as can be verified by the image below
93+
94+
<img src="./box_1.png" width="600" height="337"/>
95+
96+
Rigid bodies in Chrono have a lot of attributes and the API is quite rich. Here are some of the method that we are typically interested
97+
98+
@code{.cpp}
99+
100+
// position of the CoM
101+
GetPos()
102+
103+
// linear velocity of the CoM
104+
GetLinVel()
105+
106+
// linear acceleration of the CoM
107+
GetLinAcc()
108+
@endcode
109+
110+
Let's change the main function so that it prints the position and the velocity of the box:
111+
112+
@code{.cpp}
113+
int main()
114+
{
115+
116+
using namespace example_11;
117+
chrono::ChSystemSMC sys;
118+
119+
// build the body and add it to the system
120+
// we want to simulate
121+
auto box = create_box(1.0, 1.0, 1.0, 1.0, true);
122+
sys.Add(box);
123+
124+
BOOST_LOG_TRIVIAL(info)<<"Position of CoM: "<<box -> GetPos();
125+
BOOST_LOG_TRIVIAL(info)<<"Linear velocity of CoM: "<<box -> GetLinVel();
126+
127+
}
128+
@endcode
129+
130+
This prints the following
131+
132+
@code
133+
[2026-02-07 11:40:39.051115] [0x00007fdfbd563800] [info] Position of CoM: 0 0 0.22
134+
[2026-02-07 11:40:39.051130] [0x00007fdfbd563800] [info] Linear velocity of CoM: 0 0 0
135+
@endcode
136+
137+
A question to ask is to which coordinate system the output refers to, The world reference frame or the one local to the body?

examples/example_14/example_14.md

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,17 @@
1-
\page bitrl_example_14 Example 14 Create an environment using Chrono
1+
\page bitrl_example_14 BitRL Example 14 Create an RL environment using Chrono
22

33
In this example we will create an environment for reinforcement learning based
44
on the <a href="https://github.com/projectchrono/chrono">Chrono</a> library.
55
Specifically, we will create an environment that includes a <a href="https://en.wikipedia.org/wiki/Differential_wheeled_robot">differential drive system</a>.
66
Note that the model we will create will not be of high fidelity as the purpose of the example is show how
77
to use Chrono to create reinforcement learning environments.
88

9-
In order to be able to run this example you need to configure bitrl with Chrono support. You will also
9+
In order to be able to run this example you need to configure _bitrl_ with Chrono support. You will also
1010
need the <a href="https://irrlicht.sourceforge.io/">Irrlicht</a> library for visualising the robot.
1111

1212
The following image shows an image of the environment we will create
1313

14-
| ![Environment overview](./env.png) |
15-
|:------------------------------------:|
16-
17-
18-
## Create the robot
14+
<img src="./env.png" width="600" height="337"/>
1915

2016
Below is the class that handles the robot model.
2117

0 commit comments

Comments
 (0)