We closed this forum 18 June 2010. It has served us well since 2005 as the ALPHA forum did before it from 2002 to 2005. New discussions are ongoing at the new URL http://forum.processing.org. You'll need to sign up and get a new user account. We're sorry about that inconvenience, but we think it's better in the long run. The content on this forum will remain online.
IndexProgramming Questions & HelpOpenGL and 3D Libraries › OpenGL + serial library = overload
Page Index Toggle Pages: 1
OpenGL + serial library = overload? (Read 1082 times)
OpenGL + serial library = overload?
Sep 5th, 2007, 1:41pm
 
Hello

I'm working on a version of a Pong game, where three paddles are going to be physically controlled by big magnet switches. They are read by an Arduino board who sends the input (just digital) to Processing via the serial port, so i need the processing.serial library for that reason. And for the graphics to be speedy enough I have used the opengl library, with success so far..
But when I combine the switch code with the game code, the application won't run. (If I remove the opengl library it works again, so there's nothing wrong with the code). Seems like serial and opengl libraries don't fit together. Is there anybody who's experienced the same, and maybe know what else to try? I can't remove the serial library for functional reasons, but is there another way to speed up the graphics that could work in ths case?

Best Wishes

Sara
Re: OpenGL + serial library = overload?
Reply #1 - Sep 5th, 2007, 4:43pm
 
that shouldn't be the case. my guess would be that you're doing something like having serial block or wait (rather than using serialEvent()) which is freezing things up. but without any code, there's not much we can do but guess.
Re: OpenGL + serial library = overload?
Reply #2 - Sep 5th, 2007, 8:32pm
 
Hi again

That might be the case, even though I'm not sure exactly what you mean. We have tried to solve the communication from Arduino into Processing with a seemingly simple routine called Firmata, that unfortunately didn't work the way it should. Now we're using a 'call and response' method created by Tom Igoe. I have tried to simplify the code as much as I could, to post it here if you want to take a look. I've also marked out the passages with code that has to do with the serial reading. Do you have any suggestions of how to change the code, please tell me.

Thanks

Sara

/**
* Serial Call-Response
* by Tom Igoe.
*
* Sends a byte out the serial port, and reads 3 bytes in.
* Sets foregound color, xpos, and ypos of a circle onstage
* using the values returned from the serial port.
* Thanks to Daniel Shiffman for the improvements.
*/


import processing.serial.*;
import processing.opengl.*;

//////////////// SENSOR CODE //////////////////////////////////////////////
int sensor1;
int sensor2;
int sensor3;

Serial port;                         // The serial port
int[] serialInArray = new int[3];    // Where we'll put what we receive
int serialCount = 0;                 // A count of how many bytes we receive

boolean firstContact = false;        // Whether we've heard from the microcontroller
////////////////////////////////////////////////////////////////////////////

// Global variables for the beam
float beamX;
float beamY;
float distA;
float beamDir = 1;
float beamSize = 50;             //////////// tune radius of beam
float dy = 0;  // Direction
float speed = 0.5;               //////////// tune speed of pads
int beamSpeed = 2;               //////////// tune speed of beam

// Global variables for paddle
float paddleWidth = 5;          /////////////////////////////// scale
float paddleHeight = 20;        /////////////////////////////// scale
float paddleY;
float earthX;
float earthY;
float earthSize;


void setup()
{
 ///////////////// SENSOR CODE /////////////////
 // Print a list of the serial ports, for debugging purposes:

 //println(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].
 // On Windows machines, this generally opens COM1.
 // Open whatever port is the one you're using.
 port = new Serial(this, Serial.list()[0], 9600);
 port.write(65);    // Send a capital A to start the microcontroller sending
 ///////////////////////////////////

 frameRate(200);
 size(1400, 787, OPENGL);
 rectMode(CENTER_RADIUS);
 ellipseMode(CENTER_RADIUS);
 colorMode(HSB,100);
 //noStroke();
 smooth();
 beamX = 1;
 beamY = height/2;
 distA = width*0.1;
 paddleY = height;
 earthX = width*1.05;
 earthY = height/2;
 earthSize = height/4;

}

void draw()
{
 background(55,40,90);
 noStroke();

 //////////////// SENSOR CODE ////////////
 print(" s1: ");
 print(sensor1);
 print(" s2: ");
 print(sensor2);
 print(" s3: ");
 print(sensor3);
 println();

 // Get any new serial data
 while (port.available() > 0) {
   serialEvent();
   // Note that we heard from the microntroller:
   firstContact = true;
 }
 // If there's no serial data, send again until we get some.
 // (in case you tend to start Processing before you start your
 // external device):
 if (firstContact == false) {
   delay(300);   /////
   port.write(65);
 }
 /////////////////////////////////////////

 beamX += beamDir * beamSpeed;
 beamY += dy;

 if(beamX > width + beamSize) {
   beamX = -width/2 - beamSize;
   beamY = random(0, height);
   dy = 0;
 }

 // Place paddle on screen
 if(paddleY < height-paddleHeight && paddleY > 0+paddleHeight) {
   if (sensor1 == 255) {
     paddleY = paddleY - speed;
     paddiY = paddiY + speed;
     padY = padY + speed;
   }
   else if (sensor1 == 0) {
     paddleY = paddleY + speed;
     paddiY = paddiY + speed;
     padY = padY + speed;

   }
 }
 
 if(paddleY >= height-paddleHeight) {
   paddleY = height-paddleHeight-speed;
 }
 if(paddleY <= 0+paddleHeight) {
   paddleY = paddleHeight+speed;
 }  

 // Draw beam
 fill(random(40,60),10,100);
 ellipse(beamX, beamY, beamSize, beamSize/6);

 // Draw paddle
 fill(0,0,100);
 rect(width - distA, paddleY, paddleWidth, paddleHeight);  

 // Draw earth
 fill(32,40,70);
 ellipse(earthX, earthY, earthSize, earthSize);

}

////////////// VOIDS ///////////////
void serialEvent() {
 processByte((char)port.read());
}

void processByte(char inByte) {
 // Add the latest byte from the serial port to array:
 serialInArray[serialCount] = inByte;
 serialCount++;
 // If we have 3 bytes:
 if (serialCount > 2 ) {
   sensor1 = (int)serialInArray[0];
   sensor2 = (int)serialInArray[1];
   sensor3 = (int)serialInArray[2];

   // Send a capital A to request new sensor readings:
   port.write(65);
   // Reset serialCount:
   serialCount = 0;
 }
}




Re: OpenGL + serial library = overload?
Reply #3 - Sep 5th, 2007, 10:34pm
 
I just came to think of, that the code I just posted might be to complex to tell just how we're now trying to do the serial communication. This is the code in processing, without the opengl library, that just reads the three sensors. The code itself does not work without the serial input, I'm afraid.. Sorry for that. But maybe it can sort out the type of call-response we're using. Do you think we could change this with something built on serialEvent() instead?

Sara

/**
* Serial Call-Response
* by Tom Igoe.
*
* Sends a byte out the serial port, and reads 3 bytes in.
* Sets foregound color, xpos, and ypos of a circle onstage
* using the values returned from the serial port.
* Thanks to Daniel Shiffman for the improvements.
*/



import processing.serial.*;


int sensor1;
int sensor2;
int sensor3;

Serial port;                         // The serial port
int[] serialInArray = new int[3];    // Where we'll put what we receive
int serialCount = 0;                 // A count of how many bytes we receive

boolean firstContact = false;        // Whether we've heard from the microcontroller

void setup() {
 size(256, 256);  // Stage size
 noStroke();      // No border on the next thing drawn


 // Print a list of the serial ports, for debugging purposes:
 println(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].
 // On Windows machines, this generally opens COM1.
 // Open whatever port is the one you're using.
 port = new Serial(this, Serial.list()[0], 9600);
 port.write(65);    // Send a capital A to start the microcontroller sending
}

void draw() {
 background(100);
 fill(200);
 
 print(" s1: ");
   print(sensor1);
   print(" s2: ");
   print(sensor2);
   print(" s3: ");
   print(sensor3);
   println();

 // Get any new serial data
 while (port.available() > 0) {
   serialEvent();
   // Note that we heard from the microntroller:
   firstContact = true;
 }
 // If there's no serial data, send again until we get some.
 // (in case you tend to start Processing before you start your
 // external device):
 if (firstContact == false) {
   delay(300);
   port.write(65);
 }
}

void serialEvent() {
 processByte((char)port.read());
}

void processByte(char inByte) {
 // Add the latest byte from the serial port to array:
 serialInArray[serialCount] = inByte;
 serialCount++;
 // If we have 3 bytes:
 if (serialCount > 2 ) {
   sensor1 = (int)serialInArray[0];
   sensor2 = (int)serialInArray[1];
   sensor3 = (int)serialInArray[2];

   // Send a capital A to request new sensor readings:
   port.write(65);
   // Reset serialCount:
   serialCount = 0;
 }
}
Re: OpenGL + serial library = overload?
Reply #4 - Sep 6th, 2007, 3:59pm
 
Hi, replying my own question once again..
I just want to say that the problem is solved, since I was so lucky to get guiding from Marius Watz today. It turns out that the draw loop is called twice when using opengl, and that made my program hang up, since this made it check for first contact twice.. The solution was to put the initialization of the serial port in a separate routine. Let me know if anybody needs code for this, if they are trying to do something similar, then I can post it here..

Thank's again for really quick response :)

Sara
Re: OpenGL + serial library = overload?
Reply #5 - Sep 6th, 2007, 7:12pm
 
this is case in point for why, as stated in the reference, size() must go first inside setup()...

--your favorite broken record
Page Index Toggle Pages: 1