Monday, December 31, 2012

Pi for Christmas

I got a new toy for Christmas: A Raspberry Pi.  My plans are to get back into my old car audio hobby, by replacing my factory head unit with an Rπ with a Wireless Nano USB Dongle, a Lilliput 669GL HiBrightness Touch Screen Monitor mounted in the dash, and replacing the little back right jump seat in my tundra with a 10" or 12" subwoofer + amp, and new 6.5"s in the doors and tweeters in the stock tweeter locations.

BUMP BUMP BUMP

Saturday, November 24, 2012

Process Control for dummies like me

I'm not a controls engineer, or even an engineer. I'm just a guy.  Anyway, process controls are like Tonka Trucks to me.  I like to build them to do useless stuff that could probably be done with $40 worth of hardware, but what is fun about that?  Here's a primer on the workhorse of the control world, the PID control algorithm.  You can learn all you ever want to know about PIDs from this now famous article, but even it assumes a LOT of engineer-y smarts, which I don't have.  So, here's an even more boiled down version of even that seminal article for the rest of us.

PID Basics

PID controls are used to control something.  Vague, right?  Let's define some things first, by using a super simple example (that actually has some very complex boundary issues, but for 90% of our scenarios is pretty straight forward).

The problem? We want to heat up something.  Maybe a BBQ pit, or a kettle of water, or maybe a coop full of baby chickens or rabbits.  Normal "bang-bang" thermostats are actually horrible at temperature control. They slam the temperature all over the place, like this:
Set an oven temp to 350F. The thermostat turns on the heater element until temp reaches 350, and turns off. This actually means the temp will shoot up to around 360, since the element is very hot, and the process of cooling down means the oven gets hotter. When the temperature hits 349, turn on, which takes a few minutes, so the oven temp hits 340 quickly, until the element gets hot again. That's a 20 degree swing in temp!

Now, lets see how a pretty simple "process control" PID does this.  Lets define a few terms and look at a real world example first:

Definitions:

  • SP: Set Point - What we define as homeostasis (temp = 350F, speed = 20 RPM, etc)
  • PV: Process Value - What the actual value of what we are measuring is (330F, 18 RPM, etc)
  • PE: Process Error - How far away from SP is PV? So PE=SP-PV. Easy Peasy.
  • Pt, It, Dt - the Term, or each part of the PID controls contribution to the output.
  • ADC: Analog to Digital Converter - Converts analog measurements like temp and rpm to a digital number. A 10 bit ADC has 10 bits of resolution, or 1024 discrete "steps". For an ADC that reads 0-5 volts, 0v = 0, and 5v = 1023, and 2.5v = 512.

OK, lets look at the PID algorithm now:

  • P: Proportional - In many control systems, the process control isn't on or off, it has a range of states, like a valve that can be on a tiny bit, on half way, or on full blast.  That's proportional control. In the PID world you need the machine to determine how much to open a valve (or for on/off things like heaters, how long to turn it on for).  Boiled down even further, the farther away from SP that PV is, the higher P needs to be to get PV=SP. The calculation is very simple: 
    • Pt = PE * pGain. pGain is described below. Back to the example of trying to get an oven to 350F:
    • SP = 350F
    • PV =  100F
    • PE = SP - PV : 350 - 100 = 250, so PE = 250
  • pGain, what? pGain is a constant that is chosen so as to scale the control output to a range usable by the ADCs and output controls by multiplying it with the PE.  Two examples, both using a pGain of 2: 
    • SP = 350
    • PV = 100
    • PE = 250
    • Pt = (350 - 100) * 2 = 500
    • Pt = 500 so we need a Proportional term of 500 of some type of control output to get our oven up to temp. Compare that to:
    • SP =350F
    • PV=340F
    • PE = 10, 
    • Pt = (350 - 340) * 2 = 20, so we need a lot less output from our oven element to get PV=SP. The bigger the PE, the higher Pt needs to be to get SP = PV. The secret sauce is the pGain: too high, you overshoot the temp, too low, your heater takes too long and cakes don't rise, and chickens freeze to death, and beer tastes skunky. pGain is adjusted by you to get the right amount of Pt for your application. Start with pGain of 1.

Another real world example is cruise control on your car - the cruise makes very slight adjustments when you are near SP, say 65 MPH, but when you are traveling at 35 MPH, the cruise control 'floors' the gas to get to SP as quickly as possible. Pt is the amount of artificial "foot" the controller adds to the gas pedal to get to speed.

Proportional control (PE * pGain) isn't enough to maintain stasis on systems like temperature control. Since Pt decreases the closer we get to SP, It will never actually get there on its own. To solve that, we need to add It, Integral, (and later maybe D, Derivative to stabilize).

  • I: Integral - It's a fancy term for cumulative error.  It lets the PID loop "remember" previous errors, by summing the errors as the algorithm runs. That means the I will change less and less as PE approaches SP, which means the integrator's value increase will slow over time, meaning the control loop is getting stable.  In the previous example of the oven, I is 250 on the first pass of the loop, then as the temp rises, the PE is added to I, so the next pass I = I + PE (10), or 260, and as the PV approaches SP, I stops growing as fast, so less and less It is added to the system, causing the system to "settle".  
  • iGain:  is another constant that is multiplied with I to get a control output (It), which is then added to P and sent to the control.
Now, just as quickly as Integral can stabilize a system, it can also cause Windup, which is when your error is very high at the beginning of the process loop, like when the kettle is cold, or the motors are off, etc.  You can saturate your Integral such that it's outside the bounds of what you can push out as control (heat, speed, etc), so you have to have a safe upper and lower limit for I. The problem with I is it usually decreases no faster than it rose, because the growth and reduction of I is fixed at the sample rate of your PID loop. If it took 500 loops to get over saturated and overshoot SP by a wide margin, it's gonna take just as long to get it back down.  I lowers because when you overshoot, your PE goes negative, so when added to each loop, I starts to decrease. i.e. if PE looks like: 10,9,8,7,6,5,2,-1,-2,-3, etc, you see how I grows and shrinks based on accumulated PE).  The good part about controlling windup in your control algorithm is to just limit the output to the maximum output of the process that is driving SP.  If it's a motor, it may be speed or current, just limit it to the max speed and current the motor is capable of. It's as easy as:

If It > cMAX, then It = cMAX
If It < cMIN, then It = cMIN.
If your system under control is inherently stable, you can raise/reduce cMin and cMax even inside the  lower and upper control limits. iGain is usually a very small number, like .002
  • D: Derivative - if P deals with the current state of the system under control, and I deals with the past performance of the system, guess what D does? Yep, it tries to predict the future state of the system to prevent overshoot. Again, Derivative  is a fancy term for a pretty simple calculation:
Dt=(Current  PV - Previous PV) * dGain.
NOTE: There are two versions of D in controls.  The one I'm using is better for systems like heaters, in that it subtracts control away from the system as the rate of PV change slows via Pt and It. The other method of deriving Dt involves the rate of change of PE, which can cause "derivative kick" since the PE is very high at the beginning of the control loop (when the PID loop is first started). Error is high, but PV change is relatively stable and slow. Subtle yet significant in the beginning of the process.
The amount of change of PV each loop gives you a bit of insight on what the next value of PV is going to be, or its slope/rate of change. For example, if our oven PV was 340 F last loop, and 341 F this loop, what do you think PV will be next loop? Again, easy peasy. The steeper the slope, the higher the delta, the higher probability of an overshoot, which Dt helps correct.
  • dGain: this is the value multiplied with D to do predictive control. 
  • NOTE: I, P are summed, and then D is subtracted, which eliminates overshooting when PE and
    is very high and as  the rate of change slows, Dt subtracted from Pt+It will make the approach to SP even more stable. 
  • And there you have your PID control output! That's it.


  • MSP430

    For $4.30, this MCPU is the bee's knees:

    http://www.ti.com/product/msp430f4794

    4 x 16 Bit Delta Sigma ADCs for < $5? Come on.

    Sunday, October 28, 2012

    Stella Bulbs

    This is not just a blog, I also use it as a brain dump, so in that regard, I always forget the type of light bulbs my 2009 Genuine Stella scooter has. To do a proper LED conversion, you need LEDs that are the color of the lenses you are installing the bulbs in:


    • Tail Lights
      • Running: BA15s Base 5W Clear (OEM:Osram 5007 R5W)
        • 32.5mm H x 18mm W
        • 4 Lumens, .32 CP
        • LED Replacement:
      • Stop: BA15s 10w Clear (OEM: Osram  5008 R10W)
    • Turn Lights: 7507L BAU15s Base, Amber Bulb 21W (OEM: Osram 7507L)
      • Dimensions: 47mm H x 25mm W
      • PY21W-21W-12V-BAU15S ECE
      • 22 Lumens, 1.8 CP
      • LED Replacement:

    Sunday, October 21, 2012

    jlinq for Node.js

    I ported Hugo Bonacci's  jLINQ to work with Node.js: http://npmjs.org/package/jlinq .  Source is on Github

    Thursday, October 04, 2012

    I have a Cloudstack VHD template I want to modify - now what?

    vdfuse -f virtdisk.vhd /somedir
    losetup /dev/loop0 /somedir/Partition2

    file Partition2
    Partition2: LVM2 PV (Linux Logical Volume Manager), UUID: 20dMth-jvua-lFfN-j1a9-4iar-acFF-xoI3Pa, size: 20949499904
    pvscan

    lvm vgchange -ay

           2 logical volume(s) in volume group "VolGroup" now active
    lvs
      LV      VG       Attr   LSize  Origin Snap%  Move Log Copy%  Convert
      lv_root VolGroup -wi-a- 11.80g                                      
      lv_swap VolGroup -wi-a-  7.70g       
    mount /dev/mapper/VolGroup-lv_root /mount/lv_root

    when done:
    vgchange -an
    losetup -d /dev/loop0

    DONE!

    Monday, October 01, 2012

    MySQL Pager Trick

    I got tired of always having to set the pager (\P) in mysql cli, so I set up an alias in my bash profile:


    alias mysql="mysql --pager='less -niS'"

    Now, I get nice tabular formatted output in my queries.

    Centos 6.x VMs on CloudStack

    If you want to create a template for deploying Centos 6 VMs, you need to do a few things to the VM before snapshotting the root volume and converting it into a template:

    • yum -y update (of course)
    • set the root password to a well known one ("password" comes to mind)
    • delete the SSH keys in /etc/ssh/ so on first boot sshd will generate new keys
    • rm /etc/udev/rules.d/70-persistent-net.rules
    • yum -y install vim :)
    • edit /etc/sysconfig/network-scripts/ifcfg-eth0 to look like:
      • DEVICE=eth0
      • BOOTPROTO=dhcp
      • ONBOOT=yes
      • TYPE=Ethernet
    That should get you going.

    Saturday, September 15, 2012

    Dynamic Dns on an Edgewater Edgemarc device

    Edgewater Networks makes some decent VOIP routers.  I've used then for some time now in various situations, and always when configuring a network that uses hosted VOIP. One thing that all router manufacturers seem to do (rather poorly) is supply a Dynamic DNS feature.  Edgewater is no exception, since they hard code a few providers, and almost none of them work or are now paid.  I needed to use Name Cheap's excellent Dynamic DNS service. Well it's no secret that Edgewater's VOS is Linux with Busybox providing all the standard shell apps and utilities.  It also has a crontab and wget, so problem 99.9% solved!

    Sunday, February 05, 2012

    UPDATE 8/01/2012:  Zen Cart is total and utter garbage!  The internationalization is terrible, and the configurability is pitiful.

    I've been playing around with a mail order business idea, and went looking for a free shopping cart site package.  I settled on Zen Cart after reading several reviews.  I'll have to say it's quite impressive once you grok the template override system. I started working on it this morning, and by 3pm had a working site.  I'll post a link to the store if I decide to actually launch it. Also props to Nearly Free Speech for building such a simple and functional hosting business.  It's really nice to be able to actually build a website at a web host that cares about web hosting.

    Sunday, January 15, 2012

    MySQL Backup+email bash script

    EDIT: January 11 2013 - I tried this on a newer Centos server and it appears the syntax to mutt has changed - you need to add "--" between the attachment and the destination email to have the command processed properly.

    There are TONS of MySQL backup scripts around, but none on the first two pages of a Google search that did just what I wanted: backup each MySQL database on a server separately, then email them to me, one at a time. I found a few that were close, and modified them to do what I needed.  Credit is given to the original author when known.

    Credit to jamesmcdonald.id.au for the bulk of the main script


    Saturday, January 07, 2012

    Pizza

    Made a few pizzas last weekend.  Worked with a slightly drier dough, and a very simple sauce of San Marzano tomatoes and 2 tbsp butter.  Also tried a new cheese from Sam's Club that is claimed to be nearly as good as Grande, which sets the standard for block pizza mozzarella.  Fresh mozzarella is always welcomed, but not always available or affordable.