Pages: [1]   Go Down
Author Topic: The simplest possible software PWM routine?  (Read 406 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
God Member
*****
Karma: 27
Posts: 829
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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?
Logged

Global Moderator
UK
Offline Offline
Brattain Member
*****
Karma: 240
Posts: 24452
I don't think you connected the grounds, Dave.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
Logged

"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.

Global Moderator
Boston area, metrowest
Offline Offline
Brattain Member
*****
Karma: 443
Posts: 23837
Author of "Arduino for Teens". Available for Design & Build services. Now with Unlimited Eagle board sizes!
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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, ...
Logged

Designing & building electrical circuits for over 25 years. Check out the ATMega1284P based Bobuino and other '328P & '1284P creations & offerings at  www.crossroadsfencing.com/BobuinoRev17.
Arduino for Teens available at Amazon.com.

Offline Offline
God Member
*****
Karma: 27
Posts: 829
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
Logged

Global Moderator
Boston area, metrowest
Offline Offline
Brattain Member
*****
Karma: 443
Posts: 23837
Author of "Arduino for Teens". Available for Design & Build services. Now with Unlimited Eagle board sizes!
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
Logged

Designing & building electrical circuits for over 25 years. Check out the ATMega1284P based Bobuino and other '328P & '1284P creations & offerings at  www.crossroadsfencing.com/BobuinoRev17.
Arduino for Teens available at Amazon.com.

Offline Offline
God Member
*****
Karma: 27
Posts: 829
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Ahh... of course. smiley

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.
Logged

UK
Offline Offline
Shannon Member
****
Karma: 184
Posts: 11197
-
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
Logged

I only provide help via the forum - please do not contact me for private consultancy.

Offline Offline
God Member
*****
Karma: 27
Posts: 829
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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?
Logged

UK
Offline Offline
Shannon Member
****
Karma: 184
Posts: 11197
-
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
Logged

I only provide help via the forum - please do not contact me for private consultancy.

Pages: [1]   Go Up
Jump to: