-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexample_12.cpp
More file actions
235 lines (187 loc) · 6.25 KB
/
Copy pathexample_12.cpp
File metadata and controls
235 lines (187 loc) · 6.25 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
#include "bitrl/bitrl_config.h"
#ifdef BITRL_CHRONO
#include "bitrl/bitrl_types.h"
#include "bitrl/bitrl_consts.h"
#ifdef BITRL_LOG
#define BOOST_LOG_DYN_LINK
#include <boost/log/trivial.hpp>
#endif
#include <chrono/physics/ChSystemNSC.h>
#include <chrono/physics/ChBodyEasy.h>
#include "chrono/assets/ChVisualSystem.h"
#include <chrono_irrlicht/ChVisualSystemIrrlicht.h>
#include <chrono/physics/ChLinkMotorRotationSpeed.h>
#include <chrono/functions/ChFunctionConst.h>
#include <memory>
namespace example_12
{
using namespace chrono;
using namespace chrono::irrlicht;
using namespace bitrl;
const real_t WHEEL_RADIUS = 0.1;
const real_t CHASSIS_HEIGHT = 0.1;
const real_t WHEEL_WIDTH = 0.05;
const real_t CASTER_RADIUS = 0.05;
void draw_world_axes(chrono::irrlicht::ChVisualSystemIrrlicht& vis,
real_t scale = 1.0) {
auto* driver = vis.GetVideoDriver();
// X axis (red)
driver->draw3DLine(
{0, 0, 0},
{static_cast<float>(scale), 0, 0},
irr::video::SColor(255, 255, 0, 0));
// Y axis (green)
driver->draw3DLine(
{0, 0, 0},
{0, static_cast<float>(scale), 0},
irr::video::SColor(255, 0, 255, 0));
// Z axis (blue)
driver->draw3DLine(
{0, 0, 0},
{0, 0, static_cast<float>(scale)},
irr::video::SColor(255, 0, 0, 255));
}
auto build_generic_material()
{
auto material = chrono_types::make_shared<ChContactMaterialNSC>();
material->SetFriction(0.8f);
material->SetRestitution(0.1f);
return material;
}
auto build_floor(std::shared_ptr<ChContactMaterialNSC> material)
{
auto floor = chrono_types::make_shared<chrono::ChBodyEasyBox>(
5, 5, 0.1, // size
1000, // density
true, true, material); // visual + collision
floor->SetPos(chrono::ChVector3d(0,0,-0.05));
floor->SetFixed(true);
floor->EnableCollision(true);
return floor;
}
auto build_chassis(std::shared_ptr<ChContactMaterialNSC> material)
{
auto chassis = chrono_types::make_shared<ChBodyEasyBox>(
0.5, 0.3, CHASSIS_HEIGHT,
1000,
true, true, material);
chrono::ChVector3d pos(0.0, 0.0, WHEEL_RADIUS + CHASSIS_HEIGHT/2.0 + 0.01);
chassis->SetPos(pos);
chrono::ChVector3d vel(0.5, 0.0, 0.0);
chassis->SetPosDt(vel);
chassis->EnableCollision(true);
return chassis;
}
auto build_wheel(std::shared_ptr<ChContactMaterialNSC> material, const chrono::ChVector3d& pos)
{
auto wheel = chrono_types::make_shared<chrono::ChBodyEasyCylinder>(
chrono::ChAxis::Y,
WHEEL_RADIUS,
WHEEL_WIDTH,
1000,
true, true, material);
wheel->SetPos(pos);
wheel->EnableCollision(true);
return wheel;
}
auto build_motor(std::shared_ptr<ChBodyEasyCylinder> wheel,
std::shared_ptr<ChBodyEasyBox> chassis,
std::shared_ptr<ChFunctionConst> speed_func,
const chrono::ChVector3d& pos)
{
auto motor = chrono_types::make_shared<chrono::ChLinkMotorRotationSpeed>();
chrono::ChQuaternion<> rot = chrono::QuatFromAngleX(chrono::CH_PI_2);
motor->Initialize(
wheel,
chassis,
chrono::ChFrame<>(pos, rot)
);
motor->SetSpeedFunction(speed_func);
return motor;
}
auto build_caster_wheel(std::shared_ptr<ChContactMaterialNSC> material)
{
auto caster = chrono_types::make_shared<ChBodyEasySphere>(
CASTER_RADIUS, // radius
1000, // density
true, // visualization
true, // collision
material);
caster->SetPos(chrono::ChVector3d(0.2, 0, 0.05));
caster->EnableCollision(true);
return caster;
}
auto build_caster_joint(std::shared_ptr<ChBodyEasySphere> caster,
std::shared_ptr<ChBodyEasyBox> chassis)
{
auto caster_joint = chrono_types::make_shared<ChLinkLockRevolute>();
caster_joint->Initialize(
caster,
chassis,
ChFrame<>(chrono::ChVector3d(-0.25, 0, CASTER_RADIUS), QUNIT));
return caster_joint;
}
}
int main() {
using namespace example_12;
ChSystemNSC system;
system.SetGravitationalAcceleration(chrono::ChVector3d(0,0,-bitrl::consts::maths::G));
system.SetCollisionSystemType(chrono::ChCollisionSystem::Type::BULLET);
// create a generic material to use
auto material = example_12::build_generic_material();
// build floor
auto floor = example_12::build_floor(material);
system.Add(floor);
// build chassis
auto chassis = example_12::build_chassis(material);
system.Add(chassis);
// build wheels
auto left_wheel = example_12::build_wheel(material,chrono::ChVector3d(0, 0.2, 0.1));
auto right_wheel = example_12::build_wheel(material,chrono::ChVector3d(0,-0.2, 0.1));
system.Add(left_wheel);
system.Add(right_wheel);
// build motor
auto motor_left = example_12::build_motor(left_wheel, chassis,
chrono_types::make_shared<chrono::ChFunctionConst>(-4.5), chrono::ChVector3d(0,0.2,0.1));
auto motor_right = build_motor(right_wheel, chassis,
chrono_types::make_shared<chrono::ChFunctionConst>(-4.5), chrono::ChVector3d(0,-0.2,0.1));
system.Add(motor_left);
system.Add(motor_right);
auto caster = example_12::build_caster_wheel(material);
system.Add(caster);
auto caster_joint = example_12::build_caster_joint(caster, chassis);
system.Add(caster_joint);
// create the visualization system
chrono::irrlicht::ChVisualSystemIrrlicht vis;
vis.AttachSystem(&system);
vis.SetWindowSize(1280,720);
vis.SetWindowTitle("Example 12 Differential Drive Robot");
vis.Initialize();
vis.AddSkyBox();
vis.AddLogo();
vis.AddSkyBox();
vis.AddCamera({0, -2, 1}, {0, 0, 0});
vis.AddTypicalLights();
vis.BindAll();
real_t step_size = 0.002;
while (vis.Run()) {
vis.BeginScene();
vis.Render();
draw_world_axes(vis, 2.0);
auto position = chassis -> GetPos();
std::cout << position <<std::endl;
system.DoStepDynamics(step_size);
vis.EndScene();
}
return 0;
}
#else
#include <iostream>
int main()
{
std::cerr<<"You need PROJECTCHRONO configured with "
<<"bitrl in order to run this example "
<<"Reconfigure bitrl and set ENABLE_CHRONO=ON"<<std::endl;
return 1;
}
#endif