Skip to content
bloc0105 edited this page Dec 1, 2024 · 2 revisions

Forces, Dynamics, and Motion

Applying Forces

Let's use Chrono to perform a real simulation of something. This is a real example coming from my old Engineering classes, so it might be something you'll encounter in the future. What if we wanted to simulate launching a homemade rocket, then letting it fall down back to the ground.

Let's start by setting up our rocket parameters:

  • We'll use this rocket. It has a mass of 184g.
  • The engine recommended is the D12-5 engine so let's use that.
  • For simplicty we'll ignore wind and other factors, we'll also assume the rocket is just going up and down.

The Code

#include "chrono/physics/ChSystemNSC.h" // Include Chrono NSC physics system
#include "chrono/physics/ChBody.h"      // Include Chrono body
#include <iostream>                     // Include for console I/O

int main()
{
    std::shared_ptr<chrono::ChBody> the_rocket = chrono_types::make_shared<chrono::ChBody>();
    the_rocket->SetPos(chrono::ChVector3d(0.0, 0.0, 0.0));
    the_rocket->SetMass(0.184);
    
    chrono::ChVector3d impulse_force(0, 30, 0);
    chrono::ChVector3d steady_force(0, 10, 0);
    double impulse_time = 0.3;
    double steady_time = 1.6;

    chrono::ChSystemNSC physics_system = chrono::ChSystemNSC();

    physics_system.Add(the_rocket);

    double timestep = 0.01;
    size_t time_counter = 0;

    while (the_rocket->GetPos()[1] >= 0 || time_counter == 0)
    {
        double sim_time = (double)time_counter * timestep;

        if (sim_time < impulse_time)
        {
            the_rocket->AccumulateForce(impulse_force, the_rocket->GetPos(), false);
        }
        else if (sim_time < steady_time)
        {
            the_rocket->AccumulateForce(steady_force, the_rocket->GetPos(), false);
        }

        physics_system.DoStepDynamics(timestep);

        the_rocket->EmptyAccumulators();
        time_counter++;

        std::cout << sim_time << ", " << the_rocket->GetPos()[1] << std::endl;
    }

    stream_out.close();
    return 0;
}

What it does

Let's break down exactly what the simulation does:

  • There is an initial force of 30N for .3 seconds
  • After that, the engine gives off a force of 10N for up to 1.6 seconds.
  • The rocket then follows a parabolic trajectory according to gravity. We use Chrono's default gravity of 1g (9.81m/s^2^) in the y-direction.
  • The simulation stops when the rocket reaches the ground.

The final output can be plotted as a function of time:

Rocket

Challenges

  • Real Rockets deploy a parachute after a delay time. Lookup the delay time and see how to add this into your simulation. Asume a constant fall-rate after the parachute is deployed.
  • In order to get the plot, I had to use a filestream output instead of just std::cout. See if you can get Chrono to save to a file.
Clone this wiki locally