Author Topic: PWM problem with servos, very sluggish  (Read 1682 times)

0 Members and 1 Guest are viewing this topic.

Offline joe61Topic starter

  • Supreme Robot
  • *****
  • Posts: 417
  • Helpful? 16
PWM problem with servos, very sluggish
« on: February 04, 2011, 05:57:50 PM »
I didn't see anything about this here, my apologies if I missed it.

I'm trying to use a couple Parallax continuous rotation servos to drive the wheels on a new robot I'm putting together. I apparently don't understand pwm because I can't get anything out of them but a slow chugging. I've got a six volt battery pack that I just checked and is putting out 6.5 volts, so I don't think it's a lack of power. The output from the battery goes through an LM2940CT so the system sees a regulated 5V (which I've measured to be 5V).

I'm using avr-gcc, and an ATtiny84 because it's small and has a 16 bit timer. The internal oscillator runs at 8MHz, which I'm dividing by 8. I tried using the code from one of the tutorials here, and have been monkeying around with it to no avail. If anyone can see what I'm doing wrong I'd appreciate a clue. Here's the code I'm using now.



/*
 * for ATtiny 84.
 *
 */

#define F_CPU 1000000
#include <avr/io.h>
#include <util/delay.h>

void initPWM()
{
    TCCR1A  = 0;
    ICR1      = 19999;
    DDRA     = _BV(DDA6) | _BV(DDA5);

    TCCR1A  = (1 << WGM11);
    TCCR1B  = (1 << WGM13) | (1<<WGM12) | (1 << CS11);
    TCCR1A |= (1 << COM1A1) | (1 << COM1B1);
}

int main()
{
    initPWM();
   
    OCR1A = 500;
    OCR1B = 1000;
    _delay_ms(1000);

    OCR1A = 750;
    OCR1B = 1000;
    _delay_ms(1000);

    OCR1A = 800;
    OCR1B = 1000;
    _delay_ms(1000);

    for(;;)
    {
    }
}


Thanks

Joe

Offline waltr

  • Supreme Robot
  • *****
  • Posts: 1,944
  • Helpful? 99
Re: PWM problem with servos, very sluggish
« Reply #1 on: February 04, 2011, 09:59:31 PM »
When you set the motors (modified servos) to stop do they?
How do run in reverse?
How heavy is you Bot?
Do the motors run sluggish if you lift the Bot off the ground?

Have you tried to set the pulse width further from the stop value?

Offline joe61Topic starter

  • Supreme Robot
  • *****
  • Posts: 417
  • Helpful? 16
Re: PWM problem with servos, very sluggish
« Reply #2 on: February 04, 2011, 10:54:25 PM »
When you set the motors (modified servos) to stop do they?
How do run in reverse?
How heavy is you Bot?
Do the motors run sluggish if you lift the Bot off the ground?

Have you tried to set the pulse width further from the stop value?

The motors don't do anything predictable, at times I think my programmer isn't working because they do the same thing through variations in the code. They will go in reverse, and the servos are sluggish whether they're on the ground or in my hand. The whole thing weighs (I'm guessing) a couple pounds; I'll add a picture of it to this.

I've set the compare registers all over the map. A couple times I made the main loop vary the duty cycle from 0 to 1023; the motors didn't change speed or direction at all. I'm reallly kind of baffled by this, because I've tested these servos with an Arduino, and they work fine with it.

Here's a picture of it


Offline joe61Topic starter

  • Supreme Robot
  • *****
  • Posts: 417
  • Helpful? 16
Re: PWM problem with servos, very sluggish
« Reply #3 on: February 05, 2011, 10:12:00 AM »
I might have just done the arithmetic wrong. Would someone here check me on this? System oscillator frequency is 8000000 and PWM frequency is 50Hz.

TOP = (system_clock / (2 * P * Fpwm)) - 1 = (8000000 / (2 * 8 * 50)) - 1 = (8000000 / 800) - 1 = 19,9999

Which if I understand correctly means that the conversion factor for number of milliseconds of duty cycle to OCR1x setting is

PWMres / Fpwm = 20000 / 50 = 400

so

1 ms -> OCR1x = 400
1.5 ms -> OCR1x = 600
2 ms -> OCR1x = 800

Did I mess something up there?

Thanks

Joe

Offline dunk

  • Expert Roboticist
  • Supreme Robot
  • *****
  • Posts: 1,086
  • Helpful? 21
    • dunk's robot
Re: PWM problem with servos, very sluggish
« Reply #4 on: February 05, 2011, 06:36:40 PM »
hi Joe,
when i am trying to make sure a timer is running at the correct speed i make it increase a software counter and have that counter toggle an I/O pin after a set number of timer loops.
then you can time the speed an LED connected to the I/O pin flashes and verify your timer if running at the correct speed.

for example,
increase a counter every time your 20ms servo timer overflows.
when the counter reaches 500, toggle the I/O pin and reset the counter.
now it will be easy to time (with a stop watch) if the LED toggles every 10seconds.


i don't have time to check the AVR registers you are setting just now but this is the reference page i used to use for timers when i was getting to grips with them:
http://members.shaw.ca/climber/avrtimers.html


dunk.

Offline hopslink

  • Robot Overlord
  • ****
  • Posts: 202
  • Helpful? 14
Re: PWM problem with servos, very sluggish
« Reply #5 on: February 06, 2011, 04:17:27 AM »
Try disconnecting one servo and the distance sensors, and possibly add a reasonably large capacitor, just to check it isn't a power issue... it looks like you could be cutting it fine and possibly seeing brownout.

My maths isn't the strongest but (8000000 / 800) != 20000  :P

That said be sure you are using the correct formula for the pwm mode you have selected. It looks like you are using fast pwm so the formula you use above doesn't apply. Also worth a try would be increasing your ICR1 value to give a ~25ms total period.

Offline joe61Topic starter

  • Supreme Robot
  • *****
  • Posts: 417
  • Helpful? 16
Re: PWM problem with servos, very sluggish
« Reply #6 on: February 06, 2011, 07:09:49 AM »
As it turns out, I was misunderstanding how the clock is defined. I thought the timer prescalar was applied to the system oscillator, when it's applied to the system clock. So I was working on the assumption that the base frequency was 8MHz when it's really 1Mhz. Changing the prescalar to 1 did the trick. Using 1000, 1500 and 2000 for OCR settings I get 1ms, 1.5ms and 2ms duty cycles.

Thanks

Joe
« Last Edit: February 06, 2011, 07:14:11 AM by joe61 »

Offline joe61Topic starter

  • Supreme Robot
  • *****
  • Posts: 417
  • Helpful? 16
Re: PWM problem with servos, very sluggish
« Reply #7 on: February 06, 2011, 07:12:08 AM »
Try disconnecting one servo and the distance sensors, and possibly add a reasonably large capacitor, just to check it isn't a power issue... it looks like you could be cutting it fine and possibly seeing brownout.

Turns out the problem was in my head, and I misunderstood the clock definition, as I mentioned above.

Quote
My maths isn't the strongest but (8000000 / 800) != 20000  :P

That said be sure you are using the correct formula for the pwm mode you have selected. It looks like you are using fast pwm so the formula you use above doesn't apply. Also worth a try would be increasing your ICR1 value to give a ~25ms total period.

I edited out some stuff that was commented in the code when posting it, and apparently went temporarily insane. I'm not sure where those numbers came from. Sorry for the confusion.

Thanks

Joe

Offline waltr

  • Supreme Robot
  • *****
  • Posts: 1,944
  • Helpful? 99
Re: PWM problem with servos, very sluggish
« Reply #8 on: February 06, 2011, 10:17:29 AM »
You found the problem and got it working. That is important.

 


Get Your Ad Here