pyPhotometry: Triggering digital/analog output

73 views
Skip to first unread message

HE Tan

unread,
Dec 7, 2022, 10:24:06 AM12/7/22
to pyControl
Hi

How would you modify the pyPhotometry to generate a digital output (e.g. Analog or TTL pulse(s) from pyPhotometry hardware to an Arduino or other device) based on the current photometry voltage readings (or dF/F based on a predetermined F0 window, etc)?

I think it may need 2 modifications (i) process the sensor readings (live) and (ii) have some way to send a TTL/analog output.

For i) It seems inserting this analysis in plotting.py (highlighted), after the de-mean code, would be most straightforward: I think I could write something to compare if the ADC1 value of mean(history) values are higher or lower than something, etc.

For ii) More crucially, I am wondering, is it possible for one of the BNC digital inputs to be programmed into a BNC digital (or analog) output? If so, could you guide me on where that is in the code/ how to do so?

Alternatively, if not by converting the digital inputs, can an output signal be made through any other pins on the pyboard?

thoma...@gmail.com

unread,
Dec 12, 2022, 4:34:46 PM12/12/22
to pyControl
Hi,

Apologies for the delay getting back to you, your message got stuck in my spam filter..

It certainly would be possible to use one of the BNC digital inputs as a digital output by modifying the code - the BNC connectors are connected directly to pins on the pyboard microcontroller so no modification to the hardware would be necessary.

It should also be possible to modify the code to trigger a digital output based on the value of the photometry signal.  The simplest way to do this would be if you could do all the data processing on the pyboard itself.  Alternatively you could do the processing on the computer and tell the board when to trigger the pulse, but this would be more complicated to implement.

Assuming you can do all the processing on the pyboard, the only file you would have to modify is photometry_upy.py. This file contains code that is transferred to the pyboard microcontroller and runs there, handing data acqusition and streaming data back to the computer.  It defines a class called Photometry, the GUI instantiates an instance of this class when it connects to the board, and calls methods of the class to start/stop acquisition, set the acquisition mode etc.  The methods you would need to modify are:

- In the __init__ method, when the digital pins are instantiated (line 31-32) you need to specify it is an output not an input (see micropython docs here).

- In the start method, which is called at the start of data acqusition, you would need to initialise any variables that are going to be used for the data processing.

- The method time_div_ISR is called by a timer twice each acqusition cycle.  Each time it is called it acquires a sample from one of the analog and one of the digital channels.  You would need to modify this function to set the value of the digital output pin based on the value of the analog signal.  You should also set the self.dig_sample variable to match the state of the output pin, so that this gets sent to the GUI and plotted. 

A couple of things to be aware of when working on this code:

- The file photometry_upy.py is uploaded to the pyboard when you connect to the board, so after you make changes to the file and save them on your computer, you will need to disconnect from the board using the GUI, then reconnect to upload the modified file.

- It may be tricky debugging changes to photometry_upy.py because it runs on the pyboard not the computer, and errors that occur there may not always get saved to the ErrorLog.txt file.  I need to check the logic of how errors occuring on the pyboard at different times are processed.  I have a look at this over the next few days and get back to you.

best,

Thomas

thoma...@gmail.com

unread,
Dec 14, 2022, 3:49:43 PM12/14/22
to pyControl
I've made some changes to the code in the development (dev) branch on Github to ensure that if errors occur in the code running on the pyboard, the traceback indicating what the problem was and the line where it occured are saved to the ErrorLog.txt file in the main pyPhotometry folder.  This should make it a lot easier to debug problems during development work on that code.

Also, one thing to be aware of when modifying that code is that the methods in photometry_upy.py that control the acqusition cycle (cont_2_col_ISR and  time_div_ISR for the continuous and time divison modes respectively) are called by timer interrupts, and there are some restrictions on what you can do in interrupt handler functions - basically not allocating new memory.  These restrictions and how to work around them are detailed here in the micropython docs.
Reply all
Reply to author
Forward
0 new messages