static void mdlZeroCrossings(SimStruct *S) { // Get the C++ object back from the pointers vector tres::Network *ns = static_cast<tres::Network *>(ssGetPWork(S)[0]); // Get the time resolution back from the real vector workspace double time_resolution = ssGetRWork(S)[0]; ssGetNonsampledZCs(S)[0] = ns->getTimeOfNextEvent()/(time_resolution) - ssGetT(S); }
static void mdlZeroCrossings(SimStruct *S) { //printf("mdlZeroCrossings at %g\n", ssGetT(S)); if (!rtsys->mdlzerocalled) { rtsys->mdlzerocalled = true; } int i; rtsys = (RTsys*) ssGetUserData(S); if (rtsys->init_phase) { /* Failure during initialization */ return; } /* Copy analog inputs */ InputRealPtrsType inputs = ssGetInputPortRealSignalPtrs(S,0); for (i=0; i<rtsys->nbrOfInputs; i++) { rtsys->inputs[i] = *inputs[i]; } /* Copy interrupt inputs, check for events */ inputs = ssGetInputPortRealSignalPtrs(S,1); for (i=0; i<rtsys->nbrOfTriggers; i++) { if (fabs(*inputs[i]-rtsys->interruptinputs[i]) > 0.1) { if (ssGetT(S) < rtsys->nextHit) { rtsys->nextHit = ssGetT(S); } //printf("mdlZeroCrossings: interrupt detected at %2.20g\n", ssGetT(S)); } rtsys->interruptinputs[i] = *inputs[i]; } /* Copy network input, check for event */ inputs = ssGetInputPortRealSignalPtrs(S,2); for (i=0; i<rtsys->nbrOfNetworks; i++) { if (fabs(*inputs[i]-rtsys->networkinputs[i]) > 0.1) { if (ssGetT(S) < rtsys->nextHit) { rtsys->nextHit = ssGetT(S); } } rtsys->networkinputs[i] = *inputs[i]; } /* Check the energy level */ rtsys->energyLevel = *ssGetInputPortRealSignalPtrs(S,3)[0]; ssGetNonsampledZCs(S)[0] = rtsys->nextHit - ssGetT(S); }
static void zeroCrossings_c1_newtons_cradle(SFc1_newtons_cradleInstanceStruct *chartInstance) { boolean_T c1_stateChanged; int32_T c1_i; int32_T c1_exitg1; real_T *c1_zcVar; real_T (*c1_p)[3]; c1_p = (real_T (*)[3])(ssGetContStates(chartInstance->S) + 0); c1_zcVar = (real_T *)(ssGetNonsampledZCs(chartInstance->S) + 0); _sfTime_ = (real_T)ssGetT(chartInstance->S); if (chartInstance->c1_lastMajorTime == _sfTime_) { *c1_zcVar = -1.0; } else { c1_stateChanged = (boolean_T)0; c1_i = 0; do { c1_exitg1 = 0; if (c1_i < 2) { if ((*c1_p)[c1_i] < (*c1_p)[c1_i + 1]) { c1_i = 1; c1_exitg1 = 1; } else { c1_i++; _SF_MEX_LISTEN_FOR_CTRL_C(chartInstance->S); } } else { c1_i = 0; c1_exitg1 = 1; } } while (c1_exitg1 == 0); if (c1_i != 0) { c1_stateChanged = TRUE; } if (c1_stateChanged) { *c1_zcVar = 1.0; } else { *c1_zcVar = -1.0; } } }
static void mdlZeroCrossings(SimStruct *S) { int i; double now = ssGetT(S); RTnetwork *nwsys = (RTnetwork*)ssGetUserData(S); InputRealPtrsType input_0 = ssGetInputPortRealSignalPtrs(S,0); InputRealPtrsType input_1 = ssGetInputPortRealSignalPtrs(S,1); InputRealPtrsType input_2 = ssGetInputPortRealSignalPtrs(S,2); /* Check for external events */ for (i=0; i < nwsys->nbrOfNodes; i++) { if (fabs(*input_0[i] - nwsys->inputs[i]) > 0.1) { nwsys->nextHit = now; break; } } /* Copy inputs */ for (i=0; i < nwsys->nbrOfNodes; i++) { nwsys->inputs[i] = *input_0[i]; nwsys->nwnodes[i]->xCoordinate = *input_1[i]; nwsys->nwnodes[i]->yCoordinate = *input_2[i]; } ssGetNonsampledZCs(S)[0] = nwsys->nextHit - now; }
/* Function: mdlZeroCrossings =============================================== * Abstract: * If your S-function has registered CONTINUOUS_SAMPLE_TIME and there * are signals entering the S-function or internally generated signals * which have discontinuities, you can use this method to locate the * discontinuities. When called, this method must update the * ssGetNonsampleZCs(S) vector. */ static void mdlZeroCrossings(SimStruct *S) { real_T *x = ssGetContStates(S); real_T *zcSignals = ssGetNonsampledZCs(S); zcSignals[0] = (x[1] >= 0 ? 1.0 : -1.0) * (x[0] - rw_gamma) - alpha; }
static void mdlZeroCrossings(SimStruct *S) { rtsys = (RTsys*) ssGetUserData(S); debugPrintf("'%s': mdlZeroCrossings at %.16f\n", rtsys->blockName, ssGetT(S)); int i; if (rtsys->init_phase) { /* Failure during initialization */ return; } /* Copy analog inputs */ InputRealPtrsType inputs = ssGetInputPortRealSignalPtrs(S,0); for (i=0; i<rtsys->nbrOfInputs; i++) { rtsys->inputs[i] = *inputs[i]; } /* Check trigger input port for events */ inputs = ssGetInputPortRealSignalPtrs(S,1); for (i=0; i<rtsys->nbrOfTriggers; i++) { Trigger* trig = &(rtsys->triggers[i]); // Only check for events if there is a handler attached if (trig->handler != NULL) { double input = *inputs[i]; double oldinput = rtsys->oldtriggerinputs[i]; int event = 0; switch (trig->state) { case ZERO: if (input > 0.0) { trig->state = POSITIVE; event = RISING; } else if (input < 0.0) { trig->state = NEGATIVE; event = FALLING; } break; case NEGATIVE: if ((input > 0.0 && oldinput <= 0.0) || (input == 0.0 && oldinput < 0.0)) { trig->state = POSITIVE; event = RISING; } break; case POSITIVE: if ((input < 0.0 && oldinput >= 0.0) || (input == 0.0 && oldinput > 0.0)) { trig->state = NEGATIVE; event = FALLING; } break; } if (event & rtsys->trigType) { if (trig->minimumInterval <= 0.0 || rtsys->time - trig->prevHit >= trig->minimumInterval) { debugPrintf("'%s': external trigger %d activated at %.14f\n", rtsys->blockName, i+1, rtsys->time); // Trigger interrupt handler invoke_task(trig->handler, trig->trigName); if (trig->handler->nbrInvocations == 1) { rtsys->nextHit = ssGetT(S); } trig->prevHit = rtsys->time; } } rtsys->oldtriggerinputs[i] = *inputs[i]; } } /* Check network input port for events */ inputs = ssGetInputPortRealSignalPtrs(S,2); for (i=0; i<rtsys->nbrOfNetworks; i++) { NetworkInterface *nwi = &(rtsys->networkInterfaces[i]); // Only check for events if there is a handler attached if (nwi->handler != NULL) { double input = *inputs[i]; double oldinput = rtsys->oldnetworkinputs[i]; if (input != oldinput) { debugPrintf("'%s': incoming packet from network %d at %.14f\n", rtsys->blockName, nwi->networkNbr, rtsys->time); char trigname[MAXCHARS]; snprintf(trigname, MAXCHARS, "network:%d", nwi->networkNbr); invoke_task(nwi->handler, trigname); if (nwi->handler->nbrInvocations == 1) { rtsys->nextHit = ssGetT(S); } } rtsys->oldnetworkinputs[i] = input; } } /* Read the energy level input */ rtsys->energyLevel = *ssGetInputPortRealSignalPtrs(S,3)[0]; // Schedule next major time step according to rtsys->nextHit ssGetNonsampledZCs(S)[0] = rtsys->nextHit - ssGetT(S); }