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();
|
||||
}
|
||||
|
||||
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)
|
||||
float currentLight = getCurrentLight();
|
||||
float res = targetLightLevel - currentLight;
|
||||
|
||||
if (abs(res) <= tolerance) {
|
||||
// 已达到目标,但不退出,继续监控
|
||||
lastAdjustTime = currentTime;
|
||||
return;
|
||||
}
|
||||
|
||||
int lastWiperValue = digitalPot.getWiper();
|
||||
|
||||
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 = maxAttempts; // 最大尝试次数,防止死循环
|
||||
const int max_retries = min(maxAttempts, 10); // 限制快速调节次数
|
||||
|
||||
do {
|
||||
currentLight = getCurrentLight();
|
||||
res = target - currentLight;
|
||||
|
||||
if (abs(res) <= tolerance) {
|
||||
Serial.println("Target reached.");
|
||||
Serial.println("Initial target reached.");
|
||||
break;
|
||||
}
|
||||
|
||||
int lastWiperValue = digitalPot.getWiper();
|
||||
|
||||
if (res > 0) {
|
||||
// 需要增加亮度 -> 增加电位器阻值
|
||||
if (lastWiperValue <= 127) {
|
||||
lastWiperValue++;
|
||||
}
|
||||
} else if (res < 0) {
|
||||
// 需要减少亮度 -> 减少电位器阻值
|
||||
if (lastWiperValue > 0) {
|
||||
lastWiperValue--;
|
||||
}
|
||||
}
|
||||
|
||||
digitalPot.setWiper(lastWiperValue);
|
||||
Serial.printf("Current light: %.0f, Target: %.0f, Res: %.0f, Wiper: %d\n", currentLight, target, res, lastWiperValue);
|
||||
delay(200); // 等待响应,必要!!
|
||||
Serial.printf("Quick adjust: Current light: %.0f, Target: %.0f, Wiper: %d\n", currentLight, target, lastWiperValue);
|
||||
delay(200);
|
||||
|
||||
attempt++;
|
||||
|
||||
// 超出最大尝试次数退出
|
||||
if (attempt >= max_retries) {
|
||||
Serial.println("Max attempts reached. Exiting...");
|
||||
Serial.println("Quick adjust completed, continuing with auto mode.");
|
||||
break;
|
||||
}
|
||||
|
||||
|
||||
@ -19,6 +19,8 @@ public:
|
||||
|
||||
void runUntilTargetReached(float target, int maxAttempts = 500);
|
||||
|
||||
void adjustToTarget(); // 新增:持续调节函数
|
||||
|
||||
private:
|
||||
BH1750& lightSensor; // 引用 BH1750 传感器对象
|
||||
DS3502& digitalPot; // 引用 DS3502 数字电位器对象
|
||||
|
||||
@ -105,11 +105,14 @@ void TJC_Show::showInfo() {
|
||||
sendCommand(str);
|
||||
|
||||
// === autolight 页面 ===
|
||||
extern bool isAutoAdjustEnabled;
|
||||
snprintf(str, sizeof(str),
|
||||
"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.lightB
|
||||
sensorData.lightB,
|
||||
isAutoAdjustEnabled ? 1 : 0
|
||||
);
|
||||
sendCommand(str);
|
||||
|
||||
@ -284,6 +287,7 @@ void TJC_Show::processSerial() {
|
||||
LightOpen();
|
||||
} else if (buffer[2] == 0x00 && buffer[3] == 0x00) {
|
||||
LightClose();
|
||||
isAutoAdjustEnabled = false;
|
||||
} else {
|
||||
Serial.printf("Unknown cmd: %02X %02X\n", buffer[2], buffer[3]);
|
||||
}
|
||||
@ -294,14 +298,33 @@ void TJC_Show::processSerial() {
|
||||
// 自动调节指令,根据帧长度输出 ASCII 字符串
|
||||
// Serial.print("Received ASCII: ");
|
||||
// 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;
|
||||
Serial.printf("AutoLightControl is %s\n", isAutoAdjustEnabled ? "on" : "off");
|
||||
}
|
||||
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;
|
||||
|
||||
|
||||
14
src/main.cpp
14
src/main.cpp
@ -264,8 +264,9 @@ void WebServer_Init(void) {
|
||||
if (request->hasParam("target", true)) {
|
||||
int targetLux = request->getParam("target", true)->value().toInt();
|
||||
if (targetLux > 0) {
|
||||
lightCtrl.runUntilTargetReached(targetLux, 150);
|
||||
//lightCtrl.setTargetLight(targetLux);
|
||||
lightCtrl.setTargetLight(targetLux);
|
||||
//lightCtrl.runUntilTargetReached(targetLux, 150);
|
||||
lightCtrl.adjustToTarget();
|
||||
isAutoAdjustEnabled = true;
|
||||
request->send(200, "text/plain", "Auto adjust started");
|
||||
} else {
|
||||
@ -422,10 +423,11 @@ void loop(void)
|
||||
Buzzer();
|
||||
ButtonControl();
|
||||
|
||||
// if (isAutoAdjustEnabled) {
|
||||
// lightCtrl.runUntilTargetReached(lightCtrl.getTargetLight(), 100); // 每次最多尝试100次
|
||||
// }
|
||||
}
|
||||
// 修改:如果启用自动调节,持续进行调节
|
||||
if (isAutoAdjustEnabled) {
|
||||
lightCtrl.adjustToTarget(); // 使用新的持续调节函数
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user