LearningForward

Kent Chesnut's technology in education blog.

November 13, 2009

Scratch as a Simulation Engine - Part 2

Filed under: Simulation, Scratch — kchesnut @ 10:32 pm

In this post I’ll be considering a fairly complex simulation with Scratch.

Control systems commonly use a control algorithm called the PID Control loop.  The PID stands for Proportional - Integral - Differential.  You can find more information about the PID algorithm here.

A few months ago at work we were putting together a control system that would control the level of fluid in a tank.  Fluid was coming into the tank from various systems, with rates that could change over time.  Fluid was to be pumped from the tank using a pump driven by a Variable Speed Drive (see here if you’re really interested).  As we did not have access to the actual system, but wanted to try to test the control, a colleague had built a simulation of the tank level into our device.  We weren’t really understanding what we were seeing (turned out there was an undesirable feature in the PID algorithm in the software - what most folks refer to as a bug).  In an effort to understand what was going on, I decided to model the overall system.  And I decided to see if Scratch could get the job done.pid.jpg

A screenshot of the project stage is shown on the right.  The blue in the upper left illustrates the fluid level in the tank and is reflected by the TankLevel variable.  The IntakeRate is the rate at which fluid is entering the tank.  The PID MaxOutputRate is the rate fluid is being pumped from the tank when the VSD is running at full speed (100%).

Run the project here.   Click on the green flag and then on the big green dot to start the simulatin.  Note that the fish labeled Ft graphs the tank level and the fish labeled Hz graphs the VSD speed.  Download the project here.

It’s beyond the scope of this post to explain the innards of the simulation.  Suffice it to say that you adjust various start values in the PID sprite to try to match the system you wish to simulate and then run the simulation.  Repeat as necessary to optimize the PID response.  The purpose of the post is to describe what I learned in using Scratch to create a fairly complex simulation.runpid.jpg

  • As you can see from the image at right, the actual PI algorithm is not too complicated.  (I didn’t simulate the D term as we seldom use it and the anomaly we were seeing was evident even when we only had a P term.)
  • The stage size was somewhat limiting.  In a simulation of this nature, you’d typically display a bunch of data.  All the stuff I really needed fit, but didn’t leave much room for any graphics or animations (often referred to as “eye candy” at work).
  • Often one wants to save the simulated data to a file as the simulation progresses.  Scratch doesn’t have this capability.  However, one of the things you typically do with the saved data is to graph it to observe the control response (in this case, Drive Speed) with respect to the feedback variable (in this case, tank level).  Scratch can do the graph.
  • I really liked the little fish building the graph.
  • My coworkers and boss seemed either impressed or somewhat amused that I was using a kids’ program to do a “work” simulation.
  • The speed of the simulation was fine on a 3 year old laptop (1.6GHz Centrino Duo processor).

As always, I appreciate any feedback.  In the next post, we’ll look at another example of a simulation - but this time back to a school problem.

No Comments »

No comments yet.

RSS feed for comments on this post. TrackBack URL

Leave a comment

Powered by WordPress