/*
* 专利全流程仿真:基于嵌入式多目标优化的断路器智能控制
* (轻量化版 - 适配 STM32C031C6 32KB Flash)
*/
#include <math.h>
// 硬件引脚定义
const int PIN_DIDT = A0; // 模拟故障电流斜率
const int PIN_TEMP = A1; // 模拟环境温度
const int PIN_IGBT = PA5; // 动作指示灯
// --- 专利核心模块 1: 嵌入式轻量化预测模型 ---
// 优化:使用 x * sqrt(x) 代替 pow(x, 1.5) 以节省 Flash
float Predict_Arc_Energy(float t_delay_us, float current_slope) {
// 归一化时间,避免数值溢出
float t_norm = t_delay_us / 1000.0;
// Energy = k * slope * t^1.5
return 0.001 * current_slope * (t_norm * sqrt(t_norm));
}
float Predict_Thermal_Stress(float t_delay_us, float temp_env) {
// Stress = Base_Temp + (k / t)
return temp_env + (500000.0 / (t_delay_us + 100.0));
}
// --- 专利核心模块 2: 多目标综合评估函数 ---
float Cost_Function(float t_test, float slope, float temp) {
float w1 = 0.6;
float w2 = 0.4;
float energy = Predict_Arc_Energy(t_test, slope);
float stress = Predict_Thermal_Stress(t_test, temp);
return (w1 * energy) + (w2 * stress);
}
// --- 专利核心模块 3: 黄金分割搜索算法 ---
float Golden_Section_Search(float min_t, float max_t, float slope, float temp) {
const float phi = 0.618; // 简化的黄金分割比
float a = min_t;
float b = max_t;
float c = b - phi * (b - a);
float d = a + phi * (b - a);
// 减少迭代次数到 8 次,足够演示且更快
for (int i = 0; i < 8; i++) {
float cost_c = Cost_Function(c, slope, temp);
float cost_d = Cost_Function(d, slope, temp);
if (cost_c < cost_d) {
b = d;
d = c;
c = b - phi * (b - a);
} else {
a = c;
c = d;
d = a + phi * (b - a);
}
}
return (b + a) / 2.0;
}
void setup() {
Serial.begin(115200);
pinMode(PIN_IGBT, OUTPUT);
pinMode(PIN_DIDT, INPUT);
pinMode(PIN_TEMP, INPUT);
analogReadResolution(12); // 设置ADC精度
Serial.println("\n=== Patent Sim Start ===");
}
void loop() {
// 1. 感知
float raw_didt = analogRead(PIN_DIDT) * (10000.0 / 4095.0);
float raw_temp = analogRead(PIN_TEMP) * (100.0 / 4095.0);
// 2. 决策:在线寻优
unsigned long t_start = micros();
float optimal_delay = Golden_Section_Search(100.0, 5000.0, raw_didt, raw_temp);
unsigned long t_cost = micros() - t_start;
// 3. 输出 (优化:转为整数打印,避免引入浮点打印库)
Serial.print("Slope:");
Serial.print((int)raw_didt);
Serial.print(" Temp:");
Serial.print((int)raw_temp);
Serial.print(" -> Opt_Delay:");
Serial.print((int)optimal_delay); // 关键优化:只打印整数部分
Serial.print("us (Calc:");
Serial.print((int)t_cost);
Serial.println("us)");
// 4. 执行
digitalWrite(PIN_IGBT, HIGH);
delay((int)optimal_delay / 10); // 同样转为int
digitalWrite(PIN_IGBT, LOW);
delay(200);
}Loading
st-nucleo-c031c6
st-nucleo-c031c6