Guy on Simulink

Simulink & Model-Based Design

This is machine translation

Translated by Microsoft
Mouseover text to see original. Click the button below to return to the Original version of the page.

Zero-Crossing Detection… what are your options? 8

Posted by Guy Rouleau,

When using Simulink variable-step solvers, zero-crossing detection is very useful to capture events accurately. However for some equations, configuring zero-crossing detection can be challenging.

Last week I received the following question and example model:

In the attached model, I implemented a basic bang-bang controller for a plow. Using a Sign block, I generate a 1 for the plow to go up, -1 to go down and 0 to stop. The problem is that the plow never seems to stabilize around zero. Can you help me understand why?

Here is an image of the model:

Bang Bang Controller for a plow

Nonadaptive zero-crossing detection

With the default settings, this model errors out as soon as we try to give a non-zero command with the following error:

At time 1.0000000000236877, simulation hits (1000) consecutive zero crossings. Consecutive zero crossings will slow down the simulation or cause the simulation to hang. To continue the simulation, you may 1) Try using Adaptive zero-crossing detection algorithm or 2) Disable the zero crossing of the blocks shown in the following table.
Number of consecutive zero-crossings : 1000
Zero-crossing signal name : Input
Block type : Signum
Block path : 'myPlowController/Sign'
You can turn off this message by using the MATLAB command:

This error is relatively simple to understand by looking at the ideal plant model (an Integrator block) and the switching logic. If the plow position is slightly negative, the controller makes it move upward, leading to a positive error. The controller sees this positive error, makes the plow go down, bringing us back to our original state .

With zero-crossing detection enabled for the Sign block, Simulink will detect that we crossed zero and try to reduce its step-size to capture this transition accurately. But here, reducing the step size will not help. The step size can be a small as possible, a small negative error will always lead to a jump on the positive side, and vice-versa.

Let's try the suggestions offered by the error message. What suggestions? If you didn't read the error message, go back and read it now.

Suggestion 1: Adaptive zero-crossing detection

In the solver section of the model configuration, we can try selecting adaptive zero-crossing detection. This will dynamically activate and deactivate zero-crossing bracketing, helping with models that exhibit strong chattering.

Adaptive zero-crossing detection

With this setting the simulation completes, but the variable step solver takes way too many steps when it is not needed, and the position error is too large.

Simulation results with adaptive zero-crossing detection

The reason is that the adaptive algorithm encountered multiple consecutive zero-crossings for the Sign block and decided to disable zero-crossing detection for this block. Once this is done, the solver takes larger steps and gives inaccurate results.

Let's try suggestion 2 from the error message.

Suggestion 2: Disabling zero-crossing

The second suggestion from the error message is to disable zero-crossing detection for the problematic block.

Sign block with disabled ZC

In this case, the tracking is significantly better because the solver uses the state of the integrator to limit its step size, but we see a lot of switching happening when the plow desired height is constant.

results with zero-crossing disabled

The main problem here is that it is impossible for the Simulink engine to stop exactly on the zero value of the Sign block with this system. With the equations involved, Simulink can take very small steps around zero, but will not stop exactly on it. This not what the Sign block is designed to do.

Possible solutions

If we want a system that can be simulated efficiently using a variable step solver, we have to modify the equations. If we want to keep the plant as it is, we need a controller with a logic that will stop when the error is within a certain range around zero. For example, we could use a Stateflow chart or a block with hysteresis like the Relay block:

Controller with dead zone

In this case, the plow stops moving when the command is fixed and the zero-crossing detection helps ensure that the solver takes appropriate steps when needed.

Results of the Controller with dead zone

Now it's your turn

If you have some interesting plowing or zero-crossing stories to share, leave a comment here

8 CommentsOldest to Newest

Matthew Bosworth replied on : 1 of 8
I've developed a model with simPowerSystems, of which I would like to enable a breaker(3phase)at a zero crossing. I work in other platforms like PSCAD, which sequence a fault trigger at the 'next zero crossing' after I command the breaker to cause a fault. Case explaination: Syncronous generator, salient pole, connected to 1pu load in delta. A the terminals of the machine a 3phase branch connects to a 3phase breaker, in which I command them to close at 5s, after the machine reaches steady state operation. I would like the command to the breaker to occur at a zero crossing of the phase A voltage. Any suggestions? Thanks in advance.. Matt
Rafin replied on : 2 of 8
Could you please suggest me, how do I detect the zero crossing of the output sine current of an inductive load?
ISHAN CHAWLA replied on : 3 of 8
Sir, I designed a bang bang controller using MATLAB code while i also designed its similar MATLAB simulink model, but the results for the code and simulink model are different. I have already tried enabling and disabling zero crossing detection, adaptive and non adaptive zero crossing options, still the results are different. I want to know that What settings do i have to do in MATLAB simulink to behave exactly as MATLAB code? waiting for your reply....
malith123 replied on : 4 of 8
Thank you so much sir, your solution works perfectly, i had the same problem with my sliding mode controller, now it works superbly thanks to you by adding relay, abs and product block. Again so great-full to you.
Brett Lenhardt replied on : 6 of 8
I am receiving a suggestion to disable zero crossing detection on a simscape block. Where is that disable feature??
@Brett: Can you provide more details? When you mean "receive a suggestion", do you mean that Simulink throws a warning suggesting that? Can you paste here the exact complete error message?