View previous topic :: View next topic |
Author |
Message |
Thanuja Guest
|
RC Servo |
Posted: Sat Aug 22, 2009 9:38 am |
|
|
Hi All,
Im trying to workout a RC Servo these days, and Im having a bit trouble.
The Servo is "RKI-1210". If any one who knows how to turn it 90 degrees please let me know. I really appreciate it.
Cheers |
|
![](templates/subSilver/images/spacer.gif) |
dyeatman
Joined: 06 Sep 2003 Posts: 1924 Location: Norman, OK
|
|
![](templates/subSilver/images/spacer.gif) |
bungee-
Joined: 27 Jun 2007 Posts: 206
|
|
Posted: Sat Aug 22, 2009 2:51 pm |
|
|
It is written all over the forum about RC servos. Use search. I have written a sample code for driving 8 servos on PIC, HERE is the code.
But generally servos need some kind of PWM with these specifics:
pulse length 1-2ms every 20ms.
So for servo to be at one extreme the pulse should be 1ms .... when you need servo in middle then it is usually 1.5ms (it depends of manufacturer) and to drive it to other extreme pulse should be 2ms.
Try and if you'll still have problems, post the code we'll gladly help. |
|
![](templates/subSilver/images/spacer.gif) |
Thanuja Guest
|
RC Servo |
Posted: Sun Aug 23, 2009 2:49 am |
|
|
Guys, Thanx a lot for the help.
But I'm still having sm problems. I can't use my PWM modules as I'm using them to drive wheels of the robot. I need to turn the servo 90 degrees using another pin to give the pulse.
It's for a 18F452 pic.
Here's the code that I have been trying. But its not working well.
Code: |
#include <18F452.h>
#device adc=8
#FUSES NOWDT //No Watch Dog Timer
#FUSES WDT128 //Watch Dog Timer uses 1:128 Postscale
#FUSES HS //High speed Osc (> 4mhz)
#FUSES NOPROTECT //Code not protected from reading
#FUSES NOOSCSEN //Oscillator switching is disabled, main oscillator is source
#FUSES BROWNOUT //Reset when brownout detected
#FUSES BORV20 //Brownout reset at 2.0V
#FUSES NOPUT //No Power Up Timer
#FUSES STVREN //Stack full/underflow will cause reset
#FUSES NODEBUG //No Debug mode for ICD
#FUSES LVP //Low Voltage Programming on B3(PIC16) or B5(PIC18)
#FUSES NOWRT //Program memory not write protected
#FUSES NOWRTD //Data EEPROM not write protected
#FUSES NOWRTB //Boot block not write protected
#FUSES NOWRTC //configuration not registers write protected
#FUSES NOCPD //No EE protection
#FUSES NOCPB //No Boot Block code protection
#FUSES NOEBTR //Memory not protected from table reads
#FUSES NOEBTRB //Boot block not protected from table reads
#use delay(clock=20000000)
#use rs232(baud=9600,parity=N,xmit=PIN_C6,rcv=PIN_C7,bits=8)
#define SERVO_SIGNAL PIN_B4
void main()
{
setup_adc_ports(AN0_AN1_AN2_AN3_AN4);
setup_adc(ADC_OFF);
setup_psp(PSP_DISABLED);
setup_spi(FALSE);
setup_wdt(WDT_OFF);
setup_timer_0(RTCC_INTERNAL);
setup_timer_1(T1_INTERNAL|T1_DIV_BY_4);
setup_oscillator(False);
setup_timer_2(T2_DIV_BY_1,249,1); // pwm freq. is set 20Hz
// setup_oscillator(False);
setup_ccp1(CCP_PWM); // configure pwm1 module, pin c2
setup_ccp2(CCP_PWM); // configure pwm2 module, pin c1
// make pin c1 and c2 as outputs
output_drive(pin_C1);
output_drive(pin_C2);
while(true){
operate_servo();
}
}// end of main
void operate_servo(){
output_high(SERVO_SIGNAL);
delay_ms(2);
output_low(SERVO_SIGNAL);
delay_ms(18);
}
/*
Please note that I am using PWM modules to drive two wheels of the motor. I have not indicated that part of the code.
*/ |
Thanks. |
|
![](templates/subSilver/images/spacer.gif) |
bungee-
Joined: 27 Jun 2007 Posts: 206
|
|
Posted: Sun Aug 23, 2009 2:26 pm |
|
|
2ms would drive servo to one of the extreme positions. Try to change the delay_ms(2) to delay_us(1500) .... and then experiment with values like that.
Here is the interrupt routine that you can use....
Code: | int degrees; // 0=0deg, 1=90deg, 2=180deg <----!!!!
int1 pause_cycle;
#int_TIMER3
void TIMER3_isr(void)
{
if (!pause_cycle)
{
Output_high(Servo_pin);
switch (degrees) {
case 0: setup_timer3(60535); // 65535 - (1ms/(4/20MHz))
break;
case 1: setup_timer3(58035); // 1.5ms
break;
case 2: setup_timer3(55535); // 2ms
break;
}
pause_cycle=1; //Next cycle is pause ....
}
else
{
output_low(servo_pin);
pause_cycle=0; //Next cycle is pulse cycle
// No setting for timer because at this speed it will overflow in 13,5 ms ...
}
} |
Don't forget to setup timer3 ( setup_timer_3(T3_INTERNAL|T3_DIV_BY_1); ) and of course activate timer3 interrupt ...
Timer3 interrupt routine will take care of servo. So if you want servo to be at 0deg than you set variable degrees=0, to move it to 90° then set degrees=1 .... this is it. |
|
![](templates/subSilver/images/spacer.gif) |
|