diff options
Diffstat (limited to 'qolab/hardware')
-rw-r--r-- | qolab/hardware/scope/_basic.py | 49 |
1 files changed, 49 insertions, 0 deletions
diff --git a/qolab/hardware/scope/_basic.py b/qolab/hardware/scope/_basic.py index fde7bfc..4e546ac 100644 --- a/qolab/hardware/scope/_basic.py +++ b/qolab/hardware/scope/_basic.py @@ -153,6 +153,55 @@ class Scope(BasicInstrument): self.setRun(old_run_status) # start running if it was old run state return allTraces + def chanAutoScale(self, chNum, margin=0.25, timeout=5): + """Auto scale channel to fit signal on screen. + + Tunes Volts per division and Channel offset to fit signal + on screen (vertically). + + Parameters + ---------- + chNum : int + Channel to auto scale + margin: float + How much extra space (margin) to have with respect to full screen. + Default is 0.25 (i.e. 25%), + i.e. 1 vertical division at top and bottom for 8 division scope. + """ + + starttime = time.time() + deadline = starttime+timeout + scaled_corectly = False + while (not scaled_corectly) and (time.time()<deadline): + tr = self.getTrace(chNum) + vPerDiv = self.getChanVoltsPerDiv(chNum) + offset = self.getChanVoltageOffset(chNum) + v_range = vPerDiv * self.vertDivOnScreen + v_max = v_range / 2 - offset + v_min = -v_range / 2 - offset + y = tr.y.values + tr_max = y.max() + tr_min = y.min() + screen_portion = (y.max - y.min) / vPerDiv / self.vertDivOnScreen + top_margin = (v_max - tr_max) / v_range + bottom_margin = (tr_min - v_min) / v_range + is_top_margin_good = (top_margin > margin / 2) and (top_margin < margin) + is_bottom_margin_good = (bottom_margin > margin / 2) and (bottom_margin < margin) + if (is_bottom_margin_good) and (is_top_margin_good): + scaled_corectly = True + break + offset = (tr_max+tr_min)/2 + vPerDiv = (tr_max -tr_min)/(self.vertDivOnScreen*(1-margin*1.5)) + + self.setChanVoltageOffset(chNum, offset) + self.setChanVoltsPerDiv(chNum, vPerDiv) + scaled_corectly = False + if (time.time()>deadline): + logger.warning( + f"Scope did not make proper channel {chNum} scaling within {timeout=} sec, try to increase it." + ) + + def plot(self, **kwargs): allTraces = self.getAllTraces(**kwargs) allTraces.plot() |