Society of Robots - Robot Forum
Software => Software => Topic started 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.
#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);
}
}
-
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.
-
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?
-
you need to learn how to increment and decrement a variable in a loop
-
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.
-
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
-
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
-
60 in both or one direction? did you make sure your servo arm is attached in the centered position?
-
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?
-
Here is a picture in paint of the range I'm getting:
-
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:
#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.
-
I nicer way to write this:
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);
}
-
wow, I just got that in the book I'm reading about and now it makes total sense, thanks!