 # Controlling 2 DC Motors by a PWM

Hello everyone,

I need a help with my project which is a 2 dc motors controlled by a pwm signal and this signal is depends on the value of the joystick.

Joystick output is 0-1023 and by mapping the value to 0-255 we can control the dc motor by a pwm signal, but this called "Open loop control" and the "Closed loop" requires a feedback which can be represented by an encoder.

I need the basic principle of designing and implementing a closed loop control system, Actually i have found more libraries that make the design and equations for feedback is more easiest. But i can't understand what i should do exactly? did the values i've got it is true or false?

Maybe if the rpm value is computed by the encoder so i can convert it to a bits and compares it with the input value of joystick and the desired output will be the difference between two values (feedback & input). I don't know how to convert the rpm value to a bits because i'm working with a libraries and don't know what the equations are used.

Please feel free to comment, but no one knows everything and he should learn to know, right?

Maybe if the rpm value is computed by the encoder so i can convert it to a bits and compares it with the input value of joystick and the desired output will be the difference between two values (feedback & input).

You can't know the "desired output"... If you knew the desired output, you wouldn't need feedback... You only know if the motor is going too fast or too slow.

Building a stable linear-feedback system can actually get very "mathematical" (and I never took that class in college). But with trial-and-error you can probably get it to work, and you might be able to make it stable by simply slowing-down the changes so you don't overshoot your target speed.

I don't know how to convert the rpm value to a bits because i'm working with a libraries and don't know what the equations are used.

Again you don't really need to do that, you just need to know if you should speed it up or slow it down, and then you increase or decrease the PWM until you hit the target speed.

If there's enough rotational inertia and if you can read the RPM quickly enough, you can "create your own PWM" by turning-on motor power whenever the speed is below target and turning it off when it goes over-target. (That's how a thermostat works to control heat... It doesn't try to guess how much heat you need, it just turns-on until the target temperature is reached and then it turns off.)

DVDdoug:
You can't know the "desired output"... If you knew the desired output, you wouldn't need feedback... You only know if the motor is going too fast or too slow.

Building a stable linear-feedback system can actually get very "mathematical" (and I never took that class in college). But with trial-and-error you can probably get it to work, and you might be able to make it stable by simply slowing-down the changes so you don't overshoot your target speed.
Again you don't really need to do that, you just need to know if you should speed it up or slow it down, and then you increase or decrease the PWM until you hit the target speed.

If there's enough rotational inertia and if you can read the RPM quickly enough, you can "create your own PWM" by turning-on motor power whenever the speed is below target and turning it off when it goes over-target. (That's how a thermostat works to control heat... It doesn't try to guess how much heat you need, it just turns-on until the target temperature is reached and then it turns off.)

The speed control in my project have no problem and the target speed ouput (pwm) is a variable value depends on the joystick output so i think there's no overshoots and the speed is controlled by a joystick from slow to speed up but i can't be ensure the joystick value will give me the same speed in other circumstances as you know the motor is affected not just by the voltage right? So what is the gurantee that the joystick output is giving me the desired output? And to be ensure of that, the encoder should be used to compares the input and feedback by an mathmatical equation:

E(s)=N(s) - H(s)

E(s): The error
N(s): The input (joystick output)
H(s): The feedback of motor

If i got the error value as a bits not rpm the problem will be solved.

If (error=0) output will be the same.
If (error=.25) output will be (output+error)

This is a simply which i studied in college and need suggestions to done my project.

Thanks

UnoWatt:
My code attached does exactly that.

I didn't use a joystick, but have an LCD with buttons: the up and down buttons scroll the speed up or down, then use Select to pass that displayed value in to the sketch. So motorSpeedRequested is the one you change with the buttons, and that becomes motorSpeedSelected once you hit select.

Left button is stop, right button toggles the backlight.

The encoder figures out the speed motorSpeedActual, and the PWM is adjusted if that differs from motorSpeedSelected. Depending on how many slots your encoder has, change the variable pulsesPerRev.

Only funny in the code is that my motor doesn't spin with PWM below about 125 so I never let PWM below that, except for when I ask it to stop.

Let me have a look and adjust it with a joystick...

Why don't you use a PID controller for the motor speed?

DC motors do not react proportionally on PWM and load changes, and don't start below a minimal voltage. That's why a closed loop and controller is required.

DrDiettrich:
Why don't you use a PID controller for the motor speed?

DC motors do not react proportionally on PWM and load changes, and don't start below a minimal voltage. That's why a closed loop and controller is required.

Actually, I need more suggestions about this research and the PID in my mind but i don't know from where exactly should i starts!

Could you tell me what is the difference between the PID controller and the classic methods?

Thanks

PID is a simple classic controller. See wikipedia.

If the actual speed is less than the max speed then continue increasing the pwm respect with joystick value.

But if the actual speed is more than the max speed then:

pwm=speed requested - (actual speed - max speed)