Controlling brushed motor speed with variable load with a PWM
Your idea is wrong and won't work. But there is a very similar idea that will work.

"a brushed motor with a constant-voltage power supply, it will always rotate at about the same speed" No. If you load it a lot, it will slow down a lot.

Your idea is to basically monitor the average voltage across the motor terminals and adjust the PWM so that a constant average voltage is applied to the motor in order to maintain constant speed with torque (this would also require monitoring the battery voltage as well). But this won't maintain constant speed with torque. It will just maintain an ratio between battery voltage and average voltage applied to the motor as the battery voltage decreases as it is drained.  Also, if this is your goal it's easier to just to measure the battery voltage directly and adjust the PWM accordingly. No need to measure the messy voltage across motor terminals. But motor speed will still change as torque changes.



What you really want is to measure the motor's back-EMF (BEMF) and adjust the PWM to keep the BEMF constant. The BEMF is the voltage a motor generates when it is coasting. In other words, this is the voltage the motor generates when it is being back driven and acting as a generator. But now the question is, how do you measure the voltage across the motor while it is coasting coasting if you are driving it with a PWM waveform so it is not coasting?

Well, first, you do not want to continuously being sampling the motor terminal voltage in an uncontrolled manner while PWM is happening because the drive voltage (when the switches are closed and sending current through the motor) are mixed in with the BEMF voltage (when the switches are open and current is not going through the motor)

If you want to sample the BEMF while PWMing the motor you need to do the sampling when you know both HI side switches are open so that the motor isn't being driven, but you want one LO side switch to be closed so that you are measuring with respect to GND.

Your MCU may allow your PWM timer to trigger ADC samples so you can sample it directly and ignoring it when the motor current is not being forced through the motor and ignore it at all other times. Might need some RC filtering but not something like an LC.

If you can't do this, there are less elegant work arounds like having the PWM signal drive interrupts that cause the ADC to take a sample. At worst, you could directly feed the PWM signal from an output pin directly back into an interrupt pin.