PWM control while running other code (BS2)
MCA
Posts: 13
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
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
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?
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.
Maybe use a dedicated PWM controller like the Parallax PSC to off load the PWM processing.
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.
BR / Adam