Let's assume that you're using a 3D graphics engine with built in physics. You create a inverted pendelum in a 3D designing software, e.g Blender, and then import the model into your 3D grapics engine and you set the mass etc.
A 3D grapics engine such as OpenGL, jMonkey, Unity etc. uses C, C++, C# or Java, Java for jMonkey.
As I know, to build an adaptive controller, I need to have the following:
- Recursive identifier algorithm
- A description of the structure of the physical model
- An algorithm who computes the control law
- An ODE solver who simulate the physical model
My idea is to set up a recursive algorithm, quite simple. Then a dscription of the structure of the physical model. In this case it must be a 2x2 LTV state space model. It will be done by a library for control theory.
Now to my question: Assume that I run my simulation with a constant step interval. Assume it's a programming language I'm using. Is it possible tell the ODE solver the past step, time and other types of parameters such as states?
I have planned to use the Apache Commons library for control engineering. http://commons.apache.org/proper/commons-math/userguide/ode.html
Where a simulation example looks like this:
private static class CircleODE implements FirstOrderDifferentialEquations {
// Parameters for the class
private double[] c;
private double omega;
// Class constructor
public CircleODE(double[] c, double omega) {
this.c = c;
this.omega = omega;
}
// Not required
public int getDimension() {
return 2;
}
// This is the ODE solver and its arguments
public void computeDerivatives(double t, double[] y, double[] yDot) {
yDot[0] = omega * (c[1] - y[1]);
yDot[1] = omega * (y[0] - c[0]);
}
}
And to start the simulation
// Choose the solver method
FirstOrderIntegrator dp853 = new DormandPrince853Integrator(1.0e-8, 100.0, 1.0e-10, 1.0e-10);
// Create the object
FirstOrderDifferentialEquations ode = new CircleODE(new double[] { 1.0, 1.0 }, 0.1);
// initial state vector
double[] y = new double[] { 0.0, 1.0 };
// Now simulate from 0 to 16 seconds.
dp853.integrate(ode, 0.0, y, 16.0, y); // now y contains final state at time t=16.0
Is it possible to use generate a new object for every iteration and simulate the object when the past time and past state? For every iteration, a recursive identifier should compute better parameters for the future objects of the class CircleODE.
Notice that Java has a garbage collector, so I don't need to destroy the past objects by my self. :)
The ODE solver has no sense of "time," it will just try to solve whatever you give it.
You want to store state information outside of the solver. Maybe a list of previous positions or velocities (depending on what type of controller you are creating). For example, if you are creating a position based PID controller, you will need access to the current position and target position to calculate $$ P = P_{target} - P_{current} $$ you'll need the previous position as well to calculate the derivative term $$ \frac{P_{current}-P_{previous}}{timestep} $$ and you'll need a variable for accumulating the integral error $I$.
So the steps in your simulation are $$\text{Use controller to determine what forces to apply}$$ $$\downarrow$$ $$\text{Formulate the dynamics, including controller forces}$$ $$\downarrow$$ $$\text{Pass the dynamics problem to the solver}$$ $$\downarrow$$ $$\text{Use solver results to update body positions and velocities}$$ $$$$ Since you can just store state information in a variable and retrieve it whenever you want, you can use that state information in the first step when the controller is calculating what forces to apply.
Also, it would probably be noticeably inefficient to create a new object for every iteration, just like creating and destroying variables over and over. You would probably want to have objects with attributes that you can overwrite, for example an object that represents bodies in the simulation where you can update their attributes like position, velocity, etc.