 This talk will be modeling and simulation of physical systems for hobbyists. Manuel Eipel wanted to lower the entry thresholds for the subject and so explaining modeling and simulation techniques that help even small projects for makers and hobbyists. So please welcome Manuel for the talk about modeling simulation and physical systems for hobbyists. Thank you for the kind introduction and welcome everybody. Actually I forgot to say that all examples that I will show in this talk can be downloaded on the website of the talk so if you want to try it out already right now while I'm actually going to show the examples you can do so. So let's get started by taking about this title a little bit to see what is meant by the individual words and by this have a rough outline of what we're going to talk about. So first modeling in this context means to create a mathematical description of the system so we look at the physics of for example a robot that we want to build or a mechanism then we extract the different physical effects look at the equations that are represented in these effects put it all together and then we obtain a mathematical description. And simulation that means that once we have this system, this model we are going to use the computer to perform calculations from one time step to the next so to basically extrapolate from an initial state of the system to how the behavior is going to be in time. In this talk I want to talk about physical systems so hardware that we can create and not some sort of fictional physics for computer games or something but release something like a mobile robot with a small drone or similar that as a maker you want to create. And I'm addressing this talk to hobbyists. I don't want to reduce the knowledge and competences of hobbyists but only say by this that the resources that we're going to use in this talk are commonly available tools and not simulation software that you have to buy for tens of thousands of euros and I'm not exaggerating about it. So maybe a quick round like who did math courses at university level? Okay, it's quite a lot. So actually I prepared the talk to address it to everybody who had 10th grade school math so it might be a bit familiar to you already but then you can help your neighbors who have difficulties. So let's get started. What's the motivation behind doing simulation? Why do we want to simulate something? So imagine with your friend you want to build a small self-balancing robot like this so it's a very easy design. Two wheels left and right which have one motor, each wheel has one motor and then there's a rod going up with a camera fixed on top so that you actually have a higher point of view so that you see something and this is actually an inverted pendulum so if you build it like this without a controller it will just fall over but it's quite easy to implement at least hardware-wise. So you want to build this with your friend and you say okay I'm good at programming but I am not so good with the mechanical aspects so your friend says okay no problem I will build the hardware but then you're stuck okay you don't have the hardware yet so how do you want to develop the software if you don't have the hardware? And this is where simulation can help for example. So in the time that your friend is building the hardware you can already start the software you just have your simulator, you recreate the hardware while it's absent and then you can program your controller everything that you want and once the hardware is ready you keep the controller you throw away the simulator and ideally then the system is complete so I build a small simulator for this example and Python you can also download the code so you see here the simulation is already running but it's perfectly balanced so it's not dropping yet but if I push it a little bit from the side it will tip over I didn't activate the controller yet so it's like if you have your robot without power if you push it a little bit it will tip over so now I can activate the controller and then push it a little bit and then you see that the controller will actually bring it back so the controller is the part that you will implement in software and the hardware is basically this part the tipping over part the hardware that your friend will build and let's see I cannot even make it drive around just as one example of what you can simulate with the methods that we're going to see okay the problem is I don't have that much time so we're not going to go all the way start with very basic models and then we see how far we go so to summarize the motivation simulation can be used as a placeholder so while the hardware is built you have the simulator and then you exchange it or it can be used as a virtual test bench for example for scenarios where you don't necessarily want the real hardware because it's risky behavior and it can be damaged okay now one step back modeling what is it and what are the basic foundations so imagine our friend Newton here under his apple tree and now we cut the apple loose and we ask ourselves okay what's going to happen we can look at that at different levels of detail from a very simple level to a very detailed level at the simple level you could say okay the apple moves down so you see on the plot the line just goes down constant velocity then well this is an observation that you can make very easily just by looking at it you don't need to measure anything then at the more detailed level you see that if you make a more precise measurement actually your apple is accelerating so you have this parabolic curve because of the gravitation the apple becomes faster and faster and then if you go at an even more detailed level you notice that there's actually aerodynamic drag so it won't accelerate infinitely but it will eventually reach a saturation velocity this is for example when a skydiver is falling from the sky then you clearly notice this effect of a longer period so the take home message that I want to show with this slide is that actually depending on the question that you want to look at with your modeling and your simulation you should choose an appropriate level of detail so as simple as possible and as detailed as necessary and this might actually change for the same system so at one point in time you might be interested in this starting behavior so you need the acceleration and at another point in your project you might be interested only at what happens here at saturation velocity okay now we go even one step further back in the mathematical methods so differentiation and integration because this is very essential that many physical measures are linked to each other through differentiation or integration for example if you take the position x velocity v and acceleration a then if you differentiate x you get velocity v and if you differentiate that again then you get the acceleration a and the other way around by integration then you obtain velocity again and the position so you might remember this equation from school for calculating the derivative so basically the derivative is defined as the ratio of the function you want to differentiate at two points in time, point t plus h and t so the difference of this divided by the distance in time so the h and if you then have this corner case where h goes towards zero you actually obtain the derivative at the point t so in this case the velocity at the point t now if you take this equation and you modify it a little bit so you multiply by h and you add the x of t then you obtain this equation which is one way of calculating the integration so x in this case at the point t plus h in time so you see that it depends on the x of t so a little bit earlier in time and it also depends on the velocity so the derivative of x a little bit earlier in time multiplied by h and again if you take this corner case where h goes towards zero then you obtain x of t plus h so this means actually by integrating we can look a tiny little bit into the future because the derivatives are always running ahead in time of the function that was differentiated if you integrate it backwards you can look a bit into the future and that's exactly what we want to do in simulation we want to say we have an initial state at point t and then we're interested to know what happens at point t plus a little bit so in the following we're always going to integrate for the simulation and not differentiate the only problem is now this corner case definition that's nice for mathematicians but it's not really useful for computations we cannot write this in software so what we're going to do is we're going to use the so-called Euler method and this simply means that we replace this corner case limit of h going towards zero of h by a finite value ts which is often called the sampling period so finite means we just take a small but defined value and not this limit thingy and then with ts we're actually only going to calculate the points in time at integer multiples of ts so we call k the index and then we calculate the points in time at t equals k times ts so this is discretization and yeah so we transform this equation into this one we do invite by ts and we obtain this nice equation which is x of k plus 1 equals x of k plus v of k times ts so this is something that we can easily implement in software we just have a for loop and every time we do that so I'm going to show you a simple example in spreadsheet just to show that well it's pretty independent of what software you use so let's define some constants here on the left side we take a sampling period of 1 millisecond let's put the unit here so we have it in seconds then we have the index here we call it k, we have the time here we call it t and we're interested in the position we call it x and in the velocity we call it v we call it one point in time so we begin at time zero and everything is zero like this let's get a bit more space here so now we have this formula so we need a k plus 1 that's not a problem, we just say ok the next k is equal to previous k plus 1 the time we say is equal to this index k times the sampling period so we use these dollar signs so it doesn't change if we can't be pasted and then we have the integration formula so the x is the previous x times the previous v times the previous velocity and then we apply it by the sampling period like this again with the dollar signs and now we would be boring if the velocity is zero all the time so let's put a velocity of 0.5 meters per second for example and now we want the next moment in time so we just do the same operation over again so I just copy paste the lines and this is now the time evolution for example I can now plot position over time I just add this plot I tell it to take the time on the x axis and take the position on the y axis ok, insert it here alright so yeah it's not very exciting but it's exactly what we expect if we integrate a constant over time so we just obtain a straight curve so let's add the acceleration now we call it A ok we start the acceleration from the beginning like this and now what we do is we actually use the same formula that we used for the position so this one we also use it for the velocity we copy paste it and then we can copy this line and now if we want we can repeat this like a bit longer ok so you see now we have a constant acceleration we integrate two times so we get the position as a parabola shape like this actually I could have added a negative acceleration just to have the drop from before so this is already quite useful if we now say that the acceleration input is not a constant but for example we have the acceleration input from an acceleration sensor then just by using this method which doesn't rely on any knowledge of the shape of the acceleration right so we can just give it random input and it will always integrate it so if we have the sensor input from the accelerometer and we integrate it twice then we can keep track of our position ok but now we want to model something so we don't have an accelerometer yet so where do we get the acceleration from ok one warning always keep the sampling period small we're going to discuss later on what small means ok so some building blocks I can obviously not summarize all physical equations that exist but in mechanics I will show some of electromechanics that are useful like for mobile robots if you have a motor turning something with gears maybe and then a sort of lever mechanism for example so we start from the simplest version so a moving mass just imagine a billiard ball for example which has a mass m and a velocity v and now if we want to change this movement we need a force F again this is our friend Newton telling us that and this is the second law of motion so the force that we need is proportional to the mass times the acceleration ok I need to accelerate a little bit because time is running out so the same equation that you have for linear motions you also have for rotational motions which is the right side and then you can also have the weight so this is in the apple example so the force from the weight is just a mass times the gravitational constant and other effects that you can have are for example spring force and viscous damping these two are actually not only useful if you truly have spring damper mechanism like you would have in a suspension for example but it is also useful if you for example want to model a hard contact like I had in the beginning in the simulation example the robot tipping over and falling on the ground so you just put the stiffness quite high and the damping as well and you will obtain this effect of an input of a contact so let's see if we now combine this for example we want to have just for the sake of the example a small mass spring damper so we put the gravitation here we put the spring here and the damper here damper so what I didn't say here is that this F is not necessarily one force but it is the resultant force so this is actually a vector addition so you take all forces together and then this is the F which drives your acceleration so this is what I am going to do here some from here to there I need some constant so for the gravitation I need the mass so let's say 2 kilo it's a bit heavy but ok let's make it half a kilo then let's say the spring stiffness we just put 100 Newton per meter and the damper let's put 10 so of course this setting of the parameters now I do a bit random but ideally you would look at your system when you design it and then calculate these values so that they correspond to what you are actually building now I just make a simple example so I pick them at random ok so then we have the gravitation if we look here m times g ok so I also need the g so that's the well known 9.81 meter per second square so that's the mass the mass times this we again add the dollar signs and then we have the spring so the spring I set the x0 at zero so the x0 is the equilibrium position and so we had minus ok times position and now what you see here what I didn't say but if you know a little bit about physics and math you see that this is actually a differential equation that we are going to solve here but yeah you don't need to worry too much right now because the Euler method is taking care of that so we also need the damping here times velocity ok and now I'm just going to copy it all the way down so that I have like 60 seconds it takes a bit longer because yeah it's a lot ok and actually what you see here is that what I said before about the sampling period was a problem right now because we're actually doing an error in the Euler method in the integration and if the normally because we have this feedback loop in the differential equation it's not a problem because if there is an error then it will be compensated in the feedback loop but if the sampling period is too big then this error is actually multiplied and then you get this instability behavior so I can also show it with a bit bigger effect like if I take only 100th of a second then you see here if you look at the values it's actually going to infinity so my simulation is not robust and this means that the sampling period is just too big and the simulation is becoming unstable so now I picked a smaller value which of course is a problem because now I'm only simulating 6 seconds instead of 60 but you see that here is a trade-off to make between model complexity and precision which means more computation resources are needed and the robustness of the simulation sorry? so the question was whether this was a resonation yeah right but in theory this maybe I made an error in my calculations let me show you the one that I prepared okay I'm just going to take the one where I prepared it properly because obviously I made an error in the calculations so this is the effect that you are expecting right? so you start at a certain position then you have an oscillation through the spring which is bringing back the mass to the center and then you have a damping so it slowly fades out okay actually I'm also running out of time I wanted to also treat some electric principles and electromechanics to show an electric motor I can quickly show it to you in the slides so the nice thing of the electric motor is that it shows how to solve problems where you actually have two equations which are entangled with each other and you can also see in this example the effect of taking into account one physical principle or ignoring it in this case the effect of the coil so as I said you can download it from the website and then have a look at it if you're interested right so this brings me already to the end of the talk just some tips and tricks to summarize so as I said the sampling period needs to be small as a rough estimate I usually take around 100 times faster than the system time constant so for a mechanical system usually 1 millisecond is okay I don't know why I had an error before but 1 millisecond usually works fine if you have the electric motor with the coil then you need to reuse the sampling period maybe to 10 microseconds that depends on the system time constant as I said then as a second point use block diagrams so this is what I mean by block diagrams it helps you to keep track of which physical measures influence which other physical measures well it's a bit difficult to read maybe in the beginning but you get used to it and then it's really useful and then if you want to be sure about what you're doing or you're having more complex models maybe use some specialized tools for example SciPy which has a good ordinary differential equation solver or open modellica or an edit where you can basically just compose a system as you find it you just add a block for the spring you add another block for the damper and then it takes care of that or a SciLab with XCOS where basically you use this block diagram method to define your equations instead of writing them down and so as I said they have better differential equation solving they're also more efficient because often they use variable time steps so they see when it's critical to reduce the sampling period and when it's less critical and also very helpful other data logging and visualization tools that come with the software otherwise it's sometimes really cumbersome to keep track of all your variables and then plot them nicely okay so actually I think I accelerated that much that I still have some time so I can maybe show you how this works in OMEdit which is also free software and it's basically a graphical tool to use open modellica and open modellica itself as a modelling, programming well let's say definition language okay something is not working because it's not starting as it should okay then I can show you the python code of the simulator that I showed in the beginning so this one so what I did here was to define two classes just to have them run in different threads one thread for the simulation and another one for the visualization and actually the simulation part is pretty simple and then the visualization part takes more lines of code so you see here that there's a reset function where you just define the initial state and then if you look at the simulation part itself you find again what we had before so there's a simulation loop here I do a busy idle well here's the busy idle just to have a sort of fake realtime and then here you see the Euler method integration so in this case it's just a bit contracted with the plus equal notation but basically it means theta equals theta plus omega times the sampling period and then once more for the yeah omega the rotational speed then the different torque effects so in this case that's the rotational version and not the linear version and then at some point the controller I didn't separate it nicely in this example but usually you should and the same again for the linear motions so I got the sign that time is up and yeah I would be happy to take some questions yeah time is up but we're not letting you go yet because we do have time for questions out in the room there are microphones please if you have any questions stand behind one of the microphones and I will call out the microphone number and you can ask a question please don't be shy seems like there are a lot of you that are quite knowledgeable about the subject stand up behind the microphones we have a question from microphone number four hello and thanks for the talk I would like to ask if you have the code somewhere available all the examples that I showed are on the side of the talk itself so if you go in the far plan then there are links to where you can download these files are there examples online somewhere not from me probably millions of examples but for this talk I just created these ones I just have one more for the drag with the drop of drag which you also find but yeah so this is drop after so this is your chance if you want to ask any questions and share the answer to your question with the rest of us please get up to one of the microphones and are there any questions from the internet there is no signal from the signal so is there a question from the internet no let's give Manuel a big round of applause thank you for his talk