Shop Learn
Step/Dir Stepper motor set point follower demo program using smart pins and mouse input — Parallax Forums

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.



  • msrobotsmsrobots Posts: 3,419

    this is cool,

    thank you


  • ScroungreScroungre Posts: 95
    edited 2021-07-21 03:39

    Definitely cool.

    Me being me, I'd skip your #2 - Steppers with feedback encoders. The whole point of using steppers is that you don't have to use encoders - unless overloaded, they go where you tell them to, and stay there.

    There are many other (and cheaper!) ways to tell that a stepper in a certain application has been overloaded.

    DC brush and brushless motors, au contraire - they very much need optical (or some sort) of additional position feedback. That's actually something I'm looking at right now, but we'll see.

    Still, highly keen! Carry on! S.

    Eta PS: When you're closing the loop, you will need also Integrative and Derivative terms, thus the famous "PID Loop". At the moment you're getting away with just the "P" part because steppers are low inertia and highly 'notchy' at the speeds you're dealing with now. Brush and Brushless motors won't be so forgiving.

  • Looks cool, yes. But in the code I see no limitation of the acceleration or velocity. So if the command is too fast or dicontinous the motors will stall I suspect. I have implemented a motion following algorithm for the handwheel of a CNC machine and the code is much much more complex. With a+v limitation you have to be careful to avoid overshot so you need to start ramping down before you actually reach the destination setpoint.

  • @msrobots @Scroungre @ManAtWork
    Thanks for the positive feedback...
    @Scroungre, I am going to get the derivitive gain to work on the PWM dc servo. I've played with that on other systems before, with just a derivative term, no proportional or integral. You can get a feel for it by turning the motor shaft by hand... The derivitive gain is damping, the faster you turn it by hand, the more it resists, like a viscous fluid, and it is just a torque applied against the rate of change of the position error... but it makes the system stable. Pretty sure you know that, but others might not.
    @ManAtWork, This pulse generation technique reminds me of a CNC without feed forward on, the following error is proportional to feed rate and loop gain. I was making it follow a sinusoidal setpoint and using the random number generator to inject a disturbance every second to see how it would handle a discontinuity, viewing the steps output on a debug graph, and it works pretty good...

  • ScroungreScroungre Posts: 95
    edited 2021-07-24 04:26

    Pretty much what ManAtWork is talking about is the derivative term - The change in position over time - aka speed (velocity, technically).

    You have to have a proportional term, or you have no idea where you are. This might be okay for a constant-velocity system, but not if you expect to stop anywhere near where you think you should.

    And you'll probably be okay with also ignoring the integrative term until you start putting loads upon these motors. That part makes sure that eventually it'll get there, no matter how many gorillas of friction or load are holding it back.

    If you're building a roller-coaster, you have to worry about derivatives of the derivative terms, but hey... ;-)


    And I've built systems that had, to become stable, integrate their integrative terms, but that's getting a bit exotic...

  • With servomotors you mainly control the voltage (PWM duty cycle). In motors with high efficiency L dominates over R so current depends mainly on the integration of voltage over time. Acceleration is proportional to current. Velocity is the integration of acceleration and position is the integration of velocity. So the control reference depends on the 3rd order integrative term of the control output. That's why a servo position control loop (including nested velocity and current control loop) is often so difficult to tune.

    Stepper motors are much easier. They blindly follow the command and stall only if overloaded. But you have to avoid overloading by limiting acceleration so that the motor always can keep up. This is no problem if there's not much mass/inertia. The hand moving the mouse is always slower than the (unloaded) motor can follow. But in heavy machines it's different.

Sign In or Register to comment.