|
11 | 11 | #endif |
12 | 12 |
|
13 | 13 | #include "bitrl/bitrl_consts.h" |
| 14 | +#include "bitrl/rigid_bodies/chrono_robots/diff_drive_robot.h" |
| 15 | + |
14 | 16 | #include "chrono/core/ChRealtimeStep.h" |
15 | 17 | #include "chrono/physics/ChSystemNSC.h" |
| 18 | +#include <chrono/physics/ChSystemSMC.h> |
16 | 19 | #include <chrono/physics/ChBodyEasy.h> |
| 20 | +#include "chrono/assets/ChVisualSystem.h" |
17 | 21 | #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 | + |
19 | 25 |
|
20 | 26 | #include <filesystem> |
21 | 27 | #include <string> |
22 | 28 |
|
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" |
49 | 33 |
|
| 34 | +#include "chrono_irrlicht/ChVisualSystemIrrlicht.h" |
50 | 35 |
|
51 | | -} // namespace example_13 |
| 36 | +using namespace chrono; |
| 37 | +using namespace chrono::irrlicht; |
52 | 38 |
|
53 | | -int main() |
| 39 | +namespace |
54 | 40 | { |
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 | +} |
69 | 64 |
|
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; |
72 | 218 |
|
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()) { |
76 | 220 |
|
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); |
80 | 224 |
|
81 | | - // Simulation loop |
| 225 | + auto position = chassis -> GetPos(); |
| 226 | + std::cout << position <<std::endl; |
82 | 227 |
|
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(); |
109 | 230 | } |
110 | 231 |
|
111 | 232 | return 0; |
|
0 commit comments