0
Offline
Newbie
Karma: 0
Posts: 19
Arduino rocks
|
 |
« on: November 09, 2009, 12:37:36 pm » |
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??.... #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
Brattain Member
Karma: 143
Posts: 19365
I don't think you connected the grounds, Dave.
|
 |
« Reply #1 on: November 09, 2009, 12:38:53 pm » |
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.
|
|
|
|
0
Offline
Newbie
Karma: 0
Posts: 19
Arduino rocks
|
 |
« Reply #2 on: November 09, 2009, 12:49:50 pm » |
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
Brattain Member
Karma: 143
Posts: 19365
I don't think you connected the grounds, Dave.
|
 |
« Reply #3 on: November 09, 2009, 12:55:33 pm » |
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.
|
|
|
|
0
Offline
Newbie
Karma: 0
Posts: 19
Arduino rocks
|
 |
« Reply #4 on: November 09, 2009, 01:10:07 pm » |
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
Newbie
Karma: 0
Posts: 11
Arduino rocks
|
 |
« Reply #5 on: November 09, 2009, 10:08:21 pm » |
I'm having the same problem, did you solve it motote?
|
|
|
|
|
Logged
|
|
|
|
|
0
Offline
Newbie
Karma: 0
Posts: 19
Arduino rocks
|
 |
« Reply #6 on: November 10, 2009, 02:01:21 am » |
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
Brattain Member
Karma: 143
Posts: 19365
I don't think you connected the grounds, Dave.
|
 |
« Reply #7 on: November 10, 2009, 02:37:29 am » |
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.
|
|
|
|
0
Offline
Newbie
Karma: 0
Posts: 11
Arduino rocks
|
 |
« Reply #8 on: November 10, 2009, 05:03:57 pm » |
That's not a bad idea, I'll try sweeping in larger increments.
|
|
|
|
|
Logged
|
|
|
|
|
Toronto, Canada
Offline
Full Member
Karma: 0
Posts: 144
Arduino rocks
|
 |
« Reply #9 on: November 10, 2009, 05:28:50 pm » |
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
Newbie
Karma: 0
Posts: 19
Arduino rocks
|
 |
« Reply #10 on: November 12, 2009, 02:03:05 am » |
Thanks Ro-Bot-X... I will try this afternoon...
|
|
|
|
|
Logged
|
|
|
|
|
0
Offline
Newbie
Karma: 0
Posts: 19
Arduino rocks
|
 |
« Reply #11 on: November 12, 2009, 01:28:33 pm » |
Nope, It doesn't work, I was using arduino 0017 with the servo library and I switch to softwareServo, the result is even worst  .. 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
Newbie
Karma: 0
Posts: 11
Arduino rocks
|
 |
« Reply #12 on: November 22, 2009, 01:31:35 pm » |
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
|
|
|
|
|
|