Sunday, August 14, 2011

Acceleration Calibration and GCode Parser

I spent some time tweaking the firmware and host software recently. I fixed a few bugs on both mostly related to automated program running (the main task of the system, "running the GCode"). It seemed the firmware was not moving the spindle as far as it was supposed to. It seemed to move it different distances depending on the position of the throttle (feed speed override).

The class modeling the stepper driver has a vector that it is moving along. One component is the target speed, another is the number of steps to take. When the spindle is supposed to come to a complete stop, the period (time between steps) will be "MAX_PERIOD". The function that tests whether the spindle is moving asks whether the number of steps to take has been reduced to zero, or the period has reached MAX_PERIOD. When running at full speed it was going as far as it needed to. But when I slowed it down it didn't go far enough. That's because the deceleration rate is the same regardless of top speed and it was in fact stopping "faster" (over a shorter distance).

It probably wouldn't make sense to evaluate the period equaling MAX_PERIOD to see if the spindle should stop when the system were only to run in automatic (GCode) mode. But my system has several modes including a "continuous manual" mode. The firmware doesn't know when I'm going to release the button nor how long it takes to come to a complete stop. So I set the vector to a million steps and it stops when the speed slows to MAX_PERIOD. So I just told the host software to target the speed of MAX_PERIOD - 1 so now it runs out of steps before hitting the stop speed.

I also calibrated the FAD (full acceleration distance) by writing a graphing routine that mimics the firmware acceleration and counts the number of steps and microseconds it takes to accelerate. I dropped the 2 middle split points in the ramp formula since I decided to go with as straight a ramp as possible. Changing the period by large amounts (25 microseconds per step) at low speed and interpolating the change to 1 microsecond at high speed produces a pretty straight ramp. Up near high speed it is problematic. I'm using integer math to save clock cycles but this creates a bit of a problem at high speed:


Above is the integer-math solution. The ramp is great right close to the top then it gets a little screwy. At some point I start taking 1 microsecond off the period every loop. This creates an exponential curve. If I switch to floating point math I get this ramp:


The whole thing is nice and straight. I don't want the cost of floating point math, at least not now. I may experiment with it later, but it probably doesn't matter that much. I've settled on a minimum period of 300 microseconds between steps. If I go down to 250 or 200, that sharp upward curve causes the steppers to rattle. It doesn't rotate, it just snaps back and forth between 2 positions. 300ms is fast enough. I don't want to wear out my machine and it's WAYYYY faster than cranking by hand.

Now that I've calibrated the FAD I can actually go back and plug that in to the firmware to make a better solution to the stopping bug above. When the button is released, I could set the number of steps = FAD and remaining steps would be the only thing to evaluate.

I wrote a simple GCode parser. All it does is take the X, Y, and Z move commands and build the path in my Java object model. Running a few paths generated by external programs revealed some weaknesses in the host software. I patched a few things up and dropped one of the 3 acceleration scenarios. In some cases the distance between 2 points and the change in speed is such that the spindle can get to full speed before immediately slowing back down to the end speed. This distance is very short, the FAD is only 600 steps (0.075 inches). I decided it might be smoother to leave this out and it will certainly reduce the number of vectors to send to the controller. A lot of this sort of stuff I'll continue to tune after I start running programs.

I'm going to start using the Fritzing software to design my circuit boards. Eagle is pretty powerful, I've read, but Fritzing has a pretty low barrier to entry and I like its association with the other technologies I'm using. Here is a very simple test PCB design in Fritzing:


Fritzing doesn't export GCode but it does export Gerber, PDF, SVG and others. I messed with Gerber and a program called Visolate but had some trouble with it. I then exported as "etchable SVG". I opened the resulting file in Inkscape, did some path operations and came up with this:


I found a simple plugin to export selected paths as GCode. I ran the file in my host software to produce this. It's not pretty, but holy cow, it's a tool path for cutting circuit boards! Sweet:


Thursday, August 11, 2011

Found It!

A couple of months ago I got new tires for one of my cars. I was heading out with the family on a road trip. I wanted to track the wear on the tires so I grabbed my metal machinist's ruler. Other than my calipers (and a four-foot drywall t-square), it's the only measuring device I have that can measure to "the bottom" or an inside edge of something. That is, the end of the ruler is where the measuring starts.

I recorded the tread depth and placed the ruler on the end table by the front door. As I said we were on our way out.

I'm married with children. I love my wife, I really do. There's a song by Sixx A.M. with the line, "and I'm losing my mind 'cause she hides all my shit". I always think of her when I hear that. She doesn't hide my things per se, she just moves them. For some reason she just doesn't like where I leave things.

When I set the ruler on the end table I knew I had better put it away when I got back or she would find it and move it. Sure enough I delayed and it was moved. I saw it later on the kitchen table. (We don't have a kitchen table, but that's what she calls it, don't get me started on that.) I thought, "hey I better put that away or it will disappear." Still no action from lazy me. I didn't do anything and it disappeared.

Neither my wife nor kids had seen it or remembered putting it anywhere. For months I cursed her name (whatever that means) as I used a six-sided architect's scale to try to measure "from the bottom" of stuff. I thought about buying a new one but I couldn't shake off the lazy.

Yesterday I was looking for the blades for the power knife. I didn't find them in the first two places I looked. My wife thought they should be in the utensil drawer as did I so I really went to town digging for them. I lifted the silverware tray out of the drawer and much to my surprise, there was my metal ruler UNDER the silverware tray in the utensil drawer.

So let that be a lesson to you, married men. Don't leave your toys out if you want to keep playing with them.