View Full Version : Multithreading (Parallel loops) on an Arduino

01-12-2013, 12:20 PM
Hi everyone,

I wondered if anyone knew of any easy way of achieving multithreading with an Arduino. I have googled it but I wasn't able to find any concrete answers. I know Trossen Robotics use an Arduino compatible board for their own robots, so I wondered how they do it (or is it all in one loop?)

More information -
The reason I ask is that I'd like the ability to run multiple parallel loops for controlling the motors in each leg of a hexapod I want to build.

Currently I'm actually using an SBRIO (An embedded controller with onboard FPGA from National Instruments), which inherently supports parallelism. I was wondering if a similar functionality could be achieved with an Arduino.



01-12-2013, 02:56 PM
AVR's don't really have the computing power to do a lot of multi-threading (nothing like an FPGA). The 'hard real-time' stuff is usually just done with interrupts and callback functions configured to each program, but there are a few full real-time operating systems out there that make it easier. The ARM-based Arduino and Maple boards are a bit more capable.

The arbotix does not control the motors in the bots as each servo has its own AVR or ARM microcontroller to do all of the motor power switching and position sensing. The arbotix simply polls the servos for their current position and then computes where they need to go and sends them a new goal position to get them there smoothly.

01-12-2013, 03:46 PM
For single-core CPUs (including microcontrollers,) you're usually best off just building a state machine of everything you want to control (or multiple, parallel state machines,) and just update each of the state machines in a main loop. As long as the total time to run your main loop is shorter than the required reaction time for each of the state machines, you're good.

01-12-2013, 04:39 PM
A quick Google search turned up these libraries:

And now for something completely different!:

-James Nugen

01-12-2013, 05:18 PM
Cool. Thanks guys.

It looks like I'm gonna have to learn a bit more about it (it's not as simple as writing a separate loop or anything).

The state machine idea sounds like a good plan as well.