PWMServo High frequency Digital Servos issues

Status
Not open for further replies.
Hello paul,
i am using Teensy3.6 with PWMServo library. I am using Hitech High speed servo which needs 760uS to 1020uS, with 200 to 500Hz. standard servo PWM range of 1000 to 2000us,50hz wont work. How can i increase the PWMfrequency to 200hz from 50Hz. i think,Library still accepts input min & Max 760 to 1020. also I have few more issues associated with this Library
1. if i make a write call, will it keep generating the pulses continuously 50Hz or 200Hz rate or ,do i need to call the Write function every 1/50Hz ms?
2.I have a RPM read function which works on interrupt counting method.RPM counting worked well,after adding the PWMservo function calls, RPM function stopped working. Does interrupts gets disabled when PWMservo is used?

Thank u

with regards
Dr.N.Chandra sekhar,M.tech,Phd
 
I would use analogWrite instead of the Servo library. I've successfully used this on servos requiring the smaller range that you mention with up to a 800 Hz refresh rate. Plus, you can send all of the servo commands simultaneously instead of the round robin fashion that Servo library uses, which is often advantageous.

Here is more information on analogWrite:
https://www.pjrc.com/teensy/td_pulse.html
 
1. if i make a write call, will it keep generating the pulses continuously 50Hz or 200Hz rate or ,do i need to call the Write function every 1/50Hz ms?
2.I have a RPM read function which works on interrupt counting method.RPM counting worked well,after adding the PWMservo function calls, RPM function stopped working. Does interrupts gets disabled when PWMservo is used?

you only need to issue the write() once. The library does briefly disable interrupts when the write() is issued.
you can change frequency in the library's attach() function:
analogWriteFrequency(pin, 50);
see https://github.com/PaulStoffregen/PWMServo/blob/master/PWMServo.cpp#L179

also review https://www.pjrc.com/teensy/td_libs_Servo.html
 
Last edited:
Hi Brtaylor,
Thank you for Quick reply..Actually in my code i am updating the servo position in variables and making a call to update all servos every 10msec. so if i change to analog write i got to use as following.
1. in setup
analogWriteFrequency(4, 300)
analogWriteResolution(12);

2.in updateservos
analogWrite(4, xxx); //scaled value between 760 to 1020 i.e (760/(1000000s/300Hz)*1023)=233 , (1020/(1000000s/300Hz)*1023)=313

correct me if i am wrong. Also Does it create any issues with interrupts?
 
To change the frequency, edit line 179 as Mantiou suggested. You will also need to edit the write function. The simplest way would be to comment out lines 196 & 197 and uncomment 198 & 199. Then edit "20000.f" (which is the number of microseconds for 1 cycle of 50 Hz) to "5000.0f". Since you're using Teensy 3.6 which has a FPU, there's little point to the extra effort adapting the much less intuitive integer-only lines.

The extremely short interrupt disable time is unlikely to have caused your pulse counting to fail. Unless you post a complete program demonstrating the problem, I'm not going to even being to speculate why it stopped working, other than saying it's very likely a mistake in your code, not PWMServo.

Internally, the PWMServo library uses the analogWrite approach brtaylor suggested. It's fairly simple to do without PWMServo. If you want to avoid PWMServo very brief interrupt disable, you can just set analogWriteResolution() once in your setup() function. Then you won't need to disable interrupts. But of course if you use any other non-servo PWM, remember analogWrite will be expecting whatever resolution you configured. PWMServo disables interrupts very briefly so it can temporarily change this setting, do analogWrite at higher resolution, and then restore your setting. The interrupt disable is a cautious step to prevent any other code that might be doing analogWrite from interrupts (like the Talkie library) from possibly seeing the altered setting.

You might also consider using FreqMeasure or FreqMeasureMulti to read RPMs. It uses input capture, which offers much better accuracy than pulse counting at the relatively low frequencies of most RPM measurements. If you do use these and also PWMServo, you need to be careful to choose pins controlled by different FTM timers. This is documented on the PWM page. Scroll down to PWM Frequency and check out the table showing the 5 groups of pins on Teensy 3.6. You want to make sure the pins used PWMServo aren't within the same group as those used by FreqMeasure or FreqMeasureMulti.
 
Status
Not open for further replies.
Back
Top