Beefy Boxes and Bandwidth Generously Provided by pair Networks
Problems? Is your data what you think it is?
 
PerlMonks  

Check whether two numbers are within a range

by stevieb (Canon)
on Feb 26, 2021 at 16:07 UTC ( #11128842=perlquestion: print w/replies, xml ) Need Help??

stevieb has asked for the wisdom of the Perl Monks concerning the following question:

Hey Monks, I come here seeking help from our mathematicians again, as I'm feeling apathetic today and am having a brain fart moment.

My objective is to find out if my car is parked within a 1.1 metre proximity of where it should be. This will identify whether the vehicle is in my garage or not. I then will read the vehicle's charge level, and using an LED strip on the garage wall, indicate the level on that strip, and if it's below a threshold, sound an audible alarm to remind me to plug the car in to recharge it. I will be using motion and sonar proximity sensors to identify when I've come into the house as to turn the warning off if I don't plug the car in (the car will indicate if the charger is plugged in via my software, and I'll disable the alert when this happens too).

Anyway, with GPS, the fifth decimal place indicates the 1.1 metre area I'm looking to find. My question is, how do I compare the known set location, say 3.1566789, with the actual location of the vehicle, say 3.1565323, and verify whether that they are within at least five decimal places of one another?

I don't know why I'm having this moment, I know it's a silly, easy thing. It'll likely come to me the second I hit submit. I know I could regex the numbers out, but I'd rather have a math calculation as I want to apply it to other areas.

I have no code for this simple example, so I'll just post the following to at least make it perlish.

use 5.10.0; $p=japh;push@a,w();$s=j4;sub n{"8fbac6c6e252"};unshift@a, "b4d6c7ea52a7";$k=crypt($s,$p);$o="aeafa7cfdbd58c";@h= map{sprintf"%x",ord$_}split//,$k;push@a,$o;$a[3]=pop@a; $a[2]=n();sub w{"bcb3d8dec8dd"}$x.=$_ for@a;@b=($x=~m/..?/g); push@z,@h until @z>@b;for(@b){push@japh,hex($_)-hex($z [$n]);$n++;}say map{chr$_}@japh;

Cheers,

-stevieb

Replies are listed 'Best First'.
Re: Check whether two numbers are within a range
by LanX (Cardinal) on Feb 26, 2021 at 16:29 UTC
    This?

    DB<54> $b=3.1565323 DB<55> $a=3.1566789 DB<56> p abs(1e5*($b-$a)) <1 DB<57> p abs(1e5*($b-$a)) 14.660000000033 DB<58>

    of course you could avoid the multiplication by providing an accurate threshold smaller than 1.

    It'll become more complicated with two coordinates, than I'd say Pythagoras is accurate enough°

    edit

    Pythagoras sqr( d_x ** 2 + d_y ** 2 ) with d_ for delta= abs(c0-c1)

    Cheers Rolf
    (addicted to the Perl Programming Language :)
    Wikisyntax for the Monastery

    °) even for spherical coordinates of that size (but don't use this for the distance between New York and Seattle, you might fall off the edge of the world)

      Thank you. This works well, regardless if the vehicle's actual location will be plus or minus the pre-determined coordinates I'll be comparing against.

      Mock up:

      use warnings; use strict; use feature 'say'; use constant { ACCURACY => 1e5, RANGE => 1.2, LON => -119.31665, }; my $lon_current = -119.31666; my $deviation = abs(ACCURACY * (LON - $lon_current)); say "Within range: $deviation" if $deviation < RANGE;
        I hope you are not only testing the longitude but also the latitude.

        Cheers Rolf
        (addicted to the Perl Programming Language :)
        Wikisyntax for the Monastery

Re: Check whether two numbers are within a range
by Anonymous Monk on Feb 26, 2021 at 16:37 UTC

    You could use Manhattan (a.k.a. L1) distance, that is, check if abs($x - $x0) + abs($y - $y0) <= $thresh, but the area where the expression is true is a square on the latitude/longitude grid. You could also use the Euclidean (L2) distance, i.e. sqrt(($x - $x0)**2 + ($y - $y0)**2) <= $thresh, which designates a circle with radius $thresh around the point ($x0, $y0).

    On a very small scale, you can ignore the fact that the latitude and longitude grid describes a spheregeoid instead of a flat area and keep the assumptions above (about the square and the circle). If you are truly interested, you can find out the coordinates of the corners of the garage and check if the point falls inside the polygon described by those coordinates.

    By the way, is your GPS precise enough to position your car when inside the garage?

      "By the way, is your GPS precise enough to position your car when inside the garage?"

      100% yes (even if it lost GPS signal, I assume the car itself would be able to calculate speed, direction etc enough to formulate its position within a high degree of accuracy. For example, last week I drove through a long snow tunnel on my way to and fro Vancouver, and the car had no complaints driving itself and keeping track of its position on the map. I don't pretend to know exactly how it all works though).

      Update: I am reading directly from Tesla's API which provides vehicle data from the car's internal systems, including GPS.

        > I assume the car itself would be able to calculate speed, direction etc enough to formulate its position

        my guess is rather the grid of local wifi (UD: and Bluetooth) signals. GPS accuracy is dumbed down for military reasons.

        Cheers Rolf
        (addicted to the Perl Programming Language :)
        Wikisyntax for the Monastery

      > but the area where the expression is true is a square on the latitude/longitude grid.

      Yes, a 90° rotated square with the tips on the axes.

      I think abs($x - $x0) <= $d_x and abs($y - $y0) <= $d_y is more intuitive here.

      This really depends on the local geometry though and torsion between geo-grid and garage (i.e. a rotation could help, too)

      Cheers Rolf
      (addicted to the Perl Programming Language :)
      Wikisyntax for the Monastery

        I'll also be using two HC-SR04 sonar-based proximity sensors to doubly confirm that the car is actually where it says it is, as I'm also including the ability for the garage door to close once the vehicle is in place, so the GPS is just initial location data to start firing the proximity sensors.

        A HC-SR501 PIR sensor will sense motion which will start monitoring GPS for proximity to its proper place in the garage. Once it's within the desired range (2 metres or so), it'll fire the sonar sensors to ensure it's inside the garage so the door can shut. If it's just someone in the garage and the car isn't anywhere near the house, we stop doing all work.

        I'm using a Raspberry Pi Zero for the backend API logic which will communicate to a Wemos D1 mini over bluetooth in the garage which will automate the garage door, display the charge level via LED strip, and sound the audible "PLUG IN YOUR DAMNED CAR" warning if battery percent is lower than my desired level.

Re: Check whether two numbers are within a range
by karlgoethebier (Abbot) on Feb 26, 2021 at 20:10 UTC
    «...feeling apathetic today...brain fart moment»

    I guess I know what you mean. An alternative might be to go for something totally different. This is the way I do it: I recently enrolled for a course at Berklee. Not so bad. Best regards, Karl

    «The Crux of the Biscuit is the Apostrophe»

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://11128842]
Approved by marto
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others lurking in the Monastery: (3)
As of 2021-04-15 23:22 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found

    Notices?