Using state machines and callbacks to model friction and limit stops

Modeling friction is always an annoying task, requiring extra characterization of your system and convincing modeling tools to operate through all the nonlinearities of the model.

Here we'll make a state machine to think about the different states of the model and then use the VectorContinuousCallback feature of the Julia DifferentialEquations.jl package.

Consider a sliding mass system in which an external force is applied to the mass and the mass is subject to static and kinetic friction, and bounded by limit stops which abruptly stop its motion.

10.4 μs
36.5 ms

We can identify five different states for the system:

  • LEFT_STOP: mass against left stop, motion resisted by static friction and stop

  • SLIDING_RIGHT: mass moving, being resisted by dynamic friction

  • STOPPED: mass not against either stop, motion resisted by static friction

  • SLIDING_LEFT: like SLIDING_RIGHT but in the other direction

  • RIGHT_STOP: like LEFT_STOP but at the other end.

9.0 μs
13.1 μs

There are twelve transitions between states, organized in eight lines:

  • LEFT_STOP to SLIDING_RIGHT if force (on the mass) is greater than static friction

  • SLIDING_RIGHT to STOPPED orSLIDING_LEFT (depending on force) if speed drops to zero

  • SLIDING_RIGHT to RIGHT_STOP or SLIDING_LEFT (depending on COR) if position reaches right stop

  • STOPPED to SLIDING_RIGHT if force is greater than static friction

  • STOPPED to SLIDING_LEFT if force is greater than static friction (but in the other direction)

  • SLIDING_LEFT to STOPPED or SLIDING_RIGHT (depending on force) if speed drops to zero

  • SLIDING_LEFT to LEFT_STOP or SLIDING_RIGHT if position reaches left stop

  • RIGHT_STOP to SLIDING_LEFT if force is greater than static friction (but to the left)

17.0 μs

Let's enumerate our states and make a default parameter object using some tools from Parameters.jl.

3.9 μs
7.6 ms
Params
14.2 ms

Let's write a state-aware differential equation.

5.9 μs
sbode (generic function with 1 method)
39.8 μs

The tests we use to move from one state to the other are defined in FricSMCondx.

6.8 μs
FricSMCondx (generic function with 1 method)
41.8 μs

The corresponding actions are defined in FricSMaffect!

6.0 μs
FricSMaffect! (generic function with 1 method)
59.9 μs
23.6 ms

Create a forcing function.

3.3 μs
force_ext (generic function with 1 method)
22.0 μs

Set up a sim function which sets initial conditions, timespan, parameters, and simulation options. We set dtmax = 0.01 because without it the solver skips forward too quickly.

3.0 μs
sim (generic function with 1 method)
78.5 μs
2.3 s

Plot the results! Here we can see the system being pushed back and forth by an amplitude-modulated sinusoidal force.

2.7 μs
347 ms
27.3 s
Conclusions

The callback feature of the DifferentialEquations.jl package allows one to implement a state machine to change the behavior of a system by letting the user

  • Define a set of states,

  • Define the behavior of each state in the ODE function,

  • Define the conditions on which states will change in a conditions function, and

  • Define what changes occur to the simulation on each change in the affect function.

The user can in this way define highly nonlinear actions and systems with discontinuous derivatives without causing solver issues because the solvers are aware of the callbacks and do not try to solve across the callback points.

15.4 μs

This Julia language document was prepared using the Pluto reactive notebook system.

David Klaffenbach, 2021-01-01.

Revised 2021-01-05.

10.9 μs