EEVblog Electronics Community Forum

Electronics => Projects, Designs, and Technical Stuff => Topic started by: Martinn on June 09, 2024, 05:11:27 pm

Title: Brushed DC motor closed loop speed control
Post by: Martinn on June 09, 2024, 05:11:27 pm
Hi all,

I just bought a powerfeed unit for my mill https://www.paulimot.de/Maschinen/Maschinenzubehoer/Vorschubmotoren/1121/Vorschubmotor-X-Achse-fuer-SIEG-SX4 (https://www.paulimot.de/Maschinen/Maschinenzubehoer/Vorschubmotoren/1121/Vorschubmotor-X-Achse-fuer-SIEG-SX4) which it not really fantastic. Slowest feeds are too fast for milling and speed stability is poor - not sure if it has feedback at all. So I'm thinking of adding a closed loop speed controller (or simply replace the motor with a stepper, but this is not what I'd like to discuss here). The motor is a 24 V brushed DC with 1800 rpm and 1:7.5 reduction gear. No encoder or feedback. The original electronics does some PWM and overload limiting, see PCB photo (LM324, power transistor IRF540. I work on motion and motor control professionally, I just never came across a brushed motor, so I'm asking here if someone has experience with controlling brushed DC motors.
Specifically, I am wondering if closed loop speed control with decent stiffness can be achieved down to a few rpm. This is critical, as it defines the chip load of a milling cutter. Faster speeds (up to 1800 rpm) are for moving the table only and are not critical.

As the motor is now, the only option to get feedback from it would be via back EMF sensing. Not sure how well this would work with the brushes. I guess I would not bother with it.
I think that without a decent encoder, stable low speed performance will be difficult to achieve. Simplest method I could think of would be to add a magnet at the end of the shaft (drill threaded hole in shaft) and put a AS5047 on axis. I have used this sensor for other control projects and it is great when you need high resolution, but only moderate accuracy. Ideal for speed estimation. System options would be going for an STM32 (cascaded PI current and PI velocity control loops), finding a controller chip (Trinamic has some nice ones) or maybe even improving the existing analog control.

Not sure if I am trying to beat a dead horse here. :horse: (and an expensive one at € 383 btw)

Has anyone experience with decent quality (stiffness/dynamic range) control of small brushed DC motors?

Thanks, Martin
Title: Re: Brushed DC motor closed loop speed control
Post by: IanB on June 09, 2024, 05:46:49 pm
No direct experience, but I think speed regulation of brushed DC motors is best done with PWM.

To control the speed, I think you would need an encoder on the output shaft and a feedback loop to adjust the PWM duty cycle.

My guess is you would have to experiment and see what you can achieve.
Title: Re: Brushed DC motor closed loop speed control
Post by: fourtytwo42 on June 09, 2024, 07:15:54 pm
There are three sensorless methods I have used,

One is a current compensated constant voltage, that is the voltage increases by a small amount with increasing current to compensate for Rm.
Another is to AC couple and low pass filter the commutation voltage and count the frequency.
And finally I have tried PWM sampling the back emf during switch off time.

All of course are not as good as a mechanical sensor.

I am guessing that pcb only implements constant voltage OTH the heatsink is quite small so maybe it's a PWM output but with no feedback.
Title: Re: Brushed DC motor closed loop speed control
Post by: Zero999 on June 09, 2024, 07:34:59 pm
It's not something I've done before.

How precise does it need to be?

If precision is important, then how about a PLL controller?
https://www.ti.com/lit/an/slua085/slua085.pdf (https://www.ti.com/lit/an/slua085/slua085.pdf)
Title: Re: Brushed DC motor closed loop speed control
Post by: Martinn on June 09, 2024, 07:36:38 pm
Another is to AC couple and low pass filter the commutation voltage and count the frequency.

Great idea! Did that work? Probably pretty messy I'd think?

And finally I have tried PWM sampling the back emf during switch off time.
That would be the straightforward way, I wonder how well that works with the commutation noise?

I am guessing that pcb only implements constant voltage OTH the heatsink is quite small so maybe it's a PWM output but with no feedback.
Yes, it's PWM, you can hear it (few hundred Hz).

So how was your success rate with those methods?
Title: Re: Brushed DC motor closed loop speed control
Post by: Martinn on June 09, 2024, 07:42:42 pm
How precise does it need to be?

It does not need to be precise. Setpoint is from a simple pot anyway. Maybe +-20% at low RPM would be totally fine.
I'm more concerned about practical issues. How well can you get current control to work with the switching of the brushes? After all it's massively varying impedance and resistance.
Also, stick-slip or generally friction from the commutator could be nasty.
If it would be a brushless motor, we'd be back in well established practise. Adding an encoder would make even position control straightforward. Sensorless operation is more difficult, but also possible (probably not for a few rpm).

It's the brushed motor thing that makes me wonder.
Title: Re: Brushed DC motor closed loop speed control
Post by: amyk on June 09, 2024, 08:02:06 pm
Look at treadmill motor controllers. They're much higher voltage than your application but they use brushed DC motors too.
Title: Re: Brushed DC motor closed loop speed control
Post by: Doctorandus_P on June 09, 2024, 11:05:56 pm
Brushed motors can be quite easily controlled to about 10% of their max PWM, and with some proper tuning to 3% of their maximum RPM or even lower, but there are a bunch of limitations.

First the control algorithm has to be quicker then the time constant of the motor, and you will need a reasonable resolution encoder to get enough speed resolution quick enough at low RPM.

Slip-stick is a bit of a problem, but as long as the motor is moving, static friction and motor load are also important factors. I have made a controller for a heavy movie screen and when unrolling it I had to use different PI parameters then for rolling it up.

Your commutator does not have many contacts. This will result in some torque ripple, and this will also degrade the controlability of the motor a bit.

If you can manage to add a flywheel to the motor to increase it's inertia, it's also easier to control at lower speed.

Motor torque is determined by motor current. For short periods you can overload DC motors significantly (2x nominal torque is quite normal)  but you can't increase torque as with mechanical transmissions.

But overall, with feedback, you can get much better control over your motor, and it is not very difficult to do. You just need some microcontroller FET + FET driver, and the feedback electronics (Hall sensor + magnet). It's a doable project and quite a fun to get to know PID loops and how to do them in software. But it probably will cost you days or weeks of your free time the first time you do it. if that is OK with you, then go for it.

PWM frequency does not matter much. At low RPM, a low PWM frequency (few hundred Hz) is even advantageous. You have to put some effort in the FET driver, because the FET has to switch quick enough. If you connect it directly to a pin of your microcontroller (often used for DC loads) It will not switch quick enough and will overheat because of the many switching cycles of PWM control.

Title: Re: Brushed DC motor closed loop speed control
Post by: schmitt trigger on June 09, 2024, 11:36:08 pm
If you would like to use the back-emf method, just be aware that some motors spark a lot (read: create a  lot of noise), more so under load.
As others have mentioned, the voltage sample should be taken during the PWM’s off time. Even so, the voltage won’t be a straight line but a wobbly one. Most likely you may have to add analog filtering and/or software averaging. 
Title: Re: Brushed DC motor closed loop speed control
Post by: fourtytwo42 on June 10, 2024, 09:57:30 am
Another is to AC couple and low pass filter the commutation voltage and count the frequency.

Great idea! Did that work? Probably pretty messy I'd think?
The problem is the commutation spikes are similar to brush noise and in my case (model railway) noise from the rolling wheel contact so I would rate it the least successful method.

And finally I have tried PWM sampling the back emf during switch off time.
That would be the straightforward way, I wonder how well that works with the commutation noise?
The commutation noise is easy to remove as it is impulse in nature, the two problems are settling time after turning off the mosfet and ripple that needs removing by a sliding average or similar filter. This was the most successful method, implemented in a small micro-controller.

I am guessing that pcb only implements constant voltage OTH the heatsink is quite small so maybe it's a PWM output but with no feedback.
Yes, it's PWM, you can hear it (few hundred Hz).
So equivalent to constant voltage with no feedback

I never got the Rm compensation method to work stably, it would either not work (motor slowed with increasing load) or go nut's (immediately go to maximum rpm), possibly with additional filtering and hours of tinkering it might work but I prefer something more repeatable. I believe it has been used for some audio turntables but they have a heavy flywheel and almost constant load.

Title: Re: Brushed DC motor closed loop speed control
Post by: Picuino on June 10, 2024, 11:13:04 am
The method of measuring EMF voltage works surprisingly well at low revolutions. It is able to keep the motor stopped or at very low speed even though you try to turn it in both directions. The problem is that it is complex to implement.

Switching sensing is also difficult to achieve.

If you don't want very high accuracy, supply current compensation is the simplest thing to implement and good enough. You can even get the motor to speed up a bit when you try to brake it, it depends on the setting you make.
Title: Re: Brushed DC motor closed loop speed control
Post by: Picuino on June 10, 2024, 11:21:26 am
If you decide to control it with back EMF, you can take a look at how some brushless motor controllers do it.
A normal control with a single transistor and a freewheel diode is not enough, because it takes a long time to reduce the current through the diode. The ideal is an H-bridge, which can be connected so that the current is reduced quickly and then the back-emf measurement can be made. This ensures that the motor will be without power for as little time as possible.
Title: Re: Brushed DC motor closed loop speed control
Post by: Martinn on June 10, 2024, 05:21:53 pm
Thanks a lot for the very interesting answers! (I love motor control).
Regarding commutation peaks sensing, I found a few interesting references:
https://ww1.microchip.com/downloads/aemDocuments/documents/OTH/ApplicationNotes/ApplicationNotes/Sensorless-Position-Control-of-Brushed-DC-Motor-Using-Ripple-Counting-Technique-00003049A.pdf (https://ww1.microchip.com/downloads/aemDocuments/documents/OTH/ApplicationNotes/ApplicationNotes/Sensorless-Position-Control-of-Brushed-DC-Motor-Using-Ripple-Counting-Technique-00003049A.pdf)

https://www.ti.com/lit/ta/sszt984/sszt984.pdf (https://www.ti.com/lit/ta/sszt984/sszt984.pdf)

https://www.ti.com/tool/TIDA-01421 (https://www.ti.com/tool/TIDA-01421)  reference design including schematic

Sensorless position feedback appears to be interesting for automotive, like memorizing seat positions. But it's also quite tricky to get to work, it seems.
And while I really enjoy designing around motors, I typically work with BLDC/FOC and this is just a personal side project. And truth be told I mainly want this powerfeed to work - not make a half year project out of it (although it sounds fun). Main problem with collector pulse counting could be the large dynamic range, 1 - 1800 rpm would be a nice target.
With luck there are geared steppers that might just fit: https://www.omc-stepperonline.com/nema-23-stepper-motor-bipolar-l-76mm-w-gear-ratio-10-1-spur-gearbox-23hs30-2804s-sg10 (https://www.omc-stepperonline.com/nema-23-stepper-motor-bipolar-l-76mm-w-gear-ratio-10-1-spur-gearbox-23hs30-2804s-sg10)
On top, they also make a controller https://www.omc-stepperonline.com/integrated-stepper-motor-controller-1-5-4a-12-40vdc-for-nema-17-23-24-stepper-motor-isc04 (https://www.omc-stepperonline.com/integrated-stepper-motor-controller-1-5-4a-12-40vdc-for-nema-17-23-24-stepper-motor-isc04) which can directly be controlled via a potentiometer, so I wouldn't have to design a pulse generator.
Title: Re: Brushed DC motor closed loop speed control
Post by: Picuino on June 10, 2024, 05:33:37 pm
I think it's better to keep the DC motor. It's going to have more torque throughout the speed range and it's going to be more powerful for the same size.

Plus you don't need precise control of POSITION, just SPEED, which is much simpler and can be easily achieved with a DC motor with supply current compensation.
Title: Re: Brushed DC motor closed loop speed control
Post by: Picuino on June 10, 2024, 05:43:39 pm
That motor has approximately 3.7 ohms of armature resistance (you can measure it with a multimeter and tell us if this is the case).

Therefore, you only have to add 3.7 volts in the power supply for each ampere consumed, and the motor will run at constant speed.
Title: Re: Brushed DC motor closed loop speed control
Post by: Martinn on June 10, 2024, 07:06:40 pm
That motor has approximately 3.7 ohms of armature resistance (you can measure it with a multimeter and tell us if this is the case).

Therefore, you only have to add 3.7 volts in the power supply for each ampere consumed, and the motor will run at constant speed.
Multimeter says 4.3 ohms.
But would you think that you can reliably run the motor at say 10 rpm? It is rated at 1800 rpm at 24 V, which would give 130 mV @ 10 rpm. I doubt you can control the voltage precisely enough to get that dynamic range.
What dynamic range do you think is realistic for a brushed motor? I am only aware of this for capstan drives on tape decks (or maybe turntables), which are constant speed.

You are right that with a stepper it is more difficult to retain high speed at high rpm. To some degree one can counter that with a low inductance motor and a high supply voltage. Ideally I'd keep the existing 24 V supply though. The geared motor I linked earlier probably won't be able to achieve the necessary speed (like 300 rpm), more likely it would be direct drive with a NEMA24 like this https://www.omc-stepperonline.com/s-series-nema-24-bipolar-1-8deg-4-2nm-594-77oz-in-4-2a-60x60x100mm-4-wires-24hs40-4204s (https://www.omc-stepperonline.com/s-series-nema-24-bipolar-1-8deg-4-2nm-594-77oz-in-4-2a-60x60x100mm-4-wires-24hs40-4204s)

Main advantage of a stepper would be that it is relatively easy to achieve arbitrarily slow speeds. With my last mill powerfeed
https://forum.zerspanungsbude.net/viewtopic.php?t=50489#p607066 (https://forum.zerspanungsbude.net/viewtopic.php?t=50489#p607066)
i settled for a speed resolution of 0.01 mm/s, which required a 64 bit position accumulator in the step generation, but was otherwise straightforward. The total effort I put in this design was however gigantic, and this time I am aiming for something simpler.

Title: Re: Brushed DC motor closed loop speed control
Post by: Picuino on June 10, 2024, 07:29:49 pm
I don't know the limits of current compensated control, but properly adjusted it can achieve relatively slow constant speeds. In reality the motor will be supplied with higher voltage than 0.13V, because the voltage will increase with the supply current. You can test it with a simple analog control.

According to my tests, the back-emf control can indeed achieve those speeds without problem (from 10 rpm, one revolution every 6 seconds, up to 1800 rpm).
Title: Re: Brushed DC motor closed loop speed control
Post by: Picuino on June 10, 2024, 07:52:09 pm
You can try this.


EDIT:
10k Pot = Speed control
1k Pot = Current feedback control

Multimeter says 4.3 ohms.
Is the resistance of the cables already subtracted?
Title: Re: Brushed DC motor closed loop speed control
Post by: Doctorandus_P on June 10, 2024, 08:40:29 pm
You can try this.

I am guessing something similar is already implemented.
There are a few power resistors near the FET and diode. LM434 has 4 opamps and I see two adjustment pots.

Maybe you can improve it by changing the pots, but start with some reverse engineering to figure out what the potentiometer do.
Title: Re: Brushed DC motor closed loop speed control
Post by: Zero999 on June 11, 2024, 07:54:53 am
You can try this.


EDIT:
Multimeter says 4.3 ohms.
Is the resistance of the cables already subtracted?
Using an N-channel source follower like that will require a 5V or more higher voltage power supply to the op-amp, than the motor. It also appears to be a linear controller, which will dissipate a lot of power.
Title: Re: Brushed DC motor closed loop speed control
Post by: Picuino on June 11, 2024, 08:56:26 am
Digital version.
Title: Re: Brushed DC motor closed loop speed control
Post by: Martinn on June 12, 2024, 04:52:25 pm
I am guessing something similar is already implemented.
Here we go.
A bit messy, not sure if 100% accurate, but the overall principle is clear.
Opamp 3 is a triangle generator, 420 Hz.
OP 1 is a comparator, generating a PWM based on the velocity pot.
Power FET has a 82 mR shunt for current sensing.
OP2  compares the current sense value and triggers the SCR, which displays "overload" and clamps the PWM setpoint to 0.
The OFF switch also sets the PWM setpoint to 0 and resets the SCR

And now it comes, Opamp 4 feeds the amplified current signal back to the PWM setpoint (via the 100 k resistor between output 4 and + input 1).

If you hold the shaft (which is pretty difficult without mill attached to it), you can provoke a slight current rise.

But the problem is the poor low speed performance. The motor starts to rotate at 5% PWM DC, but torque is pretty low and you can easily stall it by hand. PWM then rises with in a few seconds to 6% where you need a firmer grasp, but would still not advance the mill table. Only when you advance the speed knob further the motor turns with reasonable torque, but then it is far too fast for milling.
Not sure if this can be tuned to acceptable performance. Obvious start would be the 100 k current compensating resistor, I'll try to reduce that and see how this goes. What worries me a bit is that this circuit is peppered with filter caps. Obviously they are fitghting PWM noise, but still it looks a bit excessive.
EDIT changing this resistor did not help much...
Title: Re: Brushed DC motor closed loop speed control
Post by: Martinn on June 12, 2024, 04:57:11 pm
You can try this.

Multimeter says 4.3 ohms.
Is the resistance of the cables already subtracted?
Thanks! Looks interesting. I wonder if the opamp feedback (10k) could be from the motor instead of the FET gate? This way the opamp would remove the GS threshold.
My parts bin should be sufficient to try this out...

Resistance is with cables. But I would expect the motor resistance anyway to vary significantly depending on rotor position.
Title: Re: Brushed DC motor closed loop speed control
Post by: Picuino on June 12, 2024, 05:15:13 pm
The problem with the circuit you posted earlier is that the current sense resistor is only connected for a very short time at slow speeds.
Therefore you are not really sensing current, but current multiplied by duty cycle.
For it to work properly you have to place the resistor on the positive side of the supply or sense the peak current.

That is the reason for using a P mosfet in the digital circuit with PWM that I posted earlier.


EDIT:
In addition, the sensed current should not be filtered too much, because then the control loop does not have enough speed to be able to respond to sudden current changes / motor torque changes.

With a good control circuit, it should work much better.
Title: Re: Brushed DC motor closed loop speed control
Post by: Picuino on June 12, 2024, 05:22:03 pm

Thanks! Looks interesting. I wonder if the opamp feedback (10k) could be from the motor instead of the FET gate? This way the opamp would remove the GS threshold.
My parts bin should be sufficient to try this out...

Yes, it could work better.
Anyway the FET resistance can be added to the motor resistance and compensated with the 1K potentiometer.
Title: Re: Brushed DC motor closed loop speed control
Post by: Martinn on June 12, 2024, 06:39:48 pm
A quick try: Looks great so far, FET overheats of course, will need to find heatsink.
Out of time for today, will try tomorrow.
EDIT:
Have a 2N3055 on a heatsink still lying around. Maybe try to make a darlington out of that and replace the FET.
Title: Re: Brushed DC motor closed loop speed control
Post by: Picuino on June 12, 2024, 07:01:03 pm
At low speeds you can decrease the power supply voltage to 12V, reducing the heat.
Title: Re: Brushed DC motor closed loop speed control
Post by: Doctorandus_P on June 12, 2024, 11:51:52 pm
I was a bit bored, and I took this opportunity to be a bit silly.

First I took your schematic, imported it in KiCad put resistors & capacitors and such on it. and the result looks like:

[attachimg=3]

Then I did the same with the PCB, which looks like:

[attachimg=1]

I have not verified it completely, partially because I can't see all tracks on the PCB, but it looks like your reverse engineering effort was quite right. I've also attached the whole KiCad project. In the PCB editor, images can be turned off, to "clean up" the view of the PCB. As far as I know you can't do that in the schematic, but you can of course delete the old imported hand drawn image.

Do note it's not "complete" (nor verified) I have not added the speed poteniometer, I only just now saw that is only on the "other" schematic.

[attachmini=2]
Title: Re: Brushed DC motor closed loop speed control
Post by: Doctorandus_P on June 13, 2024, 12:12:22 am
A quick try: Looks great so far, FET overheats of course, will need to find heatsink.

From the drawing in the picture. It looks like the posted schematic from Picuino.

If you use an P-channel FET instead of an N channel, and add the sawtooth (or triangle) generator as in the original schematic, heating of the FET is minimized. You will be back to a PWM circuit, but now with better feedback.
If you do this, then also add a freewheel diode over both the motor and the current shunt, so the current gets recirculated though the shunt when the PWM switch is "off".

With real speed measurement and PI(d) control you can get much better regulation, but if this is good enough, then it's good enough.  :P
Title: Re: Brushed DC motor closed loop speed control
Post by: Martinn on June 14, 2024, 04:27:05 pm
OK, so I put the analog version together:
[attach=1]
I also went into the workshop and made a "knob" for the motor so I can brake this by hand. Easy to make if you have a broach set for the keyway and a hydraulic press...
Performance is quite good, decent regulation also with slow rpm. However, there's a problem.
This screenshot shows the output of the current sense amplifier with the feedback removed (1k pot = open):
[attachimg=2]
You can see that in this constant voltage mode there is significant current ripple. Probably due to cogging of the motor. When you close the loop, you amplify those pulses and feed them back into the drive voltage:
[attach=3]
(same measurement point, current sense). As you decrease the 1k pot (increase current feedback), this gets worse to a point where the motor almost oscillates. Turning it down reduces the stiffness.
So although this works amazingly well, I think it is not a solution for milling feed speeds.

EDIT: Experimenting with embedding images. What do you think of embedding the full size or thumbnain scope plot? I gues the full size is hefty, on my 4k monitor probably OK, but smaller ones?
Title: Re: Brushed DC motor closed loop speed control
Post by: Martinn on June 14, 2024, 04:30:24 pm
I was a bit bored, and I took this opportunity to be a bit silly.
Wow! You have a serious capacity overflow! Can I contact you when I have a design that needs getting done?

I also thought about putting it in KiCad. But as I have the real thing, I'm not sure there is a point. For me a pencil sketch is sufficient.
Title: Re: Brushed DC motor closed loop speed control
Post by: Martinn on June 14, 2024, 04:47:10 pm
The problem with the circuit you posted earlier is that the current sense resistor is only connected for a very short time at slow speeds.
Therefore you are not really sensing current, but current multiplied by duty cycle.
For it to work properly you have to place the resistor on the positive side of the supply or sense the peak current.
I also thought I could put a regular current sense amplifier inside the motor loop and feed that back into the circuit. But considering the heavy filtering they implemented (several seconds time constants), I doubt the final result would be worth the effort. My guess would be that they had problems with stability and noise and added so many farads that everything was quited down. So if you'd try to increase the control speed, probably all those issues pop up again.

With real speed measurement and PI(d) control you can get much better regulation, but if this is good enough, then it's good enough.  :P

Adding a high resolution feedback (magnet and rotaty encoder sensor) was my first thought also. However, with that much torque ripple, it's going to be difficult to maintain constant speed even then because the velocity control loop gain will vary accordingly. Funnily enough I just recently had the same problem on a customer project (small BLDC) where the torque ripple was leading to excessive set current fluctuations as the velocity PD controller tries to compensate. This is not the first time I see this issue, but for now I have gotton away by just having enough bandwidth. However, if you have the absolute rotor position, you could in theory observe the torque ripple from the current and compensate for that dynamically (using a lookup table), but I have not yet tried that.

Not sure what to do right now. Maybe I would need to try it on the mill - or try to put a stepper on there.


Title: Re: Brushed DC motor closed loop speed control
Post by: Picuino on June 14, 2024, 06:17:51 pm
As you decrease the 1k pot (increase current feedback), this gets worse to a point where the motor almost oscillates. Turning it down reduces the stiffness.
So although this works amazingly well, I think it is not a solution for milling feed speeds.

Note that the feedback potentiometer has only one ideal working point.
If you increase the feedback further, the motor will accelerate as you brake it (it will not run at constant speed and may eventually oscillate).
If you reduce the feedback, the motor will slow down as you brake it (and will not run at constant speed).

I don't see a problem with oscillation. If you prefer it not to be there, you can try adding a small capacitor (around 1uF) in parallel to the 100k resistor of the op amp, but it will detract from the speed of the control loop.
Title: Re: Brushed DC motor closed loop speed control
Post by: Picuino on June 14, 2024, 06:34:40 pm
If you try to turn the motor with your hand without power supply, you can see the motor have different torque at different positions. This is the ripple that the control try to compensate at low speed.
Title: Re: Brushed DC motor closed loop speed control
Post by: Doctorandus_P on June 15, 2024, 01:33:39 am
For the KiCad thing...
I'm not very organized, but things like this happen sometimes when enough planets get aligned.
It was a part of KiCad I do not use much, combined with making a bit of a case of what you can do with KiCad.
It's a bit of a shame this motor controller is not much good and not really worth replicating. Maybe someone can use it as a base to make a better motor controller...

You got an easily measurable torque ripple on your scope. My guess is that it's not cogging torque as Picuino seems to suggest, but the commutator switching between one and two motor windings, which changes the included magnetic field in the rotor. If so, then the number of pulses per revolution should be twice as big as the number of contacts on your commutator.

If you take this signal, and also it's filtered average, and put it through a comparator, it looks like a very good speed feedback signal, and can be the basis for a PI(d) regulator, either digital or with a few opamps.

I'm guessing there is not so much speed ripple nor torque ripple, but it's just a motor artifact, and it's frequency is a lot higher then the response time of the motor. But attempting to use it as a feedback signal could unleash some dragons that are hard to tame.

Not sure what to do right now. Maybe I would need to try it on the mill - or try to put a stepper on there.

You can have a look in how many changes you need to your original PCB to fully include the shunt resistor in the motor current recirculation path (Inside the freewheel diode). Leave the sawtooth generator so you still use PWM instead of linear control and swap the N-channel FET for a P-channel FET. (Beware of maximum gate voltage) Because the FET inverts, you may have to swap opamp inputs too.

With these changes your motor controller may react a lot better then it does now, and it's probably "good enough". It also is not much work, so seems worth a try. Especially because you won't have to fiddle with mounting of the motor etc.

The trouble with stepper motors is they have relatively low torque. a Long (112mm) Nema23 sized motor with controller costs around EUR80. Closed loop steppers run a lot quieter and cooler and have a higher peak torque then open loop controllers, and you buy them as a set, with no need for tuning as with servo motors. Price difference between open and closed loop steppers is so small that I wonder why open loop steppers are still being made and sold (Probably only bought by people who don't know how big the difference in usability is).
With a stepper motor, I don't know if you would need an extra transmission to get enough torque. You can buy steppers with bolted on gearbox for a reasonable price, and Nema34 sized motor is also cheaper then what you paid now for your auto-feed.  Maybe it's worth considering to sell the box you have now unmodified, and build something new based on a stepper motor.
Title: Re: Brushed DC motor closed loop speed control
Post by: Martinn on June 15, 2024, 08:44:33 am
Did some more measurements.
Lowest reasonable feeds for a milling machine are in the order of 5-10 mm/min. Given a 4 mm leadscrew pitch that results in about 2.5 rpm (about 1% of the max 240 rpm at the output of the gearbox).
With the current setup I can get down to about 16 rpm, which is the lowest value that is reasonably stable. This gives 64 mm/min, which is well above of what I'd like to see (around 10 mm/min). BTW I added lowpass filtering in the current path, which gets rid of the oscillations.
I think it's time to call it a day. While the linear approach with optimized I*R compensation does give a significiant improvement, I don't think it is worth the effort putting it onto the machine. There's still a 7 V dropout at full speed (24 V supply is fixed), which I'd solve probably by adding a switch FET in parallel, kicking in at high speed settings. No speed control is necessary at max feed, just give the motor the full 24 V. Or maybe replace the N channel linear FET with a BJT darlington, reducing the dropout somewhat.

Regarding stepper motors: Currently I am eyeballing a NEMA 24 closed loop motor, like
https://www.omc-stepperonline.com/closed-loop-stepper-motor?mfp=184-frame-size-mm (https://www.omc-stepperonline.com/closed-loop-stepper-motor?mfp=184-frame-size-mm)[Nema%2024%20(60%20x%2060)]
which might have enough torque to drive the spindle directly.

Title: Re: Brushed DC motor closed loop speed control
Post by: fourtytwo42 on June 15, 2024, 02:25:25 pm
It's a shame to give up but I know the feeling!

Here is a scope shot of the back emf of a small motor during the PWM off time, a high side switch is used to make the emf ground referenced for simplicity.
A recovery time is required after the PWM switches off to allow the field to collapse and you can see that where the emf is negative.
I used an MPU both to generate the PWM, measure the emf & do the control loop, I think that kind of control would be hard in pure analogue.

That's a very nice 14? pole motor, I was working with just 5 poles although your target speed of just 19rpm (~4 poles/sec) sounds a bit hard if I am reading it right ?
Current is proportional to torque, back emf is a much better indicator of velocity, so if you want constant speed back emf is the way to go if you have an MPU to hand  :)

On steppers I did a fairly large (14") telescope drive with them unfortunately the gearbox & motor size was pre-ordained and I had terrible trouble reaching the maximum speed required in the slew even using velocity profiles without misstepping, absolute position accuracy was required without feedback  :palm: I wished I had a servo motor with optical feedback for the job  ;)

My final radical suggestion for you is get a motor with twice the reduction ratio and drive it with twice the voltage during the slew (which I assume is short duration and low duty cycle)  :wtf:
Title: Re: Brushed DC motor closed loop speed control
Post by: IanB on June 15, 2024, 03:01:38 pm
Lowest reasonable feeds for a milling machine are in the order of 5-10 mm/min. Given a 4 mm leadscrew pitch that results in about 2.5 rpm (about 1% of the max 240 rpm at the output of the gearbox).

At the other end of the scale, is 1000 mm/min a reasonable feed rate? Maybe some mechanical gearing would be appropriate to match the motor speed to the desired range of feed rates?

Title: Re: Brushed DC motor closed loop speed control
Post by: Picuino on June 15, 2024, 03:13:41 pm
Maintaining a speed of 1% of nominal speed should not be too much difficult.
I don't understand why the control you tested doesn't work well.
Of course the analog version dissipates too much heat, but it should serve as a proof of concept to keep the motor running at 1% of nominal speed with no variation under load.

EDIT:
You can tune the feedback (1k pot) by checking with the scope that the ripple current of the feedback loop maintains a constant frequency despite braking the motor.

I am setting up a version of the digital control loop. I want to see for myself the results with this type of control.
I just need to solder and connect the differential amplifier MCP6N11-10 to sense the current.
Title: Re: Brushed DC motor closed loop speed control
Post by: Picuino on June 15, 2024, 06:50:26 pm
I already have the PCB with correction included (a ground wire was missing in the previous version).


EDIT:
I change the PWM output to D10, associated to Timer1 which has higher resolution.
Title: Re: Brushed DC motor closed loop speed control
Post by: Doctorandus_P on June 15, 2024, 10:02:29 pm
I'd say that 64mm/min is slow enough. It's similar to this video:

https://www.youtube.com/watch?v=QSzaZn7g-nQ (https://www.youtube.com/watch?v=QSzaZn7g-nQ)

You've already got the power feed, your speed controller is simple to finish with this electronic improvement, and unless you want to sell it (and build a new one) there is not much reason to simply bolt it to your machine and start using it. This gives you some real life experience, which gives you a better feeling of what you really need. You can always revisit the project later when you find an improvement is needed.

Also, if you build something with a stepper motor, I suggest you put in some gears or a timing belt transmission, even if you want to start with a 1:1 transmission ratio.
First, it gives you more room for motor placement, and you can keep the handwheel in place. Second, If you already got a transmission, it's easy to adjust if you decide later you want more speed or more torque.

Title: Re: Brushed DC motor closed loop speed control
Post by: Picuino on June 16, 2024, 11:07:48 am
Testing the board.
Switching ON takes approximately 200ns to 300ns. Sufficient.

Switching OFF is much worse and takes about 6us. This is due to how long it takes the NPN to switch after being saturated and how long it takes the mosfet to switch off with a simple 1000 Ohm resistor.
I will have to be careful to use a low switching frequency so that the transistor does not get too hot.
Title: Re: Brushed DC motor closed loop speed control
Post by: Martinn on June 16, 2024, 04:46:06 pm
I'd say that 64mm/min is slow enough. It's similar to this video:
I have not been able to find out what the leadscrew pitch of the SX3L is, might be 2 mm instead of the 4 mm of my SX4. The feed shown in the video is OK for a roughing cut, but for a finishing cut with a flycutter one would definitely go slower. Normally on a hobby mill you don't want to push it; better take it easy than to break the cutter and ruin the part.

At the other end of the scale, is 1000 mm/min a reasonable feed rate? Maybe some mechanical gearing would be appropriate to match the motor speed to the desired range of feed rates?
Max feed while milling is mostly not relevant (we are not doing HSC here, although Stefan suggests a HSC like strategy for manual milling also https://www.youtube.com/watch?v=WzT4HJalwAs (https://www.youtube.com/watch?v=WzT4HJalwAs))
It's more about moving the table quickly. The SX4 has 450 mm X travel, at the 240 rpm of the powerfeed, this gives 960 mm/min, so it takes roughly half a minute to traverse the table fully. I would not mind it to be faster, but with powerfeeds there is always a chance of crashing into things, milling or not.

You've already got the power feed, your speed controller is simple to finish with this electronic improvement, and unless you want to sell it (and build a new one) there is not much reason to simply bolt it to your machine and start using it.
I would sell it right away. But the SX4 is relatively rare, so the probability of finding someone who pays a decent price (after possibly reading our thread) is near zero. At least it has a halfway decent clutch mechanism for the feed unit, that I can reuse even if I switch to a stepper.

Regarding the improved circuit by Picuino: I have it on the breadboard only. For actually using it, I'd have to do something about the 7 V dropout, make a halfway decent PCB, find a heatsink that fits and put it all together.
It would be definitely an improvement over the current unit, but I don't see a sufficient effort vs. reward.

BTW when I said 16 rpm that is the output after the 1:7.5 gearbox. At the motor this would be 120 rpm, 7% of its rated speed.

Regarding stepper and direct drive: Given the available choices of stepper motors, I don't think it is necessary to add a belt transmission. The DC motor nameplate says 0.186 Nm which is 1.4 Nm after the gearbox. I measured the torque you need to break the axis loose at about 1.2 Nm. Given the stiffness of this (still as far as mills go) rather lightweight milling machine, not much more will be necessary for milling.
NEMA24 closed loop steppers https://www.omc-stepperonline.com/closed-loop-stepper-motor?mfp=184-frame-size-mm (https://www.omc-stepperonline.com/closed-loop-stepper-motor?mfp=184-frame-size-mm)[Nema%2024%20(60%20x%2060)] should do that, although not with a huge margin. I'd start with one of those (fortunately, the x leadscrew axis is 58 mm below the table surface, so there is plenty of room for a NEMA24 motor (30 mm from axis to housing).

It's a shame to give up but I know the feeling!
I'm not giving up. If this would be a paid customer project, the I'd happily invest in more advanced control strategies, count commutation pulses and put all into some STM32 project.
But as all I want now is get the mill going, I try to assess which option gives me the best return for my invested spare time. And as I am already considering putting a stepper motor on the Z axis (it is currently a reversible 20 rpm 60 W geared AC induction motor), which would mean I would already have 90% of the necessary infrastructure (power supply, housing, pulse generator), thinking of a stepper for X seems a more powerful option. It will enable arbitrarily low feeds together with much faster (dangerous!) jog feeds.

But hey, @Doctorandus_P, as you seem to have abundant free time - if you come up with a design I can order as assembled PCB from JLCPCB (I can help with selecting the components, if you find that boring), I'll order it and see how it works on my machine!
Title: Re: Brushed DC motor closed loop speed control
Post by: H.O on June 16, 2024, 05:53:56 pm
I have a couple of Advanced Motion Control 30A8 servo drives (datasheet link (https://servo.a-m-c.com/hubfs/Datasheets/Servo-Drives/AxCent/AMC_Datasheet_30A8.pdf)) collecting dust.
Among the different operating modes are IxR which is what you've been trying here. For fine velocity control nothing will beat tachometer or high resolution encoder feedback but a well tuned IxR loop should give you decent performance.

Cover the shipping cost and I'll send you one. Drop me a PM if interested.     
Title: Re: Brushed DC motor closed loop speed control
Post by: Doctorandus_P on June 16, 2024, 09:27:45 pm
I've reached about the limit of the time I want to spend on someone else's project.  :P

What you can do, is integrate your breadboard IR compensation into the the project I posted, maybe add a uC (encoder, PI, whatever) and order that.

But if you want to put a stepper on both X and Z axis, I'd say, also put one on the Y-axis and you're pretty close to CNC. GRBL is a simple DIY controller, and the GRBL forks have mostly merged together in the GRBLhal project. Smoothie board may also be an option, but it's been years since I last looked into that. LinuxCNC has more options, but is also more complex.
Title: Re: Brushed DC motor closed loop speed control
Post by: jbb on June 17, 2024, 12:36:55 am
I think, you can improve the MOSFET turn off quite a bit by adding 3 more pieces as shown.

When drive transistor Q2 turns on, the MOSFET is turned on via D2 and R3.

When drive transistor Q2 turns off:
- D2 is reverse biased
- Q3 is switched on by R2
- MOSFET is turned off by Q3 and R8

This allows for a faster turn off.
 
Title: Re: Brushed DC motor closed loop speed control
Post by: Martinn on June 17, 2024, 10:11:36 am
But if you want to put a stepper on both X and Z axis, I'd say, also put one on the Y-axis and you're pretty close to CNC.
That's an entirely different can of worms, more a barrel of worms actually. Certainly a multi year project given the spare time I have and not something I am looking for right now.
Title: Re: Brushed DC motor closed loop speed control
Post by: Picuino on June 17, 2024, 11:05:32 am
After some testing with the digital version of the PWM control with current compensation, although not yet optimized, the performance is not as good as I expected.
The motor oscillates at low rpm when I try to brake it.

I thought the result was going to be better because the results I got some time ago with the back-emf control were very good. In this case they are not.
The back-emf control is really very good, but the digital control with current compensation is not so good.


EDIT:
I am going to try to add back-emf control to the circuit I already have set up to see if I can make an accurate control at low speeds. In the other tests I did, I used an H-bridge for back-emf control. In this case I am going to use a simple p-mosfet for control in only one direction of rotation. I hope the control will improve.
Title: Re: Brushed DC motor closed loop speed control
Post by: Doctorandus_P on June 17, 2024, 02:37:38 pm
EDIT:
I am going to try to add back-emf control to the circuit I already have set up to see if I can make an accurate control at low speeds.

The IR compensation already has gain, so you probably have to reduce gain of back EMF loop. I'm not sure whether it makes sense to do both of these. You're adding complexity but without a big chance for significant improvements. Either of these systems work, but they have similar limitations. If you want "proper" speed control you will need better speed feedback in the form of for example an encoder.

On top of that, results will be dependent on the exact motor used. It's not clear to me how these experiments from Picuino are helping Martinn.
Title: Re: Brushed DC motor closed loop speed control
Post by: Picuino on June 21, 2024, 03:31:32 pm
I have changed the schematic and soldered on the PCB two resistors to measure the back-EMF.

I have also added a capacitor to the base of the NPN transistor with the intention of accelerating its turn-off (I couldn't do much more in the little space I have left on the PCB).

Now it's time to make the program and test if the back-EMF control is good enough.
Title: Re: Brushed DC motor closed loop speed control
Post by: Picuino on June 21, 2024, 03:41:06 pm
By now the switch-off of the mosfet has improved a lot. It has been reduced from 6us to 1.5us. It is still high, but it is not going to cause worrying heating of the mosfet.
Title: Re: Brushed DC motor closed loop speed control
Post by: Picuino on June 22, 2024, 10:20:13 am
With the back-emf control the motor rotates precisely at the reference speed, even at speeds below 1% of the nominal speed.

I post also the program used with Arduino (with PID control not well tuned).

I am not using current measurement, nor do I need to.


Code: [Select]
const int PIN_PWM = 10;
const int PIN_CURRENT = A0;
const int PIN_EMF = A1;
const int PWM_MAX = 2000;

volatile int motor_speed;
volatile int motor_voltage;
volatile char timer1_compb;

int emf;
int error = 0;
int error_old = 0;
int PID_control = 0;
int PID_proportional = 0;
int PID_derivative = 0;
int PID_integral = 0;
int PID_kp = 100;
int PID_kd = PID_kp / 2;
int PID_ki = PID_kp / 2;

void setup() {
  pinMode(PIN_PWM, OUTPUT);
  timer1_setup();
}

void loop() {
   motor_voltage = 100;
   motor_speed = 10;
   
   while(1) {
      delay(18);

      for(timer1_compb = 0; timer1_compb == 0;); // Wait for end of PWM pulse
      DDRB &= ~(1 << (PIN_PWM-8));   // Set PIN PWM as input
      delayMicroseconds(1500);      // Wait disipate motor current
      emf = analogRead(PIN_EMF);    // Read back-emf voltage

      // PID
      error = motor_speed - emf;
      PID_proportional = PID_kp * error;
      PID_derivative = PID_kd * (error - error_old);
      PID_integral += PID_ki * error;
      if (PID_integral < -PWM_MAX)
         PID_integral = -PWM_MAX;
      if (PID_integral > PWM_MAX*16)
         PID_integral = PWM_MAX*16;
      motor_voltage = (PID_proportional + PID_integral + PID_derivative)/16;
      if (motor_voltage < 0)
         motor_voltage = 0;
      if (motor_voltage >= PWM_MAX)
         motor_voltage = PWM_MAX-1;
      error_old = error;
         
      OCR1B = motor_voltage;
     
      // Activate timer1 PWM
      timer1_setup();
   }
}


void timer1_setup(void) {
  // Fast PWM top at OCR2A
  TCCR1A = bit (WGM10) | bit (WGM11) | bit (COM1B1); // fast PWM, clear OC1B on compare
  TCCR1B = bit (WGM12) | bit (WGM13) | 0b010;   // fast PWM, prescaler = 4
  TIMSK1 |= 0b00000100;       // Set OCIE1B to 1 -> compare match
  OCR1A = PWM_MAX - 1;
  TCNT1 = OCR1A - 1;
  DDRB |= (1 << (PIN_PWM-8));  // Set PIN PWM as output
}


ISR(TIMER1_COMPB_vect) {
   timer1_compb = 1;  // End of PWM positive Pulse
}
Title: Re: Brushed DC motor closed loop speed control
Post by: Picuino on June 22, 2024, 06:18:57 pm
With a program that displays various internal values, the operation of the controller can be better checked better:

Code: [Select]
const int PIN_PWM = 10;
const int PIN_CURRENT = A0;
const int PIN_EMF = A1;
const int PWM_MAX = 2000;

volatile int motor_speed;
volatile int motor_voltage;
volatile char timer1_compb;

int emf;
int error = 0;
int error_old = 0;
int PID_control = 0;
int PID_proportional = 0;
int PID_derivative = 0;
int PID_integral = 0;
int PID_kp = 10;
int PID_ki = 5;
int PID_kd = 10;

void setup() {
  Serial.begin(115200);
  pinMode(PIN_PWM, OUTPUT);
  timer1_setup();
}

void loop() {
  motor_voltage = 100;
  motor_speed = 30;

  while (1) {
    Serial.print(emf);
    Serial.print("\t");
    Serial.print(PID_proportional);
    Serial.print("\t");
    Serial.print(PID_integral);
    Serial.print("\t");
    Serial.print(PID_derivative);
    Serial.println();
    delay(10);

    for (timer1_compb = 0; timer1_compb == 0;); // Wait for end of PWM pulse
    DDRB &= ~(1 << (PIN_PWM - 8)); // Set PIN PWM as input
    delayMicroseconds(1500);      // Wait disipate motor current
    emf = analogRead(PIN_EMF);    // Read back-emf voltage

    // PID
    error = motor_speed - emf;
    PID_proportional = PID_kp * error;
    PID_derivative = PID_kd * (error - error_old);
    PID_integral += PID_ki * error;
    if (PID_integral < -PWM_MAX * 8)
      PID_integral = -PWM_MAX * 8;
    if (PID_integral > PWM_MAX * 8)
      PID_integral = PWM_MAX * 8;
    motor_voltage = (PID_proportional + PID_integral + PID_derivative) / 8;
    if (motor_voltage < 0)
      motor_voltage = 0;
    if (motor_voltage >= PWM_MAX)
      motor_voltage = PWM_MAX - 1;
    error_old = error;

    OCR1B = motor_voltage;

    // Activate timer1 PWM
    timer1_setup();
  }
}


void timer1_setup(void) {
  // Fast PWM top at OCR2A
  TCCR1A = bit (WGM10) | bit (WGM11) | bit (COM1B1); // fast PWM, clear OC1B on compare
  TCCR1B = bit (WGM12) | bit (WGM13) | 0b010;   // fast PWM, prescaler = 4
  TIMSK1 |= 0b00000100;       // Set OCIE1B to 1 -> compare match
  OCR1A = PWM_MAX - 1;
  TCNT1 = OCR1A - 1;
  DDRB |= (1 << (PIN_PWM - 8)); // Set PIN PWM as output
}


ISR(TIMER1_COMPB_vect) {
  timer1_compb = 1;  // End of PWM positive Pulse
}

I have checked that there is a small speed error. The more the motor shaft is braked, the faster it turns.  It should be compensated so that higher values of the integral component reduce the measured back-emf value a little. Since I don't have any actual shaft speed meter or any method to accurately brake the shaft, I leave that job to someone else who wants to make further progress on the subject.

The conclusion I have reached is that back-emf control of speed is superior to current compensation control.

It remains to be checked how the two controls behave acting simultaneously, because at low revolutions the back-emf control has a certain oscillation that it would be desirable to eliminate and possibly it can be achieved with a combined control.
Title: Re: Brushed DC motor closed loop speed control
Post by: Doctorandus_P on June 24, 2024, 04:24:30 pm
Oscillations are likely due to a not very well adjusted PID controller. I don't know how much experience you have with those, but they can be finicky to learn to set them up properly. Another possibility is that you simply do not have enough resolution to measure the back EMF properly at low speed. I guess it's not going to be (much) better when using both algorithms.

It is useful to measure the motor current. You can use it for overload protection (also note that short overloads (up to about 200% or 300% of the nominal motor current) can be acceptable, but the motor will overheat over time. With a uC you can keep track of the motor current, and extrapolate motor temperature (with temperature decay when current is lower) These will be rough estimates, but you can get a lot more torque out of your motor for short durations, compared with a hard cut off with the comparator as soon as the motor current exceeds a limit.

For your software...
I don't like "arduino programming". One thing that looks suspicious is to call timer1_setup() repeatedly inside your while loop. And why have a while(1) loop at all? The silly arduino thing already does this in the loop() function.

I am also not sure whether your detection of the end of the PWM pulse works, and why are you switching the PWM pin to input?

Also very arduino are software delay loops. Any ISR's during such a delay will increase the delay, and this will have an influence on the timing of the PID loops. The I and D parts of a PID are timing sensitive, and timing variation will decrease it's accuracy.

You have measured mosfet switch time (as long as the thing does not get too hot then it's OK) so I assume you have an oscilloscope. You can use an arduino pin, set it to an output, set it when an ADC conversion starts, and clear it again when it finishes. This lets you verify the sampling with the back EMF voltage so you can see whether you are measuring a stable back EMF voltage. It's a simple but useful debugging trick.

I started programming long before arduino, and would use another approach.
1. Have a timer tick ISR that starts an ADC conversion.
2. When ADC conversion is complete, run the PID loop and start a single PWM pulse.

This guarantees consistent timing for an accurate PID loop, and it leaves your main loop completely free to do regular tasks such as handling user I/O. It is more work to set up, as you need to know more details about your microcontroller, but once set up properly the software is not much more complex.
Title: Re: Brushed DC motor closed loop speed control
Post by: Doctorandus_P on June 24, 2024, 05:10:54 pm
Another Idea:

You can add another voltage "divider" to measure the back EMF, but in this case use a resistor, and either a zener to GND or a skottky diode to Vcc. This gives you a 5x higher resolution to measure low back EMF and may improve PID accuracy for low RPM.

And another thing I forgot to ask:
How low can you go now with your RPM in comparison to your other methods? How far are you from your desired goal?
Title: Re: Brushed DC motor closed loop speed control
Post by: Picuino on June 24, 2024, 06:06:43 pm
Oscillations are likely due to a not very well adjusted PID controller. I don't know how much experience you have with those, but they can be finicky to learn to set them up properly. Another possibility is that you simply do not have enough resolution to measure the back EMF properly at low speed. I guess it's not going to be (much) better when using both algorithms.

I am not sure what the oscillations are due to. The PID I think is fine tuned. I will try to post the results returned by the microcontroller to see if they show anything I am not seeing.



I don't like "arduino programming". One thing that looks suspicious is to call timer1_setup() repeatedly inside your while loop. And why have a while(1) loop at all? The silly arduino thing already does this in the loop() function.
True, but I'm used to creating my own while(1) which usually takes much less time to cycle than the Arduino's loop().


I am also not sure whether your detection of the end of the PWM pulse works, and why are you switching the PWM pin to input?
The interruption starts just when the PWM pulse ends. The interrupt sets a variable which is then sampled by polling in the main loop.

Once the PWM pulse ends, it waits for the motor current to reduce to zero and the back-emf voltage is measured. The PWM duty cycle is recalculated and then Timer1 is reset to start another PWM pulse at this exact moment.


Also very arduino are software delay loops. Any ISR's during such a delay will increase the delay, and this will have an influence on the timing of the PID loops. The I and D parts of a PID are timing sensitive, and timing variation will decrease it's accuracy.
Yes, it is true. I just hope the times don't vary too much from cycle to cycle. The error cannot be very large (I estimate that there can be a maximum error of 5%).


You have measured mosfet switch time (as long as the thing does not get too hot then it's OK) so I assume you have an oscilloscope. You can use an arduino pin, set it to an output, set it when an ADC conversion starts, and clear it again when it finishes. This lets you verify the sampling with the back EMF voltage so you can see whether you are measuring a stable back EMF voltage. It's a simple but useful debugging trick.
I am going to try it.

I started programming long before arduino, and would use another approach.
1. Have a timer tick ISR that starts an ADC conversion.
2. When ADC conversion is complete, run the PID loop and start a single PWM pulse.

This guarantees consistent timing for an accurate PID loop, and it leaves your main loop completely free to do regular tasks such as handling user I/O. It is more work to set up, as you need to know more details about your microcontroller, but once set up properly the software is not much more complex.
For now I was just testing the ability to control the motor with a simple program.
Title: Re: Brushed DC motor closed loop speed control
Post by: Picuino on June 24, 2024, 06:16:35 pm
How low can you go now with your RPM in comparison to your other methods? How far are you from your desired goal?

My goal (actually Martinn's) was to achieve a speed of 1% of nominal with stability.
In a 24-volt motor, which typically has about 12-18 volts back-emf at rated operation, that means maintaining a back-emf voltage of about 150mV.
After the voltage divider this voltage become 27mV, about 5-6 ADC points with ADC reference voltage at 5V.
(If I lower the ADC reference voltage to 1.1 volts, then that would be about 25 ADC points).
Title: Re: Brushed DC motor closed loop speed control
Post by: Picuino on June 24, 2024, 06:47:20 pm
New program more verbose. Cycle time = 10ms:

Code: [Select]
const int PIN_PWM = 10;
const int PIN_CURRENT = A0;
const int PIN_EMF = A1;
const int PWM_MAX = 2000;
const int PWM_SCALE = 4;

volatile int motor_speed;
volatile int motor_voltage;
volatile char timer1_compb;

int emf;
int error = 0;
int error_old = 0;
int PID_control = 0;
int PID_proportional = 0;
int PID_derivative = 0;
int PID_integral = 0;
int PID_kp = 25;
int PID_ki = 10;
int PID_kd = 10;

void setup() {
  Serial.begin(115200);
  pinMode(PIN_PWM, OUTPUT);
  timer1_setup();
}

void loop() {
  motor_voltage = 100;
  motor_speed = 5;

  Serial.print("emf\tP\tI\tD\tmillis\n");
  while (1) {
    Serial.print(emf);
    Serial.print("\t");
    Serial.print(PID_proportional);
    Serial.print("\t");
    Serial.print(PID_integral);
    Serial.print("\t");
    Serial.print(PID_derivative);
    Serial.print("\t");
    Serial.print(millis());
    Serial.println();
    delay(7);

    for (timer1_compb = 0; timer1_compb == 0;); // Wait for end of PWM pulse
    DDRB &= ~(1 << (PIN_PWM - 8)); // Set PIN PWM as input
    delayMicroseconds(1500);      // Wait disipate motor current
    emf = analogRead(PIN_EMF);    // Read back-emf voltage

    // PID
    error = motor_speed - emf;
    PID_proportional = PID_kp * error;
    PID_derivative = PID_kd * (error - error_old);
    PID_integral += PID_ki * error;
    if (PID_integral < -PWM_MAX * PWM_SCALE)
      PID_integral = -PWM_MAX * PWM_SCALE;
    if (PID_integral > PWM_MAX * PWM_SCALE)
      PID_integral = PWM_MAX * PWM_SCALE;
    motor_voltage = (PID_proportional + PID_integral + PID_derivative) / PWM_SCALE;
    if (motor_voltage < 0)
      motor_voltage = 0;
    if (motor_voltage >= PWM_MAX)
      motor_voltage = PWM_MAX - 1;
    error_old = error;

    OCR1B = motor_voltage;

    // Activate timer1 PWM
    timer1_setup();
  }
}


void timer1_setup(void) {
  // Fast PWM top at OCR2A
  TCCR1A = bit (WGM10) | bit (WGM11) | bit (COM1B1); // fast PWM, clear OC1B on compare
  TCCR1B = bit (WGM12) | bit (WGM13) | 0b010;   // fast PWM, prescaler = 4
  TIMSK1 |= 0b00000100;       // Set OCIE1B to 1 -> compare match
  OCR1A = PWM_MAX - 1;
  TCNT1 = OCR1A - 1;
  DDRB |= (1 << (PIN_PWM - 8)); // Set PIN PWM as output
}


ISR(TIMER1_COMPB_vect) {
  timer1_compb = 1;  // End of PWM positive Pulse
}



Output of the program incrementing the brake from nothing to high brake:
Code: [Select]
emf	P	I	D	millis
0 0 0 0 0
0 125 50 50 33
0 125 100 0 43
0 125 150 0 53
0 125 200 0 62
0 125 250 0 71
0 125 300 0 81
2 75 330 -20 91
1 100 370 10 101
1 100 410 0 110
0 125 460 10 120
0 125 510 0 130
1 100 550 -10 140
4 25 560 -30 149
8 -75 530 -40 159
10 -125 480 -20 168
7 -50 460 30 179
9 -100 420 -20 188
13 -200 340 -40 198
14 -225 250 -10 207
8 -75 220 60 217
5 0 220 30 227
0 125 270 50 236
1 100 310 -10 246
0 125 360 10 256
0 125 410 0 266
0 125 460 0 275
2 75 490 -20 285
5 0 490 -30 294
8 -75 460 -30 304
10 -125 410 -20 314
8 -75 380 20 323
11 -150 320 -30 333
14 -225 230 -30 343
9 -100 190 50 353
4 25 200 50 362
2 75 230 20 371
1 100 270 10 381
0 125 320 10 391
0 125 370 0 401
0 125 420 0 410
0 125 470 0 420
2 75 500 -20 430
4 25 510 -20 440
8 -75 480 -40 449
10 -125 430 -20 459
8 -75 400 20 468
10 -125 350 -20 479
14 -225 260 -40 488
13 -200 180 10 497
7 -50 160 60 507
2 75 190 50 517
1 100 230 10 527
0 125 280 10 536
0 125 330 0 546
0 125 380 0 556
0 125 430 0 565
2 75 460 -20 575
5 0 460 -30 584
8 -75 430 -30 594
10 -125 380 -20 604
8 -75 350 20 614
11 -150 290 -30 623
13 -200 210 -20 633
8 -75 180 50 643
4 25 190 40 652
3 50 210 10 662
0 125 260 30 671
0 125 310 0 681
0 125 360 0 691
0 125 410 0 701
0 125 460 0 710
0 125 510 0 720
3 50 530 -30 730
6 -25 520 -30 740
10 -125 470 -40 749
10 -125 420 0 759
7 -50 400 30 769
11 -150 340 -40 779
15 -250 240 -40 788
11 -150 180 40 797
6 -25 170 50 807
1 100 210 50 817
0 125 260 10 827
0 125 310 0 836
0 125 360 0 845
0 125 410 0 856
0 125 460 0 865
2 75 490 -20 875
2 75 520 0 884
8 -75 490 -60 894
9 -100 450 -10 904
10 -125 400 -10 914
9 -100 360 10 923
14 -225 270 -50 933
11 -150 210 30 943
4 25 220 70 952
3 50 240 10 962
0 125 290 30 971
0 125 340 0 982
0 125 390 0 991
0 125 440 0 1001
1 100 480 -10 1010
2 75 510 -10 1020
6 -25 500 -40 1030
10 -125 450 -40 1040
9 -100 410 10 1049
7 -50 390 20 1058
11 -150 330 -40 1069
15 -250 230 -40 1078
11 -150 170 40 1088
5 0 170 60 1097
2 75 200 30 1107
0 125 250 20 1117
1 100 290 -10 1127
0 125 340 10 1136
0 125 390 0 1145
1 100 430 -10 1156
4 25 440 -30 1165
7 -50 420 -30 1175
8 -75 390 -10 1184
9 -100 350 -10 1195
7 -50 330 20 1204
12 -175 260 -50 1214
12 -175 190 0 1223
7 -50 170 50 1232
3 50 190 40 1243
2 75 220 10 1252
0 125 270 20 1262
0 125 320 0 1271
0 125 370 0 1282
0 125 420 0 1291
0 125 470 0 1301
3 50 490 -30 1310
5 0 490 -20 1319
10 -125 440 -50 1330
8 -75 410 20 1339
8 -75 380 0 1349
9 -100 340 -10 1358
14 -225 250 -50 1369
12 -175 180 20 1378
6 -25 170 60 1388
3 50 190 30 1397
0 125 240 30 1406
0 125 290 0 1417
0 125 340 0 1426
0 125 390 0 1436
0 125 440 0 1445
2 75 470 -20 1456
3 50 490 -10 1465
10 -125 440 -70 1475
9 -100 400 10 1484
9 -100 360 0 1495
9 -100 320 0 1504
13 -200 240 -40 1514
10 -125 190 30 1523
4 25 200 60 1532
2 75 230 20 1543
0 125 280 20 1552
1 100 320 -10 1562
0 125 370 10 1571
0 125 420 0 1582
0 125 470 0 1591
0 125 520 0 1600
1 100 560 -10 1610
5 0 560 -40 1619
8 -75 530 -30 1630
10 -125 480 -20 1639
7 -50 460 30 1649
10 -125 410 -30 1658
14 -225 320 -40 1669
13 -200 240 10 1678
6 -25 230 70 1688
4 25 240 20 1697
0 125 290 40 1707
0 125 340 0 1717
0 125 390 0 1726
0 125 440 0 1736
1 100 480 -10 1745
4 25 490 -30 1756
6 -25 480 -20 1765
9 -100 440 -30 1775
8 -75 410 10 1784
8 -75 380 0 1795
12 -175 310 -40 1804
12 -175 240 0 1814
5 0 240 70 1823
4 25 250 10 1832
2 75 280 20 1843
0 125 330 20 1852
0 125 380 0 1862
0 125 430 0 1871
0 125 480 0 1882
1 100 520 -10 1891
2 75 550 -10 1901
8 -75 520 -60 1910
10 -125 470 -20 1921
8 -75 440 20 1930
7 -50 420 10 1939
14 -225 330 -70 1949
13 -200 250 10 1958
7 -50 230 60 1969
4 25 240 30 1978
1 100 280 30 1988
0 125 330 10 1997
0 125 380 0 2007
0 125 430 0 2017
0 125 480 0 2026
3 50 500 -30 2036
4 25 510 -10 2045
10 -125 460 -60 2056
7 -50 440 30 2065
9 -100 400 -20 2075
9 -100 360 0 2084
12 -175 290 -30 2095
7 -50 270 50 2104
3 50 290 40 2113
3 50 310 0 2123
0 125 360 30 2132
0 125 410 0 2143
0 125 460 0 2152
0 125 510 0 2162
0 125 560 0 2171
0 125 610 0 2182
5 0 610 -50 2191
9 -100 570 -40 2201
10 -125 520 -10 2210
7 -50 500 30 2221
10 -125 450 -30 2230
14 -225 360 -40 2240
10 -125 310 40 2249
6 -25 300 40 2258
3 50 320 30 2269
1 100 360 20 2278
0 125 410 10 2288
0 125 460 0 2297
0 125 510 0 2308
0 125 560 0 2317
0 125 610 0 2327
6 -25 600 -60 2336
6 -25 590 0 2347
10 -125 540 -40 2356
7 -50 520 30 2366
12 -175 450 -50 2375
11 -150 390 10 2385
7 -50 370 40 2395
4 25 380 30 2404
3 50 400 10 2414
1 100 440 20 2423
0 125 490 10 2434
0 125 540 0 2443
0 125 590 0 2453
2 75 620 -20 2462
5 0 620 -30 2472
11 -150 560 -60 2482
7 -50 540 40 2492
8 -75 510 -10 2501
9 -100 470 -10 2511
13 -200 390 -40 2521
8 -75 360 50 2530
6 -25 350 20 2540
3 50 370 30 2549
0 125 420 30 2560
1 100 460 -10 2569
0 125 510 10 2579
0 125 560 0 2588
6 -25 550 -60 2598
6 -25 540 0 2608
8 -75 510 -20 2618
6 -25 500 20 2627
8 -75 470 -20 2637
12 -175 400 -40 2647
7 -50 380 50 2657
5 0 380 20 2666
4 25 390 10 2675
0 125 440 40 2685
1 100 480 -10 2695
0 125 530 10 2705
0 125 580 0 2714
0 125 630 0 2724
4 25 640 -40 2734
9 -100 600 -50 2744
8 -75 570 10 2753
8 -75 540 0 2763
7 -50 520 10 2772
13 -200 440 -60 2783
9 -100 400 40 2792
7 -50 380 20 2802
3 50 400 40 2811
2 75 430 10 2821
0 125 480 20 2831
0 125 530 0 2840
0 125 580 0 2850
0 125 630 0 2860
2 75 660 -20 2870
6 -25 650 -40 2879
10 -125 600 -40 2889
7 -50 580 30 2898
9 -100 540 -20 2909
12 -175 470 -30 2918
9 -100 430 30 2928
5 0 430 40 2937
3 50 450 20 2948
1 100 490 20 2957
0 125 540 10 2967
0 125 590 0 2976
0 125 640 0 2985
0 125 690 0 2996
0 125 740 0 3006
8 -75 710 -80 3015
7 -50 690 10 3025
9 -100 650 -20 3035
6 -25 640 30 3044
14 -225 550 -80 3054
12 -175 480 20 3063
6 -25 470 60 3074
6 -25 460 0 3083
1 100 500 50 3093
0 125 550 10 3102
0 125 600 0 3112
0 125 650 0 3122
1 100 690 -10 3132
6 -25 680 -50 3141
8 -75 650 -20 3151
7 -50 630 10 3161
8 -75 600 -10 3171
12 -175 530 -40 3180
9 -100 490 30 3190
5 0 490 40 3200
5 0 490 0 3209
1 100 530 40 3219
1 100 570 0 3228
0 125 620 10 3238
0 125 670 0 3248
0 125 720 0 3258
4 25 730 -40 3267
8 -75 700 -40 3277
8 -75 670 0 3287
8 -75 640 0 3297
7 -50 620 10 3306
14 -225 530 -70 3316
8 -75 500 60 3325
7 -50 480 10 3336
2 75 510 50 3345
1 100 550 10 3355
0 125 600 10 3364
0 125 650 0 3375
0 125 700 0 3384
0 125 750 0 3394
10 -125 700 -100 3403
6 -25 690 40 3414
10 -125 640 -40 3423
6 -25 630 40 3433
13 -200 550 -70 3442
5 0 550 80 3452
7 -50 530 -20 3462
2 75 560 50 3471
0 125 610 20 3481
0 125 660 0 3490
0 125 710 0 3501
0 125 760 0 3510
0 125 810 0 3520
5 0 810 -50 3529
11 -150 750 -60 3539
6 -25 740 50 3550
10 -125 690 -40 3559
10 -125 640 0 3569
12 -175 570 -20 3578
5 0 570 70 3588
6 -25 560 -10 3598
0 125 610 60 3607
0 125 660 0 3617
0 125 710 0 3627
0 125 760 0 3637
0 125 810 0 3646
0 125 860 0 3656
9 -100 820 -90 3666
6 -25 810 30 3676
9 -100 770 -30 3685
8 -75 740 10 3695
13 -200 660 -50 3705
5 0 660 80 3715
7 -50 640 -20 3724
3 50 660 40 3734
0 125 710 30 3743
0 125 760 0 3753
0 125 810 0 3763
6 -25 800 -60 3773
9 -100 760 -30 3782
7 -50 740 20 3792
7 -50 720 0 3802
11 -150 660 -40 3812
10 -125 610 10 3821
7 -50 590 30 3831
4 25 600 30 3841
2 75 630 20 3851
0 125 680 20 3860
0 125 730 0 3870
0 125 780 0 3879
6 -25 770 -60 3890
7 -50 750 -10 3899
6 -25 740 10 3909
8 -75 710 -20 3918
8 -75 680 0 3929
11 -150 620 -30 3938
3 50 640 80 3948
7 -50 620 -40 3957
0 125 670 70 3968
0 125 720 0 3977
0 125 770 0 3987
0 125 820 0 3996
0 125 870 0 4006
18 -325 740 -180 4016
0 125 790 180 4026
17 -300 670 -170 4035
7 -50 650 100 4045
11 -150 590 -40 4055
3 50 610 80 4065
5 0 610 -20 4074
0 125 660 50 4084
0 125 710 0 4093
0 125 760 0 4104
0 125 810 0 4113
0 125 860 0 4123
0 125 910 0 4132
10 -125 860 -100 4143
7 -50 840 30 4152
9 -100 800 -20 4162
10 -125 750 -10 4171
10 -125 700 0 4182
5 0 700 50 4191
5 0 700 0 4200
0 125 750 50 4210
0 125 800 0 4220
0 125 850 0 4230
0 125 900 0 4240
1 100 940 -10 4249
11 -150 880 -100 4259
3 50 900 80 4269
11 -150 840 -80 4279
6 -25 830 50 4288
16 -275 720 -100 4298
5 0 720 110 4307
8 -75 690 -30 4318
0 125 740 80 4327
0 125 790 0 4337
0 125 840 0 4346
0 125 890 0 4357
1 100 930 -10 4366
10 -125 880 -90 4376
4 25 890 60 4385
10 -125 840 -60 4396
9 -100 800 10 4405
9 -100 760 0 4415
5 0 760 40 4424
5 0 760 0 4434
0 125 810 50 4444
0 125 860 0 4454
0 125 910 0 4463
5 0 910 -50 4473
8 -75 880 -30 4483
7 -50 860 10 4493
7 -50 840 0 4502
10 -125 790 -30 4512
10 -125 740 0 4521
6 -25 730 40 4532
5 0 730 10 4541
0 125 780 50 4551
0 125 830 0 4560
0 125 880 0 4571
0 125 930 0 4580
0 125 980 0 4590
15 -250 880 -150 4600
0 125 930 150 4610
16 -275 820 -160 4620
5 0 820 110 4629
8 -75 790 -30 4638
3 50 810 50 4648
0 125 860 30 4658
0 125 910 0 4668
0 125 960 0 4677
0 125 1010 0 4687
0 125 1060 0 4698
13 -200 980 -130 4707
3 50 1000 100 4717
13 -200 920 -100 4726
7 -50 900 60 4737
13 -200 820 -60 4746
3 50 840 100 4756
6 -25 830 -30 4765
0 125 880 60 4775
0 125 930 0 4785
0 125 980 0 4795
0 125 1030 0 4804
0 125 1080 0 4814
0 125 1130 0 4824
12 -175 1060 -120 4834
4 25 1070 80 4843
13 -200 990 -90 4853
9 -100 950 40 4864
7 -50 930 20 4873
6 -25 920 10 4883
0 125 970 60 4892
0 125 1020 0 4902
6 -25 1010 -60 4912
6 -25 1000 0 4922
9 -100 960 -30 4931
4 25 970 50 4941
14 -225 880 -100 4951
6 -25 870 80 4961
8 -75 840 -20 4970
3 50 860 50 4980
0 125 910 30 4989
0 125 960 0 5000
0 125 1010 0 5009
5 0 1010 -50 5019
8 -75 980 -30 5028
5 0 980 30 5039
9 -100 940 -40 5048
8 -75 910 10 5058
5 0 910 30 5067
5 0 910 0 5078
0 125 960 50 5087
0 125 1010 0 5097
0 125 1060 0 5107
0 125 1110 0 5116
15 -250 1010 -150 5127
0 125 1060 150 5136
17 -300 940 -170 5146
4 25 950 130 5155
13 -200 870 -90 5166
0 125 920 130 5175
4 25 930 -40 5185
0 125 980 40 5194
0 125 1030 0 5204
0 125 1080 0 5214
0 125 1130 0 5224
0 125 1180 0 5233
14 -225 1090 -140 5243
0 125 1140 140 5254
14 -225 1050 -140 5263
6 -25 1040 80 5273
9 -100 1000 -30 5282
0 125 1050 90 5293
0 125 1100 0 5302
0 125 1150 0 5312
0 125 1200 0 5321
7 -50 1180 -70 5331
5 0 1180 20 5341
7 -50 1160 -20 5351
6 -25 1150 10 5361
15 -250 1050 -90 5370
2 75 1080 130 5381
8 -75 1050 -60 5390
0 125 1100 80 5400
0 125 1150 0 5409
0 125 1200 0 5420
9 -100 1160 -90 5429
2 75 1190 70 5439
14 -225 1100 -120 5449
6 -25 1090 80 5458
5 0 1090 10 5468
8 -75 1060 -30 5478
0 125 1110 80 5488
0 125 1160 0 5497
0 125 1210 0 5508
7 -50 1190 -70 5517
7 -50 1170 0 5527
7 -50 1150 0 5536
10 -125 1100 -30 5547
9 -100 1060 10 5556
0 125 1110 90 5566
11 -150 1050 -110 5576
0 125 1100 110 5585
0 125 1150 0 5596
0 125 1200 0 5605
12 -175 1130 -120 5615
1 100 1170 110 5624
15 -250 1070 -140 5635
3 50 1090 120 5644
0 125 1140 30 5654
9 -100 1100 -90 5663
0 125 1150 90 5673
0 125 1200 0 5683
0 125 1250 0 5693
0 125 1300 0 5703
0 125 1350 0 5712
21 -400 1190 -210 5723
0 125 1240 210 5732
11 -150 1180 -110 5742
9 -100 1140 20 5751
4 25 1150 50 5762
0 125 1200 40 5771
0 125 1250 0 5781
0 125 1300 0 5791
0 125 1350 0 5800
14 -225 1260 -140 5811
0 125 1310 140 5820
16 -275 1200 -160 5830
0 125 1250 160 5839
0 125 1300 0 5850
7 -50 1280 -70 5860
0 125 1330 70 5869
16 -275 1220 -160 5879
0 125 1270 160 5889
15 -250 1170 -150 5899
3 50 1190 120 5908
0 125 1240 30 5918
14 -225 1150 -140 5928
0 125 1200 140 5938
0 125 1250 0 5948
0 125 1300 0 5957
15 -250 1200 -150 5967
0 125 1250 150 5977
11 -150 1190 -110 5987
0 125 1240 110 5996
0 125 1290 0 6006
0 125 1340 0 6016
0 125 1390 0 6026
25 -500 1190 -250 6036
0 125 1240 250 6045
17 -300 1120 -170 6055
0 125 1170 170 6065
0 125 1220 0 6075
0 125 1270 0 6084
0 125 1320 0 6094
10 -125 1270 -100 6105
2 75 1300 80 6114
11 -150 1240 -90 6124
7 -50 1220 40 6133
0 125 1270 70 6144
0 125 1320 0 6153
0 125 1370 0 6163
6 -25 1360 -60 6172
4 25 1370 20 6182
6 -25 1360 -20 6193
6 -25 1350 0 6202
14 -225 1260 -80 6212
0 125 1310 140 6221
0 125 1360 0 6232
0 125 1410 0 6241
13 -200 1330 -130 6251
0 125 1380 130 6261
11 -150 1320 -110 6270
7 -50 1300 40 6281
0 125 1350 70 6290
0 125 1400 0 6300
12 -175 1330 -120 6310
0 125 1380 120 6320
16 -275 1270 -160 6330
2 75 1300 140 6339
18 -325 1170 -160 6349
0 125 1220 180 6359
0 125 1270 0 6369
0 125 1320 0 6378
0 125 1370 0 6388
17 -300 1250 -170 6398
0 125 1300 170 6408
0 125 1350 0 6418
16 -275 1240 -160 6427
0 125 1290 160 6437
0 125 1340 0 6447
0 125 1390 0 6457
0 125 1440 0 6466
0 125 1490 0 6476
10 -125 1440 -100 6487
6 -25 1430 40 6496
11 -150 1370 -50 6506
8 -75 1340 30 6515
0 125 1390 80 6525
11 -150 1330 -110 6536
0 125 1380 110 6545
0 125 1430 0 6555
0 125 1480 0 6564
7 -50 1460 -70 6575
8 -75 1430 -10 6584
0 125 1480 80 6594
0 125 1530 0 6604
19 -350 1390 -190 6614
0 125 1440 190 6624
0 125 1490 0 6633
18 -325 1360 -180 6643
0 125 1410 180 6653
20 -375 1260 -200 6663
0 125 1310 200 6673
0 125 1360 0 6682
0 125 1410 0 6692
13 -200 1330 -130 6703
0 125 1380 130 6712
10 -125 1330 -100 6722
5 0 1330 50 6731
0 125 1380 50 6742
14 -225 1290 -140 6751
0 125 1340 140 6761
0 125 1390 0 6770
0 125 1440 0 6780
0 125 1490 0 6791
23 -450 1310 -230 6800
0 125 1360 230 6810
16 -275 1250 -160 6819
0 125 1300 160 6830
0 125 1350 0 6839
0 125 1400 0 6849
0 125 1450 0 6858
13 -200 1370 -130 6868
0 125 1420 130 6879
0 125 1470 0 6888
24 -475 1280 -240 6898
0 125 1330 240 6907
0 125 1380 0 6918
0 125 1430 0 6928
0 125 1480 0 6937
0 125 1530 0 6947
14 -225 1440 -140 6957
0 125 1490 140 6967
12 -175 1420 -120 6977
5 0 1420 70 6986
11 -150 1360 -60 6996
0 125 1410 110 7006
0 125 1460 0 7016
0 125 1510 0 7025
12 -175 1440 -120 7035
0 125 1490 120 7046
21 -400 1330 -210 7055
0 125 1380 210 7065
0 125 1430 0 7074
5 0 1430 -50 7085
0 125 1480 50 7094
16 -275 1370 -160 7104
0 125 1420 160 7114
16 -275 1310 -160 7123
0 125 1360 160 7134
0 125 1410 0 7143
13 -200 1330 -130 7153
0 125 1380 130 7162
0 125 1430 0 7173
20 -375 1280 -200 7183
0 125 1330 200 7192
0 125 1380 0 7202
12 -175 1310 -120 7212
0 125 1360 120 7222
0 125 1410 0 7231
0 125 1460 0 7241
0 125 1510 0 7251
5 0 1510 -50 7261
15 -250 1410 -100 7271
0 125 1460 150 7280
20 -375 1310 -200 7290
0 125 1360 200 7301
0 125 1410 0 7310
0 125 1460 0 7320
0 125 1510 0 7329
0 125 1560 0 7340
17 -300 1440 -170 7350
0 125 1490 170 7359
0 125 1540 0 7369
16 -275 1430 -160 7378
0 125 1480 160 7389
0 125 1530 0 7398
16 -275 1420 -160 7408
0 125 1470 160 7418
13 -200 1390 -130 7428
0 125 1440 130 7438
0 125 1490 0 7447
16 -275 1380 -160 7457
0 125 1430 160 7467
0 125 1480 0 7477
15 -250 1380 -150 7487
0 125 1430 150 7496
0 125 1480 0 7506
0 125 1530 0 7516
0 125 1580 0 7526
25 -500 1380 -250 7536
0 125 1430 250 7545
10 -125 1380 -100 7556
3 50 1400 70 7565
0 125 1450 30 7575
0 125 1500 0 7584
0 125 1550 0 7595
16 -275 1440 -160 7605
0 125 1490 160 7614
0 125 1540 0 7624
22 -425 1370 -220 7634
0 125 1420 220 7644
0 125 1470 0 7653
0 125 1520 0 7663
0 125 1570 0 7673
0 125 1620 0 7683
13 -200 1540 -130 7693
0 125 1590 130 7703
0 125 1640 0 7712
22 -425 1470 -220 7723
0 125 1520 220 7732
0 125 1570 0 7742
0 125 1620 0 7751
10 -125 1570 -100 7761
0 125 1620 100 7772
16 -275 1510 -160 7781
0 125 1560 160 7791
0 125 1610 0 7800
14 -225 1520 -140 7811
0 125 1570 140 7821
17 -300 1450 -170 7830
1 100 1490 160 7840
0 125 1540 10 7849
19 -350 1400 -190 7860
0 125 1450 190 7870
0 125 1500 0 7879
14 -225 1410 -140 7889
0 125 1460 140 7899
0 125 1510 0 7909
17 -300 1390 -170 7919
0 125 1440 170 7928
0 125 1490 0 7939
0 125 1540 0 7948
0 125 1590 0 7958
0 125 1640 0 7968
20 -375 1490 -200 7977
0 125 1540 200 7988
18 -325 1410 -180 7997
0 125 1460 180 8007
0 125 1510 0 8016
0 125 1560 0 8027
0 125 1610 0 8037
12 -175 1540 -120 8046
0 125 1590 120 8056
19 -350 1450 -190 8067
0 125 1500 190 8076
0 125 1550 0 8086
0 125 1600 0 8095
0 125 1650 0 8105
20 -375 1500 -200 8116
0 125 1550 200 8125
0 125 1600 0 8135
21 -400 1440 -210 8144
0 125 1490 210 8155
0 125 1540 0 8164
0 125 1590 0 8174
13 -200 1510 -130 8184
0 125 1560 130 8194
0 125 1610 0 8204
21 -400 1450 -210 8214
0 125 1500 210 8223
0 125 1550 0 8233
0 125 1600 0 8243
0 125 1650 0 8253
23 -450 1470 -230 8263
0 125 1520 230 8272
0 125 1570 0 8283
18 -325 1440 -180 8292
0 125 1490 180 8302
0 125 1540 0 8311
0 125 1590 0 8322
10 -125 1540 -100 8332
0 125 1590 100 8341
20 -375 1440 -200 8351
0 125 1490 200 8360
0 125 1540 0 8371
0 125 1590 0 8380
0 125 1640 0 8390
10 -125 1590 -100 8400
0 125 1640 100 8410
13 -200 1560 -130 8420
0 125 1610 130 8430
0 125 1660 0 8439
16 -275 1550 -160 8450
0 125 1600 160 8459
0 125 1650 0 8469
16 -275 1540 -160 8479
0 125 1590 160 8488
0 125 1640 0 8499
13 -200 1560 -130 8508
0 125 1610 130 8518
0 125 1660 0 8528
16 -275 1550 -160 8538
0 125 1600 160 8548
0 125 1650 0 8557
23 -450 1470 -230 8567
0 125 1520 230 8578
0 125 1570 0 8587
11 -150 1510 -110 8597
0 125 1560 110 8606
0 125 1610 0 8616
12 -175 1540 -120 8627
0 125 1590 120 8636
0 125 1640 0 8646
0 125 1690 0 8655
21 -400 1530 -210 8666
0 125 1580 210 8676
0 125 1630 0 8685
19 -350 1490 -190 8695
0 125 1540 190 8705
0 125 1590 0 8715
0 125 1640 0 8724
1 100 1680 -10 8734
20 -375 1530 -190 8744
0 125 1580 200 8754
0 125 1630 0 8764
0 125 1680 0 8773
0 125 1730 0 8783
22 -425 1560 -220 8794
0 125 1610 220 8803
0 125 1660 0 8813
24 -475 1470 -240 8823
0 125 1520 240 8833
0 125 1570 0 8843
0 125 1620 0 8852
14 -225 1530 -140 8862
0 125 1580 140 8872
0 125 1630 0 8882
20 -375 1480 -200 8892
0 125 1530 200 8901
0 125 1580 0 8911
0 125 1630 0 8921
0 125 1680 0 8931
25 -500 1480 -250 8941
0 125 1530 250 8950
0 125 1580 0 8961
15 -250 1480 -150 8971
0 125 1530 150 8980
0 125 1580 0 8990
0 125 1630 0 8999
0 125 1680 0 9010
15 -250 1580 -150 9020
0 125 1630 150 9029
0 125 1680 0 9039
0 125 1730 0 9049
17 -300 1610 -170 9059
0 125 1660 170 9069
0 125 1710 0 9078
22 -425 1540 -220 9089
0 125 1590 220 9098
0 125 1640 0 9108
0 125 1690 0 9118
1 100 1730 -10 9127
17 -300 1610 -160 9138
0 125 1660 170 9147
0 125 1710 0 9157
18 -325 1580 -180 9167
0 125 1630 180 9177
0 125 1680 0 9187
25 -500 1480 -250 9197
0 125 1530 250 9206
0 125 1580 0 9217
0 125 1630 0 9226
14 -225 1540 -140 9236
0 125 1590 140 9245
0 125 1640 0 9255
23 -450 1460 -230 9266
0 125 1510 230 9275
0 125 1560 0 9285
0 125 1610 0 9294
0 125 1660 0 9305
0 125 1710 0 9315
24 -475 1520 -240 9324
0 125 1570 240 9334
0 125 1620 0 9344
16 -275 1510 -160 9354
0 125 1560 160 9364
0 125 1610 0 9373
0 125 1660 0 9383
18 -325 1530 -180 9394
0 125 1580 180 9403
0 125 1630 0 9413
0 125 1680 0 9422
0 125 1730 0 9433
22 -425 1560 -220 9443
0 125 1610 220 9452
0 125 1660 0 9462
22 -425 1490 -220 9472
0 125 1540 220 9482
0 125 1590 0 9491
0 125 1640 0 9501
0 125 1690 0 9511
19 -350 1550 -190 9521
0 125 1600 190 9531
0 125 1650 0 9540
0 125 1700 0 9550
17 -300 1580 -170 9561
0 125 1630 170 9570
0 125 1680 0 9580
24 -475 1490 -240 9590
0 125 1540 240 9600
0 125 1590 0 9610
0 125 1640 0 9619
0 125 1690 0 9629
19 -350 1550 -190 9639
0 125 1600 190 9649
0 125 1650 0 9659
18 -325 1520 -180 9668
0 125 1570 180 9678
0 125 1620 0 9688
0 125 1670 0 9698
12 -175 1600 -120 9708
0 125 1650 120 9717
0 125 1700 0 9728
22 -425 1530 -220 9738
0 125 1580 220 9747
0 125 1630 0 9757
0 125 1680 0 9766
16 -275 1570 -160 9777
0 125 1620 160 9787
0 125 1670 0 9796
20 -375 1520 -200 9806
0 125 1570 200 9816
0 125 1620 0 9826
0 125 1670 0 9835
12 -175 1600 -120 9845
0 125 1650 120 9856
21 -400 1490 -210 9865
0 125 1540 210 9875
0 125 1590 0 9884
0 125 1640 0 9894
24 -475 1450 -240 9905
0 125 1500 240 9914
0 125 1550 0 9924
16 -275 1440 -160 9934
0 125 1490 160 9944
0 125 1540 0 9954
0 125 1590 0 9963
0 125 1640 0 9973
23 -450 1460 -230 9984
0 125 1510 230 9993
0 125 1560 0 10003
18 -325 1430 -180 10012
0 125 1480 180 10022
0 125 1530 0 10032
0 125 1580 0 10042
11 -150 1520 -110 10052
0 125 1570 110 10061
18 -325 1440 -180 10072
0 125 1490 180 10081
0 125 1540 0 10091
0 125 1590 0 10100
0 125 1640 0 10110
24 -475 1450 -240 10121
0 125 1500 240 10130
0 125 1550 0 10140
20 -375 1400 -200 10150
0 125 1450 200 10160
0 125 1500 0 10170
11 -150 1440 -110 10179
0 125 1490 110 10189
0 125 1540 0 10199
21 -400 1380 -210 10209
0 125 1430 210 10219
0 125 1480 0 10228
0 125 1530 0 10238
0 125 1580 0 10248
24 -475 1390 -240 10258
0 125 1440 240 10267
0 125 1490 0 10277
16 -275 1380 -160 10288
0 125 1430 160 10297
0 125 1480 0 10307
0 125 1530 0 10316
14 -225 1440 -140 10327
0 125 1490 140 10337
14 -225 1400 -140 10346
0 125 1450 140 10356
0 125 1500 0 10365
0 125 1550 0 10376
0 125 1600 0 10386
17 -300 1480 -170 10395
0 125 1530 170 10405
22 -425 1360 -220 10415
0 125 1410 220 10425
0 125 1460 0 10434
0 125 1510 0 10444
0 125 1560 0 10455
16 -275 1450 -160 10464
0 125 1500 160 10474
0 125 1550 0 10483
22 -425 1380 -220 10493
0 125 1430 220 10504
0 125 1480 0 10513
0 125 1530 0 10523
19 -350 1390 -190 10532
0 125 1440 190 10543
0 125 1490 0 10552
21 -400 1330 -210 10562
0 125 1380 210 10572
0 125 1430 0 10582
0 125 1480 0 10592
18 -325 1350 -180 10602
0 125 1400 180 10611
0 125 1450 0 10620
0 125 1500 0 10631
0 125 1550 0 10641
16 -275 1440 -160 10650
0 125 1490 160 10660
0 125 1540 0 10670
21 -400 1380 -210 10680
0 125 1430 210 10690
0 125 1480 0 10699
0 125 1530 0 10710
0 125 1580 0 10719
0 125 1630 0 10729
16 -275 1520 -160 10739
0 125 1570 160 10748
0 125 1620 0 10759
18 -325 1490 -180 10769
0 125 1540 180 10778
0 125 1590 0 10788
19 -350 1450 -190 10798
0 125 1500 190 10808
0 125 1550 0 10817
23 -450 1370 -230 10827
0 125 1420 230 10838
0 125 1470 0 10847
0 125 1520 0 10857
0 125 1570 0 10866
19 -350 1430 -190 10876
0 125 1480 190 10886
0 125 1530 0 10896
15 -250 1430 -150 10906
0 125 1480 150 10915
0 125 1530 0 10926
0 125 1580 0 10935
24 -475 1390 -240 10945
0 125 1440 240 10955
0 125 1490 0 10964
17 -300 1370 -170 10975
0 125 1420 170 10984
0 125 1470 0 10994
0 125 1520 0 11003
0 125 1570 0 11014
20 -375 1420 -200 11024
0 125 1470 200 11033
0 125 1520 0 11043
10 -125 1470 -100 11054
0 125 1520 100 11063
0 125 1570 0 11073
14 -225 1480 -140 11082
0 125 1530 140 11092
20 -375 1380 -200 11103
0 125 1430 200 11112
0 125 1480 0 11121
0 125 1530 0 11131
13 -200 1450 -130 11142
0 125 1500 130 11151
0 125 1550 0 11161
21 -400 1390 -210 11171
0 125 1440 210 11181
0 125 1490 0 11191
0 125 1540 0 11200
0 125 1590 0 11210
24 -475 1400 -240 11220
0 125 1450 240 11230
0 125 1500 0 11240
24 -475 1310 -240 11249
0 125 1360 240 11259
0 125 1410 0 11269
0 125 1460 0 11279
16 -275 1350 -160 11289
0 125 1400 160 11298
0 125 1450 0 11309
16 -275 1340 -160 11318
0 125 1390 160 11328
0 125 1440 0 11337
0 125 1490 0 11347
0 125 1540 0 11358
6 -25 1530 -60 11367
14 -225 1440 -80 11377
8 -75 1410 60 11386
0 125 1460 80 11397
17 -300 1340 -170 11407
0 125 1390 170 11416
0 125 1440 0 11426
0 125 1490 0 11436
0 125 1540 0 11446
12 -175 1470 -120 11456
0 125 1520 120 11465
20 -375 1370 -200 11475
0 125 1420 200 11485
0 125 1470 0 11495
0 125 1520 0 11504
0 125 1570 0 11514
21 -400 1410 -210 11525
0 125 1460 210 11534
0 125 1510 0 11544
24 -475 1320 -240 11553
0 125 1370 240 11564
0 125 1420 0 11573
0 125 1470 0 11583
0 125 1520 0 11592
15 -250 1420 -150 11602
0 125 1470 150 11613
0 125 1520 0 11622
23 -450 1340 -230 11632
0 125 1390 230 11641
0 125 1440 0 11652
0 125 1490 0 11662
0 125 1540 0 11671
22 -425 1370 -220 11681
0 125 1420 220 11691
0 125 1470 0 11701
24 -475 1280 -240 11711
0 125 1330 240 11720
0 125 1380 0 11730
0 125 1430 0 11740
0 125 1480 0 11750
0 125 1530 0 11759
19 -350 1390 -190 11769
0 125 1440 190 11780
0 125 1490 0 11789
21 -400 1330 -210 11799
0 125 1380 210 11808
0 125 1430 0 11819
0 125 1480 0 11828
10 -125 1430 -100 11838
0 125 1480 100 11848
4 25 1490 -40 11857
19 -350 1350 -150 11868
0 125 1400 190 11877
0 125 1450 0 11887
0 125 1500 0 11897
13 -200 1420 -130 11907
0 125 1470 130 11917
16 -275 1360 -160 11926
0 125 1410 160 11936
0 125 1460 0 11945
14 -225 1370 -140 11956
0 125 1420 140 11966
17 -300 1300 -170 11975
0 125 1350 170 11985
16 -275 1240 -160 11995
0 125 1290 160 12005
7 -50 1270 -70 12014
0 125 1320 70 12024
0 125 1370 0 12035
8 -75 1340 -80 12044
10 -125 1290 -20 12054
8 -75 1260 20 12063
0 125 1310 80 12073
15 -250 1210 -150 12083
0 125 1260 150 12093
0 125 1310 0 12102
0 125 1360 0 12112
0 125 1410 0 12123
0 125 1460 0 12132
0 125 1510 0 12142
28 -575 1280 -280 12152
0 125 1330 280 12162
21 -400 1170 -210 12172
0 125 1220 210 12181
0 125 1270 0 12191
0 125 1320 0 12200
0 125 1370 0 12211
0 125 1420 0 12220
16 -275 1310 -160 12230
0 125 1360 160 12239
18 -325 1230 -180 12250
0 125 1280 180 12260
0 125 1330 0 12269
13 -200 1250 -130 12279
0 125 1300 130 12289
18 -325 1170 -180 12299
0 125 1220 180 12308
11 -150 1160 -110 12318
10 -125 1110 10 12327
0 125 1160 100 12338
0 125 1210 0 12347
9 -100 1170 -90 12357
4 25 1180 50 12367
11 -150 1120 -70 12377
5 0 1120 60 12387
12 -175 1050 -70 12396
3 50 1070 90 12406
5 0 1070 -20 12416
0 125 1120 50 12426
0 125 1170 0 12435
12 -175 1100 -120 12445
8 -75 1070 40 12454
12 -175 1000 -40 12465
13 -200 920 -10 12475
10 -125 870 30 12484
8 -75 840 20 12493
3 50 860 50 12504
5 0 860 -20 12513
4 25 870 10 12523
18 -325 740 -140 12533
10 -125 690 80 12542
20 -375 540 -100 12553
18 -325 410 20 12562
12 -175 340 60 12571
2 75 370 100 12581
2 75 400 0 12591
0 125 450 20 12601
0 125 500 0 12610
5 0 500 -50 12620
6 -25 490 -10 12630
11 -150 430 -50 12640
7 -50 410 40 12649
8 -75 380 -10 12658
12 -175 310 -40 12668
15 -250 210 -30 12678
9 -100 170 60 12688
5 0 170 40 12697
1 100 210 40 12707
0 125 260 10 12717
0 125 310 0 12726
0 125 360 0 12736
0 125 410 0 12745
1 100 450 -10 12755
4 25 460 -30 12765
6 -25 450 -20 12775
8 -75 420 -20 12784
9 -100 380 -10 12794
8 -75 350 10 12804
13 -200 270 -50 12814
12 -175 200 10 12823
6 -25 190 60 12832
3 50 210 30 12843
3 50 230 0 12852
0 125 280 30 12862
0 125 330 0 12871
0 125 380 0 12881
0 125 430 0 12891
1 100 470 -10 12901
2 75 500 -10 12910
8 -75 470 -60 12919
8 -75 440 0 12930
10 -125 390 -20 12939
6 -25 380 40 12949
12 -175 310 -60 12958
14 -225 220 -20 12968
11 -150 160 30 12978
4 25 170 70 12987
3 50 190 10 12997
0 125 240 30 13006
0 125 290 0 13017
0 125 340 0 13026
0 125 390 0 13036
1 100 430 -10 13045
2 75 460 -10 13056
7 -50 440 -50 13065
9 -100 400 -20 13075
9 -100 360 0 13084
8 -75 330 10 13093
11 -150 270 -30 13104
12 -175 200 -10 13113
7 -50 180 50 13123
4 25 190 30 13132
2 75 220 20 13143
1 100 260 10 13152
0 125 310 10 13162
0 125 360 0 13171
0 125 410 0 13180
0 125 460 0 13191
1 100 500 -10 13200
2 75 530 -10 13210
6 -25 520 -40 13219

Title: Re: Brushed DC motor closed loop speed control
Post by: Picuino on June 24, 2024, 07:33:33 pm
The motor is too fast. The DC motor has little inertia coupled to the shaft and is faster than the control system.
Title: Re: Brushed DC motor closed loop speed control
Post by: Picuino on June 25, 2024, 08:40:50 am
Plot of dc motor position over time, estimated from the sum of the measured back-emf voltages.
Green = Expected position
Red = measured position

Title: Re: Brushed DC motor closed loop speed control
Post by: Doctorandus_P on June 28, 2024, 11:15:49 am
This:

After the voltage divider this voltage become 27mV, about 5-6 ADC points with ADC reference voltage at 5V.

So with one count of difference of the ADC, you get a 20% change of the input signal to your PID algorithm. You're running out of resolution of your control system here, and can't expect it to do much better.

The motor being faster as the control algorithm also does not help. You could add intertia or switch to a higher PWM frequency, but you're probably in the region where much more effort is not going to improve overall performance much. But if you think it's fun, you can of course see how far you can push it.
Title: Re: Brushed DC motor closed loop speed control
Post by: Picuino on June 28, 2024, 11:54:19 am
ADC resolution is not the problem. I have changed the reference voltage to INTERNAL (1.1v) and now, with 5 times more ADC resolution, the problem remains. The motor is too fast.
I'm going to try sticking a weight with enough inertia on the shaft to see if it improves control.
Title: Re: Brushed DC motor closed loop speed control
Post by: Picuino on June 28, 2024, 12:15:27 pm
Results by adding inertia to the axis.
The reference is at 20 points of the ADC with reference voltage of 1.1v (22mV emf). That corresponds in my motor to about 60rpm.

Code: [Select]
emf	P	I	D	millis
0 0 0 0 0
0 600 20 200 33
0 600 40 0 43
0 600 60 0 53
0 600 80 0 62
0 600 100 0 72
0 600 120 0 81
0 600 140 0 92
0 600 160 0 101
0 600 180 0 111
0 600 200 0 120
0 600 220 0 131
1 570 239 -10 140
10 300 249 -90 150
18 60 251 -80 159
22 -60 249 -40 169
21 -30 248 10 179
20 0 248 10 189
19 30 249 10 198
18 60 251 10 208
18 60 253 0 218
20 0 253 -20 227
22 -60 251 -20 237
22 -60 249 0 246
22 -60 247 0 257
21 -30 246 10 266
20 0 246 10 276
20 0 246 0 285
20 0 246 0 294
18 60 248 20 305
16 120 252 20 315
15 150 257 10 324
14 180 263 10 333
14 180 269 0 344
15 150 274 -10 353
15 150 279 0 363
16 120 283 -10 372
17 90 286 -10 382
18 60 288 -10 392
17 90 291 10 402
17 90 294 0 411
18 60 296 -10 421
21 -30 295 -30 431
22 -60 293 -10 441
21 -30 292 10 450
20 0 292 10 459
20 0 292 0 470
20 0 292 0 479
19 30 293 10 489
18 60 295 10 498
16 120 299 20 508
13 210 306 30 518
11 270 315 20 528
10 300 325 10 537
12 240 333 -20 547
16 120 337 -40 557
19 30 338 -30 567
21 -30 337 -20 576
21 -30 336 0 586
20 0 336 10 595
20 0 336 0 605
21 -30 335 -10 615
22 -60 333 -10 624
24 -120 329 -20 634
22 -60 327 20 644
21 -30 326 10 654
22 -60 324 -10 663
22 -60 322 0 673
22 -60 320 0 683
21 -30 319 10 693
20 0 319 10 702
17 90 322 30 712
14 180 328 30 721
13 210 335 10 732
15 150 340 -20 741
18 60 342 -30 751
19 30 343 -10 760
20 0 343 -10 770
20 0 343 0 780
19 30 344 10 789
19 30 345 0 799
19 30 346 0 808
20 0 346 -10 819
21 -30 345 -10 828
19 30 346 20 838
19 30 347 0 847
21 -30 346 -20 858
21 -30 345 0 867
20 0 345 10 876
18 60 347 20 886
14 180 353 40 897
11 270 362 30 906
8 360 374 30 915
5 450 389 30 925
0 600 409 50 935
0 600 429 0 945
10 300 439 -100 955
31 -330 428 -210 964
39 -570 409 -80 974
38 -540 391 10 984
34 -420 377 40 993
32 -360 365 20 1003
29 -270 356 30 1012
27 -210 349 20 1022
25 -150 344 20 1032
23 -90 341 20 1042
20 0 341 30 1051
16 120 345 40 1061
13 210 352 30 1071
13 210 359 0 1080
13 210 366 0 1090
15 150 371 -20 1099
17 90 374 -20 1110
19 30 375 -20 1119
20 0 375 -10 1129
20 0 375 0 1138
18 60 377 20 1148
17 90 380 10 1158
17 90 383 0 1168
17 90 386 0 1177
16 120 390 10 1187
15 150 395 10 1197
17 90 398 -20 1207
22 -60 396 -50 1216
24 -120 392 -20 1226
22 -60 390 20 1235
20 0 390 20 1245
17 90 393 30 1255
14 180 399 30 1265
13 210 406 10 1274
14 180 412 -10 1285
17 90 415 -30 1294
19 30 416 -20 1303
21 -30 415 -20 1313
21 -30 414 0 1323
22 -60 412 -10 1333
24 -120 408 -20 1342
23 -90 405 10 1352
22 -60 403 10 1361
22 -60 401 0 1372
22 -60 399 0 1381
21 -30 398 10 1391
21 -30 397 0 1400
22 -60 395 -10 1411
19 30 396 30 1420
16 120 400 30 1430
15 150 405 10 1439
17 90 408 -20 1448
19 30 409 -20 1459
21 -30 408 -20 1468
21 -30 407 0 1478
22 -60 405 -10 1487
22 -60 403 0 1498
22 -60 401 0 1507
22 -60 399 0 1517
21 -30 398 10 1526
21 -30 397 0 1537
22 -60 395 -10 1546
23 -90 392 -10 1556
24 -120 388 -10 1565
23 -90 385 10 1575
21 -30 384 20 1585
17 90 387 40 1594
15 150 392 20 1604
14 180 398 10 1613
16 120 402 -20 1624
20 0 402 -40 1633
22 -60 400 -20 1643
23 -90 397 -10 1652
23 -90 394 0 1662
22 -60 392 10 1672
21 -30 391 10 1682
22 -60 389 -10 1691
24 -120 385 -20 1701
23 -90 382 10 1711
23 -90 379 0 1720
23 -90 376 0 1730
23 -90 373 0 1739
23 -90 370 0 1750
22 -60 368 10 1759
20 0 368 20 1769
19 30 369 10 1778
18 60 371 10 1788
19 30 372 -10 1798
19 30 373 0 1808
20 0 373 -10 1817
21 -30 372 -10 1827
21 -30 371 0 1837
22 -60 369 -10 1847
21 -30 368 10 1856
22 -60 366 -10 1866
22 -60 364 0 1875
22 -60 362 0 1885
22 -60 360 0 1895
23 -90 357 -10 1904
25 -150 352 -20 1914
25 -150 347 0 1924
23 -90 344 20 1934
21 -30 343 20 1943
18 60 345 30 1953
16 120 349 20 1963
15 150 354 10 1973
15 150 359 0 1982
18 60 361 -30 1992
20 0 361 -20 2001
22 -60 359 -20 2011
21 -30 358 10 2021
20 0 358 10 2030
20 0 358 0 2040
22 -60 356 -20 2050
23 -90 353 -10 2060
24 -120 349 -10 2069
24 -120 345 0 2079
24 -120 341 0 2088
24 -120 337 0 2099
24 -120 333 0 2108
23 -90 330 10 2117
22 -60 328 10 2127
20 0 328 20 2137
18 60 330 20 2147
17 90 333 10 2156
17 90 336 0 2166
18 60 338 -10 2176
18 60 340 0 2186
18 60 342 0 2195
19 30 343 -10 2205
20 0 343 -10 2214
20 0 343 0 2224
21 -30 342 -10 2234
22 -60 340 -10 2244
22 -60 338 0 2253
21 -30 337 10 2263
20 0 337 10 2273
20 0 337 0 2282
22 -60 335 -20 2292
22 -60 333 0 2301
20 0 333 20 2312
17 90 336 30 2321
15 150 341 20 2331
14 180 347 10 2340
14 180 353 0 2351
15 150 358 -10 2360
18 60 360 -30 2370
20 0 360 -20 2379
21 -30 359 -10 2390
21 -30 358 0 2399
21 -30 357 0 2408
22 -60 355 -10 2418
22 -60 353 0 2427
23 -90 350 -10 2438
23 -90 347 0 2447
22 -60 345 10 2457
21 -30 344 10 2466
22 -60 342 -10 2477
23 -90 339 -10 2486
22 -60 337 10 2496
20 0 337 20 2505
18 60 339 20 2515
15 150 344 30 2525
15 150 349 0 2534
17 90 352 -20 2544
19 30 353 -20 2553
19 30 354 0 2564
19 30 355 0 2573
19 30 356 0 2583
19 30 357 0 2592
20 0 357 -10 2603
22 -60 355 -20 2612
22 -60 353 0 2622
21 -30 352 10 2631
20 0 352 10 2641
19 30 353 10 2651
21 -30 352 -20 2661
21 -30 351 0 2670
19 30 352 20 2679
15 150 357 40 2690
12 240 365 30 2699
9 330 376 30 2709
6 420 390 30 2718
10 300 400 -40 2728
17 90 403 -70 2738
23 -90 400 -60 2748
24 -120 396 -10 2757
23 -90 393 10 2767
22 -60 391 10 2777
21 -30 390 10 2787
21 -30 389 0 2796
21 -30 388 0 2806
21 -30 387 0 2816
22 -60 385 -10 2826
23 -90 382 -10 2835
23 -90 379 0 2844
22 -60 377 10 2854
21 -30 376 10 2864
18 60 378 30 2874
16 120 382 20 2883
14 180 388 20 2893
14 180 394 0 2903
14 180 400 0 2913
15 150 405 -10 2922
18 60 407 -30 2932
20 0 407 -20 2941
21 -30 406 -10 2952
20 0 406 10 2961
20 0 406 0 2971
20 0 406 0 2980
19 30 407 10 2991
18 60 409 10 3000
17 90 412 10 3010
16 120 416 10 3019
19 30 417 -30 3030
22 -60 415 -30 3039
22 -60 413 0 3048
20 0 413 20 3058
18 60 415 20 3067
16 120 419 20 3078
15 150 424 10 3087
16 120 428 -10 3097
18 60 430 -20 3106
20 0 430 -20 3117
20 0 430 0 3126
20 0 430 0 3136
21 -30 429 -10 3145
22 -60 427 -10 3155
22 -60 425 0 3165
22 -60 423 0 3175
22 -60 421 0 3184
22 -60 419 0 3194
23 -90 416 -10 3204
24 -120 412 -10 3214
23 -90 409 10 3223
21 -30 408 20 3233
19 30 409 20 3243
17 90 412 20 3252
17 90 415 0 3262
19 30 416 -20 3271
21 -30 415 -20 3281
21 -30 414 0 3291
21 -30 413 0 3301
22 -60 411 -10 3310
22 -60 409 0 3320
22 -60 407 0 3330
22 -60 405 0 3340
22 -60 403 0 3349
22 -60 401 0 3359
23 -90 398 -10 3368
24 -120 394 -10 3379
23 -90 391 10 3388
21 -30 390 20 3397
18 60 392 30 3407
15 150 397 30 3417
13 210 404 20 3427
15 150 409 -20 3436
20 0 409 -50 3446
23 -90 406 -30 3456
23 -90 403 0 3466
23 -90 400 0 3475
23 -90 397 0 3485
22 -60 395 10 3494
22 -60 393 0 3505
23 -90 390 -10 3514
23 -90 387 0 3524
23 -90 384 0 3533
23 -90 381 0 3543
24 -120 377 -10 3553
24 -120 373 0 3562
23 -90 370 10 3572
21 -30 369 20 3581
20 0 369 10 3592
19 30 370 10 3601
18 60 372 10 3611
18 60 374 0 3620
19 30 375 -10 3631
20 0 375 -10 3640
21 -30 374 -10 3650
22 -60 372 -10 3659
22 -60 370 0 3668
22 -60 368 0 3679
23 -90 365 -10 3688
22 -60 363 10 3698
22 -60 361 0 3707
24 -120 357 -20 3718
25 -150 352 -10 3727
24 -120 348 10 3737
23 -90 345 10 3746
20 0 345 30 3757
17 90 348 30 3766
15 150 353 20 3776
16 120 357 -10 3785
17 90 360 -10 3795
19 30 361 -20 3805
21 -30 360 -20 3814
21 -30 359 0 3824
21 -30 358 0 3833
21 -30 357 0 3844
21 -30 356 0 3853
22 -60 354 -10 3863
23 -90 351 -10 3872
23 -90 348 0 3883
24 -120 344 -10 3892
24 -120 340 0 3902
23 -90 337 10 3911
22 -60 335 10 3920
22 -60 333 0 3931
22 -60 331 0 3940
21 -30 330 10 3950
19 30 331 20 3959
18 60 333 10 3970
18 60 335 0 3979
18 60 337 0 3989
18 60 339 0 3998
18 60 341 0 4008
19 30 342 -10 4018
20 0 342 -10 4028
20 0 342 0 4037
21 -30 341 -10 4047
22 -60 339 -10 4057
22 -60 337 0 4066
21 -30 336 10 4076
20 0 336 10 4085
20 0 336 0 4096
21 -30 335 -10 4105
21 -30 334 0 4115
20 0 334 10 4124
17 90 337 30 4134
14 180 343 30 4144
13 210 350 10 4154
14 180 356 -10 4163
16 120 360 -20 4173
18 60 362 -20 4183
20 0 362 -20 4192
21 -30 361 -10 4202
21 -30 360 0 4211
21 -30 359 0 4221
22 -60 357 -10 4231
22 -60 355 0 4241
23 -90 352 -10 4250
23 -90 349 0 4260
22 -60 347 10 4270
22 -60 345 0 4280
22 -60 343 0 4289
23 -90 340 -10 4299
22 -60 338 10 4308
20 0 338 20 4318
18 60 340 20 4328
15 150 345 30 4337
15 150 350 0 4347
17 90 353 -20 4357
19 30 354 -20 4367
19 30 355 0 4376
19 30 356 0 4386
19 30 357 0 4396
19 30 358 0 4406
19 30 359 0 4415
21 -30 358 -20 4425
23 -90 355 -20 4434
22 -60 353 10 4445
21 -30 352 10 4454
20 0 352 10 4463
21 -30 351 -10 4473
20 0 351 10 4483
17 90 354 30 4493
15 150 359 20 4502
12 240 367 30 4512
9 330 378 30 4521
4 480 394 50 4532
0 600 414 40 4541
0 600 434 0 4551
10 300 444 -100 4561
29 -270 435 -190 4571
39 -570 416 -100 4581
38 -540 398 10 4590
35 -450 383 30 4599
32 -360 371 30 4610
30 -300 361 20 4619
26 -180 355 40 4629
24 -120 351 20 4638
23 -90 348 10 4647
21 -30 347 20 4658
18 60 349 30 4667
15 150 354 30 4677
12 240 362 30 4686
11 270 371 10 4697
11 270 380 0 4706
14 180 386 -30 4716
18 60 388 -40 4725
21 -30 387 -30 4736
21 -30 386 0 4745
20 0 386 10 4755
19 30 387 10 4764
19 30 388 0 4774
19 30 389 0 4784
18 60 391 10 4794
17 90 394 10 4803
16 120 398 10 4813
18 60 400 -20 4823
21 -30 399 -30 4833
22 -60 397 -10 4842
19 30 398 30 4851
16 120 402 30 4861
15 150 407 10 4871
14 180 413 10 4881
15 150 418 -10 4890
16 120 422 -10 4900
18 60 424 -20 4910
20 0 424 -20 4920
21 -30 423 -10 4929
22 -60 421 -10 4939
22 -60 419 0 4948
22 -60 417 0 4959
22 -60 415 0 4968
22 -60 413 0 4978
23 -90 410 -10 4987
24 -120 406 -10 4998
23 -90 403 10 5007
22 -60 401 10 5016
20 0 401 20 5026
18 60 403 20 5036
16 120 407 20 5046
16 120 411 0 5055
18 60 413 -20 5065
20 0 413 -20 5074
21 -30 412 -10 5085
22 -60 410 -10 5094
22 -60 408 0 5104
22 -60 406 0 5113
22 -60 404 0 5124
22 -60 402 0 5133
21 -30 401 10 5143
21 -30 400 0 5152
21 -30 399 0 5161
23 -90 396 -20 5172
25 -150 391 -20 5182
23 -90 388 20 5191
21 -30 387 20 5200
18 60 389 30 5211
15 150 394 30 5220
14 180 400 10 5230
16 120 404 -20 5239
20 0 404 -40 5250
22 -60 402 -20 5259
23 -90 399 -10 5269
23 -90 396 0 5278
22 -60 394 10 5288
22 -60 392 0 5298
23 -90 389 -10 5308
23 -90 386 0 5317
23 -90 383 0 5326
24 -120 379 -10 5337
23 -90 376 10 5346
24 -120 372 -10 5356
23 -90 369 10 5365
22 -60 367 10 5376
20 0 367 20 5385
19 30 368 10 5395
17 90 371 20 5404
17 90 374 0 5414
19 30 375 -20 5424
21 -30 374 -20 5434
22 -60 372 -10 5443
22 -60 370 0 5452
22 -60 368 0 5463
22 -60 366 0 5472
22 -60 364 0 5482
23 -90 361 -10 5491
23 -90 358 0 5501
23 -90 355 0 5511
23 -90 352 0 5521
23 -90 349 0 5530
23 -90 346 0 5540
21 -30 345 20 5550
19 30 346 20 5560
17 90 349 20 5569
15 150 354 20 5579
15 150 359 0 5588
18 60 361 -30 5598
20 0 361 -20 5608
21 -30 360 -10 5617
21 -30 359 0 5627
21 -30 358 0 5637
21 -30 357 0 5647
21 -30 356 0 5656
22 -60 354 -10 5666
24 -120 350 -20 5676
25 -150 345 -10 5686
24 -120 341 10 5695
23 -90 338 10 5704
22 -60 336 10 5714
23 -90 333 -10 5724
23 -90 330 0 5734
21 -30 329 20 5743
19 30 330 20 5753
18 60 332 10 5763
18 60 334 0 5773
18 60 336 0 5782
18 60 338 0 5792
18 60 340 0 5801
18 60 342 0 5812
19 30 343 -10 5821
20 0 343 -10 5830
20 0 343 0 5840
21 -30 342 -10 5850
22 -60 340 -10 5860
22 -60 338 0 5869
21 -30 337 10 5879
21 -30 336 0 5889
21 -30 335 0 5899
21 -30 334 0 5908
21 -30 333 0 5918
19 30 334 20 5927
17 90 337 20 5938
15 150 342 20 5947
13 210 349 20 5957
14 180 355 -10 5966
16 120 359 -20 5977
19 30 360 -30 5986
20 0 360 -10 5995
21 -30 359 -10 6005
21 -30 358 0 6014
21 -30 357 0 6025
22 -60 355 -10 6034
23 -90 352 -10 6044
23 -90 349 0 6053
22 -60 347 10 6064
22 -60 345 0 6073
22 -60 343 0 6083
23 -90 340 -10 6092
23 -90 337 0 6102


EDIT:
I have not been able to avoid the speed oscillation despite trying to tune the PID with higher derivative gain.
Perhaps another control system would be better, one based on the internal model of the dc motor.
Title: Re: Brushed DC motor closed loop speed control
Post by: khach on June 28, 2024, 12:18:37 pm
What about microchip appnote https://ww1.microchip.com/downloads/aemDocuments/documents/OTH/ApplicationNotes/ApplicationNotes/Sensorless-Position-Control-of-Brushed-DC-Motor-Using-Ripple-Counting-Technique-00003049A.pdf (https://ww1.microchip.com/downloads/aemDocuments/documents/OTH/ApplicationNotes/ApplicationNotes/Sensorless-Position-Control-of-Brushed-DC-Motor-Using-Ripple-Counting-Technique-00003049A.pdf) with commutator ripple feedback?
Title: Re: Brushed DC motor closed loop speed control
Post by: Picuino on June 28, 2024, 12:21:21 pm
I think the back-emf voltage measurement gives realistic motor speed feedback and is simpler to implement.
The problem is in the control system. With Ripple Counting Technique the problem would be similar.
Title: Re: Brushed DC motor closed loop speed control
Post by: Doctorandus_P on June 28, 2024, 12:58:00 pm
Increasing derivative gain is making your system more stable. It's use is to make the motor respond faster, but how can you expect that to work if your control algorithm is already slower then your motor response time? As a minimum, your PID algorithm should run at 4x the speed of the motor response time and 10x is more comfortable.
But why do you think the motor response time is faster then the PID algorithm in the first place?
I see a sampling time of around 10ms (With a lot of Jitter, that is not good as I wrote before) and I see an oscillation with a period of around 12 samples, i.e. 120ms.

Also, derivative term is for increasing response time. This is not important for a power feed motor. It's likely better to not use this at all in this case.

Another suspicious pat is that both the P and D terms are multiples of 10.
Title: Re: Brushed DC motor closed loop speed control
Post by: Postal2 on June 28, 2024, 01:11:48 pm
Has anyone experience with decent quality (stiffness/dynamic range) control of small brushed DC motors?
You can open any schematics of VCR from 1980's (70's ?) and use solution from it. Top loading VCR, as I remember.
Title: Re: Brushed DC motor closed loop speed control
Post by: Picuino on June 28, 2024, 04:45:16 pm
I just realized that this low speed, low friction control has a major problem because it is nonlinear.
Accelerating the motor is very simple, just give it more current by increasing the PWM cycle, but no matter how much you reduce the PWM cycle at any time you can not brake the motor. For that we depend on the external friction. When the external friction is low, we have a nonlinear control that cannot be controlled as a traditional PID.
That is probably the reason why in the result of a few posts ago the "jerky" dc motor operation was reduced when the shaft brake was increased.

I am going to try to implement another type of control, a model based control, which I think can become more accurate.
Title: Re: Brushed DC motor closed loop speed control
Post by: Postal2 on June 28, 2024, 05:10:10 pm
Are you generally familiar with DC motor controllers in VCRs? Do your research before writing anything.
http://www.ids-elektronik.de/159.pdf (http://www.ids-elektronik.de/159.pdf)
Title: Re: Brushed DC motor closed loop speed control
Post by: Picuino on June 29, 2024, 08:33:32 am
Sorry, I have researched and read that datasheet (the English translated version) and did not understand how to apply it to the dc motor control.
Do you have any document explaining the issue?
Title: Re: Brushed DC motor closed loop speed control
Post by: Postal2 on June 29, 2024, 11:49:53 am
Do you have any document explaining the issue?
I have it, but in russian. I think the first thing to understand is that a speed sensor (FG) is definitely needed.
Title: Re: Brushed DC motor closed loop speed control
Post by: Martinn on June 30, 2024, 07:29:34 am
I have a couple of Advanced Motion Control 30A8 servo drives (datasheet link (https://servo.a-m-c.com/hubfs/Datasheets/Servo-Drives/AxCent/AMC_Datasheet_30A8.pdf)) collecting dust.
So the servo amplifier has arrived (thanks), some info on the unit:
[attach=1]
More details in the manual (link above).
For Doctorandus_P  here's the PCB image
[attach=2]
Unfortunately all markings are sanded off. As the four power MOSFETs are soldered from the bottom and bolted to the case, I can't show an image of the bottom side.

- Martin
Title: Re: Brushed DC motor closed loop speed control
Post by: H.O on June 30, 2024, 09:30:32 am
Here's an image of the underside.
MOSFETs are IRFP150.
Title: Re: Brushed DC motor closed loop speed control
Post by: Picuino on June 30, 2024, 10:29:47 am
Do you have any document explaining the issue?
I have it, but in russian. I think the first thing to understand is that a speed sensor (FG) is definitely needed.
With the back-emf control system, the speed sensor is the dc motor itself, which acts as the motor and also acts as the tachometer when the current drops to zero. This system has the disadvantage of having to disconnect the motor from the power supply several times per second, so it cannot be continuously connected to 24V. But that matters little at low speeds.
The control I am implementing right now has the motor connected to the power supply via PWM for 8 milliseconds and disconnected for 2 milliseconds (20% of the time). This means that at most I can deliver 19V to the motor when I set the PWM control to 100%.

During the time the motor is off, it simply acts as a tachometer delivering a voltage proportional to the speed the motor is at that moment (dc motor acts as speed sensor).
Title: Re: Brushed DC motor closed loop speed control
Post by: Postal2 on June 30, 2024, 01:39:24 pm
... (dc motor acts as speed sensor). ...
Okay.


What about microchip appnote https://ww1.microchip.com/downloads/aemDocuments/documents/OTH/ApplicationNotes/ApplicationNotes/Sensorless-Position-Control-of-Brushed-DC-Motor-Using-Ripple-Counting-Technique-00003049A.pdf (https://ww1.microchip.com/downloads/aemDocuments/documents/OTH/ApplicationNotes/ApplicationNotes/Sensorless-Position-Control-of-Brushed-DC-Motor-Using-Ripple-Counting-Technique-00003049A.pdf) with commutator ripple feedback?
Beautifully packaged incompetence.
Title: Re: Brushed DC motor closed loop speed control
Post by: Martinn on June 30, 2024, 03:58:54 pm
Did some more measurements.
Lowest reasonable feeds for a milling machine are in the order of 5-10 mm/min. Given a 4 mm leadscrew pitch that results in about 2.5 rpm (about 1% of the max 240 rpm at the output of the gearbox).
With the current setup I can get down to about 16 rpm, which is the lowest value that is reasonably stable. This gives 64 mm/min, which is well above of what I'd like to see (around 10 mm/min). BTW I added lowpass filtering in the current path, which gets rid of the oscillations.
Played a bit with the 30A8. Tried out different modes - current, voltage, IR compensation.
And I have to say this thing just... works. Out of the box with default values performance is already decent. For IR compensation I tried different compensation values (try to find R8 on the PCB) and you quickly find the one that is just right.
While there is not much to adjust overall, what they have as defaults is clearly very well designed.
So what did I get? With IR tuned and loop gain quite a bit below oscillation, lowest usable speed seems about 12 s/rev (after gearbox) or 5 rpm, resulting in 20 mm/min feed. Not quite as low as I'd like, but 3x lower than the previous analog implementation.
It looks like I have to come up with a mechanically sound way to integrate the amplifier to the feed unit. It is many times too large to fit into the original housing, so I'll have to 3d print something.
Title: Re: Brushed DC motor closed loop speed control
Post by: Martinn on August 21, 2024, 06:34:42 pm
I have a couple of Advanced Motion Control 30A8 servo drives (datasheet link (https://servo.a-m-c.com/hubfs/Datasheets/Servo-Drives/AxCent/AMC_Datasheet_30A8.pdf)) collecting dust.
[attach=1]
[attach=2]

3D printed an adaptor housing and connected the amplifier to the motor!
And it works! Velocity control loop is slightly unstable at low feeds, but I don't have easy access to measurement tools or other instrumentation in my workshop (anyway near the mill it's constantly full of chips), so most likely it will stay that way.
At some time a stepper feed on all axes might come, but certainly not this year.

Thanks again! Martin
Title: Re: Brushed DC motor closed loop speed control
Post by: H.O on August 21, 2024, 08:06:34 pm
You're welcome, I'm glad it worked out allright even if it's not perfect.
And this is without any sort of feedback. If you'd put a good old tachometer on the motor you'd have much better low speed control.

Anyway, thanks for the update!