Skip to content

Instantly share code, notes, and snippets.

@hanafiah
Created February 21, 2020 02:06
Show Gist options
  • Save hanafiah/23484917d5e2ea5a32affa50e8c0ec54 to your computer and use it in GitHub Desktop.
Save hanafiah/23484917d5e2ea5a32affa50e8c0ec54 to your computer and use it in GitHub Desktop.
trilateration
based on https://www.tothenew.com/blog/indoor-positioning-systemtrilateration/
/**
* Calculates the distance from Beacon to device.
**/
private void calculateBeaconDistance(Beacon beacon) {
float txPower = -74.0; // Manufacture set this power in the device
if (beacon .rssi == 0){
return -1.0; // if we cannot determine accuracy, return -1.
}
double ratio = beacon.rssi*1.0 / txPower;
if (ratio < 1.0){
return pow(ratio,10);
}
else{
double accuracy = (0.89976)*pow(ratio,7.7095) + 0.111;
return accuracy;
}
}
/**
* It needs distanceA, distanceB, distanceC, pointA1, pointA2, pointB1, pointB2, pointC1, pointC2
*/
private void getMeetingPoints(distanceA, distanceB, distanceC, pointA1, pointA2, pointB1, pointB2, pointC1, pointC2) {
double w,z,x,y,y2;
w = distanceA * distanceA - distanceB * distanceB - pointA1 * pointA1 - pointA2* pointA2 + pointB1 * pointB1 + pointB2 * pointB2;
z = distanceB * distanceB - distanceC * distanceC - pointB1* pointB1 - pointB2 * pointB2 + pointC1 * pointC1 + pointC2 * pointC2;
x = (w * ( pointC2 - pointB2) - z * ( pointB2 - pointA2)) / (2 * (( pointB1 - pointA1) * ( pointC1 - pointB2) - ( pointC1 - pointB1) * ( pointB2 - pointA2)));
y = (w - 2 * x * (pointB1 - pointA1)) / (2 * ( pointB2 - pointA2));
y2 = (z - 2 * x * ( pointC1 -pointB1)) / (2 * ( pointC1 - pointB2));
y = (y + y2) / 2;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment