Society of Robots - Robot Forum

Software => Software => Topic started by: katherina on November 11, 2007, 01:04:08 AM

Title: how to code servo motor PWM in c code
Post by: katherina on November 11, 2007, 01:04:08 AM
i used 2 servo motor that already modified which can rotate continously and being controlled by motor driver L293D. i need to know how to set the PWM in c code if i want PWM motor a  higher PWM than motor b. this is for my line following project and i use PIC16F77A....please sumone help me!!!!
Title: Re: how to code servo motor PWM in c code
Post by: Admin on November 12, 2007, 03:22:42 PM
You shouldn't use a motor driver with a servo.

So at the moment Im assuming you have two pins outputing a signal to your motor driver right? Instead, send that signal directly to your servo.

This is the code I used for my Stampy robot. It has commented servo code for the PIC16F877:
http://www.societyofrobots.com/downloads/stampy_sumo_source_code.zip
Title: Re: how to code servo motor PWM in c code
Post by: benji on November 26, 2007, 11:08:37 AM
how much delay (minimum) should i give after changing the pulse width in order for the servo to settle accuratly?
Title: Re: how to code servo motor PWM in c code
Post by: bens on November 26, 2007, 12:37:49 PM
This would depend on the speed of the servo, how far you're telling it to move, and how much load is on it.  If you have access to an oscilloscope you can empirically figure out how to obtain this delay as a function of pulsewidth change.  If not, I'd recommend using trial and error involving large angular changes in servo position.
Title: Re: how to code servo motor PWM in c code
Post by: Admin on November 26, 2007, 12:59:25 PM
as an addition to bens comment . . .

I believe the rule of thumb is like 3 pulses for analog servos, and just 1 for digital servos . . . but as bens said, it depends on other factors, too . . .
Title: Re: how to code servo motor PWM in c code
Post by: benji on November 26, 2007, 01:37:47 PM
Quote
I believe the rule of thumb is like 3 pulses for analog servos
wouldnt that depend on the freq of the pulses?

well , i dont have an osci , what should i do? when turning 2 degrees, how can i be sure its setteled
at minimum time?
Title: Re: how to code servo motor PWM in c code
Post by: Admin on November 26, 2007, 01:40:59 PM
Quote
Quote
I believe the rule of thumb is like 3 pulses for analog servos
wouldnt that depend on the freq of the pulses?
put like 10 to 20ms between each pulse

Quote
well , i dont have an osci , what should i do? when turning 2 degrees, how can i be sure its setteled
at minimum time?
the servo will vibrate and/or draw current if it hasn't settled. just use a multimeter to measure the current.
Title: Re: how to code servo motor PWM in c code
Post by: benji on November 26, 2007, 01:51:09 PM
well thanks admin but here is a point i wanna make clear....
i just wanna change the control pulses width, not stop em.
so the servo should still draw current even after setteling , right? (to keep torque)
or then it will be loose
Title: Re: how to code servo motor PWM in c code
Post by: DomoArigato on November 26, 2007, 02:08:25 PM
benji, are you just asking how to do PWM?  What microcontroller are you using?  Most have a few different modes for PWM generation and control.
Title: Re: how to code servo motor PWM in c code
Post by: Admin on November 26, 2007, 02:36:06 PM
Quote
i just wanna change the control pulses width, not stop em.
so the servo should still draw current even after setteling , right? (to keep torque)
or then it will be loose
The servo will draw almost no current as long as it is at your desired angle. Applying torque on the servo forces it away from the angle, and hence it will draw current to correct it. You will see much more current being drawn when torque is applied.
Title: Re: how to code servo motor PWM in c code
Post by: benji on November 26, 2007, 04:21:57 PM
no domo i know how to do that, thanks admin for makin this clear
Title: Re: how to code servo motor PWM in c code
Post by: DomoArigato on November 26, 2007, 07:46:27 PM
I think the best way to do PWM on most microcontrollers is to use the PWM features of the timer.  You just need to check out the specs of your particular microcontroller.  For an example this code will produce waves on 3 pins of an ATMega 128 and turn on the output of pin A (that should center a servo on pin A):
from the header file
Code: [Select]
#define TIMER1_OUTPUT_A = 0x80,
#define TIMER1_PRESCALER_8 0x02
#define TIMER1_WGM_A_PC_ICR_PWM  0x02 /* Wave generation mode settings for register A */
#define TIMER1_WGM_B_PC_ICR_PWM  0x12
from the c file
Code: [Select]
// Initialize Timer1 to Phase and Frequency correct PWM mode with ICR as top.
TCCR1A |= TIMER1_WGM_A_PC_ICR_PWM;
TCCR1B |= TIMER1_WGM_B_PC_ICR_PWM;
// Set Timer1 prescaler.
TCCR1B |= TIMER1_PRESCALER_8;
// Set Timer1 top value using a base frequency of 50Hz and a prescaler of 1.
// The equation is topValue =  CLOCK_FREQUENCY / (baseFrequency * prescaler * 2)
ICR1 = (unsigned int)20000;
// Set Timer1 default compare values.
OCR1A = 1500;
OCR1B = 1500;
OCR1C = 1500;

// This turns on out to pin A.
TCCR1A |= TIMER1_OUTPUT_A;
DDRB |= 1 << 5;