Skip to Main Content Skip to Search
Product Documentation

Modeling a Bouncing Ball in Continuous Time

Try It

The following topics give you step-by-step instructions for modeling a bouncing ball as a Stateflow chart in continuous time using the workflow described in Workflow for Creating Continuous-Time Charts.

Dynamics of a Bouncing Ball

The dynamics of a bouncing ball describes the ball as it falls, when it hits the ground, and when it bounces back up.

You can specify how the ball falls freely under gravity using the following second-order differential equation:

In this equation, describes the position of the ball as a function of time, and , which is the acceleration due to gravity.

A Stateflow chart requires that you specify system dynamics as first-order differential equations. You can describe the dynamics of the free-falling ball in terms of position p and velocity v using the following first-order differential equations:

EquationDescription

Derivative of position is velocity

Derivative of velocity is acceleration

The bounce occurs after the ball hits the ground at position p <= 0. At this point in time, you can model the bounce by updating position and velocity as follows:

Modeling the Bouncing Ball

The following steps take you through the workflow for modeling a bouncing ball in continuous time. To view the completed model, open the bouncing ball demo.

Task 1: Configure the Bouncing Ball Chart for Continuous Updating

  1. Create an empty Stateflow chart and open its properties dialog box.

    If you need instructions, see Creating an Empty State Chart.

  2. Set the update method to Continuous.

Task 2: Decide Whether to Enable Zero-Crossing Detection for the Bouncing Ball

For this example, enable zero-crossing detection (the default) so that the Simulink model can determine exactly when the ball hits the ground at p <= 0. Otherwise, the Simulink model might not be able to simulate the physics accurately. For example, the ball might appear to descend below ground.

Task 3: Define Continuous-Time Variables for Position and Velocity

  1. Define two continuous-time variables, p for position and v for velocity. For each variable, follow these steps:

    1. In the Stateflow Editor, select Add > Data > Local.

    2. Enter the name for the local data.

    3. Set the update method to Continuous.

    4. Leave all other properties at their default values and click OK.

        Note   For each continuous local variable that you define, the chart implicitly creates its time derivative as a variable of the same name with the suffix _dot. In this example, the chart defines p_dot as the derivative of position p and v_dot as the derivative of velocity v.

  2. Define two outputs, p_out and v_out for exposing continuous state to the Simulink model. For each variable, follow these steps:

    1. In the Stateflow Editor, select Add > Data > Output to Simulink.

    2. Enter the name for the output data.

    3. Leave all other properties at their default values and click OK.

Your chart should contain the following data, as viewed in the Model Explorer:

Task 4: Choose a Solver for the Bouncing Ball Chart

For this example, you can use ode45 (Dormand-Prince), the default variable-step solver used by Simulink models with continuous states.

Task 5: Add Dynamics for a Free-Falling Ball

  1. Add a state named Falling with a default transition. In the default transition, set initial position to 10 meters and initial velocity to 15 meters/second.

  2. Add a during action to the Falling state that defines the derivatives of position and velocity, as follows.

    The derivative of position is velocity and the derivative of velocity is acceleration due to gravity (-g).

Task 6: Expose Ball Position and Velocity to the Simulink Model

In the during action, assign position to the output p_out and assign velocity to the output v_out, as follows.

Task 7: Validate Semantics of Bouncing Ball Chart

Check semantics against the requirements defined in Design Considerations for Continuous-Time Modeling in Stateflow Charts.

This chart meets design requirements:

Task 8: Simulate Bouncing Ball Chart

  1. Attach each output to a scope.

  2. Simulate the chart and view the outputs in the scope.

    Note that the ball appears to fall below the ground (below position p = 0) because the chart does not yet include a check for the bounce.

Task 9: Check for the Bounce

  1. Add a self-loop transition to state Falling.

      Note   The chart uses a self-loop transition so it can model the bounce as an instantaneous mode change — where the ball suddenly reverses direction — rather than as a finite time collision.

  2. Add a condition on the transition that indicates when the ball hits the ground.

    The condition should check for position p <= 0 and velocity v < 0, as follows.

     Why not just check for p == 0?

     Why add the second check for v < 0?

  3. When the ball hits the ground, reset position and velocity in a condition action, as follows.

  4. Simulate the chart again. This time, the scope shows the expected bounce pattern.

  


Free Stateflow Interactive Kit

Learn how engineers use Stateflow to model state machines in their Simulink models.


Get free kit

Trials Available

Try the latest version of Stateflow.


Get trial software
 © 1984-2012- The MathWorks, Inc.    -   Site Help   -   Patents   -   Trademarks   -   Privacy Policy   -   Preventing Piracy   -   RSS