Stick Slip
Simulation of a mechanical system exhibiting stick-slip behavior due to Coulomb friction.
You can also find this example as a single file in the GitHub repository.

This system has two possible states:
The slip state where the box oscillates freely. Here we have the dynamical behaviour of a classical damped harmonic oscillator, a 2nd order ODE.
The stick state where box exactly follows the belt. Here the box velocity is clamped to the belt velocity (algebraic constraint) and the system dynamics is reduced to a pure 1st order integration.
The two states transition from one to another depending on the relative velocity of the box to the belt and the force acting on the box. If the relative velocity is zero and the force is below some threshold, the system enters the stick state. When the force exceeds a certain threshold, the box breaks free and enters the slip state.
The continuous time dynamics for the two states have the following ODE(s):
MATHDISPLAY0ENDMATH
with the sticking condition:
MATHDISPLAY0ENDMATH
the transition condition from slip to stick, when:
MATHDISPLAY1ENDMATH
and from stick to slip, when
MATHDISPLAY2ENDMATH
The resulting switched system is shown in the block diagram below:

Note that the event manager tracks the system state and sets the switch to select the input of the position integrator.
The event manager effectively switches between the two signal flow graphs in the figures below. The slipping state:

And the stick state where the velocity is clamped and the position is just determined by the integrated belt velocity:

Now lets implement this hybrid dynamical system into PathSim starting with importing the Simulation and Connection classes and the required blocks from the block library:
Next are the system parameters, including the function definitions for the Source and the Function blocks:
Now we can construct the system by instantiating the blocks we need with their corresponding prameters and collect them together in a list:
Afterwards, the connections between the blocks can be defined. The first argument of the Connection class is the source block and its port (Src[0] would be port 0 of the instance of the Source block, which is also the default port).
Next we need to define the two event managers for the state transitions of the system. They are of type ZeroCrossing:
Finally we can instantiate the Simulation with the blocks, connections, events and some additional parameters such as the timestep. We use an adaptive timestep ODE solver RKBS32 (its essentially the same as Matlabs ode23) so the event managemant system can use backtracking to accurately locate the events. Then we can run the simulation for some duration which is set as 2*T (two periods of the source term) in this example.
12:44:05 - INFO - LOGGING (log: True) 12:44:05 - INFO - BLOCKS (total: 11, dynamic: 2, static: 9, eventful: 0) 12:44:05 - INFO - GRAPH (nodes: 11, edges: 17, alg. depth: 5, loop depth: 0, runtime: 0.113ms) 12:44:05 - INFO - STARTING -> TRANSIENT (Duration: 100.00s) 12:44:05 - INFO - -------------------- 1% | 0.0s<0.2s | 4393.9 it/s 12:44:05 - INFO - ####---------------- 20% | 0.1s<0.2s | 3875.5 it/s 12:44:06 - INFO - ########------------ 40% | 0.2s<0.2s | 3944.6 it/s 12:44:06 - INFO - ############-------- 60% | 0.3s<0.2s | 2809.4 it/s 12:44:07 - INFO - ###############----- 75% | 1.3s<04:20:59 | 2677.5 it/s 12:44:07 - INFO - ################---- 80% | 2.1s<0.1s | 2576.8 it/s 12:44:08 - INFO - #################### 100% | 2.3s<--:-- | 2137.9 it/s 12:44:08 - INFO - FINISHED -> TRANSIENT (total steps: 5055, successful: 2409, runtime: 2304.13 ms)
Lets have a look at the scopes and see what we got for the position and velocity:
And the scope that recorded the forces: