298 lines
16 KiB
C++
298 lines
16 KiB
C++
/*
|
|
* DTU 模拟采样、电量积分及延迟遥信发布,由共享引擎在发布队列定义后包含。
|
|
*/
|
|
|
|
/* 普通模式先积分上一拍 P/Q,再更新采样;读/冻结时补算到当前时刻。
|
|
* 内部保留 Wh/varh 小数,Type206/207 再转 kWh/kvarh;整数编码使用截断值。
|
|
* 冻结文件模式由独立计量器接管,避免重复累计。 */
|
|
static void Example_Dtu_Accumulate_Energy(ExampleApp *app, uint64_t now)
|
|
{
|
|
double p = 0.0, q = 0.0, hours;
|
|
size_t index, quadrant;
|
|
if (app->energy != NULL || app->events_interval_ms == 0U) { return; }
|
|
if (now <= app->dtu_energy_last_ms) { app->dtu_energy_last_ms = now; return; }
|
|
hours = (double)(now - app->dtu_energy_last_ms) / 3600000.0 *
|
|
Dtu_Measurement_Energy_Factor(&app->dtu_settings);
|
|
app->dtu_energy_last_ms = now;
|
|
for (index = 0U; index < EXAMPLE_MEASUREMENT_COUNT; ++index) {
|
|
if (dtu_measurements[index].source_addr == 0x9627U) { p = Example_Physical_Measurement(app, index); }
|
|
if (dtu_measurements[index].source_addr == 0x9628U) { q = Example_Physical_Measurement(app, index); }
|
|
}
|
|
app->dtu_energy_wh[p >= 0.0 ? 0U : 2U] += (p >= 0.0 ? p : -p) * hours;
|
|
app->dtu_energy_wh[q >= 0.0 ? 1U : 3U] += (q >= 0.0 ? q : -q) * hours;
|
|
/* 后四项顺序为 I、IV、II、III 象限,按 |Q| 积分,不是按象限编号排序。 */
|
|
quadrant = p >= 0.0 ? (q >= 0.0 ? 4U : 5U) : (q >= 0.0 ? 6U : 7U);
|
|
app->dtu_energy_wh[quadrant] += (q >= 0.0 ? q : -q) * hours;
|
|
for (index = 0U; index < EXAMPLE_COUNTER_COUNT; ++index) {
|
|
app->counter_current[index] = app->dtu_energy_wh[index] >= (double)INT32_MAX ?
|
|
INT32_MAX : (int32_t)app->dtu_energy_wh[index];
|
|
}
|
|
}
|
|
|
|
/* 每点只保留最新值和原变位时刻;历史记录与发布队列均接手后清 pending 位。 */
|
|
static bool Example_Dtu_Flush_Signals(ExampleApp *app)
|
|
{
|
|
size_t index;
|
|
for (index = 0U; index < EXAMPLE_SIGNAL_COUNT; ++index) {
|
|
ExamplePublication entry;
|
|
uint64_t mask = UINT64_C(1) << index;
|
|
if ((app->dtu_signal_pending & mask) == 0U) { continue; }
|
|
memset(&entry, 0, sizeof(entry));
|
|
entry.has_change = true;
|
|
entry.change.common_address = IEC104_Station_Get_Common_Address(&app->station);
|
|
entry.change.station_id = IEC104_Station_Get_Id(&app->station);
|
|
entry.change.time_kind = IEC104_SPONTANEOUS_TIME_MONOTONIC;
|
|
entry.change.time_ms = app->dtu_signal_time[index];
|
|
entry.change.kind = IEC104_SPONTANEOUS_SINGLE;
|
|
entry.change.ioa = dtu_signals[index].ioa;
|
|
entry.change.value.single = app->signal_values[index];
|
|
if (!Example_History_Change(app, &entry.change) || !Example_Queue_Publication(app, &entry)) { return false; }
|
|
app->dtu_signal_pending &= ~mask;
|
|
}
|
|
return true;
|
|
}
|
|
|
|
/* 本地仿真输入为 source=value,source 是十六进制源地址,模拟量使用二次单位。
|
|
* 整个文件验证后才推进一拍;无效行不允许部分更新。B001..B005 是明确的测试输入,
|
|
* 分别为负序电流、二次谐波电流、两侧相角差、小接地检测、非电量故障。 */
|
|
static bool Example_Dtu_Load_Input(ExampleApp *app, DtuProtectionInput *protection,
|
|
uint8_t external[DTU_PROTECTION_SIGNAL_COUNT], uint8_t present[DTU_PROTECTION_SIGNAL_COUNT])
|
|
{
|
|
FILE *file;
|
|
char line[192], extra;
|
|
unsigned source, number = 0U;
|
|
double value;
|
|
uint8_t seen[DTU_MEASUREMENT_SOURCE_COUNT + DTU_PROTECTION_SIGNAL_COUNT + 5U] = {0};
|
|
if (app->dtu_input_path == NULL) { return true; }
|
|
file = fopen(app->dtu_input_path, "rb");
|
|
if (file == NULL) { fprintf(stderr, "DTU_INPUT cannot open %s\n", app->dtu_input_path); return false; }
|
|
while (fgets(line, sizeof(line), file) != NULL) {
|
|
char *first = line;
|
|
size_t slot;
|
|
++number;
|
|
while (*first == ' ' || *first == '\t') { ++first; }
|
|
if (*first == '#' || *first == '\r' || *first == '\n' || *first == '\0') { continue; }
|
|
if (sscanf(first, "%x = %lf %c", &source, &value, &extra) != 2 || !isfinite(value)) { goto invalid; }
|
|
if (Dtu_Measurement_Source_Valid(source)) {
|
|
slot = Dtu_Measurement_Index(source);
|
|
if (!Dtu_Measurement_Override_Secondary(&app->dtu_input, source, value)) { goto invalid; }
|
|
} else if (source >= 0x9141U && source <= 0x919DU) {
|
|
if (value != 0.0 && value != 1.0) { goto invalid; }
|
|
slot = DTU_MEASUREMENT_SOURCE_COUNT + source - 0x9140U;
|
|
present[source - 0x9140U] = 1U;
|
|
external[source - 0x9140U] = (uint8_t)value;
|
|
} else if (source >= 0xB001U && source <= 0xB005U) {
|
|
slot = DTU_MEASUREMENT_SOURCE_COUNT + DTU_PROTECTION_SIGNAL_COUNT + source - 0xB001U;
|
|
if (source == 0xB001U) { protection->negative_current = value; }
|
|
if (source == 0xB002U) { protection->second_harmonic = value; }
|
|
if (source == 0xB003U) { protection->phase_angle = value; }
|
|
if (source >= 0xB004U && value != 0.0 && value != 1.0) { goto invalid; }
|
|
if (source == 0xB004U) { protection->small_earth_fault = value != 0.0; }
|
|
if (source == 0xB005U) { protection->non_electrical_fault = value != 0.0; }
|
|
} else { goto invalid; }
|
|
if (seen[slot] != 0U) { goto invalid; }
|
|
seen[slot] = 1U;
|
|
}
|
|
if (ferror(file)) { fclose(file); return false; }
|
|
return fclose(file) == 0;
|
|
invalid:
|
|
fprintf(stderr, "DTU_INPUT invalid/duplicate entry at line %u\n", number);
|
|
fclose(file);
|
|
return false;
|
|
}
|
|
|
|
/* 输出取反不影响保护和遥控许可;所有93个源均可通过Type102源地址观察。 */
|
|
static void Example_Dtu_Update_Digital(ExampleApp *app, uint64_t now)
|
|
{
|
|
size_t source, index;
|
|
uint64_t delay = (uint64_t)(dtu_setting(app, 0x802BU, 0.02) * 1000.0 + 0.5);
|
|
for (source = 1U; source <= 0x5DU; ++source) {
|
|
uint8_t raw = app->dtu_digital[source];
|
|
if (raw != app->dtu_digital_candidate[source]) {
|
|
app->dtu_digital_candidate[source] = raw;
|
|
app->dtu_digital_since[source] = now;
|
|
}
|
|
if (now >= app->dtu_digital_since[source] &&
|
|
now - app->dtu_digital_since[source] >= delay) {
|
|
app->dtu_digital_output[source] = raw ^
|
|
(dtu_setting(app, (uint32_t)(0x9E00U + source), 0.0) != 0.0);
|
|
}
|
|
}
|
|
for (index = 0U; index < DTU_SIGNAL_COUNT; ++index) {
|
|
uint16_t address = dtu_signals[index].source_addr;
|
|
if (address >= 0x9141U && address <= 0x919DU) {
|
|
Example_Dtu_Set_Signal(app, dtu_signals[index].ioa,
|
|
app->dtu_digital_output[address - 0x9140U], now);
|
|
} else if (address == 0x919EU) {
|
|
Example_Dtu_Set_Signal(app, dtu_signals[index].ioa, app->dtu_digital[0x5EU], now);
|
|
}
|
|
}
|
|
}
|
|
|
|
static bool Example_Dtu_Automatic_Breaker(ExampleApp *app, bool closed, uint64_t now)
|
|
{
|
|
ExamplePublication entry;
|
|
uint8_t value = closed ? 2U : 1U;
|
|
if (app->double_values[0] == value) { return true; }
|
|
memset(&entry, 0, sizeof(entry));
|
|
entry.has_change = true;
|
|
entry.change.common_address = IEC104_Station_Get_Common_Address(&app->station);
|
|
entry.change.station_id = IEC104_Station_Get_Id(&app->station);
|
|
entry.change.time_kind = IEC104_SPONTANEOUS_TIME_MONOTONIC;
|
|
entry.change.time_ms = now;
|
|
entry.change.kind = IEC104_SPONTANEOUS_DOUBLE;
|
|
entry.change.ioa = EXAMPLE_IOA_DOUBLE_BASE;
|
|
entry.change.value.double_point = value;
|
|
app->double_values[0] = value;
|
|
app->dtu_pulse_until[0] = now + (uint64_t)(dtu_setting(app, closed ? 0x802DU : 0x802CU, 0.15) * 1000.0 + 0.5);
|
|
app->dtu_digital[0x1FU] = closed;
|
|
app->dtu_digital[0x20U] = !closed;
|
|
return Example_History_Change(app, &entry.change) && Example_Queue_Publication(app, &entry);
|
|
}
|
|
|
|
static void Example_Dtu_Battery(ExampleApp *app, uint64_t now)
|
|
{
|
|
IEC60870_ClockValue_t wall;
|
|
IEC60870_CP56Time2a_t time;
|
|
uint32_t seconds;
|
|
uint64_t utc;
|
|
if (app->clock_valid && now >= app->clock_base_ms) { utc = app->clock_utc_ms + now - app->clock_base_ms; }
|
|
else {
|
|
if (!IEC60870_Clock_Read(&wall) || wall.is_invalid != 0U) { return; }
|
|
memset(&time, 0, sizeof(time)); time.time = wall.time;
|
|
if (!IEC60870_Time_CP56_To_Seconds(&time, IEC60870_LOCAL_UTC_OFFSET_MINUTES, &seconds)) { return; }
|
|
utc = (uint64_t)seconds * 1000U + time.time.ms;
|
|
}
|
|
uint64_t day = utc / UINT64_C(86400000);
|
|
unsigned hour = (unsigned)(utc / UINT64_C(3600000) % 24U);
|
|
uint32_t period = (uint32_t)dtu_setting(app, 0x802EU, 90.0);
|
|
if (!app->dtu_activation_scheduled) {
|
|
app->dtu_activation_day = day;
|
|
app->dtu_activation_scheduled = true;
|
|
}
|
|
/* 周期为0禁自动活化;使用示例UTC墙钟,倒拨不补发,手动启动也重置周期。 */
|
|
if (period != 0U && day >= app->dtu_activation_day && day - app->dtu_activation_day >= period &&
|
|
hour == (unsigned)dtu_setting(app, 0x802FU, 0.0) && !app->dtu_activation) {
|
|
app->dtu_activation = true;
|
|
app->dtu_activation_since = now;
|
|
app->dtu_activation_day = day;
|
|
}
|
|
if (app->dtu_activation) {
|
|
double elapsed = (double)(now - app->dtu_activation_since) / 3600000.0;
|
|
double maximum = app->dtu_frame.secondary[0x9603U - 0x9601U];
|
|
if (elapsed >= maximum || app->dtu_frame.secondary[0] <= dtu_setting(app, 0x8872U, 20.0)) {
|
|
app->dtu_activation = false;
|
|
}
|
|
app->dtu_frame.secondary[3] = elapsed;
|
|
app->dtu_frame.primary[3] = elapsed * dtu_setting(app, 0xA2C4U, 1.0);
|
|
}
|
|
app->dtu_digital[2] = app->dtu_activation;
|
|
}
|
|
|
|
static uint16_t Example_Dtu_Recording_Bits(const ExampleApp *app)
|
|
{
|
|
bool trip = app->dtu_digital[0x36U] || app->dtu_digital[0x37U] || app->dtu_digital[0x38U] ||
|
|
app->dtu_digital[0x39U] || app->dtu_digital[0x3AU] || app->dtu_digital[0x41U] ||
|
|
app->dtu_digital[0x42U] || app->dtu_digital[0x43U] || app->dtu_digital[0x44U];
|
|
return (uint16_t)((trip ? 1U : 0U) | (app->dtu_digital[0x46U] ? 2U : 0U) |
|
|
(app->dtu_digital[0x2AU] ? 4U : 0U) | (app->dtu_digital[0x2BU] ? 8U : 0U) |
|
|
(app->dtu_digital[0x2CU] ? 16U : 0U) | (app->dtu_digital[0x4CU] ? 32U : 0U) |
|
|
(app->dtu_digital[0x43U] ? 64U : 0U) | (app->double_values[0] == 2U ? 0x80U : 0x100U));
|
|
}
|
|
|
|
/* 测量、保护、遥信分别使用上送一次量、二次量和未取反逻辑,避免彼此污染。 */
|
|
static bool Example_Dtu_Field(ExampleApp *app, uint64_t now)
|
|
{
|
|
static const uint16_t capture_sources[8] = {0x9605U, 0x9606U, 0x9607U, 0x960BU,
|
|
0x9622U, 0x9623U, 0x9624U, 0x9625U};
|
|
DtuProtectionInput protection;
|
|
double before[8];
|
|
bool had_sample = app->dtu_sample_valid;
|
|
uint16_t before_digital = Example_Dtu_Recording_Bits(app);
|
|
uint8_t external[DTU_PROTECTION_SIGNAL_COUNT] = {0};
|
|
uint8_t present[DTU_PROTECTION_SIGNAL_COUNT] = {0};
|
|
size_t index;
|
|
for (index = 0U; index < 8U; ++index) {
|
|
before[index] = app->dtu_frame.secondary[Dtu_Measurement_Index(capture_sources[index])];
|
|
}
|
|
memset(&protection, 0, sizeof(protection));
|
|
Dtu_Measurement_Init(&app->dtu_input);
|
|
if (!Example_Dtu_Load_Input(app, &protection, external, present) ||
|
|
!Dtu_Measurement_Sample(&app->dtu_settings, now - app->dtu_started_ms,
|
|
app->double_values[0] == 2U, &app->dtu_input) ||
|
|
!Dtu_Measurement_Process(&app->dtu_settings, &app->dtu_input, &app->dtu_frame)) { return false; }
|
|
Example_Dtu_Battery(app, now);
|
|
for (index = 0U; index < 3U; ++index) { protection.phase_current[index] = app->dtu_frame.secondary[0x9622U - 0x9601U + index]; }
|
|
protection.zero_current = app->dtu_frame.secondary[0x9625U - 0x9601U];
|
|
protection.pt_voltage[0] = app->dtu_frame.secondary[0x960AU - 0x9601U];
|
|
protection.pt_voltage[1] = app->dtu_frame.secondary[0x9613U - 0x9601U];
|
|
protection.zero_voltage = app->dtu_frame.secondary[0x960BU - 0x9601U];
|
|
protection.frequency = app->dtu_frame.secondary[0x960DU - 0x9601U];
|
|
protection.control_voltage = app->dtu_frame.secondary[0];
|
|
protection.breaker_closed = app->double_values[0] == 2U;
|
|
/* 本拍接点必须先进入合闸许可,不能到重合请求产生以后才应用闭锁。 */
|
|
for (index = 1U; index < DTU_PROTECTION_SIGNAL_COUNT; ++index) {
|
|
if (present[index] != 0U && !Dtu_Protection_Manages_Source((uint16_t)(0x9140U + index))) {
|
|
app->dtu_digital[index] = external[index];
|
|
}
|
|
}
|
|
protection.external_close_blocked = app->dtu_pulse_until[0] > now ||
|
|
app->dtu_digital[0x22U] != 0U || app->dtu_digital[0x23U] != 0U ||
|
|
app->dtu_digital[0x24U] != 0U || app->dtu_digital[0x49U] != 0U || app->dtu_digital[0x5CU] != 0U;
|
|
protection.now_ms = now;
|
|
Dtu_Protection_Step(&app->dtu_protection, &app->dtu_settings, &protection, &app->dtu_protection_output);
|
|
if (!app->dtu_protection_output.input_valid) { fputs("DTU invalid protection sample\n", stderr); return false; }
|
|
app->dtu_sample_valid = true;
|
|
for (index = 1U; index < DTU_PROTECTION_SIGNAL_COUNT; ++index) {
|
|
if (Dtu_Protection_Manages_Source((uint16_t)(0x9140U + index))) {
|
|
app->dtu_digital[index] = app->dtu_protection_output.digital[index];
|
|
}
|
|
}
|
|
if (app->dtu_protection_output.trip_request && !Example_Dtu_Automatic_Breaker(app, false, now)) { return false; }
|
|
if (app->dtu_protection_output.close_request && !Example_Dtu_Automatic_Breaker(app, true, now)) { return false; }
|
|
if (app->dtu_recording_directory != NULL && app->dtu_protection_output.recording_reasons != 0U &&
|
|
app->dtu_recording_pending == 0U) {
|
|
IEC60870_ClockValue_t wall;
|
|
for (index = 0U; index < 8U; ++index) {
|
|
app->dtu_recording_after[index] = app->dtu_frame.secondary[Dtu_Measurement_Index(capture_sources[index])];
|
|
app->dtu_recording_before[index] = had_sample ? before[index] : app->dtu_recording_after[index];
|
|
}
|
|
if (app->clock_valid) {
|
|
Example_Monitor_Time(app, now, &app->dtu_recording_time);
|
|
}
|
|
if (!app->clock_valid || app->dtu_recording_time.is_invalid != 0U) {
|
|
if (!IEC60870_Clock_Read(&wall) || wall.is_invalid != 0U) { return false; }
|
|
memset(&app->dtu_recording_time, 0, sizeof(app->dtu_recording_time));
|
|
app->dtu_recording_time.time = wall.time;
|
|
}
|
|
app->dtu_recording_before_digital = before_digital;
|
|
app->dtu_recording_after_digital = Example_Dtu_Recording_Bits(app) | 2U;
|
|
app->dtu_recording_pending = app->dtu_protection_output.recording_reasons;
|
|
}
|
|
for (index = 0U; index < EXAMPLE_MEASUREMENT_COUNT; ++index) {
|
|
uint16_t source = dtu_measurements[index].source_addr;
|
|
double value = app->dtu_frame.primary[Dtu_Measurement_Index(source)];
|
|
bool overflow;
|
|
int16_t raw;
|
|
raw = Dtu_Measurement_To_Scaled(value, Example_Measurement_Scale(index), &overflow);
|
|
Example_Set_Measurement(app, index, raw);
|
|
if (app->parameter == NULL && Dtu_Measurement_Changed((double)raw / Example_Measurement_Scale(index),
|
|
(double)app->dtu_submitted[index] / Example_Measurement_Scale(index),
|
|
Dtu_Measurement_Deadband(&app->dtu_settings, source))) {
|
|
ExamplePublication entry;
|
|
memset(&entry, 0, sizeof(entry));
|
|
entry.has_change = true;
|
|
entry.change.common_address = IEC104_Station_Get_Common_Address(&app->station);
|
|
entry.change.station_id = IEC104_Station_Get_Id(&app->station);
|
|
entry.change.time_kind = IEC104_SPONTANEOUS_TIME_MONOTONIC;
|
|
entry.change.time_ms = now;
|
|
entry.change.kind = IEC104_SPONTANEOUS_SCALED;
|
|
entry.change.ioa = dtu_measurements[index].ioa;
|
|
entry.change.value.scaled = app->scaled_values[index];
|
|
if (!Example_Queue_Publication(app, &entry)) { return false; }
|
|
}
|
|
}
|
|
Example_Dtu_Update_Digital(app, now);
|
|
return Example_Dtu_Flush_Signals(app);
|
|
}
|