Author Topic: Sonar on a Servo  (Read 1793 times)

0 Members and 1 Guest are viewing this topic.

Offline WebbotTopic starter

  • Expert Roboticist
  • Supreme Robot
  • *****
  • Posts: 2,121
  • Helpful? 108
Sonar on a Servo
« on: May 09, 2008, 07:55:58 PM »
I have mounted a sonar on a servo and am scanning let/right (a la Stampy Edge detection) and storing the distance values in an array - which I later examine to move in the direction 'of least resistance'.

So this leads to a number of issues/challenges:

1 - If the robot is going 'full steam ahead' then the values from left to right aren't comparable, because if there's a wall dead ahead and at right angles to the 'bot, and we use the sonar to read left to right, then the readings don't take into account that the robot is moving. So scanning from left to right then the left value thinks the wall is further away than the right value - as by the time we take the right reading then the robot is closer to the wall. This error is therefore linear to the speed of the robot.

2. If the robot is very close to something then I make it spin on itself. Since you are spinning then the left to right values are quickly out of date unless you spin'a'bit, re-scan from left to right and then decided what to do. So the error is linear to the rotation of the robot.

So the total error is a function of the forward velocity and the speed of rotation.

Of course you could spin the sonar servo faster than the wheels so that these problems disappear (ie one step fwd, stop, re-scan sonar left to right, repeat)  but it doesn't feel correct and will make the bot into a slug where the sonar does all the work. Bear in mind there is a max sampling rate for the sonar so as to avoid ghost echoes - ie the the cycle time to scan all positions from left to right can take a few 100ms.

So it seems that 2 things lead to errors:
1 - Forward velocity (as the time to scan from left to right will mean that the robot has moved so the sonar values become out-of-date)
2 - Speed of rotation (if the robot is spinning on its own axis then the current sonar values are quickly out-of-date) 

So there is a link between 'forward velocity' and 'speed of turn' as both of these quickly invalidate your scan of the world ahead. Yes - you can fix this by stopping, resampling the world ahead, etc but this will severely restrict the overall speed. ie if the sonar can be pinged every 50ms (as any quicker may cause ghost echoes) and you have 7 positions (from extreme left to extreme right) then thats 350ms to which you have to add the delay it takes to move the servo holding the sonar to each of these positions.

No - I don't have an encoder on the wheels so I can't alter the values between sonar readings by knowing how far I have traveled since the last reading.

Does this error matter? Well - maybe? Lets assume we are heading straight at a wall at 1cm per reading and we are using sonar to scan from left to right we might get readings of:-
Left -----------Right
10 09 08 07 06
So we decide to head left since that says the wall is furthest away (10cm).

But on the next scan the sonar is now going from right to left and we are still heading for the wall. So now we get:-
06 05 04 03 02
So now we decide to go right(06cm).

Repeat the two steps and the robot makes a perfect S shape straight into the wall !!

So how do we fix this (without encoder hardware)?

Only solution I can think of is to somehow massage the figures based on what we ASKED the motors to do via software (as opposed to what they DID do via an encoder). ie if we have a rough idea of how fast the robot is meant to be going forward then we can sort of fudge the values to represent values at a given time interval.

Still think this may mean S'ing into the wall if it's not correct. (Is S'ing a new swear word!).

Over to you more clever folk for an obvious and easy answer  ;D

Webbot Home:
WebbotLib online docs:
If your in the neighbourhood:

Offline Admin

  • Administrator
  • Supreme Robot
  • *****
  • Posts: 11,632
  • Helpful? 169
    • Society of Robots
Re: Sonar on a Servo
« Reply #1 on: May 13, 2008, 07:19:02 AM »
In all honesty, error only matters if it causes your robot to fail - ie rams into a wall.

If your robot can navigate without collision despite high error, who cares? :P

But if collisions are problematic, the solution would be to increase the sensor refresh rate. Basically your bot is moving way faster than the world around it is changing, so your sensors need to uhhhhh sense more.

One solution would be to add multiple sensors that scan at the same time. Three sensors triple your sensor scan rate, etc.

As for the sensor data being different from left to right scans, this is because the sonar has rotated to a new position by the time the data is processed by your micro - and its not being consistent with left and right rotations. You can stop-start your sonar, or you can in code shift the data based on experiments on how its 'wrong'.

Read my sharp IR tutorial, as I cover a lot of these concepts in it - a different sensor but the concepts are the same:

Offline SixRingz

  • Full Member
  • ***
  • Posts: 76
  • Helpful? 0
  • Bit's and pc's = Robot.
Re: Sonar on a Servo
« Reply #2 on: May 25, 2008, 05:01:20 PM »
Webbot, what is the task of your robot? If you are aiming on not colliding with the wall or other objects in front of your bot, bare in mind that a sonar have an angle of view much bigger than for example IR allowing it not to be pointed exactly towards obstacles to see them. Maybe you could have your sonar centered and if your distance readings reach a certain threshold (minimum distance) your robot could then stop and look to its left and right to decide where to go? This of course depends what your bot is supposed to do...
Grounding things properly means burying them in the backyard...

Offline WebbotTopic starter

  • Expert Roboticist
  • Supreme Robot
  • *****
  • Posts: 2,121
  • Helpful? 108
Re: Sonar on a Servo
« Reply #3 on: May 26, 2008, 07:34:50 AM »
Thanks for the replies but I sorted out my issue. Here's how:-

For each 'sweep' ie from left to right, or right to left, I now store the results of the sonar into a temporary array. Once the array is completed (ie a full sweep has been done) then I set my resultant array to be the average values of the current result array and the new temporary array.

So with my example 2 scans with the robot heading straight at the wall giving

------------------> Servo scanning left to right
10  9  8  7  6

followed by
<----------------- Servo scanning right to left
 2  3  4  5  6

Averages out to:
 6  6  6  6  6

Which is gives a much better result in the motion of the robot.
Webbot Home:
WebbotLib online docs:
If your in the neighbourhood:


Get Your Ad Here