2
$\begingroup$

Anyone know of sample Python code, or a tutorial, on the polar coordinate math for recognizing that three ultrasonic distance readings form a straight line?

Deg  Distance

-10°  20 cm  
  0°  18 cm
+10°  16 cm

Once I understand the math, I'll have to deal with the lack of precision.

I want my bot to recognize a wall, and eventually recognize a corner.

$\endgroup$
1
$\begingroup$

Checking for three is a subset of checking for many; so, I am going to consider the more general solution. I will discuss the three point solution at the end.

First, convert the polar coordinates to Cartesian Coordinates. First, to make things simple, use your robot as the reference frame (make it the center of the universe).

That means, for each target calculate: x=Distancecos(Deg), y=Distancesin(Deg)

Here is a Post on Converting Coordinates in Python

After you have all these calculate the slope and intercept of the line using linear regression; then, calculate R-squared (the Coefficient of determination) to determine how well the measurements fit a line. Note: Any measurement system is going to have errors; so, the points will likely never perfectly fit a line.

If the number is too high, try dropping out points the deviate furthest from the line. Maybe you could group them together and if they have a low r-squared then you have found another wall.

I am sure there are lots of Python regression libraries. Here is a reference I found on Calculating R-squared in Python.

If you are only going to use three points, you could likely simplify this by using the middle point as your reference and see if the slope to the other two point are exactly/nearly opposite.

Here are some other approaches for How to find if three points fall in a straight line or not.

$\endgroup$
7
  • $\begingroup$ Thanks for the links. I knew to brute force it with computing slopes in Cartesian coords, but your links have some elegant (matrix) versions that I want to get comfortable with. $\endgroup$ – Alan McDonley Dec 27 '17 at 23:03
  • $\begingroup$ Glad to be of assistance, BTW are you most interested in 3-point or multi-point? $\endgroup$ – markshancock Dec 28 '17 at 4:55
  • $\begingroup$ Also, what sensor are you using for the range. I mentor a robotics team and I am always interested in new sensors (especially if they are inexpensive). $\endgroup$ – markshancock Dec 28 '17 at 4:56
  • 1
    $\begingroup$ We use a similar range sensor on our robot. It does have range limitations (you can't be too close or too far). We have also had issues when the wall was not perpendicular to the beam direction; so, you could have problems if you sweep the beam or try to detect an angled wall. $\endgroup$ – markshancock Jan 1 '18 at 22:43
  • 1
    $\begingroup$ Have you considered a VL53L0X time-of-flight laser sensor. I picked one up on eBay for $6; but, I haven't paled with it yet. $\endgroup$ – markshancock Jan 1 '18 at 22:46

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.