Optimal way to PWM DC motor at semi constant speed despite different load - Page 1
Forum section: electronics / projects

author: 3dgeo
author_rank: Frequent Contributor
timestamp: August 15, 2018, 02:43:57 am
content: Hi,I'm making new control board for my sit stand table, motors 24V, original PSU ~35V. I'm PWM chopping to get average 24V (probably not the best way, but it's fine for what I need.I'll tried to use voltage divider, but due to inaccurate (non linear) MCU analog reading and guessing due to different load average voltage and speed while going UP/DOWN differs – 20V up, when going down as it should be – 24V, speed difference is very noticeable.I got an idea – what if instead of using voltage divider to set PWM I calculate PWM according to time between rotary encoder pulses – measure time between pulses while average voltage is 24V, and then try to get same time while moving up, or some sort of hybrid combination?This method will not give perfect result, but it should be close enough (2 pulses per 360).I'm learning, so don't bash me too hard if my idea is stupid

author: james_s
author_rank: Super Contributor
timestamp: August 15, 2018, 03:51:54 am
content: A rotary encoder with a cheap DC motor is good enough for precisely positioning the print head in an inkjet printer so it should be cable of more than enough prcision for this. I would guess if you look around someone will have already written code to regulate the speed of a motor with an encoder. You could probably just measure the back EMF pulses from the motor and not even need to add an encoder.

author: Mechatrommer
author_rank: Super Contributor
timestamp: August 15, 2018, 04:26:30 am
content: Quote from: 3dgeo on August 15, 2018, 02:43:57 amyour newly discovered idea is not stupid, your are getting there... look up PID control. the error (or datum) will be motor speed (encoder count / dt) its been done long time before and its considered as de facto for simple (laymen) closed loop (motor) control.https://en.wikipedia.org/wiki/PID_controllerPWM = Kp * err + Ki * sum_err + Kd * diff_err (normalized to 0 - 100 or -100 to 100 depending on circuit)err = required speed - actual (encoder) speed.

author: JS
author_rank: Frequent Contributor
timestamp: August 15, 2018, 06:31:31 am
content: If you are measuring the current, and depending on the speed stability you need, you could get away without the encoder. The basic idea is that the motor has a constant velocity at constant E field, but if you apply a constant voltage, the E field will change depending on the load, as you loose some voltage in the resistance of the coils and other losses. So, there's a constant loss in velocity with the load, which keeps a linear relation with the current.Now, for a brushed DC motor, if your output driver has negative impedance, as part of the control loop, you could get away with a sensorless speed control, which is still getting feedback from the current. This highly depends on a good model of the DC motor, but that's not that hard to get and characterize. It does depend on the DC motor config, basically, measuring the applied voltage, speed and current at two different loads you could get the parameters you need. If you choose the points correctly you could get them pretty easily. For example, lock the motor and increase the voltage till you get nominal current. That's one point, zero speed, nominal current, applied voltage. The next obvious point to measure is nominal voltage, no load, you get the speed, the current should be pretty low and will reflect the losses on the system. If you have a parallel connected DC motor or a permanent magnet DC motor this method will let you know what you need.If you have a series connected DC motor you don't want to run it unloaded as it will go too fast, and the response isn't linear so you are better measuring closer to the conditions in the actual application. I would have to search a bit to tell you more about this, I'm a bit rusty with the math behind it, but I do recall the concepts.For other type of motors same rules applies, models and torque/velocity curves are a bit different, but velocity always correlates with E field, torque with current and the difference in velocity with the losses, the major difference is the relation isn't always linear as it is for universal-brushed DC motors.JS

author: 3dgeo
author_rank: Frequent Contributor
timestamp: August 15, 2018, 01:06:20 pm
content: Wow, You people are awesome, thanks!I forget to mention that actuators already have built in encoders (2 hall effect sensors).I don't need CNC machine accuracy here and definitely I don't need complicated solution, I even probably could get away with adding fixed percentage to PMW while its going up, but it's too sloppy in my opinion, I don't like being sloppyWill try implement pulse interval and go from there. To the "Arduino IDE"....

author: james_s
author_rank: Super Contributor
timestamp: August 15, 2018, 04:00:37 pm
content: If you've already got the encoder then it's relatively simple. You should be able to get much higher precision than you require without a lot of extra effort.

author: CM800
author_rank: Frequent Contributor
timestamp: August 15, 2018, 09:14:01 pm
content: Hi,There's loads of information out there on closing the loop with brushed DC motors & encoders, like what you've got.You just need to take a microcontroller with a Quad encoder input, most of them have examples of how to set it up to measure velocity. Loads of information on that if you do some digging. (E.g. STM32 quadreture encoder velocity) or Arduino Due / Zero (ARM versions of Atmel chips) can do it.Arduino Due Hardware velocity monitor:http://forum.arduino.cc//index.php?topic=140205.msg1292713#msg1292713STM32:http://www.micromouseonline.com/2013/02/16/quadrature-encoders-with-the-stm32f4/More info:https://www.embeddedrelated.com/showarticle/158.phphttps://www.embeddedrelated.com/showarticle/530.phpfrom there, just throw a simple PI / PID loop to control the PWM loop. See here for a great tutorial:http://brettbeauregard.com/blog/2011/04/improving-the-beginners-pid-introduction/I hope this helped!

author: 3dgeo
author_rank: Frequent Contributor
timestamp: August 16, 2018, 12:08:06 am
content: Quote from: CM800 on August 15, 2018, 09:14:01 pmthanks, but this is getting way too complicatedFinally I'm getting somewhat descent encoder readings, tho they are very unstable (UP: 168-201, DOWN: 66-148). Don't know if my voltage divider math messing them up (unlikely, but I'll test with fixed PWM), or my breadboard and wires connections make it unstable (I'm using 104 caps to reduce noise).My plan is after each pulse recalculate PWM based on previous divider readings and previous output PWM.Sorry, I have to skip those curly formulas cos I can't wrap my head around them and ATTINY85 doesn't have much programing space or computing power. Tho I have good enough brains to make my own mathPotential problem I'm starting to notice is that even encoders signals visually (in serial plotter) at the same time after few ups and downs one motor becomes lower than other (up to 10 CM difference from even start), I was hoping to level both sides while counting pulses, will have to dig in to it later...READINGS: P.S. I printed them all after 30 values were collected, I know that printing them one by one would mess them up.UP:0 --- 1811 --- 1892 --- 2013 --- 1944 --- 1785 --- 1696 --- 1687 --- 1828 --- 1979 --- 19310 -- 17711 -- 16812 -- 16813 -- 18214 -- 19715 -- 19116 -- 17717 -- 16818 -- 16819 -- 18320 -- 19821 -- 19222 -- 17923 -- 16924 -- 16825 -- 18226 -- 19627 -- 19328 -- 17829 -- 169DOWN:0 --- 1551 --- 1342 --- 1373 --- 1484 --- 1415 --- 1306 --- 1227 --- 1308 --- 1369 --- 14510 --- 7311 --- 6612 -- 13013 -- 12114 -- 13015 -- 13516 -- 14417 -- 13918 -- 12919 -- 12120 -- 12821 -- 13422 -- 14223 -- 13724 -- 12825 -- 11926 -- 12727 --- 6828 --- 6629 --- 73

author: james_s
author_rank: Super Contributor
timestamp: August 16, 2018, 01:09:19 am
content: I think I'd just use one of those $1.50 Arduino nano clones, or some other cheap microcontroller, attiny85 maybe? Really anything should do here, even a 20 year old part.You probably don't even need quadrature output from the encoder, just monitoring one channel will give you a pulse with the frequency dependent on RPM. You can either poll an input pin or set up a pin interrupt and time the period between pulses to get a value representing motor speed. From there you don't even need a full PID loop, just the PI part or even just the P is probably going to be enough.