go_away

Author Topic: Servo Position Slightly off from calculated value  (Read 398 times)

0 Members and 1 Guest are viewing this topic.

Offline ksquaredTopic starter

  • Jr. Member
  • **
  • Posts: 41
  • Helpful? 0
Servo Position Slightly off from calculated value
« on: July 19, 2011, 09:32:39 AM »
I have a hitec hs311 that I was trying to modify with a $50 bot board.  I disabled CKDIV8 and managed to make a one second PWM pulse (I've added headers to B1/B2, which I am using.)  I then was able to calulate the number of cycles for a given pulse in milliseconds.  For 1.5 ms pulse, for example, I would do .0015/(1/8000000), (replace .0015 with other values in seconds, ie .9 ms is .0009).  I read that the pulse "benchmarks" for this servo are .9 ms full one direction, 1.5 ms centered and 2.1 ms (full other direction.).  In my code I double the value of the number of recquired cycles for ICR1 and then simply make the comparator value (OCR1A/OCR1B) to be ICR1/2.  My code goes something like this (for a .9 ms pulse, largly ripped from the tutorial on this site):

Code: [Select]
// set up 2 PWM channels on PB1 and PB2 using Timer1
TCCR1A = 0;    
// disable all PWM on Timer1 whilst we set it up
ICR1 = 7200*2;   // frequency is every 1.8 ms
// Configure timer 1 for Fast PWM mode via ICR1, with no prescaling
TCCR1A = (1 << WGM11);
TCCR1B = (1 << WGM13) | (1<<WGM12) | (1 << CS10);
// Set PB1 and PB2 as outputs
DDRB |= _BV(1) |  _BV(2);
TCCR1A |= 2 <<  6;  // enable PWM on port B1 in non-inverted compare mode 2
TCCR1A |= 2 <<  4;  // enable PWM on port B2 in non-inverted compare mode 2
OCR1A = ICR1 / 2; // 2ms pulse to left motor on PB1
OCR1B = ICR1 / 2; // 2ms pulse to right motor on PB2
while(1){}

This code gets the servo very close the the end of its mechanical range.  However, whenever I say, change the ICRA/B values to a hardcoded 6000, it gets just a little closer to the edge.  I also believe that setting ICR1 to 6000*2, however, makes the servo oscillate madly.
Edit: ---------------------

I accidentally assumed the pin was going to have a voltage on it when the timer count was below the comparator value, not above.  That means reducing the value to 6000 actually elongates the pulse (if I'm not mistaken)!

I guess this changes the question to why does a ((7200*2)-6000)/8000000 second or 1.05 ms pulse get the servo closer to the extreme than a 7200/8000000 second or .9 ms pulse, when I believe the specification said .9 ms was the extreme?

---------------------------

I know it might not be the most practical concern by I'd really appreciate it if anyone could give me a hand!
« Last Edit: July 21, 2011, 12:45:01 PM by ksquared »

 


Get Your Ad Here