Rolling and spinning friction example (demo_MBS_friction.cpp)

A benchmark that show the feature of spinning friction (aka 'drilling' friction) and rolling friction.

These types of frictions are useful for rolling objects like wheels, spheres etc. and can be optionally turned on.

This tutorial shows how to:

  • enable rolling and spinning friction.
  • impose initial velocity and angular velocity to objects.
// =============================================================================
// PROJECT CHRONO - http://projectchrono.org
//
// Copyright (c) 2014 projectchrono.org
// All rights reserved.
//
// Use of this source code is governed by a BSD-style license that can be found
// in the LICENSE file at the top level of the distribution and at
// http://projectchrono.org/license-chrono.txt.
//
// =============================================================================
// Authors: Alessandro Tasora
// =============================================================================
//
// Demo code about
// - using rolling friction (not only sliding and static friction, available in
// all objects by default)
// - optional sharing of some assets (visualization stuff)
//
// =============================================================================
#include "chrono/physics/ChSystemNSC.h"
#include "chrono/physics/ChBodyEasy.h"
#include "chrono/utils/ChUtilsCreators.h"
#include "chrono/core/ChRealtimeStep.h"
#include "chrono_irrlicht/ChVisualSystemIrrlicht.h"
// Use the namespaces of Chrono
using namespace chrono;
using namespace chrono::irrlicht;
int main(int argc, char* argv[]) {
GetLog() << "Copyright (c) 2017 projectchrono.org\nChrono version: " << CHRONO_VERSION << "\n\n";
// Create a physical system
// Create all the rigid bodies.
double mradius = 0.5;
// Create a shared visualization material.
auto sph_vis_mat = chrono_types::make_shared<ChVisualMaterial>();
sph_vis_mat->SetKdTexture(GetChronoDataFile("textures/bluewhite.png"));
// Create some spheres that roll horizontally, with increasing rolling friction values
for (int bi = 0; bi < 10; bi++) {
auto mat = chrono_types::make_shared<ChMaterialSurfaceNSC>();
mat->SetFriction(0.4f);
mat->SetRollingFriction(((float)bi / 10) * 0.05f);
auto sphereBody = chrono_types::make_shared<ChBodyEasySphere>(mradius, // radius size
1000, // density
true, // visualization?
true, // collision?
mat); // contact material
// Set some properties
sphereBody->SetPos(ChVector<>(-7, mradius - 0.5, -5 + bi * mradius * 2.5));
sphereBody->GetVisualShape(0)->SetMaterial(0, sph_vis_mat);
// Set initial speed: rolling in horizontal direction
double initial_angspeed = 10;
double initial_linspeed = initial_angspeed * mradius;
sphereBody->SetWvel_par(ChVector<>(0, 0, -initial_angspeed));
sphereBody->SetPos_dt(ChVector<>(initial_linspeed, 0, 0));
// Add to the system
sys.Add(sphereBody);
}
// Create some spheres that spin on place, for a 'drilling friction' case, with increasing spinning friction values
for (int bi = 0; bi < 10; bi++) {
auto mat = chrono_types::make_shared<ChMaterialSurfaceNSC>();
mat->SetFriction(0.4f);
mat->SetSpinningFriction(((float)bi / 10) * 0.02f);
auto sphereBody = chrono_types::make_shared<ChBodyEasySphere>(mradius, // radius size
1000, // density
true, // visualization?
true, // collision?
mat); // contact material
// Set some properties
sphereBody->SetPos(ChVector<>(-8, 1 + mradius - 0.5, -5 + bi * mradius * 2.5));
sphereBody->GetVisualShape(0)->SetMaterial(0, sph_vis_mat);
// Set initial speed: spinning in vertical direction
sphereBody->SetWvel_par(ChVector<>(0, 20, 0));
// Add to the system
sys.Add(sphereBody);
// Notes:
// - setting nonzero spinning friction and/or setting nonzero rolling friction
// affects the speed of the solver (each contact eats 2x of CPU time repsect to the
// case of simple sliding/staic contact)
// - avoid using zero spinning friction with nonzero rolling friction.
}
// Create a container fixed to ground
auto bin = chrono_types::make_shared<ChBody>();
bin->SetPos(ChVector<>(0, -1, 0));
bin->SetBodyFixed(true);
bin->SetCollide(true);
// Set rolling and friction coefficients for the container.
// By default, the composite material will use the minimum value for an interacting collision pair.
auto bin_mat = chrono_types::make_shared<ChMaterialSurfaceNSC>();
bin_mat->SetRollingFriction(1);
bin_mat->SetSpinningFriction(1);
// Create a shared visualization material.
auto bin_vis_mat = chrono_types::make_shared<ChVisualMaterial>();
bin_vis_mat->SetKdTexture(GetChronoDataFile("textures/blue.png"));
// Add collision geometry and visualization shapes for the floor and the 4 walls
bin->GetCollisionModel()->ClearModel();
utils::AddBoxGeometry(bin.get(), bin_mat, ChVector<>(20, 1, 20) / 2.0, ChVector<>(0, 0, 0), QUNIT, true,
bin_vis_mat);
utils::AddBoxGeometry(bin.get(), bin_mat, ChVector<>(1, 2, 20.99) / 2.0, ChVector<>(-10, 1, 0), QUNIT, true,
bin_vis_mat);
utils::AddBoxGeometry(bin.get(), bin_mat, ChVector<>(1, 2, 20.99) / 2.0, ChVector<>(10, 1, 0), QUNIT, true,
bin_vis_mat);
utils::AddBoxGeometry(bin.get(), bin_mat, ChVector<>(20.99, 2, 1) / 2.0, ChVector<>(0, 1, -10), QUNIT, true,
bin_vis_mat);
utils::AddBoxGeometry(bin.get(), bin_mat, ChVector<>(20.99, 2, 1) / 2.0, ChVector<>(0, 1, 10), QUNIT, true,
bin_vis_mat);
bin->GetCollisionModel()->BuildModel();
sys.Add(bin);
// Create the Irrlicht visualization system
auto vis = chrono_types::make_shared<ChVisualSystemIrrlicht>();
vis->AttachSystem(&sys);
vis->SetWindowSize(800, 600);
vis->SetWindowTitle("Rolling and spinning friction");
vis->Initialize();
vis->AddLogo();
vis->AddSkyBox();
vis->AddCamera(ChVector<>(0, 14, -20));
vis->AddLight(ChVector<>(30.f, 100.f, 30.f), 290, ChColor(0.7f, 0.7f, 0.7f));
vis->AddLight(ChVector<>(-30.f, 100.f, -30.f), 190, ChColor(0.7f, 0.8f, 0.8f));
// Modify some setting of the physical system for the simulation
// Simulation loop
double timestep = 0.005;
ChRealtimeStepTimer realtime_timer;
while (vis->Run()) {
vis->BeginScene();
vis->Render();
vis->EndScene();
sys.DoStepDynamics(timestep);
realtime_timer.Spin(timestep);
}
return 0;
}
std::string GetChronoDataFile(const std::string &filename)
Obtain the complete path to the specified filename, given relative to the Chrono data directory (thre...
Definition: ChGlobal.cpp:95
void Add(std::shared_ptr< ChPhysicsItem > item)
Attach an arbitrary ChPhysicsItem (e.g.
Definition: ChSystem.cpp:179
void AddSkyBox(const std::string &texture_dir=GetChronoDataFile("skybox/"))
Add a sky box in a 3D scene.
Definition: ChVisualSystemIrrlicht.cpp:299
virtual void Initialize()
Initialize the visualization system.
Definition: ChVisualSystemIrrlicht.cpp:160
ChLog & GetLog()
Global function to get the current ChLog object.
Definition: ChLog.cpp:39
bool Run()
Run the Irrlicht device.
Definition: ChVisualSystemIrrlicht.cpp:217
irr::scene::ILightSceneNode * AddLight(const ChVector<> &pos, double radius, ChColor color=ChColor(0.7f, 0.7f, 0.7f))
Add a point light to the scene.
Definition: ChVisualSystemIrrlicht.cpp:344
void AddCamera(const ChVector<> &pos, ChVector<> targ=VNULL)
Add a camera in an Irrlicht 3D scene.
Definition: ChVisualSystemIrrlicht.cpp:268
virtual void BeginScene(bool backBuffer=true, bool zBuffer=true, ChColor color=ChColor(0, 0, 0))
Clean the canvas at the beginning of each animation frame.
Definition: ChVisualSystemIrrlicht.cpp:501
Namespace with classes for the Irrlicht module.
Definition: ChApiIrr.h:48
Class for a timer which attempts to enforce soft real-time.
Definition: ChRealtimeStep.h:25
Accelerated Projected Gradient Descent.
void Spin(double step)
Call this function INSIDE the simulation loop, just ONCE per loop (preferably as the last call in the...
Definition: ChRealtimeStep.h:34
Definition of a visual color.
Definition: ChColor.h:26
const ChQuaternion< double > QUNIT(1., 0., 0., 0.)
Constant unit quaternion: {1, 0, 0, 0} , corresponds to no rotation (diagonal rotation matrix)
Definition: ChQuaternion.h:451
Definition of general purpose 3d vector variables, such as points in 3D.
Definition: ChVector.h:35
void SetSolverType(ChSolver::Type type)
Choose the solver type, to be used for the simultaneous solution of the constraints in dynamical simu...
Definition: ChSystem.cpp:255
void AddBoxGeometry(ChBody *body, std::shared_ptr< ChMaterialSurface > material, const ChVector<> &size, const ChVector<> &pos, const ChQuaternion<> &rot, bool visualization, std::shared_ptr< ChVisualMaterial > vis_material)
Add a box collision shape and optionally a corresponding visualization asset to the specified body.
Definition: ChUtilsCreators.cpp:84
int DoStepDynamics(double step_size)
Advances the dynamical simulation for a single step, of length step_size.
Definition: ChSystem.cpp:1422
virtual void EndScene()
End the scene draw at the end of each animation frame.
Definition: ChVisualSystemIrrlicht.cpp:546
void SetSolverMaxIterations(int max_iters)
Set the maximum number of iterations, if using an iterative solver.
Definition: ChSystem.cpp:229
void SetWindowTitle(const std::string &win_title)
Set the windoiw title (default "").
Definition: ChVisualSystemIrrlicht.cpp:124
virtual void AttachSystem(ChSystem *sys) override
Attach another Chrono system to the run-time visualization system.
Definition: ChVisualSystemIrrlicht.cpp:144
virtual void Render()
Draw all 3D shapes and GUI elements at the current frame.
Definition: ChVisualSystemIrrlicht.cpp:556
Main namespace for the Chrono package.
Definition: ChBarrelShape.cpp:17
void AddLogo(const std::string &logo_filename=GetChronoDataFile("logo_chronoengine_alpha.png"))
Add a logo in a 3D scene.
Definition: ChVisualSystemIrrlicht.cpp:260
Class for a physical system in which contact is modeled using a non-smooth (complementarity-based) me...
Definition: ChSystemNSC.h:29
void SetWindowSize(unsigned int width, unsigned int height)
Set the window size (default 640x480).
Definition: ChVisualSystemIrrlicht.cpp:120