Sunday, December 23, 2012

System Design and Parts Selection

A while back I said I was going to create a series of posts detailing how I go about building an electronics project from start to finish.  That hasn't happened - until now!

You see I'm usually pretty lazy on my off hours.  And as lazy as I am about actually working on an electronics project in my spare time I'm lazier about writing about it.  It turns out that I actually started working on a project months ago and haven't written anything about it despite having designed the electronics, fabricated the board and started developing the firmware.  But since I have a good head start on actually making this project I figured I could do some backfill on the writing and show off some of my process.

Here's the idea (on paper):


So I have a shirt with a traffic light on it.  The lights are several sets of red, yellow and green LEDs soldered onto a PCB and attached to the inside of the shirt with velcro or sewn on.  The traffic light on the front is probably an iron-on applique or a custom shirt if I decide to get expensive.  The lights are controlled by an accelerometer: if I sit still the light is red, when I move forward the light turns green and when I stop it turns yellow for three seconds then red again until the process starts over.  It's a simple clear idea that makes for an interesting electronics project.

The first steps in a project like this are to generate a block diagram and general system description so you can start finding suitable parts.  That's what I'll discuss today.  Here's the system block diagram I came up with:


Let's go over this briefly block by block.  The most obvious are the LEDs - I need one set of LEDs in each color of the traffic light.  There aren't many requirements for the LEDs.  I don't think they'll need to be super bright or require heat sinking.  I'm not dealing with any esoteric colors or optical properties - I just need colored light.  The only requirement I can think of is that since these LEDs are going to be velcro'd to the inside of a shirt that I will be wearing I'd prefer that there not be metal pins sticking into me.  In that spirit they should probably be surface mount LEDs.  Since I can't keep my hand steady due to excessive coffee intake I'd like them to be the largest surface mount package I can find.  Also I imagine I'll need a fair number of them - 30 or so.  Thus, they should be cheap as well.  This isn't a tall order for LEDs.

I usually order my electronics on Digi-Key. Digi-Key is rather... old school.  For example, they love their paper catalog.  They love it so much that they created an app for their website that actually shows you the pages from their catalog.  If you love the paradigm of paper please use this.  If you love the Google paradigm then read on for a less-confusing way to use their website.

When finding anything on Digi-Key you have to know what you're looking for and you have to be very patient.  This is how I found the LEDs I would use for this project:
  1. Go to Digi-Key.com, type 'LED' in search in upper right corner
  2. Browse through the categories until I find one that looks right - Opto-Electronics/Discrete LEDs.  If you're looking for something common it's best to select the category that has the most items in it.  In this case these should be common LEDs and the Discrete LED category has about 15K items in it.  It's a good bet.
  3. Now you have several columns you can use to filter all of the items in this category.  Always always filter on 'In-Stock' before you do anything else.  95% of the time if something is not in stock you don't want it anyhow.  
  4. Now start filtering on what you know you need.  Most of the columns you'll never touch.  Dominant wavelength?  Lens style?  Size?  No idea.  Surface mount? Yes!  Color? Green, yellow or red! Pick the things you know for sure first but select filters one at a time.  If you don't you might end up with 0 results and have to work backwards to figure out why. 
  5. Guess with the rest.  Look at the results - specifically the pictures - and determine roughly what you want, then try to apply filters to remove the other stuff.  
  6. Finally, look at price with an eye towards minimum quantities.  Typically you won't be buying in such bulk that you get price breaks so look for minimum quantities of 1 and (generally) select the lowest price.
The filters I applied to find my LEDs were (in this order):
  • Mounting Type: Surface Mount - For the reasons I discussed earlier
  • Colors: Red, Green and Yellow - This is obvious
  • Package/Case: 1206 - From experience I know this is the largest and most readily-available surface mount LED package.
  • Lens Type: Clear - I definitely don't want tinted and I've never liked the look of diffused (think frosted) lenses.  Personal choice.
  • Test Current: 20mA - More current means more brightness.  I'd like as much brightness as I can manage without too much work.
  • Lens Style: Rectangular - Honestly I have little or no idea about this one but the domed top lenses didn't sound like what I want and I didn't like the look of them from the picture, so I stuck with rectangular and flat top. It's worth noting that at this point I have 118 options remaining.
  • Price: Sort by lowest price with a minimum of 1 piece - If you click the up arrow in the Unit Price column it offers to sort by price with a specified minimum.  Always use a minimum of 1 piece.
Now at this point everything's pretty much the same except for color.  As long as there's nothing suspicious happening with price (ie, lowest price is $.01/unit but every other manufacturer is at least $.25/unit) you might as well select the lowest priced option.  In the end I selected LEDs with a bit more brightness than the cheapest ones.  Here are my selections for LEDs:
  • Green LED: 754-1440-1-ND
  • Yellow LED: 754-1144-1-ND
  • Red LED: 754-1143-1-ND
The LEDs need something to drive them and there are two main options for driving LEDs: voltage or current. Ultimately LEDs are current-mode devices: the more milliamps you push through them the brighter they get.  There are all sorts of methods to get this current through the LEDs in a controlled fashion - here are a few general ideas:
  • Use an LM317 as a constant current source
  • Use a dedicated current-mode LED driver IC
  • Build a switch-mode current driver from discrete components
  • Bias the LEDs with a resistor and turn them on and off with a transistor
In the end biasing the LEDs with a resistor and controlling them with a transistor is the best choice for this application because it's the simplest.  While it won't give me the most bang for the buck in terms of power expended vs. light produced it has the benefit of being simple and easily controllable compared to the others.  I'm not out to prove how clever I am with this project and even if I were I'm not that clever when it comes to analog circuits.  You can be as complicated as you want when it comes to controlling LEDs and have control over ever aspect of the system but realistically all this application calls for is to produce some light and wow a few people at Halloween.  In that spirit I'm going with the simplest driver I can - a transistor switch.  However, this requires a sanity check.

Usually the transistor I would select would be an old standby: the 2N2222.  I know as a rule of thumb that you get a current gain of 10 with the 2N2222: 10mA base current gives you 100mA collector current when the transistor is saturated.  However, if I have about 30 LEDs all drawing 20mA I need to deliver 600mA.  That's getting close to the current limit of the 2N2222 and it also means that I need to deliver 60mA base current to switch it.  That will be more than a typical microcontroller can source from its pins.  For this application I need something with more current gain - in this case a Darlington Array is probably best.  A Darlington Array is basically two transistors connected together such that their current gain is multiplied - so instead of a current gain of 10 you have a current gain of 100.  Also Darlington Arrays can usually handle more current than a single transistor.  

And in fact there's a part that was made for this application: the ULN2803.  It's eight separate Darlington transistors in one chip.  The only rub is that each Darlington channel is rated for a maximum of 500mA.  However, you can double up the channels and get a maximum of 1A.  So if each LED color gets two channels that still only uses six of the eight channels.  Also, the inputs are biased to allow TTL signals (such as those generated by microcontroller output pins) to be used so I don't have to worry about how much current they're drawing.  The ULN2803 can switch the LEDs and I can bias them with resistors to control the brightness. In the end I selected a through-hole DIP version of the ULN2803: Part #497-2356-5-ND

As far as the microcontroller is concerned there are many options.  The computation and timing requirements for this application aren't excessive so I won't need anything too esoteric.  That still doesn't really narrow it down - there are plenty of good microcontroller options in the hobbyist market.  To name a few that I've used: AVR, PSoC, MSP430, PIC and the ARM Cortex-M3.  Of course, there's many potential chips in each of those families as well.  

Since I don't intend to do anything fancy with the microcontroller so I'm going to use an ATMega328 - the microcontroller that powers the Arduino.  The microcontroller is part of Atmel's AVR line of microcontrollers which are a good all-around choice for hobbyists for several reasons:
  1. The development tools are free - AVR Studio can be downloaded from Atmel and has everything you need to write C firmware for both Atmel's 8-bit microcontroller families and the AVR32 family as well.  
  2. The programmer is cheap - the Dragon is a full-featured In-System Programmer (and a less-capable debugger) that will program nearly all of Atmel's 8-bit  for a cost of $50 (and I already own one)
  3. The chips themselves are cheap and I already have several on hand.
  4. Arduino-compatible so that others can easily reproduce the project if they want.
  5. It has a wide variety of peripherals ready to use.
You can either find them on Digi-Key with this part #: ATMEGA328P-PU-ND or you can get it from Sparkfun with or without the Arduino bootloader pre-programmed.  The prices are comparable at either place.

The accelerometer is a trickier question.  I know I'm going to buy it from Sparkfun because they have a good selection of accelerometers with breakout boards.  You can't really buy accelerometers in hobbyist-friendly packages like DIP so the breakout board is a necessity.  I only really need one axis but I might as well get three so I can use this as a platform for other projects.  I could use a serial interface but the ATMega328 has only one serial peripheral, so if I use it for the accelerometer I can't have a serial port for debugging.  Given that, I'm going to try to find a three-axis accelerometer with voltage outputs.  If you go to their site on the left they have their categories: choose Sensors/Accelerometers/3-Axis.  There are about a dozen options.  We can immediately ignore the ones that aren't breakout boards.  Next, we can ignore the ones that have serial interfaces - that is, either I2C or SPI.  That pretty much leaves one option: the MMA7361.

Power is the last issue, but there's a problem.  The accelerometer is 3.3V.  The ULN2803 prefers 5V.  The green LED has a forwad drop of about 3V.  The microcontroller could be either 5V or 3.3V.  From this, it looks like the LEDs will have to be driven with 5V and the microcontroller will also have to be 5V so it can control the ULN2803.  But we need 3.3V for the accelerometer as well.  There isn't an easy solution to this: we need both a 5V and a 3.3V source.  The 5V source has to be pretty hefty as well - each set of LEDs will draw about 600mA.  Only one set should be on at a time - by design and because otherwise it will draw so much power that the battery will die quickly.  For that sort of current draw I need another old standby - the LM7805.  It can supply up to 1A at 5V and is more than enough to power the LEDs and all of the 5V components. If you search for LM7805 on Digi-Key you get the option of either a Texas Instruments version at $1.66/unit or a Fairchild version at $.66/unit.  The difference seems to be that the TI part has no lead in it.  I'm going to choose the cheaper version: LM7805CT-ND.

For the 3.3V regulator I just typed '3.3V regulator' into the Digi-Key search and selected the 'Linear-LDO' category. I know this from experience - the 7805, LM317 and other similar voltage regulators I've used are all linear so I stuck with it.  After selecting 'In-Stock' I drilled down into the columns this way:
  • Regulator Topology: Positive Fixed - I don't need adjustable or negative voltages
  • Voltage Output: 3.3V - That's all I need
  • Mounting Type: Through-hole - I tend to prefer through-hole
  • Output Current: 250mA - I need so little current, but in case I want to use this part again in the future I prefer to select a part that can accommodate more applications
That leaves part #497-7246-1-ND as the 3.3V regulator. 

The last major thing I need is some sort of battery.  I'd like to not screw around with some combination of AA batteries - they only go up to 1.5V (1.2V for rechargeables) so If I have to generate 5V that means at least 4, possibly more batteries.  A 9V battery seems ideal - plenty of voltage overhead.  Keep in mind though, 9V batteries tend to have about 500mAH of capacity.  That's not a lot of time to run this costume, but it's the tradeoff I have to make to accommodate everything else.  It's mostly for novelty anyhow.  So I decided I wanted a nice 9V battery holder and not just a couple of wires and a connector.  If you search Digi-Key for '9V battery holder' you're presented with the 'Battery holders, clips and contacts' category in which you can drill down the columns thusly:
  • Battery Cell Size: 9V - Obviously
  • Number of Cells: 1 - Also obvious
At this point there weren't very many results, so I looked through all of the pictures to find this guy: BH9V-PC-ND.  It's a through-hole full 9V cell holder.  The other options were pretty much just the contacts.  This will securely hold the battery and allow me to mount it on the PCB.

All of the other parts are just miscellaneous: tactile switches, rectangular headers, resistors, capacitors, LEDs, jumpers, etc.  All of that I have on-hand and don't have to order.  With any luck I don't have to make any other supplemental orders to get more of anything or replace something I broke.  When ordering parts make sure to note whether you're close to a price break.  I needed about 30-35 LEDs (the number I thought I needed plus extras) and was able to get a slight price break by ordering a few more than I thought I needed.  And of course, always always order extras - you WILL break something.  I broke plenty of LEDs and perhaps a few microcontrollers during this process.

Next time I'll talk about capturing/generating the schematics.  For now I'll leave you with a bit of a teaser: pictures of the board after assembly.





Saturday, September 15, 2012

Choosing data structures

I try to write tight, efficient code.  This is very important when you're working on a small microcontroller with limitations on speed and memory.  However, I also try to write code that is well-organized and safe (ie, preventing buffer overflows, recognizing and handling error conditions, etc.).  Let me tell you that these two goals are often at odds:  code suddenly becomes much less efficient when you have to constantly check for errors ("Is this passed pointer NULL?  Am I outside the bounds of my array?  Did I leave the oven on?").  Trying to write well-organized code just makes it worse - to achieve this I usually separate all functionality out into independent functions.  Pretty soon you have a class with 18 functions and the call stack for any given publicly-visible function might be five or six calls deep.  Say hello to stack overflows!

It's an eternal struggle with no good solution.  And since I don't have a solution, I'll just shut up and go to bed.  Good night!

Just kidding - you can't get me to be quiet so easily.  While there is no universal answer to the problems posed above there are application-specific solutions that a good engineer can utilize to improve efficiency, readability and robustness.  Choosing the correct data structure for your task gives you improvements on all fronts with no downsides.  Let's discuss choosing between a circular buffer and a linked-list based on the application.

(As always you can look on Wikipedia to find a good basic description of Linked Lists and Circular Buffers to supplement my descriptions).

A circular buffer is a way to turn a linear array into a circular one.  Say you have an array of bytes and it's 32 bytes long.  These arrays are linear in that people think about them having a beginning and an end: the beginning is always 0 and the end is always size-1.  Typically programmers will use a for loop to iterate over the whole range of the array from the first element to the last or perhaps use an enumeration to access specific elements in the array.  Circular buffers are conceptually different in that they have no 'beginning' or 'end' but instead a 'head' and 'tail'.  The head and tail can be any position within the array.  When the head or tail reaches the last element they wrap around back to the beginning.  Elements can be added to the bufer and removed during the course of program execution and the head and tail values change to match.  In this way the buffer keeps track of which parts of the array are unread and which can be overwritten.  Here's an example:

I instantiate a circular buffer with room for 32 elements.  At the beginning the buffer is empty, so the head and tail pointers both set to element zero.  At some point I want to add data to the buffer.  Data is always written to the tail and read from the head, so when I add data to the buffer it's written to the current position of the tail (element zero) and then the tail is incremented to now point to element one.  I can keep adding data to the buffer and the tail will continue to increment until it's equal to 31.  At this point I've added 31 elements to the buffer and read none, so the head pointer is still set to zero but the tail pointer is set to 31.  If I incremented the tail pointer again it would be 32 and my array doesn't have 32 elements and that would be a buffer overflow.  So instead of just incrementing the tail pointer I have to increment it then wrap it back around to point to the beginning of the array.  So (31+1)mod 32 = 0. Doing that would cause the head and the tail to point to the same location.  By convention I've decided that if the head pointer and the tail pointer are equal that means the buffer is empty, not full.  So I've added 31 elements and I can add no more without disturbing my convention.  In order to add more I have to remove some elements.  As I said before elements are always read from the head pointer.  So in order to remove an element I read the element pointed to by the head pointer and then increment the head pointer.  Now the head points to position 1 and I can add another element at which point the tail will be equal to 0.  As long as you maintain the state of the head and tail pointers you can reuse the same memory over and over again and hopefully not get too confused.

Circular buffers are very useful for serial ports.  Imagine for example you have a microcontroller and you need to monitor a serial port.  You have an RX interrupt that tells you when a byte comes in.  You have two options: you can handle the byte in the interrupt (which is not always a good idea depending on the amount of 'handling' you have to do) or you can just add that byte to a circular buffer and return.  Then in your main loop you just check to see if there's any data in the circular buffer and handle it there outside of the interrupt context.

I coded a nice circular buffer library with a focus on robustness and code clarity over efficiency.  This meant that I had a function to add a byte, remove a byte, increment pointers, copy multiple bytes, etc.  It ended up being a lot of functions and a lot of overhead: every time you wanted to add a byte it would check to see if the buffer was empty (one function call) then add the byte and call another function to increment pointers (two function calls, AH AH AH!).  This was probably more function calls than I should have tolerated but the performance was acceptable for its intended application of handling serial traffic.  It was so useful that when I needed a packet-based serial scheme I coded it utilizing my circular buffer as a basis: serial data would be pushed into the buffer and the packet communication functions would read the buffer looking for packets.  Stacking these two libraries on top of each other caused more function calls and more overhead but it still wasn't unbearable because this arrangement was a pretty good fit for this application.

But one fateful day I needed to handle TCP and UDP traffic instead of serial traffic.  TCP and UDP communication doesn't piddle around with single bytes but instead can transfer hundreds of bytes at a time inside a packet.  At the time I foolishly thought that code reuse was a higher priority than efficiency so I implemented the same scheme for TCP and UDP data as I did for serial.  That meant that for every packet that came in called a function to copy one byte at a time into a circular buffer.  And of course, that function called other functions which called other functions and suddenly my overhead just for copying data to the buffer was rather large.  But wait, it gets worse.  Even more foolish than copying bytes singly is copying them at all.  You see, the TCP/IP stack didn't intend for me to copy that data to another buffer - it passed a pointer to the data and the length of the data and assumed I would work with it in-place without copying.  But my circular buffer didn't work that way so instead I had to instantiate a huge circular buffer capable of holding multiple packets (we're talking a thousand bytes or so) and copy data into it one byte at a time at the cost of multiple function calls per byte when it was entirely unnecessary to do any of that!

As you can see I was a fool.  This system was not efficient in any way.  It doubled the amount of memory I needed and made many useless function calls.  There's no way this system could handle large amounts of data and sure enough my first attempt to handle TCP traffic produced an astounding data rate of 8kbps at the application layer.  This was over Ethernet by the way.

I failed to produce efficient code because the data structure I chose was totally unsuited for the task.  When I used my circular buffer for the serial port I gained the advantages of robust and safe code with a minimum loss in efficiency.  When I tried to apply the same code to a different system I lost a significant amount of efficiency with no increase in robustness or safety.  Different situations call for different solutions.  What should I have implemented to better handle TCP/IP traffic?

In this situation a linked list would have been preferable.  Linked lists can perform many of the same functions as circular buffers but they arrange the data differently.  Imagine a linked list as a bunch of Easter eggs connected by pieces of string.  Each Easter egg has yummy candy inside of it.  There's a string on one end of the egg that's secured so you can't remove it.  On the other end of the egg is a place to tie another piece of string.  In this example you're blind so you're doing everything by touch.  You need to keep track of all of your eggs and not lose any.  The first thing you do is secure one egg to the table and call it your 'head' egg.  Unless you misplace the table it's unlikely you'll misplace the head egg.  So someone (we'll call him Mr. TCP/IP Stack) hands you another egg and in order not to lose it you tie it to the ball that's secured to the table. When he hands you more, you just follow the string of eggs until you find the last one and tie the new egg there.  You can keep adding to the string of eggs this way and be sure you'll never lose any because they're all secured to a single immovable point.  At some point another person (Mr. Packet Parser) will ask for the first egg (called egg #1 hereafter) Mr. Stack gave you.  Here's how you do it without losing all of your egg:


  1. Find the head egg 
  2. Move down the string of eggs until you find the egg after egg #1(call this egg #2)
  3. Untie egg #2's string from egg #1 and keep ahold of it
  4. Untie egg #1's string  from the head egg 
  5. Tie egg #2's string to the head egg
  6. Hand off egg #1 to Mr. Parser

As you can see this approach allows you to keep an unlimited number of eggs and give any particular egg away without being worried that you'll lose the others.  This is the idea behind a linked list except that the strings holding the eggs together are pointers and the goodies inside the eggs are the payload.  The TCP/IP stack would hand me the payload it received, but instead of putting the goodies into an egg I picked them apart one by one and put them in one of those pill boxes where there's a compartment for every day of the month.  Thus, Mr. Parser had to open each compartment individually one eat one piece of candy at a time rather than opening the egg and dumping it all into his mouth.

The difference between the two approaches is striking.  With the linked list approach all I would have had to do when I received new data from the TCP/IP stack is to add the payload to the linked list.  The performance of the circular buffer approach was dependent on the amount of data received where a linked list doesn't have that restriction.  The linked list approach also reduces memory usage: there's no reason to copy all of the data into one large buffer to search through it.  With TCP and UDP unless something goes horribly horribly wrong you won't receive half of your data in one packet and half in another - it's all or nothing.  This means that you don't have to maintain two large buffers - just one.  In addition with the linked list there's no overhead in maintaining the state of the circular buffer.  This means fewer function calls compared to the circular buffer.  For this application, using a linked list is superior to the circular buffer because it reduces memory usage, scales better and has lower overhead.

But wait! I don't want your takeaway from this post to be that the linked list is superior to the circular buffer in an absolute sense.  That's not true at all - the takeaway from this post should be that you have to choose your data structure to suit your application.  Let's take a backwards look at this and see what would happen if we used the linked list in place of the circular buffer for the application of receiving serial data.

You see, the circular buffer was fine for the serial application because it was designed to handle data that came in singly.  All it had to do to add data was stick the new byte into the array at the location of the tail pointer and then increment that pointer.  Incrementing the pointer is the hardest part and even that was fairly simple.  The linked list is different.  To add something to the end of the linked list first you have to find the end.  This means starting at the head and working your way down until you find an egg that doesn't have another one tied to it.  And you have to do this every time you want to find the end.  In this serial application data comes in one byte at a time.  That means each egg only contains one jelly bean.  The efficiency of the linked list was due to the fact that you could put a lot of jelly beans in each egg.  But if you're artificially limited as to the number of jelly beans you can put in each egg suddenly a linked list isn't very efficient.  As the list gets larger you have to search longer and longer to find the end of the chain. And that list gets larger and larger with every byte that comes in.  Not every ten bytes or every 100 bytes - every single byte.  That gets very inefficient very quick.

You can see that simply by choosing the correct data structure for your application you can improve performance without sacrificing robustness, stability or readability.  By choosing your data structures wisely you can have your cake and eat it too!


Thursday, August 30, 2012

C Gotchas - The Next Generation

I just got burned by this one.  Here's the code:

modulus = a + (TRUE == display_warning_message)?1:0 + num_faults;

Let's say a is 1, display_warning_message is TRUE and num_faults is 16. What is modulus?

If you said 18, you're wrong, it's 2.

Let's try again.  Same code, but a is 2, display_warning_message is TRUE and num_faults is 12.  What's modulus?

If you said 15, you're wrong - it's 3.

But why?

Because parenthesis man, parenthesis.  Here's what C sees:

modulus = a + (TRUE == display_warning_message)?1:(0+num_faults);

Here's what you wanted:

modulus = a + ((TRUE == display_warning_message)?1:0) + num_faults;

You see, the C compiler follows the 'biggest chunk' rule for evaluating expressions: if there's ambiguity about what expressions get put together it grabs as large of a chunk of code as it can.  This is the prototypical example:

c = a+++b;
How does the C compiler see this?  Is it:

c = a + (++b);

Or:

 c = (a++) + b;

The answer is to work left to right and apply the biggest chunk rule.  The 'c=' is not ambiguous, so we don't need to worry about it.  The next ambiguous part is the 'a+++b'.  So, starting at the left, the biggest valid chunk of code we can pick is a++ - post increment on a.  That means the second interpretation of the code is correct.

But you should never have to learn the biggest chunk rule.  Instead, just use parenthesis to de-ambiguate your code. Parenthesis always win in Order of Operations Paper/Rock/Scissors - this makes it a very boring game.  

Sunday, June 10, 2012

A Suckers' Game

Yesterday I ordered five of my hookah widgets - prototype run. I plan to distribute them to my friends and family who smoke hookah and get some feedback.  They may be entirely worthless, but like a overly-optimistic parent I'm going to choose to believe that my baby will somehow change the world for the better. 

Everything else aside - even if my widget absolutely ruins hookah smoking and I get run out of town I'm excited about having made something. Just seeing a physical objects manufactured to my specifications is worth every penny of the cost of the prototype run in my opinion.  I will be more excited if it's useful to me and tickled pink if it's useful to anyone else.  If it goes that well then a Kickstarter effort wouldn't be out of the question in my opinion.  And that gets me thinking - am I going to get rich off of this?

Please?

Pretty please?

We can all dream can't we?  And I'm told that America is the place to make your dreams come true.  With enough skill, tenacity, gumption and luck you too can live the American dream - the dream of living in a giant treehouse with a helicopter pad and an upside-down pool and complaining about people poorer than you.  And I aim big - I want to complain about Mitt Romney.  Come on Mitt!  Your pool is rightside-up and it's lowering my property values because it's not awesome enough! Do I have to sue you?

 So how rich could this make me? Well I know a few things about business and I think that the more units I sell the more money I make.  But I'm only one lazy person - the more widgets, the more I have to do.  I'd rather get it over with quickly and ship my product than to draw it out months because I got greedy and tried to ship 1000 units.  Plus, I've seen Kickstarter projects that were too successful - 10x the profit sounds good, but trying to handle that many orders without the trappings of a real business (returns department, quality control, lawyer, etc) while keeping customers happy turns out badly. With those things in mind, I decided to limit myself to 100 units only for a first run. I am considering a possible stretch goal of 250 though.

Now the second thing I know about business is that I only make money on each unit if I sell it for more than I paid for it.  Thus the next question I have to answer - how much does each unit cost?  To answer this question I created a spreadsheet that has all of the costs I could think of.  Let's go over the per-unit costs before anything.  The per-unit cost is the cost for each unit sold assuming a certain number are sold.  My assumption is going to be that I will sell and ship 100 units as my Kickstarter goal.  Kickstarter won't fund me if I don't make this goal, so I don't have to worry that it's about whether I'll make it.

The first and most obvious cost is the cost of getting a manufactured unit shipped to me. Since I plan to go through eMachineShop.com I don't have to worry about separate costs for material and machining - they handle it all and ship it to me.  That gives me one cost for each manufactured unit.  I got several quotes from them and created a lookup table which you can see at row 12.  This gives the ability to change the size of my production run and still get good numbers without guessing.  The next cost is the cost to hard anodize each unit.  I found a local place that gives me a significantly better deal on hard anodizing than eMachineShop.com did, so I'm going to receive delivery of the manufactured items from eMachineShop.com and then transport them to the local place to get anodized.  That saves money. Next cost is shipping - this is easy if you want to ignore international shipping.  Through USPS.com I just did a postage calculation knowing the weight and size of the item.  It turns out to be fairly inexpensive.  International shipping however is a whole different game that I don't want to get into right now.  The next two costs get more complicated, so they get their own paragraphs.

You might think it's odd that I'm counting labor as a cost.  Do I plan on hiring someone?  No, not in the least - I'll do it all myself.  But since it's my business why pay myself?  It's a good question.  Not paying myself might seem like saving money but practically it's not.  As the owner I can re-capitalize  (read: give more money to) my business any time I want.  So even if I pay that money to myself I can just give it right back if I feel like it (with the only downside being any potential tax costs).  And in any case if this gets successful enough there will be a lot of orders and that means a lot of work.  I may want to hire someone sooner than I thought, so it's good practice to start accounting for labor costs early.  That being said it's not as if my labor is magically free.  It's only free if you ignore my health, time with my family, friends and sanity.  Plus if I don't pay myself now it will make it a greater financial shock later when I do have to account for the cost of labor. I prefer to keep costs out in the open.

Overhead is perhaps the most interesting cost.  The goal is to list every cost that is not directly associated with each unit sold and the amortize that total cost over every unit to recoup it.  There are several costs associated with overhead:
  • Business costs - licensing, insurance, web page, etc.  Things that you have to pay if you want to stay in business even if you don't sell a single thing.  Those costs are not included because I view this spreadsheet as a worksheet for this production run alone.  However, these sorts of things probably run me around $500/year if my business doesn't do much except exist and have a website.
  • Prototypes - You never want to avoid having a prototype.  There were several warnings given off by the Design Rule Check on eMachineShop's CAD software.  The pieces could come out of the shop looking terrible - holes in the wrong places or with scratches, abrasions, etc. You need to know what you're getting before you order 100 of them and you have to know if your design even works.  Perhaps not intelligently I ate this cost for now - it won't be recouped by the production run.  If things turn out well I might sell any leftover prototypes as special gifts for  generous backers to recoup the cost.
  • Refunds - Some people are going to want their money back and I want to give it to them.  Too many Kickstarter runners seem surprised and terrified of requests for refunds - they prefer to think of their customers' money as a donation rather than a business transaction.  I think that's dishonest.  If I screwed up and I know its my fault I'm not going to try to hang on to a few more dollars.  I do hope that it doesn't happen very often, but I don't have any real numbers.  I included an assumption that 10% of the units sold will result in refunds and I'll have to give a full refund plus pay for return shipping.  This turns out to be a very expensive cost, but I'd rather plan for the worst.
  • Fees - Don't forget about this!  Kickstarter takes 5% of your total and Amazon takes up to 5% for credit card processing fees.  Best to assume the worst and assume that you'll lose 10% of whatever you make to fees.
  • Unsold Units - This is a bit harder to wrap your head around, but bear with me.  If I sell 100 units on Kickstarter, should I buy only 100 units from eMachineShop?  Seems like a dumb question no?  But what if they screw up machining just one of the units?  I'm not going to ship a defective unit if I can help it, butI can't order just one more from eMachineShop.  I should add some additional units on to the initial order to ensure that I receive at least 100 shippable units.  The number of units above 100 that I order is the number of unsold units, and I have to recoup those costs by making everything else more expensive.
If you sum all of those costs up you get the cost to sell and ship one unit.  Now all you have to do is arrange your Kickstarter such that you charge more for each of the units than they cost. The final Kickstarter goal is calculated in column H and is obtained by multiplying the cost I plan to charge for each unit times the number of units that I will sell.  All I have to do is play with the price I charge until I see the level of profit that I would enjoy them most.   Being a greedy capitalist I'm thinking my profit should be $LOTS.

Of course, if I charge $LOTS some people just won't buy it.  In that case I get nothing.  Now, typical profits per unit for manufactured goods like this are pretty low - much less than 10%.  And for this Kickstarter a profit of 10% still only gives me something absurd like $150 profit when all is said and done.  That's not $LOTS.  However, if I deliver 10,000 units instead of 100 then things get different.  With 10,000 units my manufacturing costs get very low and even if I keep my profits very low (even as low as 1%) I get a lot of profit because 10,000 * $Anything = $Some.  $3,000 in profit in one year is possible if my profit on each unit is $.30 and I charge around $30.  Of course, why make $.50 when surely the market can handle a dollar of profit or even two?  Now we're talking real money - if I can fulfill all of those orders on my own AND keep my actual job.

That's pretty much the problem with manufacturing - it doesn't scale down very well.  As it is I'll have to charge around $60/unit for a run of 100 units and I won't make a whole lot of profit.  It's only when you get into high volume that it really gets to be profitable.  But to handle high volume you need capital investment in automating processes, systems to track purchases, paying workers, etc. If I'm lucky I won't have to worry about this and I can just find an existing online shop to sell the widget - I deliver manufactured units ready to sell and worry about nothing else. 

So as you can see, there's a lot of thought that goes into deciding how much to charge for a product and plenty of pitfalls you have to watch out for.  Once you start adding everything up it becomes clear that manufacturing things is a suckers' game - you either have to be fairly large or you're never going to make real money.  Kickstarters beware....

Saturday, May 19, 2012

Productively Slacking Off

We all slack off.  I'm actually amazing at getting things done - despite the fact that I slack off nearly all the time. Remember how I was supposed to write about developing for microcontrollers?  Yeah I didn't do that (yet). Wanna see what I did instead?

This is an awesome... uh... thing?
You might be wondering what this thing is.  Well, do you know what this thing is?

It's a hookah!
It's a hookah!  It's also called a water pipe.  This is because the bottom is filled with water and a pipe coming from the top extends beneath the water level.  On top is a clay bowl filled with shisha - tobacco mixed with flavored molasses. Popular flavors include rose, mint, plum, apple and my personal favorite: guava.  Lit coals are placed on top of the shisha and drawing air through the hose causes the shisha to be (theoretically) nicely and evenly burned to produce pleasant smoke.  Hookah started in India and moved across Iran and into the Middle-east before it broke out into the world at large.

Hookah used to be one of my favorite vices.  But nowadays not so much.  Tobacco is bad for you no matter how you smoke it - simply filtering the smoke through the water in the bottom of the hookah doesn't reduce any of the carcinogens to any level that could be considered 'safe'.  Plus I started to just not enjoy it.  I tried as hard as I could to get a great hookah going.  I ditched self-lighting coals - the chemicals on them tasted horrible.  I used natural, less-processed coals despite the fact you need to light them on a range burner.  They tasted better and produced better, fuller smoke.  The only problem is that they get so hot that they burn the shisha under them badly and quickly.  Or the air flow would be so bad they don't really start burning well at all and none of the shisha gets burned at all.  That's wasteful.  A good, well lit and packed hookah can be enjoyed for maybe an hour.  My typical experience these days is either everything burns up in 15 minutes and produces acrid, thin and disgusting smoke, or everything remains unburned and no smoke is produced at all.

I don't enjoy that at all.  The hot coals are theoretically good, but they require adequate air flow and some method to moderate the heat they produce..  That thin layer of tin foil separating the shisha from the coal typically has many small holes that produce good air flow (if the coals aren't blocking them completely) but don't moderate the heat at all. There are metal plates that can be used instead of or with the foil to provide a somewhat larger measure of heat moderation.  The problem with them is that they are thin and have large air holes in them.  Thin means they provide a low level of heat moderation and large air holes means that air flow isn't directed around the coals to keep them lit. 

In short, none of the available solutions is very effective. 

As an engineer I feel I can't let this situation stand.  How difficult can it be to create some sort of metal grate that will distribute the heat of a burning coal while still providing good air flow - and additionally providing some means to ensure the coals don't slide off the bowl and leave burn marks in your carpet - such as this:


Well hard is it to design something like this?  I'm an electrical engineer, so I don't really know anything about, well, I'll make a list:
  • Materials - What kind of metal will have good thermal conductivity and has high machinability characteristics but not produce, say, any poisonous gases when exposed to the heat of a lit coal? Where do I even buy all this material?  Aren't there a lot of different options?
  • Machining - How is such a thing as this made? I've seen a CNC mill, but I don't own one, know how to use one, or even know what sort of information I need to give someone to be able to make something using one. What sort of tolerances can I expect?  Can I design something with .01" dimensions and expect that it will come out right?
  • Cost - How much will this be?  How do I make it less expensive? 
In short, I don't know a whole lot.  So, I start with what I do know.

I have three different hookah bowl types.  I want this thing to sit nicely and securely inside the bowl and not move around.  So, first step, measure the inside diameters of each of the bowls so I can figure out what sort of lip I need to design into them.  For measurements like that you need every engineer's best friend:

If you don't own one of these, buy one.  I'll help, clicking on this link makes me happy: 6 Inch LCD Digital Caliper with Extra Battery and Case  My favorite part about that one: the supplier is 'Generic'.  I love China.  You can measure anything with these very accurately.  Saves your life when doing anything mechanical.  They're great for PCB work - particularly for drawing your own footprints. 

So this starts me off with at least some idea of what I need to make.  Let's start drawing something.  I could go old school and start off with pencil and paper but this is the 21st century.  We have computers with CAD software - but which to use?  I've never had to do any mechanical design like this before. 

Lucky for me I've been at this a while.  I can't even remember where I heard of it first, but I knew that eMachineShop.com existed.  It's something like PCB123 or ExpressPCB but for mechanical parts.  They provide you with free CAD software that is capable of:
  1. Drawing out what you want to make
  2. Generating a 3D rendering of your design (very useful)
  3. Deciding what machines/processes would be suit your widget
  4. Presenting you with a list of materials with information on machinability, typical uses, cost and other information as well as making limited recommendations about which materials to use for your design.
  5. Calculating the cost and delivery time for your part right from the program.
  6. Placing an order for your part.
eMachineShop.com handles most of the hard stuff - ordering material, machining it, finishing it and shipping it to you.  The software takes some getting used to, but since I have a limited mechanical design background (let's hear it for growing up on a farm and... playing with Legos!).

I eventually decided on a design that looks a lot like the picture above.  It will be made from 316 stainless steel (the same type as is used in pots and pans - that should be pretty safe) and fit three different bowl types.  On the inside there are ridges that the coals sit on top of.  In the troughs of the ridges the holes will be drilled.  These holes in the troughs allow air to flow freely over the coals - the holes will never be completely covered by the coals because the coals can't fit into the trough.  This fixes the airflow problem.  The bottom plate is about a quarter inch thick still - this will spread the heat out and not just burn the top layer of shisha to cinders.  And the walls are an inch and a half high to prevent coals from falling off (and helps to block drafts).  Overall I'm rather pleased. I think I could use about 3-5 of these as a prototype - I'll hand some out to my hookah-smoking friends and get some feedback.  So how much will it cost?

Oh, only $500 for three of them?  Well, at prices like that I might as well get a dozen eh? Now don't get me wrong - I think this idea actually has product potential.  I'm up for investing in a prototype if there's a payoff, but no matter how many units I pretend to buy (I went all the way to 10,000 units) the cost/unit is still around $30.  I'd have to sell it for around $50 and for that cost I wouldn't buy it.  Something has got to give.  Perhaps I can talk to local machine shops and see if I can get cheaper machining?

This isn't a bad idea - there are plenty of local machine shops.  I hate calling people, so I use email.  I narrowed the list down to about five that I actually liked.  What was my criteria?  Basically, how amateur their web page looked.  No, I'm not joking.  Places that talk about machining parts for the space shuttle?  No thank you.  Places that talk about making awesome chrome instrument panels for your motorcycle?  Yes please.  Places that discuss ISO 9e8 quality processes and micron-level attention to detail?  Sounds expensive.  Places that have pictures of  bearded men wearing leather gloves inhaling metal shavings? Now you're talking my language.  Places that say they'll work with you and specialize in prototype production runs?  Yes!  Look, I'm an amateur, but I'm not unprofessional and I'm not looking for a disreputable machine shop.  I'm looking for the one that will understand my plight the most - forgive my crazy unrealistic demands, help me decide what materials are safest, hold my hand through the difficult and unknown process of machining metal.  I'm in unknown territory and I'm looking for the people that will have the most patience and tolerance of a difficult designer meandering his way through a low-profit job.  I'm looking for someone humble and helpful. 

Luckily, the eMachineShop software can produce DXFs, and from what I understand about mechanical design, that's a thing.  So I proudly send off my DXF, acting all professional with my talk of what materials and processes to use (entirely cribbed from the eMachineShop software mind you).  And they get back to me very quickly!

'Uh, we don't really use DXFs, can you give us a PDF drawing or something?'

'Do you have an STL file?  Or maybe a PDF with measurements?'

'Can you maybe draw it out... and send us a PDF?'

What?!  Don't these people realize that we're in the 21st century?!  That's practically the future!  Fine - you know what?  My dad taught me how to draw things out, I have graph paper, I've seen mechanical drawings and I know how to measure.  So bring it on!

I printed out a cross-section from the eMachineShop software, sat down with my ruler and mechanical pencil and started marking out measurements.  And boy, was it a mess.  My design was absurdly over complicated.  I had angles other than 90 degrees all over the place (but they looked pretty!).  I had about fifty different measurements to mark when only about 4 of them actually mattered.  I just plain didn't align anything to anything else - wherever I had the choice to line up two lines I just didn't. It didn't matter - the software made it seem like all design decisions were equal and it's all getting built by machines anyhow.  If the Design Rule Check doesn't complain, who cares?

But now I was dealing with people.  People who would have to do more work to handle my absurd design.  People I would actually have to talk to.  Most importantly - people I asked for help.  Who might criticize my design.  I wanted to try extra hard to do the best I could - you know, to avoid embarrassment.  There will probably be plenty of that anyway given how little I know about mechanical design.

So I go back to the drawing board.  I have my important dimensions, so I start with those, but then I start aligning everything together to make it a more organized, simpler mess than before.  I also do a bit more research into materials and find some interesting information. Did you know that aluminum is less expensive than stainless steel?  Heck, most things are less expensive than stainless steel.  More importantly, anodized aluminum is used in cookware - high heat tolerant and still people-safe!  And wouldn't you know it - eMachineShop offers anodizing services and there are several local shops that will too.  Now, with the less-complex design and different materials even the eMachineShop price per unit at 100 units is about $13.  This is much more lucrative to sell.  Plus, anodizing aluminum means you can color the metal.  Everyone loves color and everyone loves choosing colors - very customer-worthy.  Aluminum also cuts down on weight, which cuts down on shipping.  It's a win-win-win situation.  Here's the revised, simplified design:

Look at that lovely handwriting!
I'm still waiting to hear back from everyone, but at this point I don't care if local places are less-expensive than eMachineShop.com - it's all pretty good.  I can even buy material easily - you can buy aluminum stock from Amazon.com!  If Amazon doesn't have it, McMasterCarr does - they're basically the Digikey of mechanical components and materials.  This summer - one way or another - my beta-testers and I will have a product in our hands.  If it's worthwhile, look for this on Kickstarter some time after.  I'm hearing a lot of interest just around the water cooler.  I'm told hipsters will love it. 

So, for any electrical engineers wanting to try their hand at mechanical design, here's some bullet points to get you started:
  • Get a good set of digital calipers
  • Sketch everything out on paper to start with (use pencil, not pen)
  • Minimize the complexity of your part by minimizing the number of measurements it takes to describe it
  • Use the eMachineShop software for help with manufacturing processes and materials and a quick price-check
  • Use Google to look for local machine shops - they've been nothing but helpful to me so far and potentially less-expensive than online shops
See you on Kickstarter!

Sunday, May 6, 2012

Scientific Zombies

I used to think of lots of big ideas back in college.  I'd think of things like 'Hey, let's use genetic algorithms to evolve a fuzzy logic model of a person's psychological makeup so that we can simulate what would REALLY HAPPEN in a zombie epidemic scenario!  For science! And to watch a bunch of dots on a screen run away from other dots that are zombies!  Realistically!'

Yes, that was a real project I had.  That was the goal anyway.  A friend and I convinced a teacher to give us credit for a semester-long special project where we created  a project titled "FUZZY DECISION MAKING IN A CELLULAR AUTOMATA-BASED EPIDEMIC DISEASE SIMULATOR".  At least that's what the cut and paste from the abstract says.  Yes, abstract - we presented this at a real symposium where real people (presumably professionals) nodded their heads and clapped.  Now of course we didn't say it was a zombie attack simulator - we instead chose a real disease (you know, to promote actual science instead of awesome science).  But they thought the idea had merit!  I wasn't boo'ed off stage at an event where people are supposed to say smart things.  I must have said smart things!

I know what you're going to ask next.  No, we didn't finish it.  Not finish it finish it, but we had both parts of it.  He could simulate the airborne dispersal of a disease and its incubation in people and I created a person who could have his own little day:  When he's tired, he slept.  When he was hungry he would either go home to eat if he was feeling antisocial or poor, or to a bar to eat if he was lonely.  When he ran out of money he went to work - not exactly a perfect model, but for an automaton that basically just implemented the first three layers of Maslow's Hierarchy of Needs it showed some very human behaviors.  I not only said smart things, I think I did a few too. 

I don't think I can say smart things anymore.  Pretty sure I can't do them either.  Not unless sitting on the couch watching Law and Order is a smart thing (geez, way to ruin my life Netflix...). Have I lost the knack?  Hey, you don't know if you don't try right?  So as a big idea pops into my head I think 'I wonder if anyone else has done this - if they haven't, I could get rich.'  Let me tell you - if one thing can get me away from Law and Order it's a whole crapload of money.  I'm just sentimental like that

Let me explain the idea.  One of the problems with linear controllers is that their performance becomes much worse the farther you move away from the operating point for which they were designed.  The operational range of a linear controller is conversely tied to performance: smaller range - better performance, larger - worse.  This is because plants tends to look more like a perfectly linear systems the smaller your operating range is.  The more linear your plant is, the easier it is to control - but only within that range.  If you can prevent the system from operating outside that range then you're fine.  But no one is going to accept a solution that can perfectly control a piston - as long as its maximum extension is no greater than 1 inch.

We can't avoid trying to span a wide range of operating conditions with a single linear controller, but we always want better performance.  How can these two be reconciled?  My Big Idea is to use two linear controllers optimized for different operating points in the wider operating range and use a fuzzy logic controller to combine the outputs from both into one controller that has better performance over the whole operating range. When near either of the two operating points the fuzzy controller would largely cede control to the appropriate linear controller, but if it is operating between them the fuzzy logic controller would combine the outputs of both to some degree.   Using two controllers in this fashion would theoretically give better performance over that wider range if you can tune the fuzzy controller to provide a smooth transition between the two controllers.  So has anyone figured that out?  Was it a smart idea?  Do people like me?

Truth is I have no idea.  I started Googling and  found this.  It's very interesting but I can't at all tell if it's what I wanted to do.  Is it just me or were textbooks always this hard to read?  No, don't answer.  If they were engineering textbooks then yes, they were always this hard to read.  Teachers were just as difficult to understand, had the same propensity to stare off into space, to offer unhelpful explanations or simply not offer any at all.  And we were the ones being graded.  It mattered to us that we could understand those inscrutable papers and confusing textbooks.  Our whole purpose for being  in college was to leap those academic hurdles.  Or, I dunno just run right through them.  Whatever gets you to the finish line since (as we all know) 'C's get degrees'.

I feel like I'm not going to get anywhere with books like that and quite frankly I don't have to care anymore even if I really want to.  I have my degree so I'm off the hook.  But it does make me ask: who is right here?  Who is the smart one here?  Does my idea have merit?  Are those textbooks full of greek letters and dry mathematical drivel necessary, or is my frank yet wordy description above preferable?

I think the most fundamental question here is was our zombie simulator fake science one second and real the next just because we changed a few parameters in the disease model, wrote an abstract and slapped together some PowerPoint slides?  Was it because people nodded and clapped?  What if they were just being polite?  Why does polite clapping at a symposium make something more scientifically legitimate than a million 'Hells yes zombie attack simulator FTW!!!11!' comments on the internet?  Can't those comments be coming from scientists?  In fact I have a sneaking suspicion that if we had presented a zombie attack simulator at that symposium we would have had that exact reaction from the scientists present. And it didn't require foaming at the mouth about  epsilons and solution spaces and lots of other mathy jargon. 

Can't normal people have good ideas?  Ideas that smart people think are good?  Can't we explain them without trying to confuse one another?  With compassion and caring - compassion in that the person on the other side of your explanation is a poor person who probably isn't nearly as smart as you but is making every effort to try, and caring in that you do everything you can to help that person understand?

There is an amazing opportunity in this country (and possibly across the world) for individuals - hobbyists, lay-people, the proletariat - to contribute to scientific progress in a real way.  Never before have we had so many educated so well.  A college degree doesn't make you a genius but it gives you something - no matter what you studied.  It is far better than not having it and today more people have a college degree than ever.  Today, more people than ever have a computer - a computer that can calculate in hours or days what a computer fifty years ago could calculate in decades.  A computer that can look up almost any fact for you. The same programming languages that run aircraft and space ships are free for you to use.  Oh, you can't make an aircraft?  Yes you can (top three hits for 'kit aircraft' on Google).  It's expensive and I'm sure time consuming, but saying it's impossible?  And who's going to tell you you can't build a space ship.  These are things that hobbyists are doing.  There's more support than ever now to do whatever you want.  Maybe you want PCBs made?  Or you need help having mechanical parts fabricated? Oh, but where will you get funding for all of these amazing ideas?

The barriers to doing things that matter - like real science and technology - are lower now than they've ever been.  But you still have sides.  On one side you have hobbyists trying everything they can as hard as they can, filled with questions and looking for a place to make a real, helpful contribution to something greater than themselves.  On the other you have symposium-goers.  People who focus on publishing in peer-reviewed journals, double-checking results, navigating scientific political squabbles and generally becoming known and respected.  But these two sides are not in opposition to each other.  Both have the same goal: scientific progress and the betterment of mankind.  The only difference is in their temperament (and how much they get paid). It's not a question of real science vs. fake science.

As we go forward the ability of hobbyists and other scientific lay-people to contribute to scientific discussion will have nothing but positive benefits for both sides.  Professional scientists should take steps to make their research more accessible by anyone with a passing interest.  But we must not think that science can be handled by hobbyists alone.  The rules of process that seem stifling to many hobbyists are a necessary part of the scientific method.  Hobbyists need to respect the scientific process because that is ultimately what keeps us honest. The best case scenario for everyone is not to choose sides and see which wins, but to respect the necessity of both while finding ways to work together for greater effect.  

Sunday, April 22, 2012

An idea blooms


There’s all sorts of problems out there to solve but you have to be careful.  Not everything can be fixed with a microcontroller.  Sometimes it seems like it’d be nice but then you hit hurdles – oops, now I need wireless.  Oops, there isn’t a battery big enough to make this work.  Oops, this is physically impossible.  The truth of the matter is that there are few problems that can be fixed cleanly with a microcontroller – and I’m all about clean.  I hate kludgy systems grafted on to other systems grafted on to Twitter.  Sometimes you just need to wait for a problem that is begging to have code written to solve it.
And I found that problem.  Here it is:

That wasn't like that before!
I know what you’re thinking – who could be responsible for this heinous misuse of the trash can?  WHO WOULD DARE?!  WHAT KIND OF ANIMAL WOULD DO SUCH A THING!?  As luck would have it, we know exactly who the offender is.  Here she is doing her hardest to appease us with cuteness:
 
Bad dog! Bad adorable dog!
Really, what kind of fool does this dog take me for?  That’s only a half-windsor!  Not bad for a creature without thumbs, I’ll admit, but this does not excuse her bad behavior. Not at all.
Naturally she only does it when we’re not in the house.  And it doesn’t take long at all – this isn’t a desperate gamble to get our attention, to force us to recognize that we are abandoning her and she has to take measures to fend for herself.  No, this is after perhaps a half hour of being alone.  That’s apparently all the time it takes her to realize that there’s bacon grease in the trash.  And boy does she love her bacon grease.  

What to do?  Hide the trash when we leave?  Not bloody likely – that sounds like work.  Get a different trash can – one that closes tightly and allows no dog to access its contents?  That’s also work – I hate going to Bed Bath and Beyond.  Plus, neither of those ideas trains our dog to be better.  Who knows where all that energy will be directed if not the trash – perhaps to the kitty litter?  I want to train her so she leaves things alone, not accommodate her bad behavior.  But she’s crafty – she only gets into the trash when we’re not present.  So how persuade her to stay away from the trash when you’re not there?  This is a solution that begs for an automated approach!  What are our options?

Well, because I keep up on the latest in tools from Cool Tools I have the inside track on a neat little device used to train cats:  the Innotek SSSCAT Cat Training Aid  It’s basically a can of compressed air with a motion sensor on it.  You put it wherever you don’t want your cat to be – usually up on counters or something like that.  When the cat jumps up there and gets within range of the motion sensor it releases a burst of compressed air that scares the crap out of the cat.  People say it works rather well – it’s a consistent negative association for the cat.  Best of all is that it’s automatic – it doesn’t rely on a person being there and consistently disciplining the cat.  That sort of consistent training helps the lessons stick.  If I want to train our dog to not get into the trash I need something like the SSSCAT.  I could probably use the SSSCAT – it wouldn’t be a bad idea for me to own one anyhow since we have two annoying cats.  But damnit, it’s spring – when a young man’s fancy turns to… engineering.  Of course.  I want to make something  and this is perfect for several reasons.
First, it’s not too complicated.  I’ve thought it out and it will only need one or two sensors as inputs and one noise maker as an output.  There are no complicated communication interfaces, no zany analog front ends, and no high frequencies.  You could code the whole thing in assembly if you wanted.   Second, nothing exactly like what I need currently exists.  The SSSCAT might work – perhaps I could place it behind the trash can or inside of it somehow, but that might just as easily not at all work.   Third is that I can add features that aren’t in any product I can find.  Inventing something that doesn’t exist but would make your life easier is the reason I got into this engineering racket.  Some people write to make the world a better place, some enter politics.  I find new ways to be lazy. Someday I dream of a world where everyone can be lazy.
So it’s decided – I’ll save the world by making something that yells at my dog when she knocks over the trash.  It’s a tall order, but I think I’m up for it. Next time I’ll discuss high-level design: why it’s important, what sort of documents you should produce as part of it and what kind of information they should contain.  We’ll also discuss project organization and folder structure if you’re lucky. 

Thursday, March 8, 2012

An In-Depth Look

I had someone ask to share the loops I was talking about in the earlier post for analysis.  Ask and ye shall receive!  Let's start with the loop that performed awfully.

Stare into the belly of the beast...
Here's a quick walkthrough (roughly from left to right):
  1. Load a text file as a spreadsheet with a break on all newline characters - this gives me all of the lines in the file in an array of strings (there's probably an easier way but this was most direct at the time.
  2. The array of strings is fed into the first FOR loop.  The loop is auto-indexed on the array of lines.
  3. Within the FOR loop there's a case statement that handles different lines in the text file differently.  There's header rows and such at the beginning so basically the case statement allows the loop to ignore those and start with the data rows.  
  4. Inside the case statement there's a second FOR loop - this one loops over each field in the line.  It is indexed by the array of integers.  Each row of that integer constant contains offsets and lengths for a string subset call. (More on this in a second)
  5. Inside the second FOR loop there's a case statement that has a case for each field.  Every field except the last is handled the same.
  6. Inside the case statement there's a string subset command fed with values from the constant integer array.  These values provide offset and length for each string subset call except for the last.  The last is the special case mentioned above - there's no length associated with the last call to string subset (not wiring the length causes it to read until the end of the line).
  7. When each field is picked off, that text replaces one of the elements in the constant string array being fed into the loops near the bottom.  The string array has an element for each field in each line, and the integer constant contains the array element within that string array to replace with the current field.
  8. When a full line has been read and turned into an array of strings, that array of strings is appended to the list of lines already read.  
  9. The loops then continue with the next line.
 I hope most of you can already guess what the main problem is so far.  It's the 'this loop inside this loop inside this case inside this loop inside....' yada yada ya. That's bad - for speed anyhow.  At first I tend to try to make my code.. shall we say, robust?  Able to handle even impossible situations?  I also try to make it clear, and clear means verbose or at least, doing much more than you need to to promote uniformity and centralization of data.  This often leads to unreasonable performance through over-engineering.

Let me give you an example -  I'll bet something like this has happened to you.  Let's say you've got a program that communicates with another system.  For what you're programming, you have to change your behavior depending on what commands the other system sends you. So as a hacker, you start hacking.  You read the first command you need to implement:
"Set mode to passive - in this mode your system will only respond to messages sent to it by the external system."  
Ah, easy!  I'll just make a boolean that tells me whether I've received that message!  Then I can read that boolean and determine whether or not to send messages automatically.  Great.  Second message:
"Set mode to active - in this mode your system will automatically send the following messages at these rates:..." 
Perfect - when you receive this just toggle the same boolean from before to set yourself back to automatic mode.  Third message:
"Set response delay to minimum - when you receive this message delay all automatically-sent messages by one frame."  
Okay... so I'll make a boolean for this one too?
"Set response delay to maximum: When you receive this message delay all automatic messages by three frames." 
Okay, okay, no, wait, I should make that last one an integer or something and not a boolean, I'll just go back and change that...

And right about here my bat-sense starts tingling.  "You're going to be doing this forever!  Use a state machine!  Otherwise you'll have a million variables and you won't be able to keep track of what combination of booleans and integers defines what state and what behavior!  STATE MACHINE!"  And a state machine sounds like a good idea - it allows you to be more rote, more design-oriented, more organized and more certain that your system works.  Implemented properly they improve deterministic behavior in complex systems.  So I add a state machine and start defining all the states I'll need: automatic, passive, umm, delay?  No, so far it's just two states but I'm sure there will be more.  Let's read the next requirement:

"....."

Ah, so we're done are we?  So I rigged up a whole state machine architecture when all I really needed was one boolean and one integer.  Sure, a state machine will WORK for this but it's certainly not efficient.  It needs memory, time, special coding practices, etc.  A boolean will do the same job much more efficiently.

And that's my problem - that's why I build loops inside of cases inside of loops (yo dawg, I herd u liek loops....). I think I'm doing something to make my programs deterministic and robust.  I'm actually making my programs robust, deterministic snails.  Like a giant snail-tank that's crushes anything in its path and is always on-time.  I don't think anyone wants that.  Unless that's what you ride to work every... month?

After much profiling of various methods of implementing this functionality, I settled on the below implementation:
So fresh and so clean!

Yeah - look at that.  ONE LOOP!  Seven calls to string subset (running in parallel) and no Build Array blocks!  Loading a 48000 line file used to take an hour.  Now it takes two seconds.  So how did I manage to rid myself of all of that cruft?

  1. First, I stripped off the header rows from the array containing each line.  That removed the need for a case statement inside of the first FOR loop (the one that ignored the header rows).  
  2. Then I removed the inner loop that looped over each field and just put all of the string subset calls in the same place.  This allows them to run in parallel instead of serially (if Labview is as smart at figuring out how to parallelize things as is claimed).
  3. Since I'm not ignoring any rows any more I can replace the Build Array block with a simple auto-indexer on the output of the FOR loop.  This significantly sped things up. 
  4. And finally, I configured the loop for parallel operation.  To do this right click the FOR loop and choose 'Configure loop parallelism' or something like that.  This lets different iterations of the loop run at the same time.  If you think about it it makes sense.  As long as the next loop iteration doesn't depend on the last loop iteration then you can just run them all at the same time.  If I had 48,000 processors then I could have each of them process one line of text and have this job done in microseconds.  When you use it though, pick a small single-digit number for the number of parallel loops. There is overhead associated with managing multiple threads so increasing the number of parallel threads starts to backfire after a while.
The second block diagram is much better programming than the first simply because it's quicker.  It's like I always say: any automated process is better than any manual one because it's consistent AND because once it works you can speed it up later.  There's no way this program would have been useful to me if it took an hour to load one log file.  So don't overcomplicate things!

Friday, March 2, 2012

Boo Labview!

So I'm not posting about the microcontroller stuff I swore I'd start last time. Well, this time I swear there's half a post sitting on Blogger ready to be finished someday. That one IS about microcontrollers. This one's about Labview - mostly because I had a fun little time with it today.

We have a customer who has designed some rather interesting test equipment utilizing Labview Real-Time. This means that for months I've been working with Labview for all sorts of fun things. Since their test equipment is all National Instruments-based we've decided to double down and use TestStand to automate the testing that utilizes their equipment. This means that one of the fun things I invented to help our workflow along was a piece of Labview that reads in a CSV file containing descriptions of what inputs to apply to various VIs and what outputs to expect from them and turns it into a TestStand-compatible sequence. This has saved us more time than I can imagine - mostly because we didn't even try to write any of the sequences by hand. I was working with someone who works for our customer and he actually attempted to write a 500 steps sequence by hand. I don't think he got even halfway through. Keep in mind this is a rather tedious set of steps. Picture something along the lines of "Press '1' on the computer keypad. Verify that '1' appears on the computer display. Record the result. Now press '2' on the computer keypad. Verify purple monkey dishwasher. Hit self in head with hammer. Melons."

You see? I couldn't even get through two steps without losing my mind and desiring to physically harm myself. When you use a CSV file you can search and replace. And copy and paste. Copy step 1, paste to step 2, search and replace 1 with 2. Presto! Extra nerd cred if you use regular expressions! Don't hire a person to do a computer's job. Use the right tools!

In the spirit of using the right tools I wrote a lot of Labview today. You see, our customer gave their test equipment an option to log a LOT of CAN bus traffic. The only problem is that due to the (fast) way they write the log you can't be certain that everything is in order time-wise. I don't blame them - it's a real-time system. You do what's fast and leave the post-processing for a bigger computer (though do note an irony - I'm fairly certain that the real-time PC is more powerful than the host PC it's 'slaved' to). They suggested a rather cumbersome process to import the data into Excel and sort it but I say nay sir, nay! I hate things with more than one step. That's why I have an iPad - only one button. There's no question about what to do. The iPad is off? Step 1: push the button. Success! You'd be surprised how often it works.

I wasn't happy about eight steps and I knew that in the future we would want to use this log to verify behavior of the system under test, so I decided to write Labview to read the log, organize it, search for certain messages, judge skew, rate, etc. So it made sense to write a Labview program to parse the log, organize it, search it and collect statistics. Now keep in mind that this log is distinctly not... how shall I say? It is distinctly not brief. The log for a 10 minute test is 20 megs. And it's all text. No pictures of cats whatsoever (not that they wouldn't improve things). Admit it - you're a little surprised that there can be that much text and that I can be interested in so much of it. But hey, we're not barbarians. We can handle this much text. It's no problem for a computer - especially the amazing computers we have today! Computers are information processing MACHINES. And a machine means I don't have to do the work for myself. That means less thinking. That are good.

And all it takes is....

Um...

Theoretically it will take around six hours.

Yeah, yeah, I know - that sucks. That's terrible. I should feel ashamed. *I* wrote something that take six hours to process 20 megs of TEXT? 20 megs of video is, what five minutes maybe? Naturally this depends on quality, but let's face it - you'll blow through 20 megs of high-definition video in much less than an HOUR let alone SIX. And I wrote a program that couldn't handle 20 megs of text in less than 75% of my work day.

Hey, I like getting paid as much as the next guy but even I get bored. Plus, I want to get this log analyzing done quickly so I can do more of it and be DONE with everything quicker. I want to respond to our customers questions in minutes when they expect the answer tomorrow. I like to impress, I like to excel and what I've done here is not it at all.

So I start profiling. So I load up a 5 meg text file (one of four in a typical log set). How long does that take? Milliseconds. Single-digits. Not worth mentioning. That is not the problem. I sort the data by timestamp - sounds intensive right? I have no idea what Labview's built-in sorting functions are like. Quicksort? Bubble sort? No idea. Could be a lot of room for improvement there. But... nope. Very brief - a small amount of the total time. Rather impressive actually. So I break it down; I limit the number of lines so I only have a thousand. Now the whole process takes a second and a half. I know it's not loading the file, I know it's not sorting the file so it must be loading all of the data line-by-line into a 2D array. One big ugly FOR loop in the middle of my VI that is slowing everything down. And no matter how much I profile, no matter how much I change the loop always seems to take about half a millisecond per line. My first attempt used one string subset block but it was inside a second FOR loop that ran once for each field in the line. Surely a FOR loop inside of another FOR loop significantly slows things down. After all, they keep telling me Labview is inherently parallel. Multi-processor, hyper-threaded blah blah blah. Putting that FOR loop inside the other one forces a serial process. If I used eight string subset blocks at once then it could thread it any way it pleased. That should help shouldn't it? No dice. No change. Ah, look! I'm indexing an array - if I can find a way to remove that from inside the loop.... no help. What if instead of string subset I used one Scan Into String block? That might.... provide no benefit at all.

I suppose if nothing else I could remove this Build Array block and see if I can't just let the FOR loop auto-index the output but I doubt that would....

....

....

Each iteration of the loop now takes 6 microseconds. Down from 500.

This is why you can't trust Labview. This is why Labview Real-Time is an oxymoron. It does not encourage nor make easy optimization. Now, I will be the first to admit that I created - from scratch in one day - a program to process and analyze tens of megs-worth of text data and display it in a user-friendly way. This program will make me more efficient and productive and I have Labview to thank for that. And I do.

But Labview just feels like a toy to me now. When you have ten lines of text data who cares about optimization? When you have a hundred it's a question of how much do you care about thirty seconds of watching a progress bar. If it's a thousand you go and get a coffee and when you come back it's done.

But if it's half a million? You can't just say 'Well some things take a day to run.' That's silly. That's wasteful. Worse - it's unprofessional. Wall Street trading firms have gone so far as to implement their risk analysis algorithms on FPGAs!. It used to take them the better part of day to process their data. Now? Something like fifteen minutes. And guess what? They win. Their competition still takes hours. Now they can act quicker or make better algorithms to work smarter. Better efficiency gives them more options. That's what a professional does. A professional does not shrug his/her shoulders and say 'I dunno. Let's just buy a faster computer.'

So I can't trust Labview. I moved all sorts of blocks around trying to figure out which one was the culprit. They all looked the same. I had no idea what was under the hood. Was it linear complexity or exponential? National Instruments won't let me look at the code. As far as I know they haven't given me the tools to thoroughly analyze their entire architecture. Where's my gprof report? And was their Build Array block seriously doing something as absurd as copying a ten thousand element array each time it wanted to append one more line of text?

If I had done it in C I could have utilized pointers and custom data structures to make a faster program. If I had done it in C I doubt it would have loaded the file in more than ten seconds to BEGIN with. Only in Labview are you encouraged to just plop down blocks without any regard for the cost in time or resources. Even if you cared there's no way to know other than tedious experience who the culprit in your bad algorithm is this time. Does that addition block break down into one assembly command or 100? You will never ever know with Labview. That makes it a toy. That's why you can't trust it.

Edit: I realize that some people (among the many I'm sure who read my blog) will wish to argue with me and say that the problem I've outlined above with respect to Labview is a problem with EVERY computer language. Seasoned programmers know that you can't just call any function willy-nilly and assume it will be fast. I agree with this. It can happen in C - should I call printf in each iteration of a loop, or build a buffer and then print it afterwards? Building the buffer will be much faster than a function call which will then significantly speed up your loop. And this is, of course, not only something you need to keep track of in Labview but in C or Python or any other computer programming language I can think of.

A seasoned programmer also knows that you adapt your approach to each unique problem you encounter. C provides you a nice framework to solve your own problems which includes pointers, function pointers, structs, a myriad of different loops, bit shifting and if none of these are sufficient you can use assembly. Nothing other than rolling your own co-processor with VHDL is faster than assembly. But with Labview you don't have these options. Pointers? Don't exist. Game over. That's your number one tool for optimizing code. For example, the block I had a problem with was build array with a 2D array. Essentially, I have an array in which each element represents a line of text with several fields. The line of text with fields is stored as an array of strings (which, if you want to get into things, are ultimately arrays of characters, null-terminated). That's a lot of arrays but the easiest way to imagine it is that I have an array where each element store several fields of information. What I would do in the innermost loop was parse each line of text to read the fields and shove it into a data structure and then append it to the array with all the data I had already collected.

Well, it seems the Build Array block was to blame. The only way I can explain the terrible slowdown is that somehow Labview stores its arrays like strings - there's one pointer to mark the start of the array and where the array stops there's a special terminator entry. Just like a null-terminated string. So when I wanted to append the latest data to the array, I think it iterated through the whole array until it found the terminator entry and then appended the data there. For small arrays this isn't too bad, but I was getting to 48000 lines per text file (with four text files worth of data). Iterating through that much data every time you want to add another line gets expensive. And it was probably also re-allocating memory for the array each time too! In C I would at least have done the following:

  1. Allocate enough memory to hold all of the file data before I start the loop. I might count all of the lines in each file but that might take too long. It'd be better just to grab then length of each file and estimate what I needed.
  2. Maintain a pointer to the last element of the array rather than have to iterate through the array each time I wanted to add something. It will not use that much memory and it significantly speeds up.
  3. Don't use another loop to process each element in each line - unnecessary loops introduce unnecessary overhead.
Those are the only ones I can think of right now (and #3 is kind of a stretch) but I already know that with those ideas I could make a much more efficient loop than Labview. If I had all that control then I could write something really fast. With Labview I simply don't. I have no idea how any of their blocks work or even how the auto-indexing on a FOR loop works. All I have to work with are the tools they give me and I have no indication how any of them are different.

Many people will say that it's good I'm thinking about Labview in this way - no programming language is perfect, so learn its quirks and work within them. The only problem is that's not how National Instruments wants us to think of Labview. We can't judge it like we judge C because it's not meant to be C. It's supposed to be a magical language that frees us from worrying about the code and cycles and memory and instead focus on our ideas, our algorithms. Labivew, they assure us, will free us from having to think about how to efficiently implement our algorithms. They say anyone can be productive with Labview. I will give them this: almost anyone can use Labview and that is impressive. I've seen Labview created by Computer Scientists, Physicists, Electrical Engineers, Technicians, Chemists - you name it. And most of it works just fine for what they want it to. But it's generally badly written and inefficient. In some ways Labview makes it much easier to be badly written and inefficient - specifically because it essentially works so hard to figure out what you mean instead of what you said. From the viewpoint of someone who's picky about code that's despicable. From the viewpoint of someone who wants computers to make peoples lives better, that's laudable.