If a voltage regulator was the only feasible solution I'd be looking at a low-drop-out buck regulator like the one below using the LT8638S (for example): -
The example circuit above is for a 12 volt regulator but it looks like it should be good for a 24 volt output at up to 5 amps without much trouble.
I'm not recommending that device in particularly (although it looks like it's a good match); I'm recommending a low-drop-out buck regulator and there are many offerings from Analog Devices and TI to choose from. The benefit is that you get high efficiency (circa 90%) and low drop-out.
The other option is (given that you have PWM control over the motor) is to use a boost regulator to provide you with about 30 volts. The LTC7841 appears to be a reasonable match: -
It should be able to work with a supply from below 20 volts to 28 volts and produce a 30 volts output. This will ensure that this situation might be avoided i.e. it provides enough voltage overhead to overcome this problem: -
@Andyaka it has to run on a 15 degrees inclined and declined surface
but it fails to do that even after full PWM signal
I have some suspicions that on a 24 volt supply it may fail to do what you expect it to do (as per the above comment) and so, using a boost regulator (like the one above) is probably something you need to consider. Or, maybe the LTC7804: -