Friday, April 8, 2011

Buffered Movement

I've been hard at work on the firmware layer. In February I described the major software components. I now have the Controller and the VectorBuffer done. This is a significant step. Previously I had what I'm now calling "continuous" mode: you press and hold a button and one motor accelerates and maintains speed while the button is pressed. Releasing the button causes the motor to decelerate to a stop. This is one of two "manual" modes. The other I called "nudge" mode but I can't put that in my code, it's just not professional, so I'm calling it "discreet" mode now: one press of a button turns the motor a fixed amount. Sort of, the amount to turn will be determined by the position of the throttle control. So if the throttle is in the "1/1000" position, three presses of one button will move the mill head in the given direction by 0.003".

Continuous mode simply requires setting the current "vector" when a button is pressed and setting it again when the button is released. The reason discreet mode is significant is that this mode requires issuing 2 "move" commands: one to accelerate 1/2 the distance and another to decelerate the other 1/2 of the distance. (If the distance was a lot, I'd have it maintain maximum speed rather than accelerate continuously, but I expect moves to be 0.0005" to 0.020" so maximum speed would never be achieved.) The 2 moves are queued in the buffer and the StepperDriver pulls the next move off when it finishes the current move. That's the basis for "automatic" mode, or the main mode the system will be running in with the host computer feeding move after move into the buffer.

I struggled a bit with things again due to my lack of experience with C. I made several minor errors in design, but I made one big error in coding for C. Consider this variable declaration:

int steps;

This means the variable named "steps" stores "integers". But I didn't set the variable to any value - I didn't initialize it. In Java, if you were to print out the value of steps, you would always get zero. In C, at least on the compiler/microcontroller I am running this on, the value appears to be zero most of the time but would occasionally be some random value. My Vector class describes what direction and how far/fast to move. I didn't initialize the values that were to be zero and the behavior was really bizarre. I was really scratching my head looking for logic flaws. Finally I thought I'd try setting the zero values explicitly and that took care of it.

No comments:

Post a Comment