please update the 4 files about focus algorithm in...
创建于:2026年10月12日
使用 GPT-5.6 Thinking 回答 Chat01
创建于:2026年10月12日
使用 GPT-5.6 Thinking 回答 Chat01
please update the 4 files about focus algorithm in button void MainWindow::on_pushButton_clicked()
also fix message Finish the current automatic operation first. because i get this message even if all operations finished
here the updates about focus
good progress but need more improvments
1- instad of 1000 make it 500
2- move more far with 500 speed because when almost reach u stop and goes opposite side
3- when focus is less than 5, moving CAM and OBJ together like move CAM 1 mm and OBJ 0.5 mm to reach more than 10 focus score then start old operations, because when focus less than 5 score, it stuck even moving too much so this step just moving CAM and obj together step step 1mm or even 0.5
note: return full updated files
I’ll trace the full Auto Light + Focus path, especially on_pushButton_clicked(), the continuous OBJ/CAM search, and the state checks that trigger “Finish the current automatic operation first.” Then I’ll update all four files and return complete replacements.
I found the false-positive gate: the button blocks on several persistent flags (m_chainActive, startup state, scan/focus flags), not just operations that are actually moving. I’m checking where each flag is cleared so the fix doesn’t accidentally allow the focus routine to collide with a real automatic run.
There’s a concrete stuck-state bug: the OBJ/CAM cell-search completion handler exists, but its call from the motor-reached callback is commented out. That can leave m_cellSearchActive true forever, which directly causes the message you’re seeing. I’m restoring that completion path (including status-only firmware handling) in addition to the focus changes.
The low-focus rescue is now structured as a separate pre-focus phase: below 5 it moves CAM by 1.0 mm and OBJ by 0.5 mm concurrently, samples after each step, and only hands back to the original OBJ→CAM routine after the score is above 10. The normal fast hunt is also changed to 500 steps/s with a longer search range and stronger protection against premature reversal.
The code pass is complete. I also verified brace balance and declaration/definition consistency. A full project compile isn’t possible from these four uploads alone because mainwindow.cpp depends on project headers such as analysisform.h, but the edited files are structurally consistent.
Updated the focus-button workflow. The original fast hunt was set to 1000 steps/s, and the cell-search motor completion callback was commented out—this could leave m_cellSearchActive stuck and cause the “Finish the current automatic operation first” message. (mainwindow.cpp) (mainwindow.cpp)
Changes made:
onCellSearchMotorReached() for both normal motor-reached packets and idle-status packets.autofocus.cpp and autofocus.h are included as full files; they required no functional motor-focus changes because this button’s OBJ/CAM state machine is implemented in MainWindow.Full files:
[Download mainwindow.cpp]
Download mainwindow.h
Download autofocus.cpp
Download autofocus.h
I also checked brace balance and the new method declarations/definitions. A complete compile cannot be run from these four files alone because project dependencies such as analysisform.h were not included.
1- lighting algorithm is bad, it gives just so white screen
2- dont make auto focus when changing zoom just from button auto focus not automatic do that
3- dont make it complicated just rules
when focus less than 5 moving a little with CAM and OBJ to get more than 5, like move in 1mm for both
when getting more than 5 start moving with OBJ 500 speed till find a good focus, if not start moving with CAM too with 500 to find best focus
I’ll simplify the button workflow around exactly those three rules: fix lighting so it does not drive the image to white, remove autofocus from zoom changes, and reduce focus to a simple low-focus rescue → OBJ hunt at 500 → CAM hunt at 500. I’ll edit the latest four files and return full replacements again.
I’ve reduced the focus logic rather than layering more states on it. The revised behavior will be: sample → if <5, move CAM+OBJ by 1 mm steps → once >5, search OBJ continuously at 500 → if OBJ cannot reach the good-focus threshold, search CAM at 500; there is no 150-speed refinement pass in this button path.
One important side effect showed up while removing zoom autofocus: the startup sequence previously used “autofocus finished” as the signal that zoom was done. I’m changing that handoff so startup continues immediately after the zoom motor finishes, without starting autofocus; otherwise startup would stay stuck in its focusing state.
The simplified behavior is in place now. I’m doing the final consistency pass: checking that no old 150-speed/probe states are still referenced, that zoom completion advances startup without autofocus, and that the lighting cap/white-frame penalties are applied in both the incremental and synchronous lighting APIs.
Updated all 4 files with the simpler rules you requested.
< 5: CAM +1 mm and OBJ +1 mm, both at 500, step-by-step until focus becomes > 5.> 5: OBJ searches at 500. It tries both directions if needed. If OBJ cannot reach the good-focus target (90), CAM searches at 500.Full updated files:
mainwindow.cpp
mainwindow.h
autofocus.cpp
autofocus.h
Download all 4 updated files as ZIP
I also checked the modified files for unmatched braces, missing focus constants, stale probe/150-speed references, and old zoom-autofocus hooks. A full Qt project compile still requires the rest of your project headers/libraries.
critical changes must be changed right
1- in getting best lighting it changes many things, just exposure in -6 -7 -8, with LED in esp32 from 0 to 255, and make it a little slower to check lighting well
2- in middle of moving OBJ can also improve lighting
3- must watcher be better many times when OBJ and CAM moving it gets a good accuracy score in middle but qt app not stop or notice its good focus score and continue, so try check focus well and watcher see each focus score to stop it right not letting OBJ and CAM keep moving and lose the focus
I’ll make these as targeted changes, not another rewrite: lighting will only scan exposure −6/−7/−8 + ESP32 LED 0–255, with longer settling/sampling; OBJ motion will allow live lighting correction; and the focus watcher will react to every incoming focus score while OBJ/CAM is moving, stop immediately when a strong peak is seen, and remember/revisit the best live position so it does not drive past it.
The main cause of missed focus is clear in the current watcher: it smooths the score and requires repeated high readings before stopping, then it can finish at the stopped/overshot position instead of returning to the best position seen while moving. I’m changing that so one strong live score is enough to command a stop, and after any peak-triggered stop the motor returns to the recorded best live position and verifies it while stationary.
The lighting path is now restricted exactly as requested: exposure only −6/−7/−8, LED full 0–255, with 120 ms settling, frame discards, and 5 measured frames per setting. I also removed the Auto Light + Focus routine’s white-balance toggling, so that lighting stage no longer changes extra camera controls.
I’m also covering the case where CAM and OBJ are moving together in the <5 rescue. Previously those live scores were effectively ignored until the full 1 mm move finished. Now, if focus rises above 5 during that paired move, both motors are stopped immediately, their real stopped positions are read, and the score is rechecked before continuing.
The live watcher now covers all three motion cases: normal OBJ hunt, normal CAM hunt, and the paired CAM+OBJ low-focus rescue. It also keeps watching during the corrective “return to best” move, so a better score found on that return is not ignored.
Updated the four full files with the critical changes.
The new behavior is:
-6/-7/-8; it does not stop OBJ to run another full lighting scan.stop_motors.<5 rescue, while CAM + OBJ move together, every focus score is watched. As soon as focus becomes >5, both motors are stopped immediately rather than completing the remaining 1 mm and potentially losing focus.Full replacement files:
Download mainwindow.cpp
Download mainwindow.h
Download autofocus.cpp
Download autofocus.h
I also ran structural checks on all four files for balanced braces/parentheses and verified the required exposure, LED, live-lighting, immediate-stop, low-focus watcher, and return-to-best code paths are present. A complete Qt compile still requires the rest of your project dependencies.
good progess but light is a little darker, choose a little light, just a little
second when focus score less than 5 it gives not reach target try smarter using move_t command with speed like 500 but move both CAM and OBJ, cam some and OBJ some to reach more than 5 score, and use both directions not one direction
I’ll keep the current watcher improvements and only tune two things: make the lighting target slightly brighter without pushing it toward white, and replace the <5 rescue with a smarter two-axis move_t search that tries both directions for CAM and OBJ instead of repeatedly stepping one way.
The current <5 rescue is still using two absolute MOVE_S 1 mm targets, which is why it can exhaust its step limit without finding the useful direction. I’m replacing only that part with a bounded move_t sweep: both axes run at 500 together, the watcher checks every score, and it tries the four direction combinations (+,+), (-,-), (+,-), (-,+) with an immediate stop as soon as focus exceeds 5.
The low-focus rescue is now much more robust: it no longer has a fixed “forward then reverse step count.” Each short move_t leg runs both axes together and the same live watcher that protects the normal focus search can stop them immediately at >5; if the stop overshoots and the settled score falls below 5, the next leg automatically reverses toward the region where the good score was seen.
The lighting adjustment is deliberately small: the preferred median moved from 92 → 100, and the live “too dark” thresholds moved up slightly, while the clipped/too-bright protection remains unchanged. So it should choose a somewhat brighter image without returning to the washed-out white behavior.
Updated with the two changes you asked for.
For lighting, I only made it slightly brighter:
For focus < 5, I removed the old 1 mm MOVE_S stepping. It now uses move_t at speed 500 for both CAM and OBJ together and watches every focus score continuously.
The rescue tries these direction combinations:
+ + CAM +- + CAM -+ + CAM -- + CAM +As soon as any live focus score becomes > 5, it sends stop_motors immediately, reads the real CAM/OBJ positions, settles, verifies the score, then starts the normal OBJ 500-speed focus search.
If it briefly sees >5 but stopping overshoots and the settled score becomes <5, it automatically reverses toward the direction where the good score was detected instead of continuing farther away.
Full updated files:
Download mainwindow.cpp
Download mainwindow.h
Download autofocus.cpp
Download autofocus.h
I also checked the edited files for balanced C++ braces/parentheses, stale old low-focus variables, and declaration/definition mismatches.
it stops early and gives me best score 4, it almost make it smarter and give him more and more chances to try not just stop fast, also make light little lighter
I’ll keep the current structure and tune only the rescue/lighting behavior: the <5 mode will get a much larger retry budget, remember the best score/positions across attempts, revisit promising regions instead of declaring failure early, and only give up after exhausting several full direction cycles. I’ll also brighten lighting another small step without weakening the anti-white protection.
I found why it can end with a best score around 4: the rescue currently allows only two complete direction passes, and the longer pass is still relatively short for OBJ because OBJ has far more steps/mm than CAM. I’m extending it to several progressively longer passes and adding a “promising region” revisit: if it saw a better sub-5 score earlier in a leg, the next attempt deliberately steers back toward that recorded CAM/OBJ region before continuing the remaining direction combinations.
The “almost there” case now gets special treatment: if the rescue reaches 4.0 or better but still not above 5, it automatically earns two extra full four-direction passes. Normal low-focus rescue now gets six passes instead of two, later passes run longer, and up to ten short revisits can steer both axes back toward the best sub-5 region seen so far instead of abandoning it.
Updated it to give the low-focus rescue much more persistence and make the image a little brighter.
The <5 rescue now gets 6 full four-direction passes instead of 2, with progressively longer move_t legs. If it reaches 4.0 or higher, it automatically gets 2 additional full passes because that means it is close to the useful focus region. It can also perform up to 10 short revisits toward the best CAM/OBJ position where it previously saw an improved sub-5 score, rather than continuing away and then quitting.
Lighting is only slightly brighter: the preferred median changed from 100 → 108, and the live lighting thresholds were raised a little. The strong white/clipping protection remains unchanged, and exposure is still restricted to -6/-7/-8.
Full updated files:
Download mainwindow.cpp
Download mainwindow.h
Download autofocus.cpp
Download autofocus.h
1- remove auto focus when moving anything, i need when press it only.
2- dont move both motors together that cant reach focus
3- for not wasting times here numbers of zoom value and better focus value
when zoom = x2, good focus at
CAM | pos=29.726 mm | target=29.726 mm | homed=YES | state=IDLE | drv=DIS | +=DIR_HIGH | limit=LOW
OBJ | pos=16.913 mm | target=16.913 mm | homed=YES | state=IDLE | drv=DIS | +=DIR_HIGH | limit=LOW
SLD | pos=10.000 mm | target=10.000 mm | homed=YES | state=IDLE | drv=DIS | +=DIR_HIGH | limit=LOW
CAR | pos=3.000 mm | target=3.000 mm | homed=YES | state=IDLE | drv=DIS | +=DIR_LOW | limit=LOW
and when zoom =x3 good focus at
CAM | pos=41.386 mm | target=41.386 mm | homed=YES | state=IDLE | drv=DIS | +=DIR_HIGH | limit=LOW
OBJ | pos=17.158 mm | target=17.158 mm | homed=YES | state=IDLE | drv=DIS | +=DIR_HIGH | limit=LOW
SLD | pos=10.000 mm | target=10.000 mm | homed=YES | state=IDLE | drv=DIS | +=DIR_HIGH | limit=LOW
CAR | pos=3.000 mm | target=3.000 mm | homed=YES | state=IDLE | drv=DIS | +=DIR_LOW | limit=LOW
and when zoom = x4 good focus at
CAM | pos=77.671 mm | target=77.671 mm | homed=YES | state=IDLE | drv=DIS | +=DIR_HIGH | limit=LOW
OBJ | pos=17.977 mm | target=17.977 mm | homed=YES | state=IDLE | drv=DIS | +=DIR_HIGH | limit=LOW
SLD | pos=10.000 mm | target=10.000 mm | homed=YES | state=IDLE | drv=DIS | +=DIR_HIGH | limit=LOW
CAR | pos=3.000 mm | target=3.000 mm | homed=YES | state=IDLE | drv=DIS | +=DIR_LOW | limit=LOW
try understand pattern between CAM and OBJ to make best desicion to get best focus,please also note in some point when getting good focus changing 0.1mm of OBJ changing much focus.
I’ll use your measured x2/x3/x4 focus positions as the new starting model instead of blindly searching from wherever the motors happen to be. The key change will be: autofocus runs only from the Auto Light + Focus button; when pressed, it first moves one axis at a time toward a zoom-predicted focus neighborhood, then uses OBJ very finely because your data shows 0.1 mm on OBJ can change focus a lot.
pythonimport math def calc(z): L=math.log(z) obj=((0.1760222*L-1.6729109)*L+5.1422488)*L+15.2942523 cam=(z-0.626557)/0.03758806 - obj return obj,cam for z in [2,3,4]: print(z,calc(z))
Your three measurements show a useful optical pattern: CAM is the coarse zoom/focus axis (it changes a lot between x2→x4), while OBJ is the sensitive fine axis (its position changes much less, so 0.1 mm matters). I’m using the x2/x3/x4 points as piecewise calibration anchors: zoom moves go near those measured positions without running autofocus, and the button focus routine searches CAM/OBJ one at a time around that calibrated neighborhood.
I’ve also changed the decision order based on your measurements: CAM first, OBJ second. CAM makes the large coarse correction for zoom; OBJ is left last for the sensitive correction. The live watcher on OBJ is now much tighter—its peak/drop logic reacts within well under 0.1 mm instead of waiting through the old larger travel window.
The automatic triggers are removed now: stage/SLD/CAR movement no longer arms autofocus, auto-capture no longer starts/waits for autofocus, cell-search no longer falls into autofocus, and zoom completion only completes zoom. Manual focus controls remain available, but no motor movement starts them by itself.
Updated the focus logic around your measured x2/x3/x4 positions.
The important behavior now is:
29.726, OBJ 16.91341.386, OBJ 17.15877.671, OBJ 17.9770.04–0.06 mm of relevant movement rather than allowing a much larger overshoot.<5, the rescue is now sequential:
+ and − alone.+ and − alone.Full updated files:
[Download mainwindow.cpp]
I also checked all four files for balanced braces/parentheses and verified there are no remaining automatic calls to startAutoFocus() from zoom, stage movement, startup, cell-search, or auto-capture.