Files
STM32F4-Base/IEC60870-Slave/examples/iec104_dtu_protection.c
T
STF bdcc8b6500
CI / L0 静态门禁 (pre-commit) (push) Failing after 7m46s
CI / L0.5 主机侧单测 (MSVC) (push) Successful in 21s
添加遥控、参数装载、文件传输等业务
2026-11-16 22:19:17 -08:00

447 lines
22 KiB
C

/* Host simulation policy: pickup delays use monotonic elapsed time; the largest
* phase RMS drives phase protection. Alarm/trip indications hold after pickup,
* but a held indication never drives an output. Output requests use live logic.
* FA topology and proprietary directional/communication schemes are excluded. */
#include "iec104_dtu_protection.h"
#include <math.h>
#include <string.h>
/* Slots with event/hold semantics. Other managed indications are live states. */
static const uint16_t dtu_latched_sources[] = {
0x916aU, 0x916bU, 0x916cU, 0x9171U, 0x9172U,
0x9176U, 0x9177U, 0x9178U, 0x9179U, 0x917aU,
0x916dU, 0x916eU, 0x916fU, 0x918dU, 0x918eU,
0x9181U, 0x9182U, 0x9183U, 0x9184U, 0x9166U,
0x917bU, 0x917cU, 0x917dU, 0x917eU, 0x917fU, 0x9180U,
0x919dU, 0x919eU, 0x9148U, 0x9186U, 0x9168U
};
enum {
TIMER_LOAD = 5, TIMER_LOW_V = 8, TIMER_HIGH_V = 10,
TIMER_VOLTAGE = 12, TIMER_FREQUENCY = 14, TIMER_BROKEN = 15,
TIMER_EARTH_ALARM = 16, TIMER_EARTH_TRIP = 17, TIMER_NEGATIVE = 18,
TIMER_ACCEL_PHASE = 19, TIMER_ACCEL_EARTH = 20, TIMER_ACCEL_VOLTAGE = 21,
TIMER_SYNC = 22, TIMER_RECLOSE_VOLTAGE = 23, TIMER_RECLOSE_HEALTHY = 24,
TIMER_UNLOCK = 25
};
static double dtu_value(const DtuSettings *settings, uint32_t ioa)
{
return Dtu_Settings_Get(settings, ioa, 0.0);
}
static bool dtu_on(const DtuSettings *settings, uint32_t ioa)
{
return dtu_value(settings, ioa) != 0.0;
}
/* Round binary32 seconds to the nearest millisecond. This avoids 0.2f requiring
* 201 ms, and gives sub-millisecond settings a documented millisecond quantum. */
static uint64_t dtu_ms(double seconds)
{
return (uint64_t)(seconds * 1000.0 + 0.5);
}
static bool dtu_delay(DtuProtection *state, unsigned slot, bool active,
double seconds, uint64_t now)
{
uint64_t mask = UINT64_C(1) << slot;
if (!active) { state->timer_active &= ~mask; return false; }
if ((state->timer_active & mask) == 0U) {
state->timer_since[slot] = now;
state->timer_active |= mask;
}
return now - state->timer_since[slot] >= dtu_ms(seconds);
}
static void dtu_signal(DtuProtectionOutput *output, uint16_t source, bool value)
{
output->digital[source - DTU_PROTECTION_SOURCE_BASE] = value ? 1U : 0U;
}
static bool dtu_signal_value(const DtuProtectionOutput *output, uint16_t source)
{
return output->digital[source - DTU_PROTECTION_SOURCE_BASE] != 0U;
}
bool Dtu_Protection_Indicator_Source(uint16_t source_addr)
{
size_t index;
for (index = 0U; index < sizeof(dtu_latched_sources) / sizeof(dtu_latched_sources[0]); ++index) {
if (source_addr == dtu_latched_sources[index]) { return true; }
}
return false;
}
bool Dtu_Protection_Manages_Source(uint16_t source_addr)
{
if (Dtu_Protection_Indicator_Source(source_addr)) { return true; }
switch (source_addr) {
case 0x9141U: case 0x9143U:
case 0x914fU: case 0x9150U: case 0x9151U: case 0x9152U:
case 0x9154U: case 0x9155U: case 0x9156U: case 0x9157U:
case 0x9170U: case 0x9173U: case 0x9174U: case 0x9175U:
case 0x918bU: case 0x918cU: case 0x9191U: case 0x9192U:
return true;
default: return false;
}
}
void Dtu_Protection_Init(DtuProtection *state)
{
memset(state, 0, sizeof(*state));
state->voltage_state[0] = state->voltage_state[1] = 4U;
}
void Dtu_Protection_Reset(DtuProtection *state)
{
Dtu_Protection_Init(state);
}
static bool dtu_input_valid(const DtuProtectionInput *input)
{
size_t index;
const double magnitudes[] = { input->zero_current, input->negative_current,
input->zero_voltage, input->frequency, input->second_harmonic, input->control_voltage };
for (index = 0U; index < 3U; ++index) {
if (!isfinite(input->phase_current[index]) || input->phase_current[index] < 0.0) { return false; }
}
for (index = 0U; index < 2U; ++index) {
if (!isfinite(input->pt_voltage[index]) || input->pt_voltage[index] < 0.0) { return false; }
}
for (index = 0U; index < sizeof(magnitudes) / sizeof(magnitudes[0]); ++index) {
if (!isfinite(magnitudes[index]) || magnitudes[index] < 0.0) { return false; }
}
return isfinite(input->phase_angle);
}
static void dtu_indications(DtuProtection *state, const DtuSettings *settings,
uint64_t now, DtuProtectionOutput *output)
{
size_t index;
uint64_t hold = dtu_ms(dtu_value(settings, 0x8822U));
uint64_t automatic = dtu_ms(dtu_value(settings, 0x8821U));
if (automatic < hold) { automatic = hold; }
/* Fault indication reset is measured from the last live assertion. Manual
* mode keeps the indicator until Reset; automatic mode observes both times. */
for (index = 0U; index < sizeof(dtu_latched_sources) / sizeof(dtu_latched_sources[0]); ++index) {
uint16_t source = dtu_latched_sources[index];
uint64_t mask = UINT64_C(1) << index;
if (dtu_signal_value(output, source)) {
state->signal_latched |= mask;
state->signal_last[index] = now;
} else if ((state->signal_latched & mask) != 0U && dtu_on(settings, 0x8820U) &&
now - state->signal_last[index] >= automatic) {
state->signal_latched &= ~mask;
}
dtu_signal(output, source, (state->signal_latched & mask) != 0U);
}
dtu_signal(output, 0x918bU, dtu_signal_value(output, 0x916aU) ||
dtu_signal_value(output, 0x916bU) || dtu_signal_value(output, 0x916cU));
dtu_signal(output, 0x918cU, dtu_signal_value(output, 0x9171U) || dtu_signal_value(output, 0x9172U));
}
static void dtu_voltage(DtuProtection *state, const DtuSettings *settings,
const DtuProtectionInput *input, DtuProtectionOutput *output)
{
unsigned index;
bool any_dead = false;
for (index = 0U; index < 2U; ++index) {
double voltage = input->pt_voltage[index];
uint16_t present = index == 0U ? 0x914fU : 0x9154U;
uint8_t classification = voltage >= dtu_value(settings, 0x8833U) ? 3U :
voltage > dtu_value(settings, 0x8834U) ? 2U :
voltage > dtu_value(settings, 0x8835U) ? 1U : 0U;
if (classification != state->voltage_candidate[index]) {
state->voltage_candidate[index] = classification;
(void)dtu_delay(state, TIMER_VOLTAGE + index, false, 0.0, input->now_ms);
}
if (dtu_delay(state, TIMER_VOLTAGE + index, true,
dtu_value(settings, 0x8836U), input->now_ms)) {
state->voltage_state[index] = classification;
}
output->voltage_state[index] = state->voltage_state[index];
dtu_signal(output, present, state->voltage_state[index] == 3U);
dtu_signal(output, (uint16_t)(present + 1U), state->voltage_state[index] <= 1U);
dtu_signal(output, (uint16_t)(present + 2U), dtu_delay(state, TIMER_HIGH_V + index,
voltage >= dtu_value(settings, 0x8029U), dtu_value(settings, 0x802aU), input->now_ms));
dtu_signal(output, (uint16_t)(present + 3U), dtu_delay(state, TIMER_LOW_V + index,
voltage <= dtu_value(settings, 0x8027U), dtu_value(settings, 0x8028U), input->now_ms));
any_dead = any_dead || state->voltage_state[index] <= 1U;
}
/* The demo models the station AC supply from PT1; neither PT fuse failure
* nor a physical broken conductor can be inferred from these two RMS values. */
dtu_signal(output, 0x9141U, output->voltage_state[0] <= 1U);
dtu_signal(output, 0x918eU, dtu_delay(state, TIMER_BROKEN,
dtu_on(settings, 0x8831U) && input->breaker_closed && any_dead,
dtu_value(settings, 0x8832U), input->now_ms));
}
static bool dtu_sync_block(DtuProtection *state, const DtuSettings *settings,
const DtuProtectionInput *input)
{
/* Compare shortest signed angular distance, including e.g. 359 degrees. */
double angle = fabs(remainder(input->phase_angle, 360.0));
bool mismatch = fabs(input->pt_voltage[0] - input->pt_voltage[1]) > dtu_value(settings, 0x8858U) ||
angle > dtu_value(settings, 0x8859U);
if (!dtu_on(settings, 0x8857U)) {
state->sync_blocked = false;
(void)dtu_delay(state, TIMER_SYNC, false, 0.0, input->now_ms);
} else if (mismatch) {
state->sync_blocked = true;
(void)dtu_delay(state, TIMER_SYNC, false, 0.0, input->now_ms);
} else if (dtu_delay(state, TIMER_SYNC, true, dtu_value(settings, 0x885aU), input->now_ms)) {
state->sync_blocked = false;
}
return state->sync_blocked;
}
static bool dtu_reclose_enabled(const DtuSettings *settings, uint32_t reason)
{
/* Every concurrent cause must permit reclosing; external trips never do. */
if ((reason & DTU_TRIP_EXTERNAL) != 0U) { return false; }
if ((reason & DTU_TRIP_PHASE) != 0U && !dtu_on(settings, 0x8863U)) { return false; }
if ((reason & DTU_TRIP_EARTH) != 0U && !dtu_on(settings, 0x8868U)) { return false; }
if ((reason & DTU_TRIP_SMALL_EARTH) != 0U && !dtu_on(settings, 0x8869U)) { return false; }
return (reason & (DTU_TRIP_PHASE | DTU_TRIP_EARTH | DTU_TRIP_SMALL_EARTH)) != 0U;
}
static void dtu_lock(DtuProtection *state, uint64_t now)
{
if (!state->locked) { state->lock_ms = now; }
state->locked = true;
state->pending_reclose = false;
state->awaiting_close = false;
}
void Dtu_Protection_Cancel_Reclose(DtuProtection *state, uint64_t now_ms)
{
/* Ordinary operator switching does not create a protection lockout. */
if (!state->awaiting_open && !state->pending_reclose && !state->awaiting_close &&
state->reclose_attempts == 0U && state->reclose_reason == 0U) { return; }
dtu_lock(state, now_ms);
state->lock_ms = now_ms;
state->awaiting_open = false;
state->reclose_reason = 0U;
(void)dtu_delay(state, TIMER_RECLOSE_HEALTHY, false, 0.0, now_ms);
}
static void dtu_breaker_feedback(DtuProtection *state, const DtuSettings *settings,
const DtuProtectionInput *input)
{
if (input->breaker_closed && !state->previous_closed) {
state->close_ms = input->now_ms;
state->have_close = true;
/* Any observed close terminates this pending operation. */
state->pending_reclose = false;
state->awaiting_close = false;
}
if (!input->breaker_closed && state->previous_closed) {
uint64_t window = dtu_ms(dtu_value(settings, 0x887dU));
if (state->consecutive_opens == 0U || input->now_ms - state->open_window_ms > window) {
state->open_window_ms = input->now_ms;
state->consecutive_opens = 0U;
}
if (state->consecutive_opens < UINT8_MAX) { ++state->consecutive_opens; }
if (dtu_value(settings, 0x887eU) > 0.0 &&
(double)state->consecutive_opens >= dtu_value(settings, 0x887eU)) {
dtu_lock(state, input->now_ms);
}
if (state->awaiting_open) {
state->awaiting_open = false;
state->reclose_open_ms = input->now_ms;
state->pending_reclose = !state->locked && dtu_reclose_enabled(settings, state->reclose_reason);
}
}
}
static void dtu_reclose(DtuProtection *state, const DtuSettings *settings,
const DtuProtectionInput *input, double phase_max, bool live_trip, DtuProtectionOutput *output)
{
unsigned maximum = (unsigned)dtu_value(settings, 0x8864U);
bool high_current = dtu_on(settings, 0x886dU) && phase_max >= dtu_value(settings, 0x886eU);
bool voltage_ready = !dtu_on(settings, 0x887bU) || dtu_delay(state, TIMER_RECLOSE_VOLTAGE,
state->pending_reclose && output->voltage_state[0] == 3U,
dtu_value(settings, 0x887cU), input->now_ms);
bool healthy = input->breaker_closed && !live_trip && !high_current &&
!output->trip_blocked && !output->close_blocked;
if (!dtu_on(settings, 0x887bU)) { (void)dtu_delay(state, TIMER_RECLOSE_VOLTAGE, false, 0.0, input->now_ms); }
if (maximum > 3U) { maximum = 3U; }
dtu_signal(output, 0x9175U, high_current);
if (high_current && (state->awaiting_open || state->pending_reclose)) { dtu_lock(state, input->now_ms); }
if (state->pending_reclose && (!dtu_reclose_enabled(settings, state->reclose_reason) ||
state->reclose_attempts >= maximum)) { dtu_lock(state, input->now_ms); }
/* Lockout can auto-release only while closed, healthy for the entire group
* recovery time, and after the minimum lockout time. An open failed feeder
* therefore cannot repeatedly auto-close forever. Reset is always explicit. */
if (dtu_delay(state, TIMER_RECLOSE_HEALTHY, healthy, dtu_value(settings, 0x886fU), input->now_ms) &&
(!state->locked || input->now_ms - state->lock_ms >= dtu_ms(dtu_value(settings, 0x8870U)))) {
state->locked = false;
state->reclose_attempts = 0U;
state->consecutive_opens = 0U;
state->reclose_reason = 0U;
}
if (state->pending_reclose && !state->awaiting_close && !input->breaker_closed && !state->locked &&
!output->close_blocked && !output->trip_request && voltage_ready &&
input->now_ms - state->reclose_open_ms >= dtu_ms(dtu_value(settings, 0x8865U + state->reclose_attempts))) {
uint16_t source = (state->reclose_reason & DTU_TRIP_PHASE) != 0U ? 0x917bU : 0x917eU;
output->close_request = true;
dtu_signal(output, (uint16_t)(source + state->reclose_attempts), true);
dtu_signal(output, 0x919dU, true);
++state->reclose_attempts;
state->awaiting_close = true;
/* A missed close feedback is fail-stop: no repeated request is emitted.
* The caller may Reset after diagnosing the output/feedback path. */
}
output->close_blocked = output->close_blocked || state->locked || high_current;
dtu_signal(output, 0x9174U, state->locked || output->close_blocked);
output->reclose_attempts = state->reclose_attempts;
}
void Dtu_Protection_Step(DtuProtection *state, const DtuSettings *settings,
const DtuProtectionInput *input, DtuProtectionOutput *output)
{
double phase_max;
uint64_t since_close;
bool harmonic, interrupt_block, control_block, frequency_block, accelerated;
bool phase_pickup = false, live_trip, first;
uint32_t reasons = 0U, record_level = 0U;
unsigned index;
memset(output, 0, sizeof(*output));
first = !state->initialized || !state->previous_sample_valid;
if (!dtu_input_valid(input) || (!first && input->now_ms < state->previous_ms)) {
state->timer_active = 0U;
if (state->pending_reclose || state->awaiting_open || state->awaiting_close) { dtu_lock(state, input->now_ms); }
state->awaiting_open = false;
state->previous_sample_valid = false;
state->have_close = false;
if (state->locked) { state->lock_ms = input->now_ms; }
state->open_window_ms = input->now_ms;
state->previous_ms = input->now_ms;
output->close_blocked = output->trip_blocked = true;
output->reclose_attempts = state->reclose_attempts;
/* Do not expire retained indicators against an invalid/rolled-back clock. */
for (index = 0U; index < sizeof(dtu_latched_sources) / sizeof(dtu_latched_sources[0]); ++index) {
dtu_signal(output, dtu_latched_sources[index], (state->signal_latched & (UINT64_C(1) << index)) != 0U);
state->signal_last[index] = input->now_ms;
}
dtu_signal(output, 0x9174U, true);
return;
}
output->input_valid = true;
state->previous_sample_valid = true;
if (first) {
state->initialized = true;
state->previous_closed = input->breaker_closed;
state->close_ms = input->now_ms;
state->have_close = input->breaker_closed;
state->area = settings->current_area;
} else if (state->area != settings->current_area) {
/* New area starts fresh pickup delays; retains completed operations. */
state->timer_active = 0U;
state->area = settings->current_area;
}
dtu_breaker_feedback(state, settings, input);
phase_max = fmax(input->phase_current[0], fmax(input->phase_current[1], input->phase_current[2]));
since_close = state->have_close ? input->now_ms - state->close_ms : 0U;
harmonic = dtu_on(settings, 0x8876U) && state->have_close &&
since_close < dtu_ms(dtu_value(settings, 0x8879U)) && input->second_harmonic >= dtu_value(settings, 0x8875U);
accelerated = state->have_close && state->reclose_attempts != 0U &&
since_close < dtu_ms(dtu_value(settings, 0x887aU));
interrupt_block = dtu_on(settings, 0x8861U) && phase_max >= dtu_value(settings, 0x8862U);
control_block = dtu_on(settings, 0x8871U) && input->control_voltage < dtu_value(settings, 0x8872U);
frequency_block = dtu_delay(state, TIMER_FREQUENCY, dtu_on(settings, 0x8873U) &&
input->frequency < dtu_value(settings, 0x8874U), dtu_value(settings, 0x8878U), input->now_ms);
output->trip_blocked = interrupt_block || control_block;
output->close_blocked = input->external_close_blocked || control_block || frequency_block ||
(dtu_on(settings, 0x8877U) && input->non_electrical_fault);
/* Always advance synchronization recovery even while another block is set. */
output->close_blocked = dtu_sync_block(state, settings, input) || output->close_blocked;
dtu_signal(output, 0x9170U, harmonic);
dtu_signal(output, 0x9173U, interrupt_block);
/* The host control supply is its simulated battery rail. */
dtu_signal(output, 0x9143U, control_block);
dtu_signal(output, 0x9191U, state->sync_blocked);
dtu_signal(output, 0x9192U, state->sync_blocked);
dtu_voltage(state, settings, input, output);
for (index = 0U; index < 5U; ++index) {
uint32_t base = 0x8840U + index * 4U;
bool phase = index < 3U;
bool enabled = dtu_on(settings, base) || dtu_on(settings, base + 1U);
bool pickup = input->breaker_closed && !harmonic && enabled &&
(phase ? phase_max : input->zero_current) >= dtu_value(settings, base + 2U);
bool expired = dtu_delay(state, index, pickup, dtu_value(settings, base + 3U), input->now_ms);
bool trip = expired && dtu_on(settings, base + 1U) && !output->trip_blocked;
uint16_t alarm_source = (uint16_t)(phase ? 0x916aU + index : 0x9171U + index - 3U);
if (phase && pickup) { phase_pickup = true; }
dtu_signal(output, alarm_source, expired && dtu_on(settings, base));
dtu_signal(output, (uint16_t)(0x9176U + index), trip);
if (trip) { reasons |= phase ? DTU_TRIP_PHASE : DTU_TRIP_EARTH; }
}
for (index = 0U; index < 3U; ++index) {
uint32_t base = 0x8046U + index * 2U;
static const uint16_t sources[] = {0x916dU, 0x916fU, 0x916eU};
dtu_signal(output, sources[index], dtu_delay(state, TIMER_LOAD + index,
input->breaker_closed && phase_max >= dtu_value(settings, base),
dtu_value(settings, base + 1U), input->now_ms));
}
dtu_signal(output, 0x9166U, dtu_delay(state, TIMER_NEGATIVE, dtu_on(settings, 0x8883U) &&
input->negative_current >= dtu_value(settings, 0x8884U), dtu_value(settings, 0x8885U), input->now_ms));
dtu_signal(output, 0x918dU, dtu_delay(state, TIMER_EARTH_ALARM, dtu_on(settings, 0x8854U) &&
input->small_earth_fault, dtu_value(settings, 0x8855U), input->now_ms));
if (dtu_delay(state, TIMER_EARTH_TRIP, input->breaker_closed && dtu_on(settings, 0x8881U) &&
input->small_earth_fault, dtu_value(settings, 0x8882U), input->now_ms) && !output->trip_blocked) {
reasons |= DTU_TRIP_SMALL_EARTH;
dtu_signal(output, 0x9184U, true);
}
for (index = 0U; index < 3U; ++index) {
uint32_t enable = index == 0U ? 0x885bU : (index == 1U ? 0x885eU : 0x887fU);
uint32_t threshold = index == 0U ? 0x885cU : (index == 1U ? 0x885fU : 0x8880U);
double measured = index == 0U ? phase_max : (index == 1U ? input->zero_current : input->zero_voltage);
/* Zero-voltage acceleration shares the zero-current acceleration delay,
* because the source provides no separate delay for that element. */
double delay = dtu_value(settings, index == 0U ? 0x885dU : 0x8860U);
bool trip = dtu_delay(state, TIMER_ACCEL_PHASE + index, input->breaker_closed && accelerated &&
!harmonic && dtu_on(settings, enable) && measured >= dtu_value(settings, threshold),
delay, input->now_ms) && !output->trip_blocked;
dtu_signal(output, (uint16_t)(0x9181U + index), trip);
if (trip) { reasons |= DTU_TRIP_ACCELERATED | (index == 0U ? DTU_TRIP_PHASE : DTU_TRIP_EARTH); }
}
if (input->breaker_closed && dtu_on(settings, 0x8877U) && input->non_electrical_fault && !output->trip_blocked) {
reasons |= DTU_TRIP_EXTERNAL;
}
dtu_signal(output, 0x919eU, (reasons & DTU_TRIP_ACCELERATED) != 0U);
live_trip = reasons != 0U;
dtu_signal(output, 0x9168U, live_trip || (dtu_on(settings, 0x8856U) && dtu_signal_value(output, 0x918dU)));
if (live_trip && !state->awaiting_open) {
output->trip_request = true;
output->trip_reasons = reasons;
state->awaiting_open = true;
state->reclose_reason = reasons;
state->pending_reclose = false;
state->awaiting_close = false;
} else if (live_trip && state->awaiting_open) {
state->reclose_reason |= reasons;
}
dtu_reclose(state, settings, input, phase_max, live_trip, output);
if (dtu_on(settings, 0x804cU) && phase_pickup) { record_level |= DTU_RECORD_OVERCURRENT; }
if (dtu_on(settings, 0x804dU) && input->pt_voltage[0] <= dtu_value(settings, 0x8053U)) {
record_level |= DTU_RECORD_VOLTAGE_LOSS;
}
output->recording_reasons = record_level & ~state->previous_record_level;
if (!first && dtu_on(settings, 0x804eU) &&
fabs(input->zero_current - state->previous_zero_current) >= dtu_value(settings, 0x8051U) &&
input->zero_current != state->previous_zero_current) { output->recording_reasons |= DTU_RECORD_ZERO_CURRENT; }
if (!first && dtu_on(settings, 0x804fU) &&
fabs(input->zero_voltage - state->previous_zero_voltage) >= dtu_value(settings, 0x8052U) &&
input->zero_voltage != state->previous_zero_voltage) { output->recording_reasons |= DTU_RECORD_ZERO_VOLTAGE; }
state->previous_record_level = record_level;
state->previous_zero_current = input->zero_current;
state->previous_zero_voltage = input->zero_voltage;
dtu_signal(output, 0x9148U, output->recording_reasons != 0U);
dtu_signal(output, 0x9186U, output->recording_reasons != 0U);
dtu_indications(state, settings, input->now_ms, output);
state->previous_ms = input->now_ms;
state->previous_closed = input->breaker_closed;
}