I have for planned to build my own drone from scratch for learning purpose and will program all controls for the drone. My question is if it's a good idea to split the code that doesn't interact with each other to another program or just have them in different threads.For example I have one program for controlling the drone and then one program for taking images.The pros I have found of this is:If on of the program crashes the other will be just fine. Which also leads to that I could have backup programs if one crash.Some negative I have found is thatAll the signals I send back and forth to it would be checked multipletimes.Total file size will be larger.
Split program to smaller programs
code organization
I would advise against multi-threading, especially for a real-time application. The main questions are how to use the cores, if it is a multi-core processor, and how to manage multiple processes (as you have already noted).Firstly, I would recommend putting sensor processing into its own process in all cases for each sensor. For example, if you have an imager and a IMU, each one should have its own process. Any specialized high-speed code for processing the sensor data stream should be in the sensor handler. In general, if the sensor is critical, like the IMU, it should be given its own dedicated core. Non-critical processes should share whatever cores are left over. Obviously you either need an RTOS to do this (recommended), or a specially configured version of Linux.Other than the sensor processing, everything else should usually be in the same monolithic process unless you have some very specific and specialized need for it to be in a separate process. This monolithic process should be designed so that it allocates appropriate amounts of processing time to the different tasks and should have a top-level exception handler that guarantees that any problem will be resolved in a useful way.It is also a good idea to have two heartbeat monitors. These processes check the other processes and restart them if they have crashed. There are two of them because if one dies, you need the other to restart it.You can communicate between processes at high speed using shared memory. Doing this is usually much faster and simpler than messing around with IPC or any OS-moderated communication.