Pages: [1]   Go Down
Author Topic: ping + servo... I'm missing something....  (Read 997 times)
0 Members and 1 Guest are viewing this topic.
0
Offline Offline
Newbie
*
Karma: 1
Posts: 19
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Hi, I'm playing with ping sensor and a servo... I have no problem moving the servo, and no problem pinging with the sensor... but when I mix things then the servo moves to slow.... Is there a better way to to this??....

Code:
#include <Servo.h>

Servo pingServo;  
const int pingServoPin = 9;
const int pingPin = 8;
 
void setup()
{
  pingServo.attach(pingServoPin);
  pingServo.write(90);  
  Serial.begin(9600);
}
 
 
void loop()
{
  int dir = pingScan();
  pingServo.write(dir);
  Serial.println(dir);
  delay(5000);
 }
//-----------------------------------------------------------------------------
int pingScan()
{
  long maxDist = 0;
  int dir = -1;
  
  for(int pos = 0; pos < 180; pos += 1)
  {                                
    pingServo.write(pos);      
    long dist = readPing();
    if (dist > maxDist)
    {
      maxDist = dist;
      dir = pos;
    }
    //Serial.println(dist);
    //delay(15);                  
  }
  return dir;
}
//-----------------------------------------------------------------------------
long readPing()
{
  long duration;
  
  pinMode(pingPin, OUTPUT);
  digitalWrite(pingPin, LOW);
  delayMicroseconds(2);
  digitalWrite(pingPin, HIGH);
  delayMicroseconds(5);
  digitalWrite(pingPin, LOW);
  
  pinMode(pingPin, INPUT);
  duration = pulseIn(pingPin, HIGH);
  
  return duration / 29 / 2;
}
Logged

Global Moderator
UK
Offline Offline
Brattain Member
*****
Karma: 291
Posts: 25898
I don't think you connected the grounds, Dave.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Maybe if you removed that 5 second delay?
Don't forget that you now have the round-trip time of the sonar pulse between servo increments.
If you want the sweep to complete faster, make the angular increments larger.
« Last Edit: November 09, 2009, 12:45:03 pm by AWOL » Logged

"Pete, it's a fool looks for logic in the chambers of the human heart." Ulysses Everett McGill.
Do not send technical questions via personal messaging - they will be ignored.

0
Offline Offline
Newbie
*
Karma: 1
Posts: 19
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

The 5s delay is just between sweeps.  Incremeting the angular step works, but I think there is something else wrong...
 
If I comment the pulseIn function then the servo speed is good, also, if I comment the servo write funcion then the ping works at full speed....
Logged

Global Moderator
UK
Offline Offline
Brattain Member
*****
Karma: 291
Posts: 25898
I don't think you connected the grounds, Dave.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
Don't forget that you now have the round-trip time of the sonar pulse between servo increments

Sound travels at around 330m/s,  so a two metre round-trip will take 6ms.
If everything that the sensor "sees" is 1 metre away, a full sweep will take 180 * 6ms - over a second
Logged

"Pete, it's a fool looks for logic in the chambers of the human heart." Ulysses Everett McGill.
Do not send technical questions via personal messaging - they will be ignored.

0
Offline Offline
Newbie
*
Karma: 1
Posts: 19
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

That is!!
With the above code each sweep take 50s,... and everything arround is much less than 1m away.

A sweep, replacing the readPing function with a delay(15) takes 3s!!!!
Logged

0
Offline Offline
Newbie
*
Karma: 0
Posts: 11
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

I'm having the same problem, did you solve it motote?
Logged

0
Offline Offline
Newbie
*
Karma: 1
Posts: 19
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Hi phokur,
I make some little advances... The problem is the call to pulseIn, sometimes it takes so much time, up to 1000ms, so I supposed it doesn't detect anything and just timeout.

So, I think the problem is the servo refresh call that runs on background that take the processor after the pulse launch and release it to late for the pulseIn call to detect anything.

One solution is to use a custom timeout.. pulseIn(pingPin, HIGH, 10000).. this make the call take at most 10ms and the return is 0, so we can detect bad pings.

But, I resist to consider this as a solution!!!! This is just a workaround  not a solution.

I have a similar problem moving a servo and reading a GPS
Logged

Global Moderator
UK
Offline Offline
Brattain Member
*****
Karma: 291
Posts: 25898
I don't think you connected the grounds, Dave.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Stepping a servo by single degree increments when the beam width of the sensor is probably in excess of 20 degrees seems a little pointless.
Logged

"Pete, it's a fool looks for logic in the chambers of the human heart." Ulysses Everett McGill.
Do not send technical questions via personal messaging - they will be ignored.

0
Offline Offline
Newbie
*
Karma: 0
Posts: 11
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

That's not a bad idea, I'll try sweeping in larger increments.
Logged

Toronto, Canada
Offline Offline
Full Member
***
Karma: 0
Posts: 144
Arduino rocks
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

You don't need larger increments. I have it sweep 180 degrees one degree at a time, calculate the coordinates and show them on the display and it doesn't take more than 2 seconds to do a full 180 sweep. Take a look at my code and video here:
http://www.arduino.cc/cgi-bin/yabb2/YaBB.pl?num=1256716463/6#6
« Last Edit: November 10, 2009, 05:30:23 pm by Ro-Bot-X » Logged


0
Offline Offline
Newbie
*
Karma: 1
Posts: 19
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Thanks Ro-Bot-X... I will try this afternoon...
Logged

0
Offline Offline
Newbie
*
Karma: 1
Posts: 19
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Nope, It doesn't work, I was using arduino 0017 with the servo library and I switch to softwareServo, the result is even worst smiley-sad.. the pulseIn call always timeout at 1068ms

Ro-Bot-X... I see you use pin 16 for the servo... which microcontroller are you using, maybe a speed issue... I'm with an Atmega328

Thanks!
« Last Edit: November 12, 2009, 02:15:12 pm by motote » Logged

0
Offline Offline
Newbie
*
Karma: 0
Posts: 11
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Got mine working with a combination of only scanning in 20 degree increments and increasing the delay to let the servo arrive before pinging. I will get an occasional 0 or timeout but I just test for that in my loops.
Logged

Pages: [1]   Go Up
Jump to: