Sending Real Time GPS data to Processing

Hey,

I was wondering if there was a way to write a program that maps out movement of a GPS device outputting lat/long data. Is there a way to 'draw' lines in processing?

thanks,

marco

I was wondering if there was a way to write a program that maps out movement of a GPS device outputting lat/long data.

Yes, there is.

Is there a way to 'draw' lines in processing?

Yes, there is.

And how could I go about doing that? The communication isn't what I'm worried about, it is the actual plotting and displaying of the movements.

And how could I go about doing that?

Doing what? Drawing a line?

Yes, but I'd like a way to actually be able to see the creation of the line, as i tried something like this:

line(30, 20, 85, 75);
delay(5000);
line(36, 20, 86, 44);

And both lines simply appeared at once.

And both lines simply appeared at once.

The draw() method takes your instructions and updates the buffer. At the end of draw(), the buffer is displayed.

Try something like this:

int i = 0;

void draw()
{
   if(i == 0)
   {
      line(30, 20, 85, 75);
   }
   else if(i == 300)
   {
      line(36, 20, 86, 44);
   }
   i++;
}

Unlike loop() being called as often as possible, draw() is called at a defined frame rate. So, there will be a noticeable interval between when the first line is drawn and the second line is drawn.

In the real program, of course, you won't be doing anything in draw(). All the line creation will happen in the serialEvent() function, as serial data arrives.

I looked up serialEvent() and found this on the website:

// Example by Tom Igoe 
 
import processing.serial.*; 
 
Serial myPort;    // The serial port
PFont myFont;     // The display font
String inString;  // Input string from serial port
int lf = 10;      // ASCII linefeed 
 
void setup() { 
  size(400,200); 
  // You'll need to make this font with the Create Font Tool 
  myFont = loadFont("ArialMS-18.vlw"); 
  textFont(myFont, 18); 
  // List all the available serial ports: 
  printArray(Serial.list()); 
  // I know that the first port in the serial list on my mac 
  // is always my  Keyspan adaptor, so I open Serial.list()[0]. 
  // Open whatever port is the one you're using. 
  myPort = new Serial(this, Serial.list()[0], 9600); 
  myPort.bufferUntil(lf); 
} 
 
void draw() { 
  background(0); 
  text("received: " + inString, 10,50); 
} 
 
void serialEvent(Serial p) { 
  inString = p.readString(); 
}

Should I change myPort to the name of my Arduino Board’s port name?

Should I change myPort to the name of my Arduino Board's port name?

It is necessary to make sure that that value of myPort is the value of the port that the Arduino is connected to. The Serial.list() method returns a list of ports that the Arduino might be attached to. Use the correct index in the brackets to access the port that the Arduino is actually connected to.

The name of the instance doesn't matter, but myArduino makes it easier to remember what is connected to the Serial instance than myPort does.

PaulS:
The draw() method takes your instructions and updates theIn the real program, of course, you won’t be doing anything in draw(). All the line creation will happen in the serialEvent() function, as serial data arrives.

My experience is different. Drawing from serialEvent does not work (at least for me).

Arduinio generator

void setup() {
  Serial.begin(115200);
}

byte chaNumber = 4;
unsigned long feedDist = 500;

void loop() {
  static unsigned long lastFeed;
  unsigned long topLoop = millis();
  if (topLoop - lastFeed >= feedDist) {
    lastFeed = topLoop;
    for (byte channel = 0; channel < chaNumber; channel++) {
      Serial.print(analogRead(channel));
      if (channel != chaNumber - 1) {
        Serial.write(',');
      }
    }
    Serial.println();
  }
}

produced stream

413,354,320,312
333,326,312,310
315,310,302,304
304,298,292,296
301,295,289,291
299,294,288,289
299,293,288,289
296,290,285,287
297,292,287,288
298,293,289,290
296,292,287,288
297,293,289,290
298,293,290,291
299,294,290,292
298,293,290,291
298,294,290,292
299,294,290,293
298,294,290,293
299,295,291,293

Processin sketch

import processing.serial.*;
Serial mySerial;
int lf = 10;    // Linefeed in ASCII

void setup() {
  size(640, 480);
  printArray(Serial.list());
  mySerial = new Serial( this, Serial.list()[2], 115200 );  // select your port
  mySerial.bufferUntil(lf);
}

boolean newValue = false;
String[] fld;

float toX(String iS) {
  return map(int(iS), 0, 1023, 0, width);
}
float toY(String iS) {
  return map(int(iS), 0, 1023, height, 0);
}
void draw() {
  if (newValue) {
    ellipse(toX(fld[0]), toY(fld[1]), int(fld[2])/10, int(fld[3])/10);
    newValue = false;
  }
}

void serialEvent(Serial p) {
  String rStr = p.readString();
  if (rStr != null) {
    fld = splitTokens(trim(rStr), ",");
    if (fld.length<4) {
      return;
    }
    newValue = true;
  }
}

void keyPressed() {
  exit();  // Stops the program
}

The above works, while having

    ellipse(toX(fld[0]), toY(fld[1]), int(fld[2])/10, int(fld[3])/10);

directly in serialEvent does not. (on Win7/64, 1.6.5, 3.0.2)