Main loop and master schedule

I’m very much a novice, so apologies for the simplicity of the question. When I was looking through the source code, I had expected some kind of obvious main flight loop, that contains a lists of functions/tasks that are performed sequentially and continuously looped through in normal flight, however I couldn’t find anything to that effect. I’m guessing I fundamentally don’t understand the architecture of the firmware, but can anyone explain why there is no obvious main flight loop (or tell me where it is)?

1 Like

@User_N here’s a diagram with the basic PX4 architecture https://dev.px4.io/en/concept/architecture.html

I hope it helps.

I’d like to reopen this. I’m a former Piccolo dev and am now getting spun up on PX4. As @User_N wrote, I was expecting main() to kick off a flowdown of major functions. Instead main() initializes a server/client setup that I’m not clear on. The architecture chart provided above is helpful in seeing how different components and modules interact but it doesn’t describe the program flow in a way that allows someone to understand how we move into and out of a state or mode.

For example, how would I determine which flight mode I am in for a given vehicle time and position?

Thanks all!

@Charles_Armer @User_N I think what you expected was something similar to the Arduino void loop()-function, right? I’m afraid you aren’t gonna find anything similar in PX4. The looping of specific functions is handled by the scheduler of the operating system that PX4 is running on (NuttX). In the code you need to watch out for calls to px4_task_spawn_cmd(). This function adds a task to the scheduler. From what I understood about this architecture so far is that it is much more dynamic than having a static loop function, that calls one task after each other. The schceduler can prioritize and deprioritize tasks and thus it can ensure real time performance for the most important tasks.

2 Likes

@max11gen you are spot on. What you described makes perfect sense. Where can I find more information on how the scheduler works?

As an example, how do I trace back where bool Commander::set_home_position() gets called from? Or maybe another way to ask, how does this method get sent to the scheduler?

Hi Charles,
Former Piccolo dev, here, too. Cheers!

Here is a diagram I created while investigating PX4 scheduling: PX4 Work Schedule Structure.pdf (50.7 KB)

A nice thing about PX4 is that the inter-module communication is pub/sub (uORB). Availability of data is what often drives execution for a module, although other scheduling options exist.

Here is a diagram showing data flow from position controllers to mixer. PX4 module dataflow - controllers.pdf (28.6 KB)

NuttX is hidden from the programmer. The work queue mechanism is what you need to examine.

To answer your actual question… grep, ctags, clang tools, etc. can tell you the call site(s). In the case of set_home_position(), it appears to be initiated from the other end of a MAVLink connection. Commander is basically a daemon that can receives requests from a console or remote protocol link.

1 Like

@Danduril Thanks for the links to those great resources, I’m not familiar with them, maybe you want to contribute them to the docs?

@max11gen essentially what @Danduril already explained, we run a very modular system, and the flight modes are internally called “FlightTasks”, I would suggest you take a look at the following session from the PX4 Developer Summit 2019 by @MaEtUgR

2 Likes

@rroche Thanks for sharing that video, it was really interesting to watch as are most of the PX4 Developer Summit videos! Btw: Is there gonna be a Dev Summit 2021?

For sure!!! I’m making plans right now, we are defining the dates, there will ample time to submit talk proposals, and I would be really interested in seeing what you are working on :slight_smile:

The dates and timeline for the CFP will be unveiled soon, the event is most likely to remain free (as last years), but the format might vary a little bit, to give folks on other timezones the same opportunity to experience the event together, we will most likely include some sort of prices and swag to some lucky viewers (we are unsure how to accomplish this right now) we have lots of plans, and we are trying to narrow down focus to make sure its not a logistical nightmare for us, but a totally enjoyable event for the community. Stay tuned, I’ll make enough noise to make sure everyone hears about it.

@rroche Great to hear! :slight_smile: I doubt that I will apply for a talk though. I just started working on my project and with PX4 in general a 3 month ago. I’m not there yet.

Your plans sound great! I’m really looking forward for it. Keep up the great work!