Go Down

### Topic: The simplest possible software PWM routine? (Read 694 times)previous topic - next topic

#### Retroplayer

##### Feb 05, 2013, 09:08 pm
I have an application where I will be controlling 10 motors, each of which use two pins (one for forward, one for reverse). I need to control the position of these motors, so PWM seems to be the way to go. But I would need 20 PWM signals total.

The parameters of what I need are actually very very simple. The period of the pulsewidth is 8ms max with 1ms resolution. That's 125Hz frequency with steps of 12.5%. That's only 8 possible steps (well, 9 if you count 0.)

Since I need 20 PWM signals, using the hardware PWM is out of the question. It would seem that the demand of what I need to accomplish is pretty simple really anyway and could be accomplished with pseudoPWM. I am looking for the simplest possible way to accomplish this within my code, preferrably without using a library, but I am investigating the softPWM library right now.

Any suggestions?

#### AWOL

#1
##### Feb 05, 2013, 09:14 pm
Why do you need two PWMs per motor?
It would be more usual to have one PWM and one direction pin (possibly plus brake).
I would think 125Hz would be doable, using an adaptation of the blink without delay example.
"Pete, it's a fool looks for logic in the chambers of the human heart." Ulysses Everett McGill.
Do not send technical questions via personal messaging - they will be ignored.

#2
##### Feb 05, 2013, 09:15 pm
Where do you get information on what the motors are supposed to be doing?
Seems like blink without delay type code would do it.
Every pass thru loop, you see if 1mS went by and adjust your hi/low outputs as needed for the individual output.
Have a counter for each output, 0 to 9, adust the count at each mS, when the limit is reached change the output and reset the count;
ex: when 6 is reached, flip the output, reset to 3, when 3 is reached flip the output, reset to 6, ...
Designing & building electrical circuits for over 25 years.  Screw Shield for Mega/Due/Uno,  Bobuino with ATMega1284P, & other '328P & '1284P creations & offerings at  my website.

#### Retroplayer

#3
##### Feb 05, 2013, 09:45 pm

Why do you need two PWMs per motor?
It would be more usual to have one PWM and one direction pin (possibly plus brake).

I am not using a motor shield. Modifying existing hardware which has simple transistor based H-Bridges. Only forward and reverse. No brake, no enable, etc...

Where do you get information on what the motors are supposed to be doing?

I used a logic analyzer to look at what is going on with the existing controller hardware.

Thanks for the suggestions, guys. I need to look at the blink without delay code then.

#4
##### Feb 05, 2013, 09:48 pm
Quote
I used a logic analyzer to look at what is going on with the existing controller hardware.

No no, I meant what determines when a PWM is at 12% vs 25% vs 75% for example.
With 20 IO, you are out of Uno pins if using them directly.
You could use SPI into 3 shift registers tho and update all outputs at one time every mS.
Designing & building electrical circuits for over 25 years.  Screw Shield for Mega/Due/Uno,  Bobuino with ATMega1284P, & other '328P & '1284P creations & offerings at  my website.

#### Retroplayer

#5
##### Feb 05, 2013, 10:51 pm
Ahh... of course.

Running the motors at full bore causes them to overshoot the position, so in order to do accurate position control, I need to slow the motor down as it approaches the correct position. I also need to scale that based on how big of a change in position I am making. So, essentially PD control. I *think* the I in PID means ramping up, running, and slowing down during the movement. I don't need to do that, just slow down at the end of the movement as it approaches the position.

I am not 100% how I will do that yet, but right now I am thinking of just inserting that part into my code where I compare the position to determine direction. I have a while loop going for that. As the numbers get closer, I step down the voltage. I am completely open to suggestions on this, however.

#### PeterH

#6
##### Feb 06, 2013, 12:18 am

I *think* the I in PID means ramping up, running, and slowing down during the movement.

No, it doesn't mean that. The Integral term eliminates steady-state errors i.e. where the system needs a non-zero control input in order to hold the error at zero.

By analogy with a car suspension: Proportional corresponds to a spring - it apply more correction the greater the offset; Integral corresponds to load leveling - it prevents the system from settling with a non-zero error; Differential corresponds to dampers - it reduces the control signal when the error is reducing, and increases it when it is increasing.

If anything, the 'ramping' effect you refer to corresponds more to the Differential element. I don't know enough about your system to judge how important that would be for you. In many systems the Differential component is crucial to prevent overshooting and hunting.
I only provide help via the forum - please do not contact me for private consultancy.

#### Retroplayer

#7
##### Feb 06, 2013, 12:29 am
That is exactly what I need for my application. As the error (error being the difference between current position and commanded position) reduces, I need to slow the motor down to prevent overshoot. For the proportional part of it, I just need to decide how long to keep the motor on based on how great the difference is.

So, I am trying to wrap my head around it and am probably over-complicating it. What I think I need to do is compare the currpos to the demanded position on a timer. At the starting error, I could probably do this by just calculating the time based on the distance. But then I need some trigger point as it gets closer, say within 16 counts of the final position and start slowing things down. That sounds overly complicated to me, though and not really uniform.

So I guess a basic question is do I need PID? Do I need PD? Is there some other, simpler way?

#### PeterH

#8
##### Feb 06, 2013, 12:39 am

So I guess a basic question is do I need PID? Do I need PD? Is there some other, simpler way?

You don't need PID - there are always alternatives, some a lot simpler. But PID is a very flexible general purpose feedback algorithm which can be used in a wide range of situations. Given that the PID library already exists, the hard work has already been done. The only barriers to using it are getting your head round the theory of the PID algorithm, understanding how to incorporate the PID library into your sketch, and experimenting to find the best feedback parameters.
I only provide help via the forum - please do not contact me for private consultancy.

Go Up

Please enter a valid email to subscribe