Library to use DC motor and encoder?

I have maybe 2 applications currently where i need the speed range of a stepper, but not its horrible inefficiency, and ideally a greater torque. Mostly im just messing around though seeing what kind of robotics i can make with my 3D printer. Anyway, i have several disk-type optical encoders, and many motors, and i dont really need any sort of precision stepping, i was wondering if anyone know of any simple to learn/use libraries or code examples of how to use a DC motor with optical encoder feedback?

What i want is for the motor to turn at a more manageable RPM for printed parts, lessening the maximum RPM anything ever has to turn at any given stage of converting mechanical rotary energy, and be able to actively maintain a desired speed at a given time and raise or lower the power as needed, so that any potential resistances impede as little as possible. So basically like how a stepper works but, like a stepper, i dont want the motor to skip, slip or otherwise be offset or delayed in any way, even if the accuracy of where the actuator or whichever things position is way off when it stops, i really couldnt care less.

Really just ignore my motives here.

What i want is a way to maintain motor speed and control torque as needed to maintain that speed.

I also have some questions, such as, is there a limit to how slow i can make a DC motor before its less efficient than a stepper motor at the same RPM? And other questions that will probably be follow ups or, not really worth asking until after i have something set up.

Thanks in advance :slight_smile:

Everyone's project is so different, I've never heard of a library for this. Sometimes people will use the PID library but you need to feed it correct numbers and then make use of the output. You mention a few desirables such as speed control which would probably be best implemented directly instead of using a library.

As for the efficiency at low speed, think about the lowest speed: zero. The DC motor is consuming zero power (assuming no great static torque like holding up a robot arm.) The stepper is consuming about 70% of its programmed maximum current.

An encoder library maintains a current position variable. You can use that to derive a current
speed estimate too.

Given either of these, a PID loop (again several libraries out there) will enable motor control,
either position or speed.

Steppers always pull the same total current-squared - if set to 1A, the sum of sqaures of the currents
in the two phases is 1 squared, ie 1. So the current in the two phases might be (1, 0) or (0.7, 0.7), or
(0.85, 0.5).

DC motors(*) pull current proportional to torque (including frictional torque of the brushes), pretty
much irrespective of speed.

(*) to be precise, this is for permanent magnet DC motors, not series wound.