# The reason(s)/benefit(s) to use realtime operating system instead of while-loop on MCU

I'm working on a wheeled-robot platform. My team is implementing some algorithms on MCU to

1. keep getting sensors reading (sonar array, IR array, motor encoders, IMU)
2. receive user command (via a serial port connected to a tablet)
3. control actuators (motors) to execute user commands.
4. keep sending sensor readings to the tablet for more complicated algorithms.

We currently implement everything inside a global while-loop, while I know most of the other use-cases do the very same things with a real-time operating system.

Please tell me the benefits and reasons to use a real-time os instead of a simple while-loop.

Thanks.

• tell me the benefits and reasons to use a real-time os ... there are none for simple projects – jsotola May 22 '18 at 5:01

Using an RTOS on a microcontroller is for reducing the complexity of developing your solution, and reduces the need for programmer expertise for device drivers, at cost of increasing the complexity of your code base and often a reduction in the efficiency of your code.

Bare metal code (your while loop) is usually cleaner and more efficient for small solutions.

If you have a bare metal solution that works, keep it!

If you find that your complexity grows to the point where you are writing something that looks like scheduler code or inter process communications code to solve your problems, your are writing your own RTOS and should consider using and existing one.

You should read the documentation and code of a small RTOS for your own education as soon as you can. It will help you make this choice easier in the future.

IT all depends on the complexity of your system.

Typically micro-controller can handle, under hard real time constraint, simple computations related to one pr two peripheral (actuator, sensor) and communication. So they are well suited for low-level interfaces.

But if you want to have more complex, i.e. computational intensive, process, like path planning, action planning, ... You will need to switch to CPU or GPU based boards. At that point if you still want to met real time constraint you need to have a real time OS.

Note that for some application the real time constraints for high level algorithms are soft, hence there is no necessity to have a real time OS/system.

In addition to the other answers, I would add that determinism provides a large benefit with an RTOS. Your while loop may execute faster sometimes, and slower other times, depending on the computations and conditional results. An RTOS can guarantee a minimum loop execution rate, or a fixed rate (which I usually prefer), given that deterministic programming practices are followed. You can do this with bare metal code, but in my experience it is not done and you just try to execute the loop as fast as possible.