Python Triggering

Post your .Net discussions here
Post Reply
jboxfordni
User
User
Posts: 2
Joined: Tue Aug 15, 2023 10:01 am

Python Triggering

Post by jboxfordni »

I'm trying to record data from a sensor on channel A. The event has a digital trigger on channel B.
I set up a the device like this:

Code: Select all

   # methods on class called pico_device
   
   def set_trigger(self, threshold_mV, timeout: int):
        maxADC = ctypes.c_int16(1024) #1024 or 32767?
        thresholdADC = mV2adc(threshold_mV, self.channel_range_B.value, maxADC)
        chandle = ctypes.c_int16(self.status["device_handle"])
        self.status["trigger"] = ps.ps2000_set_trigger(chandle, 1, thresholdADC, -50, 0, timeout)
        assert_pico2000_ok(self.status["trigger"])
        
   def setup(self):
        preTriggerSamples = 1000
        postTriggerSamples = 1000
        self.maxSamples = preTriggerSamples + postTriggerSamples
        timebase = 8 
        self.timeInterval = ctypes.c_int32()
        timeUnits = ctypes.c_int32()
        oversample = ctypes.c_int16(1)
        maxSamplesReturn = ctypes.c_int32()
        chandle = ctypes.c_int16(self.status["device_handle"])
        self.status["getTimebase"] = ps.ps2000_get_timebase(
            chandle,
            timebase,
            self.maxSamples,
            ctypes.byref(self.timeInterval),
            ctypes.byref(timeUnits),
            oversample,
            ctypes.byref(maxSamplesReturn),
        )
        assert_pico2000_ok(self.status["getTimebase"])

    def block_capture(self):
        timebase = 8
        oversample = ctypes.c_int16(1)
        chandle = ctypes.c_int16(self.status["device_handle"])
        timeIndisposedms = ctypes.c_int32()
        self.status["runBlock"] = ps.ps2000_run_block(
            chandle,
            self.maxSamples,
            timebase,
            oversample,
            ctypes.byref(timeIndisposedms),
        )
        assert_pico2000_ok(self.status["runBlock"])

    def ready(self):
        chandle = ctypes.c_int16(self.status["device_handle"])

        ready = ctypes.c_int16(0)
        check = ctypes.c_int16(0)

        while ready.value == check.value:
            # trigger event here?
            self.status["isReady"] = ps.ps2000_ready(chandle)
            ready = ctypes.c_int16(self.status["isReady"])
            
     def collect_data(self):
        chandle = ctypes.c_int16(self.status["device_handle"])

        oversample = ctypes.c_int16(1)
        # Create buffers ready for data
        bufferA = (ctypes.c_int16 * self.maxSamples)()
        bufferB = (ctypes.c_int16 * self.maxSamples)()
        bufferT = (ctypes.c_int32 * self.maxSamples)()

        # Get data from scope
        # handle = chandle
        # pointer to buffer_a = ctypes.byref(bufferA)
        # pointer to buffer_b = ctypes.byref(bufferB)
        # poiner to overflow = ctypes.byref(oversample)
        # no_of_values = cmaxSamples
        cmaxSamples = ctypes.c_int32(self.maxSamples)

        self.status["getValues"] = ps.ps2000_get_times_and_values(
            chandle,
            ctypes.byref(bufferT),
            ctypes.byref(bufferA),
            ctypes.byref(bufferB),
            None,
            None,
            ctypes.byref(oversample),
            2,
            cmaxSamples,
        )
        assert_pico2000_ok(self.status["getValues"])

        time_data = np.linspace(
            0, (cmaxSamples.value) * self.timeInterval.value, cmaxSamples.value
        )
        # find maximum ADC count value
        maxADC = ctypes.c_int16(32767)
        # convert ADC counts data to mV
        adc2mVChA = adc2mV(bufferA, self.channel_range_A.value, maxADC)
        adc2mVChB = adc2mV(bufferB, self.channel_range_B.value, maxADC)

        return {"channel_A": adc2mVChA, "channel_B": adc2mVChB, "time": time_data}
I call these functions in my main code:

Code: Select all

    picoscope_device.channel_A(channel_range=ChannelRange.PS2000_500MV) 
    picoscope_device.channel_B(channel_range=ChannelRange.PS2000_5V)
    picoscope_device.set_trigger(3000, 0)
    picoscope_device.setup()
    picoscope_device.block_capture()
    picoscope_device.ready()
    results = picoscope_device.collect_data()
    picoscope_device.stop()
   
I have to issue a command to trigger my hardware device. Where should this go? I've tried after setup and capture but these just wait forever in the ready loop. I can trigger the hardware event once in the ready loop but the data collected does not show the trigger on B or any data on A. Is there anything I'm missing about the device setup here?

NeilH
PICO STAFF
PICO STAFF
Posts: 271
Joined: Tue Jul 18, 2017 8:28 am

Re: Python Triggering

Post by NeilH »

Hi

Are you able to share a full version of your code in a .py file so I can run it myself to see exactly what is happening?

Neil
Neil
Technical Support Engineer

Post Reply