Closed-Loop Opto Stimulation

347 views
Skip to first unread message

Marco Colnaghi

unread,
Jan 20, 2022, 4:54:26 AM1/20/22
to Bonsai Users
Hello everybody, sorry to bother!

I'm currently developing a straight-forward Closed-Loop experiment with Bonsai but I'm struggling with a few things while some other require a bit of clarification.
The idea of the experiment is to trigger the opto stimulation based on the movement of the mouse in an open-field arena.

1. Speed Computing: As of now, in order to compute the speed, I'm using the Skip-Zip method, skipping three values and zipping them to the realtime ones. Given that the camera is acquiring at 60Hz, I assume that the delay between the two zipped value is 1/60 * 3 = 0.05s. Then, I calculate the speed for that particular interval, buffer the values in order to get an average that span some larger timespan (let's say 200ms - 12 frames), and average it to get the mean speed in the desired time window (I multiply it just to transform it in cm/s).
And this leads me to my first question: Does this part make sense to you?

2. Setting multiple Thresholds: Assuming that the answer is positive, I then need to set multiple conditions that trigger the laser stimulation.
Let's say: 
> 12.5 cm/s in a 100ms window
> 5 cm/s in a 200ms window
< 3 cm/s in a 500ms window

Given the different time windows, I was assuming that all I need was to change the buffer size to fit the desired lengths with the Bonsai computing window.
But then I realized that I couldn't double the PublishSubject ('Immobility'). 
Is there a way to have a single subject that triggers the optostimulation instead of having multiple of them broadcasting their value to multiple SubscribeSubjects?
Maybe I'm missing something here. Sorry!

3. Best Practices with Serial Communication: Lastly, I noticed that this script presents some issues when interfacing with Arduino.
The problem being that if the threshold has been triggered a second time while the the arduino was running its stimulation protocol, a second stimulation is buffered as soon as the first one ends. This is probably due to the Arduino reading a second <s> as soon as it is done dealing with the first one.
I was thinking of methods to 'clean' the serial buffer within the Arduino script or either setting a timer in Bonsai that blocks serial messages being sent within the 'refractory period', but I'm struggling to wrap my head around it and I was hoping for some help from you guys!

Thank you so much!
Stay safe :)

Attached you can find my workflow. Don't bother with the disabled stuff!
Marco
pylonSegment.bonsai
pylonSegment.bonsai.layout

Marco Colnaghi

unread,
Jan 20, 2022, 7:31:39 AM1/20/22
to Bonsai Users
Quick update:
I managed to solve the refractory period issue by inserting a TimedGate node, and right now it seems to be working fine!
Here's the updated version!

Marco
pylonSegment_rev.bonsai.layout
pylonSegment_rev.bonsai
Reply all
Reply to author
Forward
0 new messages