Wibbly-wobbly water. Color encodes pressure.
Hooray! I finally digged through all my references and implemented a fluid simulation in Processing. Read below for what I’ve learned, video and code.
Let’s define simulation as a prediction of real-world events (think of something you cannot or don’t want to measure or something you’d like to explore before actually building it). So why bother when there’s already reality to look at?
Here are a few reasons I can come up with:
The last point above is a little bit obscure and I’d like to elaborate. Actually, this is my favorite reason for spending free time with simulation (in contrast to paid time at work which is all about 1-3): I’d like to learn something about reality!
I remember when I was a first-year student, I had a hard time figuring out what exactly torque is. My first year of mechanics was all about static calculations and I feel torque is just so much easier to explain when you’re considering a dynamic system.
So let’s try this: Consider a body floating in free space with rocket boosters attached to it.
Force: If a rocket fires, it will exhibit a force on the body. This will make the body move (accleration = force / mass ).
The yellow rocket fires, this makes the body move to the left
Torque: If two rockets fire in opposite directions, the force will cancel out. Depending on their positions relative to each other, a torque will however be created. This will make the body rotate (rotational acceleration = torque / inertia).
Orange and red rockets fire (on the already moving body). Forces cancel out, but the body starts to rotate.
I’m a huuuuge cola enthusiast, with an appetite for non-mainstream versions (most major brands just taste boring to me). So I wanted to make OpenCola for a long time and finally got around to do so.
It took me several attempts to get the recipe right, but once I did it turned out both very delicious and very easy to make. I ended up modifying the original opencola recipe for my needs and trying several versions, so here’s my summary of what did and didn’t work.
Two years ago I’ve written a post on building an object-oriented multibody structure in Matlab. The project has since been sleeping, but since my experiments with Processing I decided to implement a comparable structure there. This means:
This approach allows a very elegant interface for simulation: Adding an object is just one line of code, as is simulating one timestep:
Processing is a programming language / IDE built on top of Java that’s intended for simple and visual programming, making it a great tool for WYSIWYS (what you see is what you simulate, actually that is not a real term, I just made it up).
If you’re unsure what Processing is, you might want to check out either the examples on processing.org or the double-pendulum I’ve just built into it. It is a port of my code for Arduino with TFT, but with Processing, it’s cleaner and way nicer to play with!
You can find an interactive version of my program here on openprocessing.org. It also includes a general implementation of the Runge-Kutta algorithm for solving ODEs, in case this is needed (without giving any guarantee of correctness or code quality – use on your own risk).
Update 2018-02-11: I’ve just published an updated version that simulates three pendulums in parallel with different algorithms (explicit euler, symplectic euler and runge-kutta). You can find it here.
I’ve got some great references and quotes here that help me explain how exactly we are able to numerically calculate the dynamics of a multibody system.
I’m not only a simulation nerd, I’m also a visualization nerd. My interest in formatting, layout and displays has proven to be extremely helpful in my daily work, where finding the right visual is often key to analyzing and communicating large measurement and simulation datasets. Sometimes, I also find the time to participate in fun events like the recent storytelling with data visualization challenge – which also is a good excuse to write this post on plots and visualization techniques.
So here are some simple tips to get better result plots and graphs. Most of my advice is focused on visuals for simulation results, especially in the context of large datasets and use-cases where you have to plot results frequently (like multibody simulations). This often boils down to getting the workflow right – the most beautiful visuals won’t help when creating them takes more time than what’s available.
I’ve finally built my Due and TFT into a nice housing and coded a reasonably well-working oscilloscope and datalogger for it. Here’s what I’ve learned along the way!
The oscilloscope showing analog input and digital output from an Arduino Uno powering a servo motor with the “knob” example sketch from the Arduino servo library.
I’ve used a lot of different configurations of Arduino-related gear as datalogging utilities. So here’s a comprehensive guide on what’s possible, how to set up stuff and on what you can expect regarding accuracy, battery life and logging speed.
(However, please keep in mind that I’m neither particularly skilled with electronics nor programming and other’s know a lot more on this than I do. I’m particularly grateful that Ed Mallon provided a link to this paper he coauthored with Patricia Beddows in the comments – the work and knowledge they put into it is just amazing)
My Arduino Uno with the datalogger shield both temperature and brightness sensors connected. I used a normal smartphone charger to power it for more than 3 days and placed it in this fireproof baking tray since I felt somewhat unsure about having it running unattended.
I recently learned about the Kalman filter and finally got to play around with it a little bit. Since I had a hard time figuring out how to get it to work, here’s a practical (but yet general) introduction with examples:
A Kalman filter works by matching a simulation model and measured data. For each data point, an estimation of the simulation model’s internal state is computed based on the estimate of the previous state. This works with noisy data and limited measurement signals (e.g. a model with 10 state variables but only 2 measurement signals, although there are obvious limitations here (the more and the better the sensor data, the better the results should be – there’s also some limit on observability).
So we have this interesting tool which does all these different things:
Another interesting use is that we might try two different simulation models on the same measurement and check which one does a better job at synchronizing to the measurement (I’ll do this in a very simple example below).
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.
It’s been more than a year since I published my post on numerical integration on an Arduino. Since then, the post has been quite popular, recieving a steady stream of visitors (mostly via Google). When I originally wrote it, I only had an Arduino Uno at hand – since then I’ve added a couple of Nanos and lately an Arduino Due to my inventory and decided it would be interesting to do a couple of speed tests to see how they perform. The latest addition to my growing circus of microcontroller boards is a Teensy 3.5 board. (Update May 2019: Added a ESP32 dev board)
As I pointed out in the original post, numerical integration relies heavily on floating-point math – which is something the Arduino’s 8-bit processor is not particularly good at. The Due features a 32-bit processor, a clock frequency of 84 instead of 16 MHz and the possibility to use double (64 bit) instead of float (32 bit) as a data type – so I was curious to see how it would compare to the Arduino Uno. The Nano is supposed to have more or less the same characteristics as an Uno, but is a lot smaller and cheaper – see below for details.
Now added to the comparison, the Teensy 3.5 includes a 32-bit processor with 120 MHz clock speed and a FPU for speedier floating-point math.
I’ve spent some time on setting up audio and video streaming on my raspberry pi (mostly used as a baby monitor right now). While there are great ressources out there, it took me a lot of effort to find them and put everything together. So here’s a handy list of instructions if you want to:
This post is not really about multibody dynamics. Instead of simulating stuff, I’ve actually built something: A glowing, more-or-less-interactive lamp.
We are awaiting our first child and with all the things we bought (mostly used), there was a small, adorable lamp. Unfortunately, it had some weird-looking electrics inside (both looking very old and labeled for 120V – so it wouldn’t work with our 230V here in Germany), so we decided to remove all cables and replace them with LEDs. Actually, WE just decided not to use the existing cables and then I talked my wife into letting me build something with an Arduino.
So I hooked up the lamp with a digital LED-stripe – I found one that came with digitally-adressable LEDs and used the Adafruit Neopixel library to adress those. Now there are 24 RGB-LEDs inside the lamp which I can control direct with the Arduino.
What’s better suited for object-oriented programming (OOP) than building a simulation of a physical system? Each physical object (e.g. mass, spring etc.) can be implemented as an object and then we should be able to easily build and modify large models.
However, there are a lot of challenges to get there, including:
Getting there turned out to be quite interesting and I finally got a simple object-oriented multibody model to simulate in Matlab and GNU Octave. This post is a wrapup of all the stuff I learned to get there.
Update: There is now also a newer post I’ve just written on a comparable multibody simulation structure in Processing / Java.
So far, I’ve mostly been working on rotational mechanics – multibody models where each mass only has one rotational degree of freedom and two state variables (angle and angular velocity). Every now and then, I use some commercially available tools to simulate more complex 2d and 3d models – often being surprised by how slow simulation is. At the same time, I’ve been wondering a lot:
An important answer to these questions is the topic of impulse-based dynamics. I stumbled upon it in some amazing literature from Prof. Jan Bender (see http://www.interactive-graphics.de/) and so far I’ve been experimenting a bit with the method myself. This post today features a small and practical introduction to modelling boundary conditions in impulse-based mechanics.
This is just a small update on my experiments with the Arduino. I implemented the runge-kutta-method for solving a multibody system a few weeks ago. So this is a working implementation of the standard 4th-order runge-kutta ODE (ordinary differential equations) solver for the arduino platform, something I haven’t seen elsewhere.
(Note: If you are just looking for the arduino sketch, you’ll find a link at the end of this blog post. If you are wondering about related questions or have trouble adopting it to your needs, please leave a comment. Also see this post on how numerical integration of differential equations works.
Update in August 2016: See also my new post on achievable simulation rates with an Arduino Uno/Nano and Due)
My main goal was to get a better grip on simulation speeds. Why is my simulation so slow? is a question I’m really thinking about a lot. Since I wasn’t able to find a good example of an arduino sketch for ODE solving online, this may be interesting anyway.
Until the beginning of this year, I’ve only been coding in Matlab, GNU Octave and a few other comparable high-level 4th generation programming languages. I’ve long wanted to learn a more basic programming language which allows smaller and faster programs (more on this in another post). Basically, I wanted an answer to the question:
I’m an engineer and want to learn about programming. What can I do to get started?
It turns out one of the best answers is this: Get an Arduino.