Converting Intersection Area to Generate Coordinate

I worked on a project where I get analog values ​​from a resistive touch screen and turn them into intersection points.

Here is an example: enter image description here

Here is my code for collecting data using Arduino Uno and plotting points using a tool called processing.

#define side1 2
#define side2 3
#define side3 4
#define side4 5
#define contact A0

void setup() {
  pinMode(contact, INPUT); 
  pinMode(side1, OUTPUT);  
  pinMode(side2, OUTPUT);  
  pinMode(side3, OUTPUT);  
  pinMode(side4, OUTPUT);  
  Serial.begin(9600);
}

void loop() {
  int sensorValue1;
  int sensorValue2;
  int sensorValue3;
  int sensorValue4;

  // SENSOR VALUE 1:
  digitalWrite(side1, LOW);
  digitalWrite(side2, HIGH);
  digitalWrite(side3, HIGH);
  digitalWrite(side4, HIGH);
  delay(5);
  for (int i = 0; i < 10; i++){
    sensorValue1 = analogRead(contact);
  }


  // SENSOR VALUE 2:
  digitalWrite(side2, LOW);
  digitalWrite(side3, HIGH);
  digitalWrite(side4, HIGH);
  digitalWrite(side1, HIGH);
  delay(5);
  for (int i = 0; i < 10; i++){
    sensorValue2 = analogRead(contact);
  }


  // SENSOR VALUE 3:
  digitalWrite(side3, LOW);
  digitalWrite(side2, HIGH);
  digitalWrite(side4, HIGH);
  digitalWrite(side1, HIGH);
  delay(5);
  for (int i = 0; i < 10; i++){
    sensorValue3 = analogRead(contact);
  }


  // SENSOR VALUE 2:
  digitalWrite(side4, LOW);
  digitalWrite(side3, HIGH);
  digitalWrite(side2, HIGH);  
  digitalWrite(side1, HIGH);
  delay(5);
  for (int i = 0; i < 10; i++){
    sensorValue4 = analogRead(contact);
  }

  Serial.print(sensorValue1);
  Serial.print(",");
  Serial.print(sensorValue2);
  Serial.print(",");
  Serial.print(sensorValue3);
  Serial.print(",");
  Serial.print(sensorValue4);
  Serial.println();
}

This is the processing code for plotting.

import processing.serial.*;


Serial myPort;  // The serial port
int maxNumberOfSensors = 4;   
float[] sensorValues = new float[maxNumberOfSensors];
float sensorValueX;
float sensorValueX1;
float sensorValueY;
float sensorValueY1;
int scaleValue = 2;

void setup () { 
  size(600, 600);  // set up the window to whatever size you want
  //println(Serial.list());  // List all the available serial ports
  String portName = "COM5";
  myPort = new Serial(this, portName, 9600);
  myPort.clear();
  myPort.bufferUntil('\n');  // don't generate a serialEvent() until you get a newline (\n) byte
  background(255);    // set inital background
  smooth();  // turn on antialiasing
}


void draw () {
  //background(255);
  //noFill();
  fill(100,100,100,100);
  ellipse(height,0, scaleValue*sensorValues[0], scaleValue*sensorValues[0]);

  ellipse(0,width, scaleValue*sensorValues[1], scaleValue*sensorValues[1]);
  ellipse(height,width, scaleValue*sensorValues[2], scaleValue*sensorValues[2]);
  ellipse(0,0, scaleValue*sensorValues[3], scaleValue*sensorValues[3]);
  //ellipse(sensorValueY, sensorValueX, 10,10);
  //println(sensorValueY,sensorValueX);
  sensorValueX = ((sensorValues[3]*sensorValues[3])-(sensorValues[2]*sensorValues[2])+600*600)/2000;
  sensorValueX1 = ((sensorValues[0]*sensorValues[0])-(sensorValues[1]*sensorValues[1])+600*600)/2000;
sensorValueY = ((sensorValues[3]*sensorValues[3])-(sensorValues[2]*sensorValues[2])+(600*600))/2000;
  sensorValueY1 = ((sensorValues[1]*sensorValues[1])-(sensorValues[0]*sensorValues[0])+(600*600))/2000;

  line(0, scaleValue*sensorValueX, height,scaleValue* sensorValueX);
  line(scaleValue*sensorValueY, 0, scaleValue*sensorValueY, width);
  ellipse(scaleValue*sensorValueY, scaleValue*sensorValueX, 20,20);
  line(0, scaleValue*sensorValueX1, height,scaleValue* sensorValueX1);
  line(scaleValue*sensorValueY1, 0, scaleValue*sensorValueY1, width);
  ellipse(scaleValue*sensorValueY1, scaleValue*sensorValueX1, 20,20);
  println(scaleValue*sensorValueX,scaleValue*sensorValueY);
}


void serialEvent (Serial myPort) {
  String inString = myPort.readStringUntil('\n');  // get the ASCII string

  if (inString != null) {  // if it not empty
    inString = trim(inString);  // trim off any whitespace
    int incomingValues[] = int(split(inString, ","));  // convert to an array of ints

    if (incomingValues.length <= maxNumberOfSensors && incomingValues.length > 0) {
      for (int i = 0; i < incomingValues.length; i++) {
        // map the incoming values (0 to  1023) to an appropriate gray-scale range (0-255):

        sensorValues[i] = map(incomingValues[i], 0, 1023, 0, width);
        //println(incomingValues[i]+ " " + sensorValues[i]);
      }
    }
  }
}

, ? : , (600 600). ? , , x y . , x y , . - ?

+4
2

, , n n- . , , Trilateration ( ). , , n .

:

Trilateration 3 .

  • 1 , .
  • 2 2 ,
  • 3 ,

, , , , - 3 , . , , , . .

.

class Sensor.

class Sensor {
    public PVector p; // position
    public float d; // distance from sensor to target (radius of the circle)

    public Sensor(float x, float y) {
        this.p = new PVector(x, y);
        this.d = 0;
    }
}

, /, :

PVector trilateration(Sensor s1, Sensor s2, Sensor s3) { 
    PVector s = PVector.sub(s2.p, s1.p).div(PVector.sub(s2.p, s1.p).mag());
    float a = s.dot(PVector.sub(s3.p, s1.p));

    PVector t = PVector.sub(s3.p, s1.p).sub(PVector.mult(s, a)).div(PVector.sub(s3.p, s1.p).sub(PVector.mult(s, a)).mag());
    float b = t.dot(PVector.sub(s3.p, s1.p));
    float c = PVector.sub(s2.p, s1.p).mag();

    float x = (sq(s1.d) - sq(s2.d) + sq(c)) / (c * 2);
    float y = ((sq(s1.d) - sq(s3.d) + sq(a) + sq(b)) / (b * 2)) - ((a / b) * x);

    s.mult(x);
    t.mult(y);

    return PVector.add(s1.p, s).add(t);
}

s1, s2, s3 - 3- , , :

PVector target = trilateration(s1, s2, s3);

. , , . , . Java, .

Java, com.lemmingapex.trilateration. 4 , :

s1, s2, s3, s4 class Sensor.

double[][] positions = new double[][] { { s1.x, s1.y }, { s2.x, s2.y }, { s3.x, s3.y }, { s4.x, s4.y } };
double[] distances = new double[] { s1.d, s2.d, s3.d, s4.d };

NonLinearLeastSquaresSolver solver = new NonLinearLeastSquaresSolver(
            new TrilaterationFunction(positions, distances),
            new LevenbergMarquardtOptimizer());
Optimum optimum = solver.solve();

double[] target = optimum.getPoint().toArray();
double x = target[0];
double y = target[1];

trilateration(), , .

1 -

3 - 3 , - .

Example 1

2 -

3 - 3 , - .

Example 2

+2

, , - , , (x1, y1), (x2, y2), (x3, y3), (x4, y4) r1, r2, r3, r4.

(x, y),

F (x, y) = Sum_i [square (d2 ((x, y), (xi, yi)) - ri)]

, . " " (, ), , ( , ). M P = -G

M - (2x2) F x y ( ), G - F (). "" P, , :

(x, y) x = x + Px, y = y + Py .. .. ( M G, P, x y, M G, P, x y). , , .

, 2x2 , F , .

1: -, , ( , , M, ). .

2: , ( , ), , , 2x2 , , , ( , , ).

0

Source: https://habr.com/ru/post/1672418/


All Articles