78 lines
3.7 KiB
C
78 lines
3.7 KiB
C
/* Deterministic secondary-quantity protection model for the host DTU demo.
|
|
* This is an explicit simulation policy, not a manufacturer's relay algorithm.
|
|
* No allocation, I/O, clock reads, RTOS objects or physical outputs occur here. */
|
|
#ifndef IEC104_DTU_PROTECTION_H
|
|
#define IEC104_DTU_PROTECTION_H
|
|
|
|
#include "iec104_dtu_settings.h"
|
|
|
|
#define DTU_PROTECTION_SOURCE_BASE 0x9140U
|
|
#define DTU_PROTECTION_SIGNAL_COUNT 0x60U
|
|
#define DTU_PROTECTION_TIMER_COUNT 32U
|
|
#define DTU_PROTECTION_LATCH_COUNT 32U
|
|
|
|
#define DTU_TRIP_PHASE 1U
|
|
#define DTU_TRIP_EARTH 2U
|
|
#define DTU_TRIP_SMALL_EARTH 4U
|
|
#define DTU_TRIP_EXTERNAL 8U
|
|
#define DTU_TRIP_ACCELERATED 16U
|
|
#define DTU_RECORD_OVERCURRENT 1U
|
|
#define DTU_RECORD_VOLTAGE_LOSS 2U
|
|
#define DTU_RECORD_ZERO_CURRENT 4U
|
|
#define DTU_RECORD_ZERO_VOLTAGE 8U
|
|
|
|
typedef struct DtuProtectionInput {
|
|
double phase_current[3], zero_current, negative_current;
|
|
double pt_voltage[2], zero_voltage, frequency, second_harmonic;
|
|
double control_voltage, phase_angle;
|
|
bool small_earth_fault, non_electrical_fault, breaker_closed;
|
|
/* Output pulse in progress or external equipment/local-control interlock.
|
|
* Blocks closing without consuming an automatic reclose attempt. */
|
|
bool external_close_blocked;
|
|
uint64_t now_ms;
|
|
} DtuProtectionInput;
|
|
|
|
typedef struct DtuProtectionOutput {
|
|
/* Only addresses accepted by Manages_Source belong to this module. */
|
|
uint8_t digital[DTU_PROTECTION_SIGNAL_COUNT];
|
|
bool trip_request, close_request, close_blocked, trip_blocked, input_valid;
|
|
uint32_t trip_reasons, recording_reasons;
|
|
/* 0=dead (<= residual), 1=residual, 2=intermediate, 3=present, 4=unconfirmed. */
|
|
uint8_t voltage_state[2], reclose_attempts;
|
|
} DtuProtectionOutput;
|
|
|
|
typedef struct DtuProtection {
|
|
uint64_t timer_since[DTU_PROTECTION_TIMER_COUNT], timer_active;
|
|
uint64_t signal_last[DTU_PROTECTION_LATCH_COUNT], signal_latched;
|
|
uint64_t previous_ms, close_ms, reclose_open_ms, lock_ms, open_window_ms;
|
|
double previous_zero_current, previous_zero_voltage;
|
|
uint32_t reclose_reason, previous_record_level;
|
|
uint16_t area;
|
|
uint8_t reclose_attempts, consecutive_opens;
|
|
uint8_t voltage_candidate[2], voltage_state[2];
|
|
bool initialized, previous_sample_valid, previous_closed, have_close, pending_reclose;
|
|
bool awaiting_open, awaiting_close, locked, sync_blocked;
|
|
} DtuProtection;
|
|
|
|
void Dtu_Protection_Init(DtuProtection *state);
|
|
/* Explicit operator reset clears indicators, timing, lockout and reclose group.
|
|
* An uncleared input fault starts its pickup delay again on the next Step. */
|
|
void Dtu_Protection_Reset(DtuProtection *state);
|
|
/* Operator opening cancels an existing reclose group, retaining fault signals;
|
|
* ordinary switching without a protection/reclose operation is a no-op here.
|
|
* Lockout remains until explicit Reset or a later closed/healthy group recovery
|
|
* (886F) after minimum lock time (8870); it never self-clears while still open. */
|
|
void Dtu_Protection_Cancel_Reclose(DtuProtection *state, uint64_t now_ms);
|
|
/* Latched fault/event indicators only; excludes live voltage/interlock states. */
|
|
bool Dtu_Protection_Indicator_Source(uint16_t source_addr);
|
|
bool Dtu_Protection_Manages_Source(uint16_t source_addr);
|
|
/* All pointers non-NULL; settings already validated. Magnitudes must be finite
|
|
* and nonnegative; phase_angle is signed degrees. Requests are one-step pulses;
|
|
* caller applies them and supplies actual/simulated breaker feedback next time.
|
|
* Trip inhibits close. Bad samples or backward time emit neither request.
|
|
* Source 8823..882E (FA), 8830 (clock switching) are intentionally not modeled. */
|
|
void Dtu_Protection_Step(DtuProtection *state, const DtuSettings *settings,
|
|
const DtuProtectionInput *input, DtuProtectionOutput *output);
|
|
|
|
#endif
|