Monday, March 7, 2011

StepperDriver class

In my last post I wrote that I was going to be all patient and disciplined and write the communications protocol for the laptop talking to the Arduino. That didn't happen. After a little thought, I realized all I needed was to be able to set some of the key properties of the StepperDriver for testing purposes. I wanted to be able to set the maximum and minimum times between steps and the acceleration constant. (Yeah that sounds weird, changing the constant.) The Arduino project has a library available for simple messaging (called Messenger). It's not a 2-way protocol, it's just a simple way to send a "phrase" such as:

acc 5000

to the Arduino and "read" it with some simple parsing. Once the message is complete (a carriage return is encountered) a function is called and we can compare the first "word" with some command to set a property. Then we can read a number, in this case a 4-byte number (long). The above example sets the acceleration constant to 5000. Once the code was uploaded to the Arduino, I opened a serial terminal and I could easily type in the simple commands to read and write the properties.

So I jumped right into the code for the StepperDriver. Since I had already prototyped it in Java there wasn't a whole lot to the conversion. I struggled with a few things related to specifics of C and specifics of the Arduino, things I'm not yet experienced enough with. Other than that, the coding was pretty academic.

I've got my Bare Bones Board plugged into a prototype breadboard with 2 LEDs and 2 buttons plus a potentiometer. One LED is a stand-in for the direction pin, the other for the step pin. One button is for turning the motor in one direction, the other the other. The actual stepper motor would receive a very fast pulse train. For development, I scaled the timing way down so I could see the lights blinking.

I've got it all working with acceleration and "feed speed override" with the pot scaling the target speed. The StepperDriver simply "goes in the specified direction" while trying to make its speed the target speed by adjusting the current speed by the acceleration constant.

The next step is to put all 6 buttons and the throttle controller together and wire it all up to the real stepper driver.

2 comments: