adicionado controle de ganho

This commit is contained in:
Diego Freitas 2026-01-22 16:43:30 -03:00
parent 8f1bc41e1b
commit 4a1561fb16
1 changed files with 32 additions and 10 deletions

View File

@ -61,9 +61,11 @@ DATA_RAW = 0
# Limites de exposição em unidades RAW (linhas)
EXP_MIN = 1
EXP_MAX = 20000
EXP_MARGIN = 200 # exemplo, em unidades de exp_raw
# Ganho analógico
GAIN_A_MIN = 0
GAIN_A_MAX = 255
GAIN_A_MAX = 50
GAIN_A_BASE = 0
# Ganho digital
GAIN_D_MIN = 0
GAIN_D_MAX = 8
@ -283,6 +285,7 @@ class AEController:
Na prática, 2 costuma ser um ótimo equilíbrio
(muito rápido, métricas quase idênticas).
"""
self.run_each = 0.05
self._last_time = 0.0
self.exp_min = exp_min
self.exp_max = exp_max
@ -384,6 +387,15 @@ class AEController:
# deadband: se está perto do alvo e sem saturação, não mexe
if abs(e) <= self.deadband and sat <= self.sat_limit:
new_gain_a = gain_a
if self.use_gain:
# Relaxar ganho em direção ao baseline quando está tudo ok
if gain_a > GAIN_A_BASE:
new_gain_a = max(GAIN_A_BASE, gain_a - 1)
elif gain_a < GAIN_A_BASE:
new_gain_a = min(GAIN_A_BASE, gain_a + 1)
dbg = {
"p90": p90,
"p95": p95,
@ -393,7 +405,7 @@ class AEController:
"hold": True,
}
self._last_time = time.time()
return exp_raw, gain_a, gain_d, dbg
return exp_raw, new_gain_a, gain_d, dbg
# cálculo do passo em log
if sat > self.sat_limit:
@ -410,9 +422,12 @@ class AEController:
new_gain_d = gain_d
if self.use_gain:
if new_exp >= self.exp_max and self.p95_ema < (self.target - self.deadband):
# 1) Muito escuro e exp no teto -> sobe ganho
if new_exp >= self.exp_max - EXP_MARGIN and self.p95_ema < (self.target - self.deadband):
new_gain_a = _clamp(gain_a + 2, GAIN_A_MIN, GAIN_A_MAX)
if new_exp <= self.exp_min and (self.p95_ema > (self.target + self.deadband) or sat > self.sat_limit):
# 2) Muito claro e exp no piso -> desce ganho
elif new_exp <= self.exp_min + EXP_MARGIN and (self.p95_ema > (self.target + self.deadband) or sat > self.sat_limit):
new_gain_a = _clamp(gain_a - 2, GAIN_A_MIN, GAIN_A_MAX)
dbg = {
@ -567,9 +582,9 @@ class Gal5000Camera:
self.dll = _load_gal_dll(dll_dir, dll_name)
self.handle: W.HANDLE | None = None
self.exp_raw: int | None = None
self.gain_a: int | None = None
self.gain_d: int | None = None
self.exp_raw: int | None = 1500
self.gain_a: int | None = 0
self.gain_d: int | None = 0
self.ae = AEController(exp_max=10000)
self.ae_enabled = use_auto_exposure
@ -657,6 +672,12 @@ class Gal5000Camera:
def _init_params(self):
if self.handle is None:
return
try:
self.set_exposure(self.exp_raw)
self.set_gain_a(self.gain_a)
self.set_gain_d(self.gain_d)
except Exception:
print("Erro ao definir parametros iniciais de AE")
try:
self.exp_raw = _param_get_int(self.dll, self.handle, PARAM_ID_SENSOR_EXPOSURETIMERAW)
except Exception:
@ -689,7 +710,8 @@ class Gal5000Camera:
if self.handle is None:
return 0
new_gain = _clamp(int(new_gain), GAIN_A_MIN, GAIN_A_MAX)
_param_set_int(self.dll, self.handle, PARAM_ID_SENSOR_GAINANALOGRAW, new_gain)
#_param_set_int(self.dll, self.handle, PARAM_ID_SENSOR_GAINANALOGRAW, new_gain)
_param_set_int(self.dll, self.handle, PARAM_ID_SENSOR_GAINDIGITRAW, new_gain)
self.gain_a = new_gain
return new_gain
@ -744,7 +766,7 @@ class Gal5000Camera:
do_ae
and self.ae_enabled
and self.exp_raw is not None
and (t1 - self.ae._last_time) >= 0.10 # máximo 10 Hz de AE
and (t1 - self.ae._last_time) >= self.ae.run_each # máximo 10 Hz de AE
)
if do_ae_now and self.ae_enabled and self.exp_raw is not None:
new_exp, new_ga, new_gd, dbg_ae = self.ae.step(
@ -755,7 +777,7 @@ class Gal5000Camera:
)
if new_exp != self.exp_raw:
self.set_exposure(new_exp)
if new_ga != self.gain_a and False:
if new_ga != self.gain_a:
self.set_gain_a(new_ga)
if new_gd != self.gain_d:
self.set_gain_d(new_gd)