As a game programmer for 30 years, when I learned iOS Swift, I had to change my way of thinking about code because Swift doesn’t function like other languages I’ve used. In Swift, there is NO “main loop”. In fact, the Apple device’s OS is the main loop of your app. Everything in your app is accessed by interrupt handling, either a timer interrupt or a human interrupt, like a button push.
Same in JavaScript, where we also don't have a main loop, programs are functions responding to events like clicking a button (or in case of Node.js like an incoming server request). And the browser is the "operating system" (or Node.js or Bun or whatever else). Okay, a function calling itself recursively using requestAnimationFrame could be something like a "main loop", it's done mostly in games using canvas element...
This is exactly the informational/tutorial series I've been looking for. Even better I found it after it's all done, so I can go straight through it! Thanks!
I remember 2 decades back when I got a little catalog from them. Only the size of a Readers Digest condensed book, for those that remember those. Now they sit prominently and honorably on the World stage, with a catalog too big to ship, and CS reputation unmatched in my history. Keep it up!
Thank you, Dr. Digi-Key. In real time, the narration is displayed in text in the right window of the video. This feature is very useful for me, who has never spoken English.
7:40 To be fair, an ESP32 is much cheaper than an Arduino, and has BT/WiFi so I tend to use them even for projects they are wildly overpowered for. Super loops are great for simple projects just due to the law of fewer moving parts. Faster to develop, lower surface area for bugs. Project complexity is the primary deciding factor. Most industrial automation, for example, runs on PLCs, and they are almost universally super loop architectures. Huge scale, loads of I/O, long programs, but still low complexity. Excellent summary, in any case.
You explained it so perfectly. I was having such hard time understanding RTOS in my embedded systems class this helps incredibly. Thank you for being so informative.
This intro is as good as any I've seen, especially relevant for deciding if the complexity of RTOS is warranted. In my case, it's not but I feel better sticking with the super loop.
When exploring embedded systems, then it is also worthwhile looking at the Orange Pi Zero board. This gives you 4 ARM A7 cores with proper DMA EMAC Ethernet.
I've been looking for Shawn. since he left "that other maker site". I like Shawn's style, and his teaching style works for me. He and Digi-key get my vote and more of my business.
Glad to see you back at it. I watched all your Arduino videos today, and I've seen all your STM32 videos as well. This is awesome content, and I can't wait for the next video in this series.
Thanks Shawn, the tutorial is very nice. One request would like to make is, music is too loud and your voice is quite low, please make adjustment for better experience. Regards
My older brother developed on Atari 1040 ST and a 520 ST computers. The operating system on those 68000 based processor systems was called TOS. The OS was burned onto internal ROMS on the main board. There was something called "realtime" for a musical cartridge back on Jun of 1991.
@@JohnWasinger Tramiel Operating System ... good ol’ days. Enjoyed that one quite a bit with the M68k family over the years. Was used where I had my first programmer job too for various application. Even developed a TokenRing network and integration into TOS.
My question. Where is the line in between assembler code (machine code) run by CPU, so when you create your code on that level, interrupts, loops, and where RTOS begins? I have feeling it is somehow in between just machine code and GPOS, a bit advanced than super loops.
There are instances of asynchronous execution within a super loop. Either by calling inbuilt functions or using command patterns, although I guess functionally they're no longer a loop even if they are structurally
Great i am searching for video like this u know what i found u just one day after u just post ur video, thank u very much i am very eager for next video.
Very interesting. A multi-tasking Computer. Must be a real head- Ache to program. Wondering How many tasks can be performed In real time. Must be like dealing With a woman. Gods only real Time biological multitasking Creature.
So if I understand that correctly, the only difference between GPOS and RTOS is the way task switching is handled so that timing deadlines are met? But isn't this already possible in modern GPOSes as well?
I just set up some peg board in my workspace. I use so many freakin tools doing robotics and other mcu projects. I kind of wonder why I haven’t already been using peg boards.
Nowadays many companies are putting Windows on everything just because they have a large pool of cheap programmers who don't know anything else. The products they make now are slower and less reliable than they were 30 years ago despite the vast improvements in hardware.
The "divide tasks in switch statement" is what many call "state machine". More precisely, a hierarchy of state machines where each switch statement could be seen as a tree structure when matching the call tree.
Does an RTOS' delay function (ex. FreeRTOS' vTaskDelay()), allow the scheduler to run other tasks? Actually, even if a low-priority task sits in a tight while(1), isn't the whole point of an RTOS to have the ability to wrestle away the CPU after too much time? I have a motor control application, and would like if a task receives a command in the queue, it will move until it reaches the destination (30+ seconds). If another command is received while it's moving, it must stop and go to the new position. Maybe the task could look like: wait for queue command start motor moving Continuously check position and stop when reached OR stop when new queue command is received Would monitoring the position block/starve all other tasks? If so, what's the proper way for it to closely monitor position while still allowing the RTOS to run other tasks?
Great question! A multi-core processor is something that's done in hardware: it allows you to run code concurrently on separate cores. An RTOS (or most OSes for that matter) can make it seem like you're running tasks concurrently (even on a single core) by rapidly switching between tasks. I recommend watching Part 2 to see how that's done. Some OSes are configured to handle multi-core systems, so you can have multiple cores that switch among tasks (all running on top of the same OS). In Part 12 (to be released), I show how you can use FreeRTOS with a dual-core system.
You can run normal Linux distros as RTOS by changing the scheduler from CFAS to FIFO. It's not super-duper accurate but it will do the job for 99.999 % of all hobby projects and probably 80 % of all commercial projects. FreeRTOS is great for small critical tasks. VXworks is to go-to for missiles, airplanes etc. where software is just a small part of the cost and things working as they should is worth lives or millions upon millions of dollars.
You don't necessarily need a degree or be in any particular profession. This is an intermediate/advanced topic related to embedded systems. I recommend having some experience with Arduino and C/C++ to understand this series. There are lots of videos and courses out there to get you started with Arduino.
You did not mention state machine cooperative multitasking, a task doesn't hog the system but instead do snippets and then for example tell schedular to come back in 250ms with the option that a tasks IRQ can tell the event handler to run this task "right away" I wrote my own in 50lines of C code. example of usage: OS_Interval_Next(this,5*1024); // give it 5sec time before turning off swclk pin
5:08 You don't need to use multi-core processor for concurrency. If you have interrupt supported by hardware, why don't you use an external timer which fire interrupt every 1ms? So you could switch to the next task after the current task's quota expires.
Most RTOSes (like FreeRTOS) use pre-emptive scheduling, which allows tasks to interrupt other tasks whenever the scheduler decides to do so. In some RTOSes, you can turn off pre-emptive scheduling to turn it into cooperative scheduling, which allows you to use coroutines.
On the principle, you can achieve all of this in a "superloop" by making sure every instruction will execute in a limited amount of time and will never hog the MCU to itself. For instance, when repainting your LCD screen (say, a 5110 LCD of 84*6 bytes), you could block the entire processor for the time to repaint the entire screen and call the function 20 times per second (you're going to kill yourself waiting for the RDY pin of the screen to become high), or repaint the screen one byte at a time and call the function at every loop (returning immediately if the RDY pin is low). Same goes with buttons, you check the state of the button once per loop instead of hogging the CPU with insane debounce delays, It's just a programming style, what's the point of adding a greedy layer of RTOS (in terms of time and memory) instead of accepting the challenges from the limitations of the processor?
For that example, RTOS are overkill. I may update more than one byte but not the entire screen on each calls. Maybe a cache could be best if RAM is many order of magnitude faster. Your code paint to a RAM buffer fast fast. Then, the paint compare if anything is different and update only what changed. The RTOS become necessary when the device must access wifi, ethernet, all brand of SD flash memory and all USB hub/mouse/keyboard/thumb drive that ever existed.
I have been running into a situation where I need something to be timed. The problem, is when the timeout occurs at some point in time when the loop is doing another process.. Let's say the loop takes 11 to 14 seconds to repeat and I want my screen to stay lit for 90 seconds.. I can't guarantee when the loop will check when the screen timer reached 90(not after). It can only tell if the set time has gone past the timeout.. (Just like your piston catastrophe reference). Is there anything like a timer interrupt?
Yes--that is well summarized. Note that heap allocation and access can be non-deterministic as well, which is why FreeRTOS lets you choose the heap allocation scheme (www.freertos.org/a00111.html). I talk about memory management and touch on this in a future episode.
If you want deterministic performance you should avoid using the heap, other than allocating everything you need on startup. Dynamic memory allocation is grossly overused nowadays.
Unfortunately i tried to learn RTOS, to create an independent 6 timers with 1 relay output. Info and examples it's inexistent. Only example, was something with Semaphores. So, i created in Arduino with millis().
QNX is still a thing. Jaguar even used it in their infotainment systems. You can still buy licenses for the desktop OS too. I think it's owned by Blackberry now.
RTOS are not difficult to debug . the issue with RTOS is the tools to do the debugging is crazy expensive and general difficult to implement as they so generic so it takes hours to implement.
This is correct, and it's something I show in the second episode. The ESP32 has two cores, which allows you to run 2 tasks concurrently, but if we only use one core (as I show in the examples), the processor is required to time slice the tasks, as only one can run at a time.
@@ArjanvanVught Yes. A super loop with a state machine is often a very good way to accomplish many (possibly most) goals with embedded code. An RTOS offers the benefit of easily switching back and forth between two tasks to give the illusion of running things concurrently (or actually running them concurrently on a multi-core system) . You can get a state machine to do that (e.g. switch tasks every 1 ms), but you've essentially written a scheduler by doing so (and thus incurring overhead) :)
@@ShawnHymel home made schedulers can avoid deadlock and starvation and priority inversion. Basically, when two threads compete for the same resource, one should timeout and manually perform a retry later.