go_away

Author Topic: sharp IR glued to Hitec servo (robot eyes)  (Read 929 times)

0 Members and 1 Guest are viewing this topic.

Offline drewsmugTopic starter

  • Beginner
  • *
  • Posts: 3
  • Helpful? 0
sharp IR glued to Hitec servo (robot eyes)
« on: December 09, 2010, 07:43:44 PM »
I am using an Arduino Uno board to control a non modified Hitec servo (0 to 180 degree) and a sharp IR.  The sensor is glued on so that it is facing "forward" when I drive the servo to 90 degrees.  My goal is to have the servo do a sweep from 0 degrees to 180 degrees while I take measurements from the Sharp IR so I can get a feel for what is in front of my robot.  I have posted the following test code I am working on to make this work.  I have the line that actually stores what the sharp IR is reading commented out below.  With it commented out this is the fastest I can get him to turn his “neck” from 0 degrees to 180 degrees. 

For now I would be happy to get a reading every 5 degrees.  So I store the reading from the sensor every 5 degrees in my array and then print it out when it is done turning his neck.  My problem is he is about 15 degrees off.  I know this because i set the delays for much longer so he turned his head very slow.  I place objects directly in front of him and on his sides.  Then I compared this slow accurate output to my output with the following code--but with the one line un-commented.  I can get accurate readings if I make the delay large enough that he is turning his head about ˝ or ⅓ the speed he is currently turning.

I feel like I shouldn’t have to increase the delay because at the point in code where he makes a reading, if he is pointing to where I think he should be (the last neckservo.write position), the reading should be accurate.  Does anyone have any ideas.

p.s. I realized on my read through that when I said accurate I am not clear.  I believe I am receiving accurate data in distance but that it is off by 15 degrees.  So my accurate readings show an object close by at val[90] and val[95] my code below shows the object close by in val[115] and val[120].

Thanks in advance for any help.  I really do appreciate it.




#include <SoftwareServo.h>

SoftwareServo neckservo;

int test_flag = 1;

int c;
int eyes = 0; 
int val[180];

void setup()
{
  Serial.begin(9600);
  neckservo.attach(2);
}

void look_left(int limit)
{
  for(c = 0; c < limit; c++)
  {
      neckservo.write(0);
      delay(15);
      SoftwareServo::refresh();
  }
}

void loop()
{
 
  while(test_flag)
  {
    look_left(100);
   
    for(c=0; c <= 180; c = c+5)
    {
      neckservo.write(c);
      SoftwareServo::refresh();

      //val[c] = analogRead(eyes);
     
      neckservo.write(c+1);
      SoftwareServo::refresh();
      delay(3);
      neckservo.write(c+2);
      SoftwareServo::refresh();
      delay(3);
      neckservo.write(c+3);
      SoftwareServo::refresh();
      delay(3);
      neckservo.write(c+4);
      SoftwareServo::refresh();
      delay(3);
      neckservo.write(c+5);
      SoftwareServo::refresh();
      delay(3);
    }
   
    for(c=0; c <= 180; c = c+5)
    {
      Serial.print("val[");
      Serial.print(c);
      Serial.print("]: ");
      Serial.print(val[c]);
      Serial.print("\n");
    }
    test_flag = 0;
  }
}

Offline rbtying

  • Supreme Robot
  • *****
  • Posts: 452
  • Helpful? 31
Re: sharp IR glued to Hitec servo (robot eyes)
« Reply #1 on: December 09, 2010, 10:59:22 PM »
It looks to me like you never actually add any data to val.  As such, the values you are reading are randomly assigned - perhaps this is the reason?

Also, try using the standard Servo library - its more accurate, and non-blockiing.

Offline drewsmugTopic starter

  • Beginner
  • *
  • Posts: 3
  • Helpful? 0
Re: sharp IR glued to Hitec servo (robot eyes)
« Reply #2 on: December 10, 2010, 12:32:19 AM »
I wrote some similar code with the standard servo library.  Then again I put an object infront of the robot.  If I run this code with a delay of 15 milliseconds and then again with 5 milliseconds it will see the object almost 15 degrees later.  The point is if I run this code I get different values.  If my servo is turning 180 degrees in both cases why am I getting different values.  Here is the code I used.

#include <Servo.h>
 
Servo myservo;

int test = 1;
int eyes = 0;
int pos = 0;
int val[180];
 
void setup()
{
  Serial.begin(9600);
  myservo.attach(9);
}
 
 
void loop()
{
 
  while(test)
  {
  myservo.write(0);
  delay(500);
 
 
      Serial.print("Start\n");
 
  for(pos = 0; pos < 180; pos += 1)
  {
    myservo.write(pos);
    delay(5);
    val[pos] = analogRead(eyes);
  }
 
 
 
      for(pos=0; pos <= 180; pos++)
    {
      Serial.print("val[");
      Serial.print(pos);
      Serial.print("]: ");
      Serial.print(val[pos]);
      Serial.print("\n");
    }
      Serial.print("\n\n\n");
   
   
  for(pos = 180; pos>=1; pos-=1)
  {                               
    myservo.write(pos);
    delay(5);
  }
 
      for(pos=0; pos <= 180; pos++)
    {
      Serial.print("val[");
      Serial.print(pos);
      Serial.print("]: ");
      Serial.print(val[pos]);
      Serial.print("\n");
    }
    test = 0;
  }     
}

Offline Ro-Bot-X

  • Contest Winner
  • Supreme Robot
  • ****
  • Posts: 1,431
  • Helpful? 25
  • Store: RoBotXDesigns.ca
Re: sharp IR glued to Hitec servo (robot eyes)
« Reply #3 on: December 10, 2010, 06:36:52 AM »
You can read new values from the Sharp IR sensor every 39ms. If you read them faster, you read the same value multiple times until the sensor updates it. So, you have to wait 39ms between sensor reads. If this gets too slow, use 2 or 3 sensors mounted at 90 (or 45) degrees to each other and do a 90 degrees sweep.
Check out the uBotino robot controller!

Offline drewsmugTopic starter

  • Beginner
  • *
  • Posts: 3
  • Helpful? 0
Re: sharp IR glued to Hitec servo (robot eyes)
« Reply #4 on: December 10, 2010, 10:24:31 AM »
that would do it.  Thanks.

 


Get Your Ad Here

data_list