Using python on a Raspberry Pi, how do I record video and keep reading other sensors simultaneously?
For my system I would like to record constantly, which independently I can do. Additionally, I need to be constantly reading some sensors (using SPI), which independently I can also do.
My present problem is combining the two tasks. I need to record all the time, but also read the sensors 10-50 times per second.
Using picamera and camera.start_recording() is usually followed by a sleep() delay. Here are the solutions I plan to explore, but was hoping for some pointers and guidance before diving into each for several hours.
Since I essentially need to record from start to stop of the program, which runs several hours, I was thinking of placing all my sensor reading functions after the .start_recording(), allowing the program only to stop_recording() after my final test conditions have been met (per sensor information).
I could try running two python programs, one recording and one sensing. I believe this would require multi-threading? Or multiprocessing?
Please share your thoughts, and suggestions of other approaches to this problem.
1 Answer 1
Multithreading is a good approach to this problem but is not required.
Logically what you can do is called cooperative multitasking.
Have a loop with time counters for when you last read your sensor and when you last grabbed an image. Define constants for how frequently you must grab an image an hiw frequently you must read sensors. So long as there is enuf tolerance in when you read (there must be some with any approach) you should be all set.
Multithreading can help achieve tighter tolerances on when you read. Also, running two python programs at once can similarly tighten the timing tollerances(as u suggested)
picamera
brings its own background thread to do the image sensor polling when you callstart_recording()
, as such you are already doing multi-threading in your proposed approach without intending it.