Author Topic: I need some help with making my servo scan back and forth.  (Read 3130 times)

0 Members and 1 Guest are viewing this topic.

Offline galannthegreatTopic starter

  • Supreme Robot
  • *****
  • Posts: 615
  • Helpful? 4
  • Blue-Lensed Blue LEDs?! What?! Impossible!!
I need some help with making my servo scan back and forth.
« 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);
 }
}
« Last Edit: February 13, 2009, 03:37:15 PM by galannthegreat »
Kurt

Offline Razor Concepts

  • Supreme Robot
  • *****
  • Posts: 1,856
  • Helpful? 53
    • RazorConcepts
Re: I need some help with making my servo scan back and forth.
« Reply #1 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.

Offline galannthegreatTopic starter

  • Supreme Robot
  • *****
  • Posts: 615
  • Helpful? 4
  • Blue-Lensed Blue LEDs?! What?! Impossible!!
Re: I need some help with making my servo scan back and forth.
« Reply #2 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?
Kurt

Offline pomprocker

  • Supreme Robot
  • *****
  • Posts: 1,431
  • Helpful? 16
  • Sorry miss, I was giving myself an oil-job.
    • Nerdcore - Programming, Electronics, Mechanics
Re: I need some help with making my servo scan back and forth.
« Reply #3 on: January 31, 2009, 11:48:43 PM »
you need to learn how to increment and decrement a variable in a loop

Offline galannthegreatTopic starter

  • Supreme Robot
  • *****
  • Posts: 615
  • Helpful? 4
  • Blue-Lensed Blue LEDs?! What?! Impossible!!
Re: I need some help with making my servo scan back and forth.
« Reply #4 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 and I am reading each section over and over until I get it, so I'll probably get to that point later on.
Kurt

Offline Admin

  • Administrator
  • Supreme Robot
  • *****
  • Posts: 11,703
  • Helpful? 173
    • Society of Robots
Re: I need some help with making my servo scan back and forth.
« Reply #5 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

Offline galannthegreatTopic starter

  • Supreme Robot
  • *****
  • Posts: 615
  • Helpful? 4
  • Blue-Lensed Blue LEDs?! What?! Impossible!!
Re: I need some help with making my servo scan back and forth.
« Reply #6 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
« Last Edit: February 13, 2009, 03:38:30 PM by galannthegreat »
Kurt

Offline pomprocker

  • Supreme Robot
  • *****
  • Posts: 1,431
  • Helpful? 16
  • Sorry miss, I was giving myself an oil-job.
    • Nerdcore - Programming, Electronics, Mechanics
Re: I need some help with making my servo scan back and forth.
« Reply #7 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?

Offline galannthegreatTopic starter

  • Supreme Robot
  • *****
  • Posts: 615
  • Helpful? 4
  • Blue-Lensed Blue LEDs?! What?! Impossible!!
Re: I need some help with making my servo scan back and forth.
« Reply #8 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?
Kurt

Offline galannthegreatTopic starter

  • Supreme Robot
  • *****
  • Posts: 615
  • Helpful? 4
  • Blue-Lensed Blue LEDs?! What?! Impossible!!
Re: I need some help with making my servo scan back and forth.
« Reply #9 on: February 13, 2009, 04:57:57 PM »
Here is a picture in paint of the range I'm getting:
Kurt

Offline galannthegreatTopic starter

  • Supreme Robot
  • *****
  • Posts: 615
  • Helpful? 4
  • Blue-Lensed Blue LEDs?! What?! Impossible!!
Re: I need some help with making my servo scan back and forth.
« Reply #10 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.
Kurt

Offline Admin

  • Administrator
  • Supreme Robot
  • *****
  • Posts: 11,703
  • Helpful? 173
    • Society of Robots
Re: I need some help with making my servo scan back and forth.
« Reply #11 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);
  }

Offline galannthegreatTopic starter

  • Supreme Robot
  • *****
  • Posts: 615
  • Helpful? 4
  • Blue-Lensed Blue LEDs?! What?! Impossible!!
Re: I need some help with making my servo scan back and forth.
« Reply #12 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!
Kurt

 


data_list