Real-time simulations on an Arduino (double pendulum)

I got a small display compatible with my Due for christmas. And since I really wanted to see some arduino-in-the-loop simulations, I decided to use it for exactly this: real-time multibody simulations on the Ardunio and the results displayed on the tft.

Surprise #1: Setting up a display takes time

I expected to by able to directly mount the display on the arduino, instead I ended up placing a lot of wires between both – which actually took me 2-3 hours to figure out how to get it running with the UTFT libary (my fault, not the library’s). If you want to run a color TFT display, consider getting a display shield to save time.


That said, once the wires were placed and I managed to set everything up, the library worked like a charm and building my own plotting routines was fairly easy. So as an example model, I decided to model a double-pendulum on a moving sleigh. Which brings me to:

Surprise #2: Equations for a double-pendulum are really easy to screw up

In the end, I used the equations given here (page 3) and simplified them (move the sleigh from the state variables to the right-hand side, set l1=l2 and m1=m2):


Then I spent several hours trying to figure out why my simulation produced obscure results until I found that I messed up my state variable assignment in the ODE function (along a few smaller mistakes).

My loop function now looks like this:

  1. Use analogRead to get the position for the sleigh (~0.004 ms)
  2. Simulate one step with the Runge-Kutta algorithm (<1 ms)
  3. Output the results on the TFT display (~13 ms)
  4. Wait for real time to catch up

1-3 overall takes 13-14 ms, so the overall setup allows a maximum simulation frequency of something around 70 Hz. If calculation time is a requirement, this can easily be upgraded to ~1kHz by skipping or optimizing the display part (and maybe to ~100 kHz by moving to a Teensy board) but for my requiremets, this worked all fine (calculation frequency is currently set to 20 Hz). What’s nice is that the system directly interacts with my input:


My whole sketch is a mess, so I’m mainly putting the code online here as a reference. Also, I plan to do more stuff with the combination of measurement and simulation data, so hopefully there will be further posts on that topic.


5 thoughts on “Real-time simulations on an Arduino (double pendulum)

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s