Stating GPS Data toa variable

edited June 2015 in Library Questions

Hi Guys, One question, my code has a serial comunication between gps module and Processing using serial library. The gps module provide the latitude and longitude in the processing scketch( figure) and I need to declarate it for a variable. Anyone can help me ? tcc

Tagged:

Answers

  • and here is my code :

    import processing.serial.*;

    import de.fhpotsdam.unfolding.mapdisplay.*; import de.fhpotsdam.unfolding.utils.*; import de.fhpotsdam.unfolding.marker.*; import de.fhpotsdam.unfolding.tiles.*; import de.fhpotsdam.unfolding.interactions.*; import de.fhpotsdam.unfolding.ui.*; import de.fhpotsdam.unfolding.*; import de.fhpotsdam.unfolding.core.*; import de.fhpotsdam.unfolding.mapdisplay.shaders.*; import de.fhpotsdam.unfolding.data.*; import de.fhpotsdam.unfolding.geo.*; import de.fhpotsdam.unfolding.texture.*; import de.fhpotsdam.unfolding.events.*; import de.fhpotsdam.utils.*; import de.fhpotsdam.unfolding.providers.*;

    Serial portaserial; // objeto criado da classe serial. String dadosgps; // var. q ira armazenar os dados recebidos da porta serial. UnfoldingMap map;

    void setup() { String portName = Serial.list()[0]; // porta no qual o arduino ira se comunicar portaserial = new Serial(this, portName, 9600);

        size(800, 600);
        map = new UnfoldingMap(this);
        MapUtils.createDefaultEventDispatcher(this, map);
        map = new UnfoldingMap(this, new Google.GoogleMapProvider());
        map.zoomAndPanTo(new Location(-31.3199163,-54.0902512), 14);
    

    }

    void draw() { if ( portaserial.available() > 0) {
    dadosgps = portaserial.readString();
    } println(dadosgps); delay(100);

    map.draw();
    Location location = map.getLocation(mouseX, mouseY);
    fill(0);
    text(location.getLat() + ", " + location.getLon(), mouseX, mouseY);
    
    Location berlinLocation = new Location(-31.3252781,-54.0958781); // I need that the gps module pass the lat e lon for this parenthesis
    

    // Create point markers for locations SimplePointMarker berlinMarker = new SimplePointMarker(berlinLocation);

    // Adapt style berlinMarker.setColor(color(255, 0, 0, 100)); berlinMarker.setStrokeColor(color(255, 0, 0)); berlinMarker.setStrokeWeight(3);

    // Add markers to the map map.addMarkers(berlinMarker); }

  • You need to parse the raw data from the serial port, i.e. converting the String into a Location.

    But before you can use that it is necessary you translate the coordinates of your GPS device into ones you can use in Unfolding. Currently, the coordinates from the serial port seem to be in some other system than WGS-84. So, not sure how to do that (as I don't know which system they are in), but in principle it goes like this:

    String dadosgps = portaserial.readString();
    
    String latStr = dadosgps.substring(dadosgps.indexOf("lat:")+4, dadosgps.indexOf("lon:")-1);
    println(latStr);
    String lonStr = dadosgps.substring(dadosgps.indexOf("lon:")+4, dadosgps.length());
    println(lonStr);
    
    // Convert from geo-system of your GPS device to Mercator
    
    float lat = Float.parseFloat(latStr);
    float lon = Float.parseFloat(lonStr);
    Location location = new Location(lat, lon);
    
  • edited June 2015

    I got this error. What'sthe wrong with my code ??

  • edited June 2015

    Null.PointerException. I can't resolve it. Can you help me ?

    
    import processing.serial.*;
    
    UnfoldingMap map;
    Serial portaserial; // objeto criado da classe serial.
    String dadosgps= portaserial.readString(); // var. q ira armazenar os dados recebidos da porta serial.
    
    String latStr = dadosgps.substring(dadosgps.indexOf("lat:")+4, dadosgps.indexOf("lon:")-1);
    String lonStr = dadosgps.substring(dadosgps.indexOf("lon:")+4, dadosgps.length());
    
    
    
     void setup(){
    String portName = Serial.list()[0]; // porta no qual o arduino ira se comunicar
    portaserial = new Serial(this, portName, 9600);
    
     
    
    println(latStr);
    
    println(lonStr);
     
    // Convert from geo-system of your GPS device to Mercator
     
    float lat = Float.parseFloat(latStr);
    float lon = Float.parseFloat(lonStr);
    Location location = new Location(lat, lon);
    
     }
      void draw()
    {
      if ( portaserial.available() > 0) 
      {  
      dadosgps = portaserial.readStringUntil('\n');         
      } 
    println(dadosgps); 
    delay(100);
    }
    
  • How I can declarate the variable latStr for example ?? latStr = null ; ????

Sign In or Register to comment.