Servo glitches?

NVHLVNOP

Member
I'm seeing a weird output glitch using the servo and servopos commands. I'm using the 20M2 chip. I initialize the servo with the servo command and use servopos to set the throttle position for a standard RC aircraft ESC (Castle Creations), but fairly frequently (maybe 2x-3x a minute the oscilloscope will catch it) the servo pulse train has a glitch – I get a pulse split up into a long and a short pulse. This glitch is audible as a brief bump in RPM, as the ESC likely interprets the first pulse as a command for 0 throttle. I've tried several things to rule out some possibilities. I've ruled out that it is not an issue with the code, the wiring length, and the Picaxe is not resetting. Something seems to be happening with the internal timing. Also FYI, the code takes a 20-sample average of the potentiometer reading to help smooth it out, and updates the servopos command at roughly 10 Hz.

clean.jpg shows the original waveform ~ 1600us
short.jpg shows the width of the first glitch ~ 36us
long.jpg shows the width of the second glitch ~ 500us

The error seems to always be with these two widths, and the start of the first glitch and end of the second line up with when the correct pulse is supposed to start and end. It doesn’t seem likely that this is a wiring issue since the pulses line up so exactly.

Has anyone seen position glitches/twitchiness in servos with servopos? Could something else be conflicting with the timer servopos uses? I am also using the ADC to read a potentiometer and i2c to send info to a DAC. Serial communication is not being used except on the download circuit.

Thanks!
 

Attachments

Goeytex

Senior Member
Glitches seem to come standard with the Picaxe servo/servopos commands. This is likely due to background operations using the timer(s).

I have found the glitches to be evenly spaced where the spacing depends upon the code and whether or not hardware serial, hardware I2C, hardware SPI, or interrupts have been enabled. Sometimes changing the value of a pause command will affect how often the glitch occurs.

In some cases, the glitching can be eliminated or minimized by adding the disconnect and disablebod commands at the beginning of the program code. If you add the disconnect command, the Picaxe will need to be hard reset for programming.

Good Luck
 

Goeytex

Senior Member
Attached is a simple program to test for inherent glitching. Connect one servo to B.2 via a 330R 330R Resistor. Remove all unnecessary components from breadboard.

Observe the servo for glitching during the 10 second delays. If glitching occurs un-remark the lines with disablebod and disconnect and see what happens. Let us know your findings.

Code:
[color=Navy]#Picaxe [/color][color=Black]20M2[/color]
[color=Navy]#no_data[/color]
[color=Green]'20M2 defaults to 4 MHz

'// Servo can only work at either 4Mhz or 16Mhz on M2

'//  Disablebod   ' Add to test
'//  Disconnect   ' Add to test[/color]

[color=Blue]Pause [/color][color=Navy]1000  [/color][color=Green]'stabilize[/color]

[color=Blue]Servo B.2[/color][color=Black], [/color][color=Navy]150 [/color][color=Green]'servo on Picaxe Pin (leg) 16 move to center[/color]
[color=Blue]gosub [/color][color=Black]delay10  [/color][color=Green]'wait 10 seconds [/color]

[color=Blue]do

    Servopos  B.2[/color][color=Black], [/color][color=Navy]80  [/color][color=Green]'move to stop
    [/color][color=Blue]Gosub [/color][color=Black]delay10      [/color][color=Green]'check for glitching
        
    [/color][color=Blue]ServoPos B.2[/color][color=Black], [/color][color=Navy]150  [/color][color=Green]'Back to center
    [/color][color=Blue]Gosub [/color][color=Black]delay10      [/color][color=Green]'check for glitching 
    
    [/color][color=Blue]ServoPos B.2[/color][color=Black], [/color][color=Navy]230  [/color][color=Green]'move to stop
    [/color][color=Blue]Gosub [/color][color=Black]delay10      [/color][color=Green]'check for glitching 
    
    [/color][color=Blue]ServoPos B.2[/color][color=Black], [/color][color=Navy]150  [/color][color=Green]'Back to center
    [/color][color=Blue]Gosub [/color][color=Black]delay10      [/color][color=Green]'check for glitching 
    [/color]
[color=Blue]Loop[/color]


[color=Black]delay10:   [/color][color=Green]'// Subroutine 10 second delay  

   [/color][color=Blue]for [/color][color=Purple]b0 [/color][color=DarkCyan]= [/color][color=Navy]1 [/color][color=Blue]to [/color][color=Navy]10
     [/color][color=Blue]pause [/color][color=Navy]1000
   [/color][color=Blue]next   
   return[/color]
 

erco

Senior Member
Goeytex is a guru and genius, definitely follow up with his BOD test.

I second using an initial 'DISCONNECT' command, which will prevent the 20M2 from periodically checking for new downloads. I have used that to get rid of some servo glitches, and overall I've found the 20M2 is remarkably stable even running 6 servos: https://www.youtube.com/watch?v=kF6TwbIT384

The only downside with using DISCONNECT is that you will have to do a hard reset for code uploads. That is, don't turn the PICAXE on until after you click "Program" in the PE.
 

NVHLVNOP

Member
So after messing around with it for the whole day, I could not get the glitch to go away while using the servopos command. I rewrote the code and am now using Pulsout command within different loops and I don't seem to get any more glitches. I had it hooked up to the scope for over 30,000 cycles and monitored wach one with pass/fail settings and everything was ok. It's not the best code out there, but it is working great so far.

Thanks to all for your help.
 

erco

Senior Member
That's disappointing. If your ESC is fairly high power, could your glitches be caused by EMI from the ESC or motor? Are your circuits isolated and powered separately, or by using a BEC?
 

Goeytex

Senior Member
Will try it. Just out of curiosity, why do you Gosub delay10 instead of simply Pause 10000?
Pause 10000 can cause a failed download with the "hardware not found" error message. Creating a long pause by using many shorter pauses prevents this error.
 
Top