Arduino control - potential delays?

1,953 views
Skip to first unread message

Vincent Prevosto

unread,
Nov 26, 2014, 2:21:55 PM11/26/14
to bonsai...@googlegroups.com
My current behavioral platform is largely controlled through Arduino. I'd like to integrate it with Bonsai, notably to read some values. I'm wondering if using the firmata framework will reduce the system's efficiency, though, as it will add serial transmission delays at every step (this is what happens with Matlab Arduino control anyway). Is it feasible to load the code, let it run and just get some feedback from the Arduino, without delaying its operations?
Thanks

Gonçalo Lopes

unread,
Nov 26, 2014, 8:58:12 PM11/26/14
to bonsai...@googlegroups.com
There are actually several possible levels of integration between Arduino and Bonsai:

1) Using the Firmata standard protocol is the easiest one. This effectively turns Arduino into a DAQ, streaming digital input pin changes and analog input (ADC) readings through the serial port and receiving commands to set digital outputs back. For this, you upload the Firmata example to the Arduino and just use the DigitalInput, AnalogInput, DigitalOutput, etc. nodes to read and write the state of pins in the board. The rate at which this loop runs can be set using Bonsai, and it is important to adjust it for the specific application. By default it runs at 19 ms, which may not be enough for some applications. From my experience, I don't recommend setting the interval to less than 2 or 3 ms, since usually the Arduino won't be able to guarantee precise timing while simultaneously running the communication protocol.

As you mentioned, this option indeed adds a serial transmission delay for responding to physical events if you're doing all your control in Bonsai, since the event has to be first detected by the Arduino (>200us), then sent to Bonsai (>1ms) which processes and sends back a response (>1ms) that then Arduino executes when the Firmata loop runs again (~1ms). I would advance a first-order estimate that this round-trip response delay will be something on the order of ~10 ms. Depending on the particular task, this latency may or may not be negligible. You can read more about these concerns at the Firmata protocol Wiki page.

2) As you suggested, it is also possible to run your own custom control code in the Arduino microcontroller, and simply exchange serial port messages with Bonsai for specific events. For example, you could activate a specific state by sending a serial port message when movement is detected in a camera. You can use the nodes SerialStringRead and SerialStringWrite to rapidly prototype something like this. These implement a simple character based protocol with line endings being the terminators. You can parse the incoming string into appropriate values using the Parse node. In this way, you can run any custom control code in Arduino that you want and have precise control over when communication happens.

3) An intermediate solution is to simultaneously use Firmata and your own control code. Firmata is just an Arduino program, so you can actually modify its execution code to fit your needs. This is actually a nice compromise for running hardware control tasks that require precise timing (e.g. stimulation train, playing audio waveforms, square pulses of precise fixed interval, etc). One way to do this is by hooking callbacks to the arrival of Firmata messages, as specified in the Firmata Arduino page (Callback Functions section). You can then run these specific control loops when certain pins are activated, but otherwise just keep to the same basic DAQ control.

Which one is more appropriate will depend on the timing constraints of the specific experiment. Note that for many behavior experiments an order of 10 ms is usually good enough as long as you can log in event times precisely for physiology. This is possible by simply routing Arduino pins to the recording systems TTL and ADC inputs so that they can be acquired synchronously with neural data.

Hope this helps.
Cheers,

Vincent Prevosto

unread,
Nov 27, 2014, 1:42:25 AM11/27/14
to bonsai...@googlegroups.com
Gonçalo,
thank you so much for the detailed answer. This is all very clear. I hadn't thought about possibility #3, but that's an attractive solution.
Best,
Vincent
Reply all
Reply to author
Forward
0 new messages