communicate with Arduino at specific time points during a task via serial connection

17 views
Skip to first unread message

Michael Graupner

unread,
Mar 28, 2022, 5:09:57 AM3/28/22
to ACQ4
Hello, 

I would like to send custom serial commands during a task execution. Specifically, I am looking for some inspiration no how to implement such a device which allows to send these commands at specific time points during the task. 

Some aspects of our experiment are controlled from a teensy (Arduino equivalent). Concretely, the teensy drives stepper motors. These movement are controlled through serial commands sent to the teensy. I would like to have ACQ4 send these serial commands at specific time points during a task. In other words, I would like to implement a device class for which I can provide a string, sent as serial command, and a time point at which this string is sent. I know that the timing of this will not be very precise due to the serial connection but this is not an issue as we record the actual rotation of the stepper motors with rotary encoders through the NiDAQ board. 

Does such a device implementation already exists, or something similar? If not, could you maybe provide some hints on how to tackle this? 

Many thanks in advance. 

Best regards,
Michael




Luke Campagnola

unread,
Mar 28, 2022, 1:50:37 PM3/28/22
to acq4
Hey Michael!

I don't have an example of this kind of device, but the approach I'd take is pretty simple:

- Create a Device subclass that handles basic interaction with the teensy (for example, it will have a method that immediately sends a serial command)
- Create a DeviceTask subclass that accepts a configuration structure like:
    {'commands': [(start_time, command), (start_time, command), ...]}
- Write TeensyDevice.createTask() to return an instance of your TeensyDeviceTask
- When your TeensyDeviceTask.start() method is called, store the current time and start a background thread that sends each command at the appropriate time

One thing to be careful of here is that you will be sending serial commands from a background thread, so it's important to use a mutex to prevent multiple threads accessing the serial port at the same time.

Does that all make sense? I can go into more detail..


Cheers,
Luke

 

--
You received this message because you are subscribed to the Google Groups "ACQ4" group.
To unsubscribe from this group and stop receiving emails from it, send an email to acq4+uns...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/acq4/a367db07-f48b-4ff3-9b6d-1e42aea2cecan%40googlegroups.com.

Michael Graupner

unread,
Mar 28, 2022, 3:26:44 PM3/28/22
to ACQ4
Hi Luke, 

Thank you for your prompt response. What you describe makes sense. I will most certainly come back with more specific questions on the way of implementation. 

Thanks again, 
Michael 

Luke Campagnola

unread,
Mar 28, 2022, 5:07:48 PM3/28/22
to acq4
One detail I just thought of: above I said that TeensyDeviceTask.start() should note the current time so that it knows when to send each of its commands, but this is wrong. You might have multiple devices in your task, in which case you want to send commands at times relative to when the _last_ device has started. To avoid this, you can ask the parent Task object when all devices were started. The signature for DeviceTask.__init__ includes a parentTask argument, and if you ask for parentTask.startTime, its value will be None before all devices have started, and it will have a float (unix timestamp) value after all devices have started. So your TeensyDeviceTask background thread could use startTime to decide when to send commands, and just sleep if it is still None.

Luke


Reply all
Reply to author
Forward
0 new messages