Shop OBEX P1 Docs P2 Docs Learn Events
problems with the speed control of motors using PWM — Parallax Forums

problems with the speed control of motors using PWM

FernandobaezFernandobaez Posts: 2
edited 2005-11-14 17:52 in General Discussion
Hi!

I´m working on a school project that consists in a vehicle that follows a line.
The movement and the change of directions depends on two· motors , located in each side of the vehicle

At the moment we use the PWM instruction like this:
PWM 5,125,255
I get a continuous voltage in the Output 5 of around 3Volts ...
but if I add in the same program· another PWM instruction, like this:

PWM 5,125,255
PWM 6,125,255

I would hope to receive another continous voltage in both Output 5 and 6.· But I do not have this result.
I got unstable voltages that produce our motors to have an uncontinous movement, turning on and off.
Í don´t know if the presence of another PWM instruction in the same program causes some kind of noise.

Do you have an explanation or a tip for achieving what we want?

I would appreciate a lot your response, because we have been stucked in this part of the project, we are supposed to hand in this for next thursday

Thanks!

Comments

  • metron9metron9 Posts: 1,100
    edited 2005-11-13 23:47
    That's because on a BS2 the PWM 5,125,255 takes 255 Milliseconds to complete, It basically stops at that command for 255 milliseconds, then issues the next command for the other motor.

    In the help file we se that:

    The term "PWM" applies only loosely to the action of the BASIC Stamp's PWM command. Most systems that output PWM do so by splitting a fixed period of time into an on time (1) and an off time (0). Suppose the interval is 1 ms and the duty cycle is 100 / 255. Conventional PWM would turn the output on for 0.39 ms and off for 0.61 ms, repeating this process each millisecond. The main advantage of this kind of PWM is its predictability; you know the exact frequency of the pulses (in this case, 1 kHz), and their widths are controlled by the duty cycle.


    BASIC Stamp's PWM does not work this way. It outputs a rapid sequence of on/off pulses, as short as 1.6 µs in duration, whose overall proportion over the course of a full PWM cycle of approximately a millisecond is equal to the duty cycle. This has the advantage of very quickly zeroing in on the desired output voltage, but it does not produce the neat, orderly pulses that you might expect. The BS2, BS2e, BS2sx, BS2p and BS2pe also uses this high-speed PWM to generate pseudo-sine wave tones with the DTMFOUT and FREQOUT instructions.

    So what it is doing is sending the duty cycle every mS and doing that for 255 mS

    You want both motors to receive 3 volts at the same time

    So to do this you need to alternate both motor drives by using a less cycles of each and putting a loop around them. To buffer the output for a smoother transition you may need to increase the capacitors as the loop will take about 1mS per iteration , so starting with oh say 5 cycles you have 5 for each motor and one for the for next loop, thats 11 mS total and you want a total of 255 mS so 255/11= about 23 so

    for i=1 to 23
    PWM 5,125,5
    PWM 6,125,5
    next


    Should drive both motors

    I have not put this on the scope but I think it will work.


    I put it on the scope, It takes about .25 mS between the two PWM instructions and about 1mS to process the NEXT and go back to the top of the first PWM statement

    So 1.25 MS per loop iteration and 5 mS per motor say 11 1/2 mS per iteration, so adjust the amount of time you want the motors ON by the number of times the loop processes.

    Using 1 ms for the cycle it's about 3 1/2 ms per iteration so if you wanted the motors to run for 1 second you would take 1000/3.5 a loop count of 285 would do the trick.
    You will have to see how the motors respond as the longest cycle/shortest loop i would think would give the best performance but too long of cycle and you are going to see a shake that body dance and that could perhaps be a good thing say if someone had an old Elvis record playing or something.

    Post Edited (metron9) : 11/14/2005 12:21:34 AM GMT
  • Beau SchwabeBeau Schwabe Posts: 6,568
    edited 2005-11-14 01:19
    Fernandobaez,

    The PWM function from a BS2 is not optimized to directly drive a motor, it is better suited for creating an analog
    voltage reference.

    "Pulse-width modulation (PWM) allows the BASIC Stamp (a purely digital device) to generate an analog voltage."



    Also, you do NOT want to drive your motors directly from a Stamp pin. You must use a transistor for this.

    These links may help...
    www.parallax.com/dl/docs/cols/nv/vol1/col/nv6.pdf
    www.parallax.com/dl/docs/cols/nv/vol1/col/nv23.pdf

    http://forums.parallax.com/attachment.php?attachmentid=37701

    webpages.charter.net/schwabelove/BasicStamp/pwm.gif
    webpages.charter.net/schwabelove/BasicStamp/StampII-to-1267.gif


    of course you could save yourself alot of headache and use one of our motor control options.
    www.parallax.com/html_pages/products/motorcontrol/motor_control.asp

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Beau Schwabe

    IC Layout Engineer
    Parallax, Inc.

    Post Edited (Beau Schwabe (Parallax)) : 11/14/2005 1:33:11 AM GMT
  • Tom WalkerTom Walker Posts: 509
    edited 2005-11-14 16:20
    Also, keep in mind that the Stamp is a single-tasking beast so that if you are expecting the PWM to start on one pin and then the next instruction (the PWM to the second pin) to execute WHILE the PWM is going to the first, you probably have to rethink your design. Your current style of programming will always result in a "wobbly" path as the first motor moves, then stops, then the second motor moves, then stops, etc...

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Truly Understand the Fundamentals and the Path will be so much easier...
  • FernandobaezFernandobaez Posts: 2
    edited 2005-11-14 17:52
    Thank you very much for your responses... They helped me a lot, and right now my project is working perfectly.

    After this, I will believe in the existence of forums!
Sign In or Register to comment.