Demo 2: Numerical Models

Let’s revisit our problem statement:

We want to evolve an autonomous wheeled mobile robot (WMR) to navigator quickly over obstacles and then stop in front of a wall.

With an analytical model, we can simulate the position of the WMR, but we did not attempt to handle obstacles and we learned that dynamic control was not possible.

Numerical Simulation

We’ll now mve on to a numerical simulation of the WMR. This will enable us to handle obstacles and dynamic control. But before we do so, let me first explain the concept of a numerical simulation.

Instead of solving an equation analytically, we can approximate the solution by taking small steps in time. Here are egestions for both analytical and numerical simulations:

\[ x_t = x_0 + \omega r t \\ \tag{1}\]

\[ x_t = x_{t-1} + \omega_{t-1} r \Delta t \tag{2}\]

where \(x_t\) is the position of the robot at time \(t\), \(\omega\) is the angular velocity of the robot’s wheels, \(r\) is the radius of its wheels, and \(\Delta t\) is the time step. The differences between Equation 1 and Equation 2 are in the dependency of the numerical solution on the previous position.

Essentially, the numerical solution computes the position by integrating the velocity over time. Here is the equivalent code:

// Closed-form solution
chassisPosition = initialPosition + angularVelocity * wheelRadius * time;

// Numerical solution
chassisPosition += angularVelocity * wheelRadius * timeStep;

And here is a numerical simulation:

Dynamic Control

Although the numerical simulation requires more computational resources to compute the position, it enables us to handle obstacles and dynamic control. Here is the simulation with a wall in front of the WMR and the following controller:

\[ \omega = \max( -\Omega, \min( \Omega, m d + b) \tag{3}\]

And here is the corresponding code snippet:

initialPosition = 0;
chassisPosition = initialPosition;

angularVelocity = 1;
wheelRadius = 1;

wheelChassisOffset = 1;
wheelPositionFront = wheelChassisOffset;
wheelPositionRear = -wheelChassisOffset;

wheelAngleFront = 0;
wheelAngleRear = 0;

timeAccumulator = 0;
timeStep = 0.01;
time = 0;

speedMax = 3;
speedSlope = 2;
speedIntercept = -8;

controlPeriod = 0.1;
controlLastUpdate = 0;

function simulateStep( frameTime: number ) {

    timeAccumulator += frameTime;

    while ( timeAccumulator >= timeStep ) {

        // p = v t = ω r t
        chassisPosition += angularVelocity * wheelRadius * timeStep;

        wheelAngleFront += angularVelocity * timeStep;
        wheelAngleRear += angularVelocity * timeStep;

        timeAccumulator -= timeStep;
        time += timeStep;

        if ( time >= controlLastUpdate ) {

            const dist = getDistanceToWall();
            angularVelocity = Math.max( - speedMax, Math.min( speedMax, speedSlope * dist + speedIntercept ) );

            controlLastUpdate += controlPeriod;

        }

    }

    wheelPositionFront = chassisPosition + wheelChassisOffset;
    wheelPositionRear = chassisPosition - wheelChassisOffset;

}

while ( !done ) {

    frameTime = getFrameTime();
    simulateStep( frameTime );
    render();

}

Here are some key points to note:

  • We can no longer compute the position of the robot for any time \(t\) without knowing the previous position.
  • We’ve decoupled simulation and visualization (rendering).
  • We’ve set a fixed time step, which is better for numerical stability.
  • In this simple example, we only need to update the wheel positions once per render time. This is not a general case, but a simplification that we can take since the wheels are not independent of the chassis.

Read Fix Your Timestep! for more information on this code structure.

Adding Complex Collisions and Dynamics

How could we change our code if we:

  1. Added an incline to the ground plane?
  2. Added a second wall in front of the robot?
  3. Added a step in front of the robot?

Although these changes seem similar, (1) and (3) are quite a bit harder to implement. Why?

Changing the incline will require us to move to a more advanced method for integration. The code above implements a simple Euler integration, which works perfectly well for constant velocity dynamics (constant between time steps).

Adding a step requires us to change the orientations of all objects, implement more complex collision detection, and implement a friction model.

Adding a second wall is as simple as the first wall.

This simple numerical simulation does not work with a step. In the next demo we will explore how to handle complex collisions and dynamics.