DC Motor shield Speed and position control using encoders and PID

Adafruit Ethernet, Motor, Proto, Wave, Datalogger, GPS Shields - etc!

Moderators: adafruit_support_bill, adafruit

Please be positive and constructive with your questions and comments.
Locked
User avatar
anupamd
 
Posts: 16
Joined: Sat Dec 14, 2013 5:21 am

DC Motor shield Speed and position control using encoders and PID

Post by anupamd »

Hello, does anyone know of a good proven open source library for DC motor speed and position control using PIDs. Unfortunately looks like Adafruit_MotorShield.h is missing such functionality .
I have the N20 DC motor with encoder working but now would like to precisely control speed and position using PID to set my targets accurately. Before I implement my own library I thought i might ask if there is something out there that people are using thats popular. 

User avatar
adafruit_support_bill
 
Posts: 88093
Joined: Sat Feb 07, 2009 10:11 am

Re: DC Motor shield Speed and position control using encoders and PID

Post by adafruit_support_bill »

does anyone know of a good proven open source library for DC motor speed and position control using PIDs. Unfortunately looks like Adafruit_MotorShield.h is missing such functionality .
It is hard to capture in a general-purpose library because there are too many variations in encoder setups and processor interrupt handling. PJRC has a very good encoder library if you are using one of the supported processors: https://www.pjrc.com/teensy/td_libs_Encoder.html

Brett Beauregard's Arduino PID library is excellent also.
http://brettbeauregard.com/blog/2011/04 ... roduction/
I have the N20 DC motor with encoder working but now would like to precisely control speed and position using PID
Quadrature encoders give you relative motion feedback, so they are good for controlling speed. For position control, at minimum you will need additional feedback to establish a reference position.

Also note that 'pure' PID does not work so well for position control with DC motors. This is mostly because motor response is not linear at low power levels, so the motor tends to stall out when making small corrections around the setpoint. There are many workarounds for this. Some more elegant than others. The simplest is to set a minimum non-zero power level for the PID output. As with the tuning, the exact value for that will depend on the total system response.

User avatar
anupamd
 
Posts: 16
Joined: Sat Dec 14, 2013 5:21 am

Re: DC Motor shield Speed and position control using encoders and PID

Post by anupamd »

I'll check out those libraries. Perhaps I should rewind a little and explain what im exactly trying to achieve. I have two wheels one attached to a motor and the other wheel (lets call it the reference wheel) is not attached to a motor but only attached to a rotary encoder like this one . https://www.amazon.com/dp/B07T3672VK?ps ... ct_details

Basically..When I move the rotary encoder I want the motor to match. (as if it was attached with a belt) . It doesnt have to be exact as this is for an animatronics project but it has to be close enough. Is this possible by only targeting speed in my PID ? If there is lag or delay on the motor thats fine as long as it eventually reaches that target speed. Oscilations .etc. can be adjusted via tuning.

The reason why the motor is continuous rotation and not a servo motor is because the motor is eventually going to be part of a mobile robot (so eventually there will be 2) and so the movement of the robot is controlled by a "puppet" robot that is rolled on the ground by hand that only has encoders on it. This hand manipulated puppet robot then drives the motorized robot. If you catch my drift.

I realize there are perhaps even more sophisticated was to do this using IMUs and targeting accelerations and velocities and targeting that via PID.. but im wondering if my more naïve approach might be simpler and sufficient for my usecase as I dont need accuracy.

As far as position, I get what you are saying in that there has to be a reference starting position ...eg "where is zero". I see how that can be tricky.. will have to do some more research there but im not sure i even need this given what im trying to do above?

Thanks a lot for your advice.

User avatar
adafruit_support_bill
 
Posts: 88093
Joined: Sat Feb 07, 2009 10:11 am

Re: DC Motor shield Speed and position control using encoders and PID

Post by adafruit_support_bill »

If the 'puppeteer' can see the robot position and react accordingly, that can be your positional feedback and you would not need any further sensors for an absolute reference. IMUs have their own limitations (e.g. drift & cumulative errors) and probably would not add anything but complexity to a project like this.

If the goal is to match motor shaft rotations to the rotations of a control encoder, life would be somewhat easier if both encoders had the same resolution. The one linked on Amazon has 20 pulses per rotation. The N20 motors in the shop have 14. If many rotations are involved, you can simply scale the error and it would not be a big deal. But for fractional rotations, you would want a closer match in resolution.

The tricky bit in a position control with DC motors is the nonlinear motor response around the setpoint. As the proportional output approaches zero, the motor will tend to stall. The integral term will eventually wind up to get you over the line. But it will likely overshoot, and then the problem just repeats itself in reverse.

If super-precise control is not required, simply limiting the low-end output to avoid a stall may be sufficient.

Locked
Please be positive and constructive with your questions and comments.

Return to “Arduino Shields from Adafruit”