Calculating the register settings for a test pulse
A servo pulse is repeated x times a second. The specific frequency is not critical it should be somewhere around 50Hz for a typical servo. What is important is the pulse length. At 50Hz the the entire cycle is 20ms. A servo pulse is some where between 1ms and 2ms. That's between 5% and 10% of the cycle. Given we only have a max of 10% of the PWM to play with any errors are going to be fairly significant.The PCA9685 uses 2 pairs registers per channel to set the pulse. It has a resolution of 4096bits, thats why it takes a pair of 1byte registers to represent a value with maximum of 4096. The value represents a count of time in the cycle. The first pair represents when the output will be turned on, the second when the output will be turned off.
To determine the frequency a pre scaler is used to scale the PCA9685 internal oscillator from 25MHz to the appropriate value. At 25Mhz 4096 counts would be over way to fast 0.16ms incase your wondering. (Ok I admit thats not actually possible, in fact the actual max of the output is 1Khz) The goal is to get a the modulation frequency to close to 50Hz.
The datasheet provides the formula for setting the prescaler to provide the expected frequency:
prescaleVal = round(oscClock/(4096 * updateRate)) - 1
My Java driver includes a method to set the PWMFrequency
public int getPreScale(int frequency) throws IOException{ LOGGER.debug("Get prescale value for frequency {}", frequency); double prescaleval = CLOCK_FREQUENCY; prescaleval /= RESOLUTION; prescaleval /= frequency; prescaleval -= 1.0; LOGGER.debug("Estimated pre-scale {}", prescaleval); prescaleval = Math.round(prescaleval + 0.5); if(prescaleval > 254){ throw new IOException("Specified frequency " + frequency + " results in prescale value " + prescaleval + " that exceed limit 254"); } int prescale = (int)prescaleval; LOGGER.debug("Final pre-scale {}", prescale); return prescale; }
To test this I needed to request to send known output to the output channel.
If the resolution is 4096 the time for that amount of counts is 20ms it simply 4096/20 = 204.8 to figure out how many counts represent a 1ms pulse, 1ms being the lowest servo pulse. I happened to round down to 204 or 0xCCh cause it was neater and is insignificant. I hard coded these to set the registers.
try { device.writeRegister(PCA9685Device.LED0_ON_HIGH, (byte) 0x00); device.writeRegister(PCA9685Device.LED0_ON_LOW, (byte) 0x00); device.writeRegister(PCA9685Device.LED0_OFF_HIGH, (byte) 0x00); device.writeRegister(PCA9685Device.LED0_OFF_LOW, (byte)0xCC); }catch(IOException ioe){ LOGGER.error("Error setting pulse", ioe); }
The four registers are set for LED0 (channel 0) the first two specify the count when it should turn on. This is set to 0 counts. The pulse will begin at the start of each cycle. 204 is less than 255 so the off high byte is 0 with 204(0xCCh) in the off low byte.
I hooked up the pocket scope to see what the output was. I was a bit surprise to see that the trace did not measure a full second. It's somewhere around 950µs. I wasn't expecting it to be off by that much.
I looked at a lot of sample code before starting my own driver. I had actually written some python code based on some example. I remember seeing one a comment in the code about some correction factor for the PCA9685. Turns out cyanogilvie on github reported an issues to adafruit Frequency accuracy issue
In that issue he reported that a correction factor of 0.9 on the frequency does the trick.
double correctedFrequency = frequency * 0.9; // Correct for overshoot in the frequency setting (see issue #11). double prescaleval = CLOCK_FREQUENCY; prescaleval /= RESOLUTION; prescaleval /= correctedFrequency;
With that correction I get the output below. Now it's much closer. It's still not perfect but it's within acceptable tolerances for my purpose now.
I had significant doubts that a $50 scope would be useful enough. I had read other people were looking to use it for measuring servo inputs. At least I can confirm its of some utility for that purpose.
I did not measure how close the cycle is to 50Hz as I mentioned it is not that important. The key factor is the pulse length.
I would recommend adding this correction. I have seen a lot of code out there that does not use this correction. At least I and two others have measured the output can validate it with these parameters.
I have read your article. It is really useful for me. Start to learn Java programming because there is a massive demand for Java developers in IT sectors. It has a great scope in future.
ReplyDeleteJAVA Training in Chennai | JAVA course in Chennai
The information you have posted here is really useful and interesting too & here, I had a chance to gather some useful information, thanks for sharing and I have an expectation about your future blogs keep your updates please.
ReplyDeleteRegards,
JAVA Training in Chennai|J2EE Training in Chennai
In near future, big data handling and processing is going to the future of IT industry. Thus taking Hadoop Training in Chennai | Big Data Training in Chennai will prove beneficial for talented professionals.
ReplyDeleteNowadays, most of the businesses rely on cloud based CRM tool to power their business process. They want to access the business from anywhere and anytime. In such scenarios, salesforce CRM will ensure massive advantage to the business owners.Cloud Computing Training in Chennai
ReplyDeleteNice Post...
ReplyDeleteLinux Training in Chennai
Red Hat Linux Training in Chennai
Red Hat Training in Chennai
Hi dear, I like your post so much, and it is very good. Hope that we can communicate with each other. I want to introduce an aso services, I try it and I feel it is so good to us beginners.
ReplyDeleteI really happy to visit your innovative blog, This is very valuable and also helpful for increasing my knowledge. I am waiting for your more post from your blog and keep it up...!
ReplyDeleteOracle Training in Chennai
Oracle Training institute in chennai
Social Media Marketing Courses in Chennai
Tableau Training in Chennai
Primavera Training in Chennai
Unix Training in Chennai
Oracle DBA Training in Chennai
Power BI Training in Chennai
Oracle Training in Chennai
Oracle Training institute in chennai
Updating with the current trend is strictly advisable and the content furnished here also states the same.
ReplyDeleteThanks for sharing this wonderful and worth able article in here. The way to expressed is simply awesome.
Keep doing this job. Thanks :)
seo training institute
seo training
seo services
seo company
seo digital marketing course
seo training institute
seo course online
seo services
seo course fees
seo course near me
seo course in chennai
seo training in chennai
seo training institute
seo training course
seo training institute in chennai
seo training institute
seo training institute enar me
seo training fees in chennai
best seo training in chennai
seo training fees in chennai
seo training center in chennai
seo training and placement in chennai
best seo training center in chennai
best seo training center in chennai
best seo training institute in chennai
Every day I always visit sites to obtain the best information for materials research I was doing.......
ReplyDeleteBig Data Hadoop Training In Chennai | Big Data Hadoop Training In anna nagar | Big Data Hadoop Training In omr | Big Data Hadoop Training In porur | Big Data Hadoop Training In tambaram | Big Data Hadoop Training In velachery
bitcoin nasıl alınır
ReplyDeletetiktok jeton hilesi
youtube abone satın al
gate io güvenilir mi
referans kimliği nedir
tiktok takipçi satın al
bitcoin nasıl alınır
mobil ödeme bozdurma
mobil ödeme bozdurma
thank you a lot for sharing this all-powerful weblog.Very inspiring and harmonious to steer too.wish you still allocation more of your ideas.i will definitely love to gate.! VB Audio Cable Download
ReplyDeleteFon Perde Modelleri
ReplyDeleteNumara Onay
turkcell mobil ödeme bozdurma
nftnasilalinir.com
ANKARA EVDEN EVE NAKLİYAT
Trafik sigortası
dedektör
Web Site Kurmak
aşk kitapları
thanks for assisting human beings income the information they mannerism. brilliant stuff as pleasing. preserve up the massive take feature!!!! Tally GST Crack
ReplyDeleteit became a first rate unintentional to visit this nice of site and i am satisfied to recognize. thank you therefore lots for giving us a chance to have this possibility..! 1st Birthday Wishes For Son
ReplyDelete