0

I'm working on Trilateration to find position of unknown location, to find distance i use path-loss with constant is 2 like in the articles, i'm using 2 phone and one laptop as the transmitter and reference. Here's the code :

distance1 = (double) Math.pow(10, ((rssi.get(k)-rssi1.get(a))/10*2));

rssi is rssi value 1m from reference and rssi1 is unknown location rssi.

And here's Trilateration code which formula is from This :

double r1 = Math.pow(distance1,2 );
            double r2 = Math.pow(distance2,2 );
            double r3 = Math.pow(distance3,2 );
            double x1 = Math.pow(X1,2 );
            double x2 = Math.pow(X2,2 );
            double x3 = Math.pow(X3,2 );
            double y1 = Math.pow(Y1,2 );
            double y2 = Math.pow(Y2,2 );
            double y3 = Math.pow(Y3,2 );
            double A = r1 - r2 - x1+x2 - y1+y2;
            double B = r2 - r3 - x2+x3 - y2+y3;
            double C = ((-2*X1)+(2*X2));
            double D = ((-2*Y1)+(2*Y2));
            double E = ((-2*X2)+(2*X3));
            double F = ((-2*Y2)+(2*Y3));
            double x0 = (((A*F)-(B*D))/((F*C)-(E*D)));
            double y0 = (((A*E)-(C*B))/((D*E)-(C*F)));

            Log.v("x", String.valueOf(x0));
            Log.v("y", String.valueOf(y0));

at first it gives quite accurate result but later the result is not change or even have a big error. I wonder why the result is not change is it because when do new calculation the previous result is not cleared or what? or there's some error in my code? I'm a little bit confuse, because there's more than one way to do trilateration from many source, like this:

            double delta = 4*((X1-X2)*(Y1-Y3)-(X1-X3)*(Y1-Y2));
            double x0 = (1/delta) * ((2*A*(y1-y3))-(2*B*(y1-y2)));
            double y0 = (1/delta) * ((2*B*(x1-x2))-(2*A*(x1-x3)));

It's using delta to calculate which i don't understand where's it come from. I understand the first code which it's a linear equation. Is my constant to calculate distance is wrong? is there any code to do Trilateration with explanation of the formula? Also i trying to expanding time for receiving rssi signal and than average it, but the result is still bad.

Full code:

ArrayList<String> buildings;
    DatabaseHelper db;
    ArrayAdapter<String> arrayAdapter;
    ArrayList<PositionData> positionsData;
    String building;
    TextView result;
    TextView result2;
    TextView result3;
    Button locate;

    public void onCreate(Bundle saveInstanceState) {
        super.onCreate(saveInstanceState);
        setContentView(R.layout.locate_wifi);
        db = new DatabaseHelper(this);
        buildings = db.getBuildings_wifi();
        locate = (Button) findViewById(R.id.locate);

        result = (TextView) findViewById(R.id.result);
        result2 = (TextView) findViewById(R.id.result2);
        result3 = (TextView) findViewById(R.id.result3);
        arrayAdapter = new ArrayAdapter<String>(this,
                android.R.layout.simple_spinner_item, buildings);

        locate.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View arg0) {
                // TODO Auto-generated method stub
                Intent intent = new Intent(getApplicationContext(), Scan.class);
                intent.putExtra("isLearning", false);
                startActivityForResult(intent,0);

            }
        });

        arrayAdapter = new ArrayAdapter<String>(this,
                android.R.layout.simple_list_item_1, buildings);
        // Set The Adapter
        if (buildings.size()==0) {
            Toast.makeText(this, "No building data available.", Toast.LENGTH_LONG).show();
            locate.setEnabled(false);
        }
        else{


            AlertDialog.Builder builder = new AlertDialog.Builder(this);
            builder.setCancelable(false);
            builder.setTitle("Choose building");
            builder.setAdapter(arrayAdapter, new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int which) {
                    // the user clicked on colors[which]
                    building = buildings.get(which);



                }
            });
            builder.show();
        }

    }



    @Override
    protected void onActivityResult(int requestCode, int resultCode,
                                    Intent intent) {
        // TODO Auto-generated method stub
        if(resultCode==RESULT_OK){


            PositionData positionData = (PositionData) intent
                    .getSerializableExtra("PositionData");
            positionsData=db.getWifiPositions(building);

            ArrayList<Router> wifis = db.getFriendlyWifis(building);

            HashMap<String, Double> rssi = positionData.getValues();
            HashMap<String, Double> rssi1 = positionsData.get(0).getValues();
            HashMap<String, Double> rssi2 = positionsData.get(1).getValues();
            HashMap<String, Double> rssi3 = positionsData.get(2).getValues();
            double X1 = positionsData.get(0).getX();
            Double X2 = positionsData.get(1).getX();
            Double X3 = positionsData.get(2).getX();
            Double Y1 = positionsData.get(0).getY();
            Double Y2 = positionsData.get(1).getY();
            Double Y3 = positionsData.get(2).getY();
            double distance1 = 0;
            double distance2 = 0;
            double distance3 = 0;




            for (String k : rssi.keySet()) {
                for (String a : rssi1.keySet()) {
                    if (k.equals(a)) {
                        Log.d("value",((Math.pow(10, ((rssi.get(k)-rssi1.get(a))/10*2))))+"");
                        distance1 = (double) Math.pow(10, ((rssi.get(k)-rssi1.get(a))/10*2));
                    }
                }
                for (String b : rssi2.keySet()) {
                    if (k.equals(b)) {
                        Log.d("value2",((Math.pow(10, ((rssi.get(k)-rssi2.get(b))/10*2))))+"");
                        distance2  = (double) Math.pow(10, ((rssi.get(k)-rssi2.get(b))/10*2));
                    }
                }
                for (String c : rssi3.keySet()) {
                    if (k.equals(c)) {
                        Log.d("value3",((Math.pow(10, ((rssi.get(k)-rssi3.get(c))/10*2))))+"");
                        distance3 = (double) Math.pow(10, ((rssi.get(k)-rssi3.get(c))/10*2));
                    }
                }

            }


            double r1 = Math.pow(distance1,2 );
            double r2 = Math.pow(distance2,2 );
            double r3 = Math.pow(distance3,2 );
            double x1 = Math.pow(X1,2 );
            double x2 = Math.pow(X2,2 );
            double x3 = Math.pow(X3,2 );
            double y1 = Math.pow(Y1,2 );
            double y2 = Math.pow(Y2,2 );
            double y3 = Math.pow(Y3,2 );
            double A = r1 - r2 - x1+x2 - y1+y2;
            double B = r2 - r3 - x2+x3 - y2+y3;
            //double delta = 4*((X1-X2)*(Y1-Y3)-(X1-X3)*(Y1-Y2));
            //double x0 = (1/delta) * ((2*A*(y1-y3))-(2*B*(y1-y2)));
            //double y0 = (1/delta) * ((2*B*(x1-x2))-(2*A*(x1-x3)));
            double C = ((-2*X1)+(2*X2));
            double D = ((-2*Y1)+(2*Y2));
            double E = ((-2*X2)+(2*X3));
            double F = ((-2*Y2)+(2*Y3));
            double x0 = (((A*F)-(B*D))/((F*C)-(E*D)));
            double y0 = (((A*E)-(C*B))/((D*E)-(C*F)));

            Log.v("x", String.valueOf(x0));
            Log.v("y", String.valueOf(y0));

            result.setText("X:  "+ x0);
            result2.setText("Y:  "+ y0);


            super.onActivityResult(requestCode, resultCode, intent);
        }
    }


    public class CustomOnItemSelectedListener implements
            AdapterView.OnItemSelectedListener {

        public void onItemSelected(AdapterView<?> parent, View view, int pos,
                                   long id) {

            building = parent.getItemAtPosition(pos).toString();
            locate.setEnabled(true);
        }

        @Override
        public void onNothingSelected(AdapterView<?> arg0) {
            // TODO Auto-generated method stub
            locate.setEnabled(false);
        }

    }



}

Enryu
  • 91
  • 11
  • What does `rssi.get()` return? If it is an integer, the `/10*2` part ruins that exponent really hard. Add a `.0` in that case, like `/10.0*2` or `/5.0`. – tevemadar Sep 15 '19 at 13:53
  • It's a double, should i add .0 in 2 also? – Enryu Sep 15 '19 at 14:07
  • Is my code for calculatig trilateration is right? – Enryu Sep 15 '19 at 14:08
  • If it is a `double`, the `/10*2` is fine. The maths part seems like what you linked from Mathematics SE. Issues may rather come from the input RSSI-s, or the distance calculation using them. You seem to log `value`, `value2` and `value3`, have you checked what they are and if they make sense in relation to the coordinates you have? – tevemadar Sep 15 '19 at 14:31
  • yes, when i check it, the distance is not right... should i filter the rssi value to remove noise? – Enryu Sep 15 '19 at 14:39

0 Answers0