Society of Robots - Robot Forum

Software => Software => Topic started by: galannthegreat on January 31, 2009, 10:26:22 PM

Title: I need some help with making my servo scan back and forth.
Post by: galannthegreat on January 31, 2009, 10:26:22 PM
what am i doing wrong? I'm trying to make a servo scan back and forth.
Code: [Select]
#include "16F877A.h"
#use delay (clock=4000000)

void main()
{
 while(1)
 {
   output_high(PIN_D0); //set pin high
   delay_us(1000);         //1 ms pulse for general original position
   output_low(PIN_D0);  //set pin low to end pulse
   delay_ms(300);        //wait 300ms in between positions
   output_high(PIN_D0);
   delay_us(1500);
   output_low(PIN_D0);
   delay_ms(300);
   output_high(PIN_D0);
   delay_us(2000);
   output_low(PIN_D0);
   delay_ms(300);
 }
}
Title: Re: I need some help with making my servo scan back and forth.
Post by: Razor Concepts on January 31, 2009, 10:34:03 PM
Basically your telling servo to:

go left, go middle, go right 10 times a second so the servo in the end doesnt really do anything.
Title: Re: I need some help with making my servo scan back and forth.
Post by: galannthegreat on January 31, 2009, 10:44:11 PM
i am trying to test this out so when i incorporate this into my bot i know that it works when i make the full code for my bot. when i compile it it says i have no erorrs but connect the control line to PIN D0 and connect the servo to a separate pwr spply but it does not want to work. do i need to connect the MCLR to Vdd or do i leave it w/o a connection?
Title: Re: I need some help with making my servo scan back and forth.
Post by: pomprocker on January 31, 2009, 11:48:43 PM
you need to learn how to increment and decrement a variable in a loop
Title: Re: I need some help with making my servo scan back and forth.
Post by: galannthegreat on February 01, 2009, 07:21:50 PM
thanks. I learning from this bookhttp://www.elsevier.com/wps/find/bookdescription.cws_home/714793/description#description (http://www.elsevier.com/wps/find/bookdescription.cws_home/714793/description#description) and I am reading each section over and over until I get it, so I'll probably get to that point later on.
Title: Re: I need some help with making my servo scan back and forth.
Post by: Admin on February 13, 2009, 12:48:37 AM
You can't use ms delays for servos, you must use us.

delay_us(1500);//good
delay_ms(1.5);//bad, doesn't work

 :P
Title: Re: I need some help with making my servo scan back and forth.
Post by: galannthegreat on February 13, 2009, 01:56:54 PM
thanks, got that fixed, but i got another problem my servo moves only in a 60 degree range of motion even if i change the timing to the limits. Could it be that my MCU is clocked at 20MHz with an external Xtal? Could that cause timing issues?

EDIT, that is my servo's natural range is 180 degrees, and i'm only getting 60 or so
Title: Re: I need some help with making my servo scan back and forth.
Post by: pomprocker on February 13, 2009, 03:47:17 PM
60 in both or one direction? did you make sure your servo arm is attached in the centered position?
Title: Re: I need some help with making my servo scan back and forth.
Post by: galannthegreat on February 13, 2009, 04:50:55 PM
I made sure that I found the exact centre and i placed the servo horn on the end with the PWM controlled center(1500us pulse), and I have 30 degrees or so from middle to each side, so overall 60 degree range of motion, i'm just simply trying to make it scan back and forth for my Sharp IR sensor avoidance system.

I'm just getting frustrated because I cannot get it to move more left or more right, could it be that my MCU is running too fast?
Title: Re: I need some help with making my servo scan back and forth.
Post by: galannthegreat on February 13, 2009, 04:57:57 PM
Here is a picture in paint of the range I'm getting:
Title: Re: I need some help with making my servo scan back and forth.
Post by: galannthegreat on February 13, 2009, 11:01:37 PM
k, I think I've solved the problem, I asked a friend I knew and he told me to simply pulse the servo with a pulse to move it into a position, except do that multiple times to get the desired positon with a very minimal delay in between, I've tested it and it works for me, so here it is:
Code: [Select]
#include "16f877a.h"
#use delay (clock=20000000)
#fuses HS

void main()
{
 while(1)
 {
  output_high(PIN_D0);// right most position
  delay_us(1000);
  output_low(PIN_D0);
  delay_ms(20);
  output_high(PIN_D0);
  delay_us(1000);
  output_low(PIN_D0);
  delay_ms(20);
  output_high(PIN_D0);
  delay_us(1000);
  output_low(PIN_D0);
  delay_ms(200);// right most position
  output_high(PIN_D0);
  delay_us(1500);
  output_low(PIN_D0);
  delay_ms(200);
  output_high(PIN_D0);
  delay_us(2000);
  output_low(PIN_D0);
  delay_ms(20);
  output_high(PIN_D0);
  delay_us(2000);
  output_low(PIN_D0);
  delay_ms(20);
  output_high(PIN_D0);
  delay_us(2000);
  output_low(PIN_D0);
  delay_ms(200);
  output_high(PIN_D0);
  delay_us(1500);
  output_low(PIN_D0);
  delay_ms(200);
 }
}

I will have a few more bugs to work out with perfect timing and whatnot, but it is working, finally.
Title: Re: I need some help with making my servo scan back and forth.
Post by: Admin on February 16, 2009, 08:47:40 AM
I nicer way to write this:

Code: [Select]
long int i;
for(i=10000;i<20000;i++)
  {
  output_high(PIN_D0);
  delay_us(i/10);
  output_low(PIN_D0);
  delay_ms(20);
  }
for(i=20000;i>10000;i--)
  {
  output_high(PIN_D0);
  delay_us(i/10);
  output_low(PIN_D0);
  delay_ms(20);
  }
Title: Re: I need some help with making my servo scan back and forth.
Post by: galannthegreat on February 16, 2009, 02:50:37 PM
wow, I just got that in the book I'm reading about and now it makes total sense, thanks!