/* WD_SATCOUNT.c Function: WD_SATCOUNT.c * * SATCOUNT is a WD saturation counter that only accounts for saturations * that occured during a given time window. The way saturations are * stored in this time window is inspired by ring buffers. Saturations * are stored in the limited number of bins the main time window is made of. * Bins get replaced, oldest first, when the main time window is full. * * Inputs: *-------- * (1) int SATURATIONS: wd saturations signal * 0: no saturation * 1+: Number of saturations registered during the current FE cycle * (2) int WINDOWTIME: total time observed by the main time window (in seconds) * (3) int RESET: reset signal * 0: Clear the saturation counter * 1: Leave the saturation counter * * Outputs: * ------- * (1) int SATURATIONS_TOTAL: total number of saturations to be compared * against the SAFE_SAT_COUNT * (2) int BUFFER: current number of saturations in the buffer - FOR TESTING * (3) int CYCLE: current FE cycle number, modulo the number of cycles in a bin. - FOR TESTING * (4) int RESET: reset request received. - FOR TESTING * (5) int SAT_SINCE_RESET total number of saturations since last reset - FOR LONG TERM MONITORING * * Authors: HRAP, BTL * July 2015 * * See ECR E1500325 / integration issue 1086 * * Aug 15, 2015 BTL removed test cases from end and fixed the CLEAR_BUG * BTL fix RESET behavior so that history clear works. * bug was that the precomputed history sum (BIN_SAT_TOTAL) was not being set to 0 */ #define MODEL_RATE FE_RATE // makes a constant out of the model rate #define BINS 60 //number of bins within time window void ISISATCOUNT(double *argin, int nargin, double *argout, int nargout){ int SATURATION_IN = argin[0]; int WINDOWTIME = argin[1]; int RESET = argin[2]; // ---------------------Initialisations------------------------------------ // Note: as of now there is no regulation to insure that the TIMEWINDOW / BINS doesn't have a remainer int WINDOW_CYCLES, NUM_BINCYCLES; WINDOW_CYCLES = WINDOWTIME * MODEL_RATE; // window duration in FE cycles NUM_BINCYCLES = WINDOW_CYCLES / BINS; // number of cycles per bin // CYCLE keeps track of the FE cycles // using "Static int" allows keeping value between FE cycles static int CYCLE = 0; // Initialize WINDOW with empty bins // "static int WINDOW[BINS]={0};" Oly works if BIN is a constant (define). // It wouldn't work otherwise because // when the compilers sees your variable declaration // interpreter returns: "variable-sized object may not be initialized" static int WINDOW[BINS]={0}; // Total number of saturations since last reset static int SAT_SINCE_RESET = 0; // Initialize: // - BUFFER: Buffer whithin which saturations are summed into bins // - AVAILABLE_BIN: Available bin (oldest one) in the WINDOW array // - BIN_SAT_TOTAL: sum of the bin history // - SATURATIONS_TOTAL: bin history + number of sat. in current BUFFER - main output static int BUFFER = 0; static int AVAILABLE_BIN = 0; static int BIN_SAT_TOTAL = 0; int SATURATIONS_TOTAL = 0; int i = 0; // simple counter // ---------------------Computation---------------------------------- // Apply RESET: if (RESET == 0) { BUFFER = 0; // clear buffer SAT_SINCE_RESET = 0; // clear the number of saturations since last reset. SATURATIONS_TOTAL = 0; BIN_SAT_TOTAL = 0; // this fixes the initial 'not clearing consistantly' bug. for(i = 0; i < BINS; i++) { WINDOW[i] = 0 ; // clear window } } // Store the number of saturations in the buffer, and the number of saturations // since last reset, at every sample BUFFER = BUFFER + SATURATION_IN ; SAT_SINCE_RESET = SAT_SINCE_RESET + SATURATION_IN ; // Update the cycle number CYCLE++; // If new bin ready if (CYCLE >= NUM_BINCYCLES) { // Store buffer in next available bin in the window WINDOW[AVAILABLE_BIN] = BUFFER; // Update AVAILABLE_BIN location in WINDOW AVAILABLE_BIN++; // Limit AVAILABLE_BIN to the number of bins avaialble in WINDOW if (AVAILABLE_BIN >= BINS) { AVAILABLE_BIN = 0 ; } // Clear BUFFER BUFFER = 0; // start counting CYCLES over CYCLE = 0; // SUM the saturations in the whole window BIN_SAT_TOTAL = 0; // initialize sum for(i = 0; i < BINS; i++) { BIN_SAT_TOTAL = BIN_SAT_TOTAL + WINDOW[i] ; // sum of the saturations in the whole window } } // SUM the current amount of saturations // (SATURATIONS_TOTAL is the output of the c-code block) SATURATIONS_TOTAL = BIN_SAT_TOTAL + BUFFER; // output the total number of saturations argout[0] = SATURATIONS_TOTAL; argout[1] = BUFFER; argout[2] = CYCLE; argout[3] = RESET; argout[4] = SAT_SINCE_RESET; return; }