Shop OBEX P1 Docs P2 Docs Learn Events
PWM control while running other code (BS2) — Parallax Forums

PWM control while running other code (BS2)

MCAMCA Posts: 13
edited 2013-09-28 18:35 in General Discussion
I am planning to use PWM control with a BS2 via an external motor driver. The motor driver prefers 100-400 Hz, so I can't use the BS2 PWM command, but instead I'll use a loop with "high, pause, low, pause".
The problem is that I then want to keep the motor running (thus the PWM loop as well) while I calculate from sensor data if a change in the PWM cycle (i e motor speed) should be made. How can that be done, I guess it's a standard issue?

I was thinking about somehow stepping through the parts of the other code by inserting it in the PWM loop, but that seems very arbitrary and crude; there has to be another way?
(Also, I read somewhere that sort of average for BS2 statement execution time is 250 µs. At 400 Hz the PWM cycle time is 2,5 ms, meaning that a single statement corresponds to around 10% of that so it would definitely make limitations to the PWM duty cycle.)

I have considered running the PWM loop a while, then making the sensor calculations and corrections and then start the PWM loop again but that would mean stopping the motor entirely for a noticable time, and I find that unacceptable.

(While writing, I just had a new idea: a capacitor may do the trick? - but anyway, I'm still interested in the control issue)

BR / Adam

Comments

  • GadgetmanGadgetman Posts: 2,436
    edited 2013-09-27 04:40
    Generally, I'd say you're up the creek without a paddle...

    Which BS2 version do you have?
    (They vary in instructions/second) )

    What kind of motor are you using?
    How heavy is the load?

    You may be able to let it 'freewheel' for a few mS while you recalculate the speed?
  • LoopyBytelooseLoopyByteloose Posts: 12,537
    edited 2013-09-27 05:00
    A Propeller will do this easily as it has two independent timers per cog that can be free running PWM while the CPU does other tasks.

    The BS2 doesn't have any free running timer access (even though some use the SX48 chip that includes two such timers). So it is rather single-minded about what it does.
  • CuriousOneCuriousOne Posts: 931
    edited 2013-09-27 05:09
    Yes, I really miss HPWM statement in Basic Stamp....
  • Mike GMike G Posts: 2,702
    edited 2013-09-27 05:22
    The STAMP executes one statement at a time. The STAMP can not execute a loop and read sensor data concurrently. The appearance of concurrency is possible by invoking sensor logic while executing in a tight loop. It has been done many times over... However, STAMP timing will not be as deterministic as a multicore controller like the propeller as stated in post #3

    Maybe use a dedicated PWM controller like the Parallax PSC to off load the PWM processing.
  • Beau SchwabeBeau Schwabe Posts: 6,566
    edited 2013-09-27 08:24
    You can setup a state machine of sorts where you effectively superimpose your program overhead with the PWM output you want. To do this though you need to have some degree (not extremely accurate) of deterministic timing inside of the Program overhead. The PWM overhead simply tracks a value determined in the Program overhead but in and of itself needs to follow somewhat of a deterministic loop between time ON versus time OFF.

    For example If the Program overhead requirement was 10% of the total period, then the MAXimium PWM output would effectively only be 90% of the supply voltage. (Assuming your driving a motor). In other words if the motor Supply volatge was 5V then effectivly you would only be driving the motor with 3.3V ... 5V x 90% = 4.5V minus 1.2V of loss within an H-Bridge (<-see note) equals 3.3V ... If you had a 3V motor then this would be perfect.

    Note: Depending on the type of motor driver itself, this can vary greatly.

    PWM.jpg
    1024 x 826 - 40K
    PWM.jpg 40.3K
  • MCAMCA Posts: 13
    edited 2013-09-28 12:49
    Thanks for your replies! I'm considering using an external driver of some kind. Actually I found that I have read about this in Robot Builders Bonanza a long time ago and there is actually a suggestion there on how to solve it, although I'm not sure it's optimal in this case. What I need would really just be something that echos the PWM loop and continues to do so until a changed loop is sent. I have not read up on the Parallax PSC yet, maybe that will work, I'm not really familiar with servo drivers.


    BR / Adam
  • kwinnkwinn Posts: 8,697
    edited 2013-09-28 18:35
    Dual presettable counters and a FF might work, but you are now looking at three additional chips.
Sign In or Register to comment.