I am currently attempting to synchronize four raspberry pi 3s using Pi Camera V2s to take photos of a stopwatch at the same time. I am using a function generator to send a signal to the GPIO pin. The images are occasionally .033 seconds apart. I believe I have pinpointed the source of this. I used a profiler on a python script that simply captures an image. Looking at the processes, a "sleep" function of duration .033 seconds is being called usually 14 times. It sometimes, however, is called 15 times, which would account for the discrepancy. I believe this extra instance of sleep is occurring before the shutter begins to open, which would make the cameras images captured .033 seconds apart. I cannot, however, for the life of me, find how/where the shutter is triggered to open in the picamera python library. My thought is that if instead of it trying to decide how long to wait, I hard coded a wait period long enough to ensure everything is in order, the cameras will all capture the image at the same time. If anyone knows of where to find when the shutter is being opened or any other insight into the matter, that would help a lot. Thanks guys. I can attempt to further clarify or provide further information if needed.
Asked
Active
Viewed 1,646 times
4
1 Answers
1
When you call the capture
function of the picamera object it is starting the encoding and waiting for a callback to reset wait
and so indicate it is complete (or for CAPTURE_TIMEOUT to be exceeded):
encoder.start(output)
# Wait for the callback to set the event indicating the end of
# image capture
if not encoder.wait(self.CAPTURE_TIMEOUT):
raise PiCameraRuntimeError(
'Timed out waiting for capture to end')
the encoder.wait
function looks like this:
result = self.event.wait(timeout)
if result:
self.stop()
# Check whether the callback set an exception
if self.exception:
raise self.exception
return result
and if you look at how event.wait
is implemented (here's a great SE question that goes into detail on it), it is made up of a number of smaller (than the timeout) delays (getting longer as time goes on) and after each one checking if the lock can be acquired.
I would expect there to be very small variances in the time that each Pi takes to acquire the lock, and possibly one occasionally rolls over to a 15th sleep.
So, in summary, not something that you have any control over...

KennetRunner
- 1,050
- 7
- 13
-
Thank you for the response. So, the event basically tells the capture to wait until the encoding is complete. If I see that the process at most takes some arbitrary amount of time x, could I just have the pi's not use the event to be told how long to wait and instead tell them to all wait some arbitrary amount of time x + y. That way, they should all be waiting the same amount of time to continue instead of waiting 14 or 15 steps, thus desyncing. – MaxwellG Jul 24 '16 at 13:54
-
I don't believe that functionality is available in the picamera library. You could add it, or write your own, but that is a much bigger task... If your concern is that one camera is frequently going to take longer and drift more and more out of sync then adding a variable/compensated delay after each capture might help 'resync' them for the next capture... That said, I defer to the comment on the OP by @Dave Jones , he clearly understand the inner workings better than I do. – KennetRunner Jul 24 '16 at 14:13
while not(GPIO.input(inputPin)): pass
to wait until the pin reads high. Is there a more optimal way to do this? Also, we are using a single function wired to both Pis. – MaxwellG Jul 25 '16 at 16:15wait_for_edge
; it uses epoll under the covers so you won't miss any edges and the processor's not spinning on Python byte-code – Dave Jones Jul 25 '16 at 20:19wait_for_edge
method. 3 out of our 15 pictures were desynced by that same .033s margin. These desynced pictures were not consecutive. At this point, the cameras should be initialized at the same time, and they should be told to capture at the same time (separate pulses). Do you have any other ideas for things we could change to further sync the images? Thanks again for all your help. – MaxwellG Jul 25 '16 at 21:28