Step/Dir Stepper motor set point follower demo program using smart pins and mouse input
This stepper motor set point follower demo program uses two smart pins per motor.
Most people think of a set point as just the output of a motion control application, like a G-code interpolator, but it can be anything, like the mouse x,y coordinates here, or a BNO085 IMU, or a head tracker...
I think it is really neat in how well it follows the mouse motion, and how little code and processor overhead there is!
The count of the Step/Dir that gets output by the P_NCO_DUTY is kept track of by a P_REG_UP_DOWN
Every millisecond, the PASM program reads the X axis setpoint from the HUB, reads the X axis Step/Dir count smart pin, and subtracts the setpoint from the Step/Dir count to get the position error.
If Position error is negative, I set the Dir bit, if not, I clear the Dir bit.
The absolute value of the position error, multiplied by the proportional gain (a shift left), is written to the NCO_DUTY ypin.
The bigger the magnitude of the position error, the higher the pulse rate out of the NCO.
This kind of reminds me of a digital version of a velocity loop regulated by a power op amp with feedback from a motor tach....
I'm guessing than when @cgracey put step/dir counting as a smart pin function, he was thinking of a use like what I'm doing here...
This would be great for a educational lab, because of the concepts involved, low cost of hardware, and the fun of seeing the stepper motors chase the mouse positions.... I smile every time I play with it...
It could also be implemented purely in spin, with a 1mSec interrupt?? maybe? @JonnyMac would have fun with this.
I used the "garryj" USB mouse demo (I hacked a lot of stuff out) and his "1CogKbM" objects for x and y set points to follow.
I'm using A4988 stepper driver boards in an Arduino stepper shield running from a 12v supply for the steppers and 3.3v from the propeller to directly power and interface to the A4988's and the Arduino shield. No 5v level shifting needed.
It's set for 1/16 micro step mode, giving 3200 steps per rev.
I'm working on a closed loop version that reads a quadrature encoder instead of the step/dir counter smart pin for "feedback"...
I'm also working on a dc brushed motor with a "PWM & Direction" H bridge type drive and quadrature encoder feedback.
Longer term I'm want to make an object so that my P2 GRBL cnc software will be able drive
1) open loop step/dir stepper motors
2) closed loop step/dir steppers with quad encoders for position feedback
3) closed loop dc brushed motors with PWM drives and quad encoders for feedback
The X mouse motion is followed by the stepper motor, and the Y mouse motion is followed by the Y axis on the creality printer. The motion is smooth with very little apparent lag.