auto
This commit is contained in:
5
.vscode/settings.json
vendored
5
.vscode/settings.json
vendored
@ -1,3 +1,6 @@
|
|||||||
{
|
{
|
||||||
"window.zoomLevel": 0
|
"window.zoomLevel": 0,
|
||||||
|
"files.associations": {
|
||||||
|
"random": "cpp"
|
||||||
|
}
|
||||||
}
|
}
|
||||||
@ -14,46 +14,88 @@ float LightControl::getCurrentLight() {
|
|||||||
return lightSensor.readLightLevel();
|
return lightSensor.readLightLevel();
|
||||||
}
|
}
|
||||||
|
|
||||||
void LightControl::runUntilTargetReached(float target, int maxAttempts) {
|
// 新增:持续调节函数,每次只调节一步
|
||||||
|
void LightControl::adjustToTarget() {
|
||||||
|
static unsigned long lastAdjustTime = 0;
|
||||||
|
const unsigned long adjustInterval = 300; // 每300ms调节一次
|
||||||
|
|
||||||
|
unsigned long currentTime = millis();
|
||||||
|
if (currentTime - lastAdjustTime < adjustInterval) {
|
||||||
|
return; // 还没到调节时间
|
||||||
|
}
|
||||||
|
|
||||||
const float tolerance = 100.0f; // 容许误差范围(±100 lux)
|
const float tolerance = 100.0f; // 容许误差范围(±100 lux)
|
||||||
float currentLight;
|
float currentLight = getCurrentLight();
|
||||||
float res;
|
float res = targetLightLevel - currentLight;
|
||||||
|
|
||||||
int attempt = 0;
|
|
||||||
const int max_retries = maxAttempts; // 最大尝试次数,防止死循环
|
|
||||||
|
|
||||||
do {
|
|
||||||
currentLight = getCurrentLight();
|
|
||||||
res = target - currentLight;
|
|
||||||
|
|
||||||
if (abs(res) <= tolerance) {
|
if (abs(res) <= tolerance) {
|
||||||
Serial.println("Target reached.");
|
// 已达到目标,但不退出,继续监控
|
||||||
break;
|
lastAdjustTime = currentTime;
|
||||||
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
int lastWiperValue = digitalPot.getWiper();
|
int lastWiperValue = digitalPot.getWiper();
|
||||||
|
|
||||||
if (res > 0) {
|
if (res > 0) {
|
||||||
// 需要增加亮度 -> 增加电位器阻值
|
// 需要增加亮度 -> 增加电位器阻值
|
||||||
|
if (lastWiperValue < 127) {
|
||||||
|
lastWiperValue++;
|
||||||
|
digitalPot.setWiper(lastWiperValue);
|
||||||
|
Serial.printf("Auto: Current light: %.0f, Target: %.0f, Wiper: %d\n", currentLight, targetLightLevel, lastWiperValue);
|
||||||
|
}
|
||||||
|
} else if (res < 0) {
|
||||||
|
// 需要减少亮度 -> 减少电位器阻值
|
||||||
|
if (lastWiperValue > 0) {
|
||||||
|
lastWiperValue--;
|
||||||
|
digitalPot.setWiper(lastWiperValue);
|
||||||
|
Serial.printf("Auto: Current light: %.0f, Target: %.0f, Wiper: %d\n", currentLight, targetLightLevel, lastWiperValue);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
lastAdjustTime = currentTime;
|
||||||
|
}
|
||||||
|
|
||||||
|
void LightControl::runUntilTargetReached(float target, int maxAttempts) {
|
||||||
|
// 保留原有函数,但现在主要用于设置目标值
|
||||||
|
setTargetLight(target);
|
||||||
|
Serial.printf("Target light set to: %.0f\n", target);
|
||||||
|
|
||||||
|
// 可选:立即进行一次快速调节
|
||||||
|
const float tolerance = 100.0f;
|
||||||
|
float currentLight;
|
||||||
|
float res;
|
||||||
|
|
||||||
|
int attempt = 0;
|
||||||
|
const int max_retries = min(maxAttempts, 10); // 限制快速调节次数
|
||||||
|
|
||||||
|
do {
|
||||||
|
currentLight = getCurrentLight();
|
||||||
|
res = target - currentLight;
|
||||||
|
|
||||||
|
if (abs(res) <= tolerance) {
|
||||||
|
Serial.println("Initial target reached.");
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
|
int lastWiperValue = digitalPot.getWiper();
|
||||||
|
|
||||||
|
if (res > 0) {
|
||||||
if (lastWiperValue <= 127) {
|
if (lastWiperValue <= 127) {
|
||||||
lastWiperValue++;
|
lastWiperValue++;
|
||||||
}
|
}
|
||||||
} else if (res < 0) {
|
} else if (res < 0) {
|
||||||
// 需要减少亮度 -> 减少电位器阻值
|
|
||||||
if (lastWiperValue > 0) {
|
if (lastWiperValue > 0) {
|
||||||
lastWiperValue--;
|
lastWiperValue--;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
digitalPot.setWiper(lastWiperValue);
|
digitalPot.setWiper(lastWiperValue);
|
||||||
Serial.printf("Current light: %.0f, Target: %.0f, Res: %.0f, Wiper: %d\n", currentLight, target, res, lastWiperValue);
|
Serial.printf("Quick adjust: Current light: %.0f, Target: %.0f, Wiper: %d\n", currentLight, target, lastWiperValue);
|
||||||
delay(200); // 等待响应,必要!!
|
delay(200);
|
||||||
|
|
||||||
attempt++;
|
attempt++;
|
||||||
|
|
||||||
// 超出最大尝试次数退出
|
|
||||||
if (attempt >= max_retries) {
|
if (attempt >= max_retries) {
|
||||||
Serial.println("Max attempts reached. Exiting...");
|
Serial.println("Quick adjust completed, continuing with auto mode.");
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@ -19,6 +19,8 @@ public:
|
|||||||
|
|
||||||
void runUntilTargetReached(float target, int maxAttempts = 500);
|
void runUntilTargetReached(float target, int maxAttempts = 500);
|
||||||
|
|
||||||
|
void adjustToTarget(); // 新增:持续调节函数
|
||||||
|
|
||||||
private:
|
private:
|
||||||
BH1750& lightSensor; // 引用 BH1750 传感器对象
|
BH1750& lightSensor; // 引用 BH1750 传感器对象
|
||||||
DS3502& digitalPot; // 引用 DS3502 数字电位器对象
|
DS3502& digitalPot; // 引用 DS3502 数字电位器对象
|
||||||
|
|||||||
@ -105,11 +105,14 @@ void TJC_Show::showInfo() {
|
|||||||
sendCommand(str);
|
sendCommand(str);
|
||||||
|
|
||||||
// === autolight 页面 ===
|
// === autolight 页面 ===
|
||||||
|
extern bool isAutoAdjustEnabled;
|
||||||
snprintf(str, sizeof(str),
|
snprintf(str, sizeof(str),
|
||||||
"autolight.t1.txt=\"照度A:%d\"\xff\xff\xff"
|
"autolight.t1.txt=\"照度A:%d\"\xff\xff\xff"
|
||||||
"autolight.t2.txt=\"照度B:%d\"\xff\xff\xff",
|
"autolight.t2.txt=\"照度B:%d\"\xff\xff\xff"
|
||||||
|
"autolight.c0.val=%d\xff\xff\xff",
|
||||||
sensorData.lightA,
|
sensorData.lightA,
|
||||||
sensorData.lightB
|
sensorData.lightB,
|
||||||
|
isAutoAdjustEnabled ? 1 : 0
|
||||||
);
|
);
|
||||||
sendCommand(str);
|
sendCommand(str);
|
||||||
|
|
||||||
@ -284,6 +287,7 @@ void TJC_Show::processSerial() {
|
|||||||
LightOpen();
|
LightOpen();
|
||||||
} else if (buffer[2] == 0x00 && buffer[3] == 0x00) {
|
} else if (buffer[2] == 0x00 && buffer[3] == 0x00) {
|
||||||
LightClose();
|
LightClose();
|
||||||
|
isAutoAdjustEnabled = false;
|
||||||
} else {
|
} else {
|
||||||
Serial.printf("Unknown cmd: %02X %02X\n", buffer[2], buffer[3]);
|
Serial.printf("Unknown cmd: %02X %02X\n", buffer[2], buffer[3]);
|
||||||
}
|
}
|
||||||
@ -294,14 +298,33 @@ void TJC_Show::processSerial() {
|
|||||||
// 自动调节指令,根据帧长度输出 ASCII 字符串
|
// 自动调节指令,根据帧长度输出 ASCII 字符串
|
||||||
// Serial.print("Received ASCII: ");
|
// Serial.print("Received ASCII: ");
|
||||||
// Serial.println(asciiStr);
|
// Serial.println(asciiStr);
|
||||||
if(totalLength == 7 && buffer[3] == 0)
|
// if(totalLength == 7 && buffer[3] == 0)
|
||||||
|
// {
|
||||||
|
// isAutoAdjustEnabled = false;
|
||||||
|
// Serial.printf("AutoLightControl is %s\n", isAutoAdjustEnabled ? "on" : "off");
|
||||||
|
// }
|
||||||
|
// else{
|
||||||
|
// lightCtrl.runUntilTargetReached(atoi(asciiStr), 150);
|
||||||
|
// Serial.printf("Set TargetLight %d\n", atoi(asciiStr));
|
||||||
|
// }
|
||||||
|
// break;
|
||||||
|
// 自动调节指令 new
|
||||||
|
if(totalLength == 7 && buffer[3] == 0x00)
|
||||||
{
|
{
|
||||||
|
// 明确的关闭指令
|
||||||
isAutoAdjustEnabled = false;
|
isAutoAdjustEnabled = false;
|
||||||
Serial.printf("AutoLightControl is %s\n", isAutoAdjustEnabled ? "on" : "off");
|
Serial.printf("AutoLightControl is %s\n", isAutoAdjustEnabled ? "on" : "off");
|
||||||
}
|
}
|
||||||
else{
|
else{
|
||||||
lightCtrl.runUntilTargetReached(atoi(asciiStr), 150);
|
// 设置目标并启用自动调节
|
||||||
Serial.printf("Set TargetLight %d\n", atoi(asciiStr));
|
int targetLux = atoi(asciiStr);
|
||||||
|
if (targetLux > 0) {
|
||||||
|
lightCtrl.setTargetLight(targetLux);
|
||||||
|
isAutoAdjustEnabled = true;
|
||||||
|
Serial.printf("AutoLightControl enabled, target: %d lux\n", targetLux);
|
||||||
|
} else {
|
||||||
|
Serial.println("Invalid target lux value");
|
||||||
|
}
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
|
|
||||||
|
|||||||
12
src/main.cpp
12
src/main.cpp
@ -264,8 +264,9 @@ void WebServer_Init(void) {
|
|||||||
if (request->hasParam("target", true)) {
|
if (request->hasParam("target", true)) {
|
||||||
int targetLux = request->getParam("target", true)->value().toInt();
|
int targetLux = request->getParam("target", true)->value().toInt();
|
||||||
if (targetLux > 0) {
|
if (targetLux > 0) {
|
||||||
lightCtrl.runUntilTargetReached(targetLux, 150);
|
lightCtrl.setTargetLight(targetLux);
|
||||||
//lightCtrl.setTargetLight(targetLux);
|
//lightCtrl.runUntilTargetReached(targetLux, 150);
|
||||||
|
lightCtrl.adjustToTarget();
|
||||||
isAutoAdjustEnabled = true;
|
isAutoAdjustEnabled = true;
|
||||||
request->send(200, "text/plain", "Auto adjust started");
|
request->send(200, "text/plain", "Auto adjust started");
|
||||||
} else {
|
} else {
|
||||||
@ -422,10 +423,11 @@ void loop(void)
|
|||||||
Buzzer();
|
Buzzer();
|
||||||
ButtonControl();
|
ButtonControl();
|
||||||
|
|
||||||
// if (isAutoAdjustEnabled) {
|
// 修改:如果启用自动调节,持续进行调节
|
||||||
// lightCtrl.runUntilTargetReached(lightCtrl.getTargetLight(), 100); // 每次最多尝试100次
|
if (isAutoAdjustEnabled) {
|
||||||
// }
|
lightCtrl.adjustToTarget(); // 使用新的持续调节函数
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user