Detecting Zero Crossings (Control Design and Simulation Module)
- Updated2023-03-14
- 3 minute(s) read
Use the Detect Zero Crossing function to detect a zero crossing in a simulation. Use the Zero Crossings functions to detect zero crossings for an external model.
A zero crossing occurs if the following equation is true:
sgn(u(ti–1)–offset(ti–1)) ≠ sgn(u(ti)–offset(ti))
| where | u(t) is the value of the signal at time t |
| ti is the time of the current step | |
| ti–1 is the time of the previous step |
The internal zero-crossing detection system of the LabVIEW Control Design and Simulation Module uses linear interpolation. For example, the Control Design and Simulation Module checks whether a zero crossing occurred at the current time step. If a zero crossing occurred between the previous time step and the current time step, the Control Design and Simulation Module rejects the current time step, if possible. The Control Design and Simulation Module then performs linear interpolation between the value of the signal at the previous time step and the value of the signal at the current time step, proposes a new step size corresponding to the time at which the interpolated line crosses the offset, and checks again for a zero crossing. The Control Design and Simulation Module continues rejecting and proposing new step sizes either until it identifies the time step corresponding to the zero crossing or until it can no longer reject any steps.
The Control Design and Simulation Module resets the ordinary differential equation (ODE) solver when a zero crossing occurs.
Detecting Zero Crossings in an External Model
Third parties can use the External Model Interface (EMI) to create an external, or third-party model in C/C++. If you are a third-party user of EMI, use the following guidelines to detect zero crossings for a model you create.
- If you need to configure the zero crossing triggers and offsets only once, such as before a simulation runs, set the triggers and offsets in the EMI_CB_InitializeModel function. Otherwise, write code that updates the triggers and offsets as necessary during the simulation.
- Define and export the EMI_CB_CalculateZeroCrossingSignals function to calculate the signals you want to monitor for zero crossings.
- Write code to respond to zero crossings. For example, you can use the EMI_GetZeroCrossingResults function to determine the type of zero crossing that occurred. The Control Design and Simulation Module also automatically calls the EMI_CB_CalculateResetStates function when a zero crossing occurs.
You also can implement a custom algorithm for checking an external model for zero crossings, for example if you want to use polynomial interpolation instead of linear interpolation. Within the EMI_CB_CheckStepAcceptance function, first check whether a zero crossing occurred at the current time step. If a zero crossing occurred between the previous time step and the current time step, use the EMI_CanRejectStep function to determine whether you can reject the current time step. If you can reject the current time step, use the EMI_RejectStep function to reject the time step and propose a new step size. If you cannot reject the current time step, you might want to use the EMI_RequestCallbackToResetStates function to request a call to the EMI_CB_CalculateResetStates function after the current time step.