Pause Between Servo Off & PWM

erco

Senior Member
My 20M2 robot (4 MHz) uses servo commands and PWM, not at the same time to avoid conflicts. I switch the servos off then immediately PWM a signal. It didn't work until I added a pause in between. Pause 200 worked for me, possibly less. Might save someone some grief in the future. I guess the internal timer takes a bit to shift gears.

servo 6, off
servo 7, off
pause 200 ' need a brief pause after switching servos off or PWM won't start
pwmout c.3, 199, 400
 

inglewoodpete

Senior Member
It's surprising that a pause is even necessary when switching the outputs of timers. It's not as though there are Windows X - styled multiple threads for multiple CPU cores, needing to be serviced.

Having said that, I have found there can be certain situations where a pause can be needed, like when you switch internal resonator speeds and it needs to stabilise.
 
Last edited:

erco

Senior Member
I start every program with a brief pause, just to let things wake up before the grind starts. Same as my own morning routine.
 

hippy

Technical Support
Staff member
I don't recall ever mixing SERVO and PWMOUT commands but, assuming the issue is related to the same timer being used for both, I would guess it has something to do with the servo timing loop still running after the SERVO OFF commands, the current servo loop needing to complete before the timer can be used for something else.

My guess is, when servos are running, at the end of the servo loop, it internally interrupts, checks to see if another loop needs to be done and, if not, the timer won't create another internal servo loop completed interrupt, the timer is freed for other use.

With the servo loop running, the PWMOUT command possibly sets the timer such that the servo loop never completes and the timer cannot be used for PWMOUT until it does.

The servo loop is approximately 20ms long so a PAUSE 20, or maybe a little more, should be enough to allow the servo loop to have ended after the last SERVO OFF.
 

AllyCat

Senior Member
Hi,
The servo loop is approximately 20ms long so a PAUSE 20, or maybe a little more, should be enough to allow the servo loop to have ended after the last SERVO OFF.
Yes, that's very similar to what I found for a [promo] code snippet [/promo] which I submitted recently. ;)

I needed to include the following (in the subroutine "start:") to achieve my desired timing result :
Code:
    setfreq m16 
    pause 100                        ;* 25 ms to allow the 20 ms (servo) timer to update *
Cheers, Alan.
 

mikeyBoo

Senior Member
hi erco,
You can avoid the Servo/PWM conflict entirely by using a 2nd PWM command to control your servo. You just set your repeat frequency (50Hz 245Hz etc.) & then vary the duty cycle to set the servo position. If my memory is correct, the 20M2 has 4 PWMs so you could have 4 independent servos.
Take a look at the attached .pdf the upper righthand block labeled “Servo Operation”.

You can take a look at procs init & TMotor.servoPosition_Set in my old Kayak Control Picaxe BASIC app to see how it’s done. Ain’t much to it, you might want to set a finer step size.
https://picaxeforum.co.uk/threads/picaxe-kayak-control-project.28063/

Wish I could help more but I got “mama duty” today.
 

Attachments

Top