Live data from Hacker News

Introducing multitasking to Arduino

blog.arduino.cc

61–70 of 88 posts

Re: Introducing multitasking to Arduino

#61
post #25

We've done quite a bit of experimentation with adding preemptive multitasking support to non-hard real time software running on MCUs at my current company. After a lot of head banging and dead ends, I've come to the conclusion personally that the embedded community could really use an implementation of the POSIX threading APIs or some meaningful subset thereof for various platforms. They're already standardized, well…

Threads are already supported by esp-idf on esp32 source: https://docs.espressif.com/projects/esp-idf/en/latest/esp32/...

And esp-idf uses FreeRTOS, which is pretty much the standard for this kind of thing.

Re: Introducing multitasking to Arduino

#62
post #8

Nice, but pointless addition to an old processor. ESP32's are cheap and readily accessible and they come with FreeRTOS running already. If I absolutely need to multitask on a Mega328 Arduino, I use the protothreads library. But really, for anything heavy, I'll turn to an ESP32 or an STM32. Use the right tool for the job!

Yeah, the art students with miniscule coding experience who just want to get stuff done are probably going to be thrilled to work with FreeRTOS. The truth is, that Arduino always has put in a lot of work to do things that could already be done only to make them more accessible, easier to use and thus more widely available. If you think this is pointless you should consider this might say more about how far you have c…

> the art students with miniscule coding experience

That is a minuscule part of the Arduino audience. For the rest of us, FreeRTOS works fine.

Re: Introducing multitasking to Arduino

#63

A couple years ago, I managed a summer/afterschool program that introduced thousands of kids (age 8-18) to the Arduino. When we would set up a software abstraction to simplify multitasking (something that came up often with more advanced students), we set up something like this: int time = 0; int max_period = 10000; void loop() { if (time % 100 == 0) { everyTenthSecond(); } if (time % 1000 == 0) { everySecond(); } //…

I’ve made some fun projects with Espruino - where you can literally use setTimeout/setInterval and it will even sleep the uC until it hits the the timeout :)

Re: Introducing multitasking to Arduino

#64

A couple years ago, I managed a summer/afterschool program that introduced thousands of kids (age 8-18) to the Arduino. When we would set up a software abstraction to simplify multitasking (something that came up often with more advanced students), we set up something like this: int time = 0; int max_period = 10000; void loop() { if (time % 100 == 0) { everyTenthSecond(); } if (time % 1000 == 0) { everySecond(); } //…

I’ve made some fun projects with Espruino - where you can literally use setTimeout/setInterval and it will even sleep the uC until it hits the the timeout :)

So it simply manages both multitasking and power management?

Re: Introducing multitasking to Arduino

#65
post #35

Earlier quoted context omitted.

> move up to a larger system I'll take this one step further and suggest that beginners not start with the AVR, but instead with a Blue/Black pill (STM32) or ESP32. Leave the AVR for the more advanced people who are trying to squeeze every penny out of a project (even there, in many cases STM32 will cost less!!!). I visit the arduino.cc forums at least once every day and the things that beginners want to do these day…

I agree- I think just about everybody should start with an ESP32 instead of an Arduino. I still use the Arduino API when programming the ESP32 (due to the large amount of compatible drivers) but I've also installed FreeRTOS and ported zork. I am embarassed to say I also got C++ STL working on an AVR with 8k of ram- enough to allocate a std::vector of length 2.

I actually use the ESP32 version of FreeRTOS professionally, and I can't imagine rolling your own scheduler and all the work involved when someone could just use the existing FreeRTOS codebase. The ESP32 stuff is awesome for my hobby projects, I do see the warts when I use it professionally though, trying to get the system to do more than a couple things at a time is not easy. We've re-written a number of core ESP-IDF packages, or gone through the pain of porting the vanilla FreeRTOS stuff over. We're also using C++, I'm looking forward to Rust.

Re: Introducing multitasking to Arduino

#66

A couple years ago, I managed a summer/afterschool program that introduced thousands of kids (age 8-18) to the Arduino. When we would set up a software abstraction to simplify multitasking (something that came up often with more advanced students), we set up something like this: int time = 0; int max_period = 10000; void loop() { if (time % 100 == 0) { everyTenthSecond(); } if (time % 1000 == 0) { everySecond(); } //…

Hell, I did that for a prototype product once. Combined it with a state machine and wrote the entire thing to be nonblocking. Worked great.

Re: Introducing multitasking to Arduino

#67
post #25

We've done quite a bit of experimentation with adding preemptive multitasking support to non-hard real time software running on MCUs at my current company. After a lot of head banging and dead ends, I've come to the conclusion personally that the embedded community could really use an implementation of the POSIX threading APIs or some meaningful subset thereof for various platforms. They're already standardized, well…

RTEMS has been providing a real time, multi threaded, POSIX API on embedded systems for decades. It's got preemption, interactive shells, a libbsd port with networking, etc. It's popular in spacecraft, especially in combination with NASA's Core Flight System (cFS). I've run it on an STM32F4 and a Raspberry Pi.

BSP support is a little lacking in some cases. The raspberry pi is currently crashing on a call to fflush(). No idea why.

I really wish more people knew about it. As a matter of fact, I'll post about it here.

Re: Introducing multitasking to Arduino

#68
post #47

Earlier quoted context omitted.

I'm a casual embedded-electronics user at best, most of my projects haven't advanced much past turn a simple servo, light some LEDs, maybe read a sensor and POST to an endpoint. For these tasks, Arduino has been pretty good for me and removed a lot of the complexity. The `setup()` and `loop()` model makes a lot of intuitive sense. That being said, I've spent no small amount of time on `String` vs `char*` (as an inexp…

I think AVR is a good architecture to learn on, because the devices are far simpler than ARM or other more modern chips. The main concept to master as an embedded developer is operating a device by its registers. The common arduino device, ATMEGA328, has a total of 84 registers across all peripherals. A simple program to blink the LED is less than 10 lines total. The datasheet is tiny compared to most ARM devices, bu…

That was true when one had to write assembly code. Now we don't really have to any more, thanx to Arduino mainly (which bilds upon avrgcc and avrdude). It's easier to learn on the new platforms because you can use higher level programming languages like MicroPython, especially for easy stuff. I can do a blinky by writing three lines in the uPy REPL.

Re: Introducing multitasking to Arduino

#69

I wonder if async Rust would be a good fit for this use case? It doesn't solve the accidental blocking problem (although async-std did experiment with blocking detection at one point), but you can get transparent support for multiple cores and a lot of thread safety.

You can have a look at https://embassy.dev Rust+async on embedded

Re: Introducing multitasking to Arduino

#70

async/await does not result in bloated code that is hard to maintain and debug. It's a strange thing to say - async/await is now built in to most major programming languages - strange that Arduino would dismiss it so easily. They are correct in that async/await does not solve multicore utilisation, but it's the most important and easiest way to implement parallelism on a single core.

TFA makes no mention of async/await, only asynchronous programming. Specifically, it says:

> The traditional way to do this is to write non-blocking code so that the loop() function can run as fast as possible, updating state variables and calling the millis() function to ensure proper timing

Indeed, that style of programming is nothing at all like async/await in, say, Javascript.

Post reply on HN