3

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 that

  • All the signals I send back and forth to it would be checked multiple times.
  • Total file size will be larger.
3
  • Is file size a problem? Also, would checking the signals multiple times be a problem? Commented Jun 15, 2015 at 17:54
  • Depending on design choices file size will probably not be a problem if I stream the photage to a computer instead of saving it locally. The signals will be fast to check. However many signals will be sent each second. The signal it will check is just the 3 first bits in a sequence of bits. I want the drone to be as responsive as possible. Would be kind of sad the drone flew strait into a tree because it took to long for it to respond to my actions.
    – Olof
    Commented Jun 15, 2015 at 18:06
  • Not sure what you mean by "All the signals I send back forth to it would be checked multiple times". It probably doesn't matter much in your case because you can just make sure a thread that crashes doesn't crash your main application and the main app restarts the thread just as reliably as your multi-program approach. In the multi-program approach, you could have a "Communications" program that does the sending and receiving to the radio and internally you have interprocess communications routing commands and receiving status and responses.
    – Dunk
    Commented Jun 15, 2015 at 20:13

1 Answer 1

1

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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.