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 & HelpOther Libraries › OSC and Processing
Page Index Toggle Pages: 1
OSC and Processing (Read 603 times)
OSC and Processing
May 21st, 2008, 10:05pm
 
Hi,

I'm new to Processing and trying to send accelerometer data to Max with OSC.
Is there any suggestions?

My code:


// Draw a cylinder centered on the y-axis, going down from y=0 to y=height.
// The radius at the top can be different from the radius at the bottom,
// and the number of sides drawn is variable.

import oscP5.*;
import netP5.*;
import processing.serial.*;
import fullscreen.*;  // for updates: http://www.superduper.org/processing/fullscreen_api/
import processing.opengl.*; //OPENGL is in an experimental mode when entering fullscreen
import quicktime.std.movies.MovieDrawingComplete; // this enable fullscreen and exit fullscreen
import quicktime.std.movies.MovieController;

// This enable fullscreen
//Keyboard shortcuts: To enter/leave fullscreen mode:  ⌘+F
OscP5 oscP5;
NetAddress myRemoteLocation;
Serial myPort;
char inByte;
int xVal, yVal, zVal = 0;
int[] serialInArray = new int[3];
int serialCount = 0;
boolean firstContact = true;
String theValues = null; //string holds
FullScreen fs;

void setup(){


// set size to 1440X900

size(1440, 900, OPENGL);


// 5 fps

frameRate(24);
       oscP5 = new OscP5(this,12000);
       myRemoteLocation = new NetAddress("127.0.0.1",12000);

// Create the fullscreen object

fs = new FullScreen(this);


// enter fullscreen mode

fs.enter();



//This will hide the mouse cursor

try {


Robot robot = new Robot();


robot.mouseMove(int(width),int(height));

}

catch (AWTException e) {

}

//setup serial port

myPort = new Serial(this, Serial.list()[0], 9600); //what is wrong?

myPort.clear();
       
       
}

void draw() {

background(127, 202, 235);

fill(155,1 ,1);

lights();
       

translate(width/2, height/2);

rotateY(map(xVal*20, 0, width, 0, PI));

rotateZ(map(yVal*20, 0, height, 0, -PI));
       //rect(xVal*6, yVal*4, 250, 250);

noStroke();

translate(0, -40, 0);
       

drawCylinder(xVal, 2*zVal, yVal, 64);  // Draw a cylinder and defrom the object!
       
}

void drawCylinder(float topRadius, float bottomRadius, float tall, int sides) { // here the object is born!

float angle = 0;

float angleIncrement = TWO_PI / sides;

beginShape(QUAD_STRIP);

for (int i = 0; i < sides + 1; ++i) {


vertex(topRadius*cos(angle), 0, topRadius*sin(angle));


vertex(bottomRadius*cos(angle), tall, bottomRadius*sin(angle));


angle += angleIncrement;

}

endShape();

// If it is not a cone, draw the circular top cap

if (topRadius != 0) {


angle = 0;


beginShape(TRIANGLE_FAN);


// Center point


vertex(0, 0, 0);


for (int i = 0; i < sides + 1; i++) {



vertex(topRadius * cos(angle), 0, topRadius * sin(angle));



angle += angleIncrement;


}  


endShape();

}

// If it is not a cone, draw the circular bottom cap

if (bottomRadius != 0) {


angle = 0;


beginShape(TRIANGLE_FAN);


// Center point


vertex(0, tall, 0);


for (int i = 0; i < sides+1; i++) {



vertex(bottomRadius * cos(angle), tall, bottomRadius * sin(angle));



angle += angleIncrement;


}  


endShape();

}
}

void serialEvent(Serial port) {

// read a byte from the serial port:

int inByte = port.read();

// if this is the first byte received,

// take note of that fact. Otherwise, add it to the array:

if (firstContact == false) {


if (inByte == 'A') {



port.clear();          // clear the serial port buffer



firstContact = true;



port.write('A');


}

}

else {


// Add the latest byte from the serial port to array:


serialInArray[serialCount] = inByte;


serialCount++;



// If we have 3 bytes:


if (serialCount > 2 ) {



xVal = serialInArray[0];



yVal = serialInArray[1];



zVal = serialInArray[2];




// print the values (for debugging purposes only):



println(xVal + "\t" + yVal + "\t" + zVal);




// Send a capital A to request new sensor readings:



port.write('A');



// Reset serialCount:



serialCount = 0;


}

}

}


void simpleOscMessage1() {
 /* in the following different ways of creating osc messages are shown by example */
 OscMessage myMessage = new OscMessage("/xVal");
 
 myMessage.add(123); /* add an int to the osc message */

 /* send the message */
 oscP5.send(myMessage, myRemoteLocation);
}

Re: OSC and Processing
Reply #1 - May 22nd, 2008, 3:18am
 
hi,
to send your data to max, put
Code:

OscMessage myMessage = new OscMessage("/xVal");
myMessage.add(xVal);
oscP5.send(myMessage, myRemoteLocation);


after
Code:

xVal = serialInArray[0];


in your serialEvent method.

if you want to send all 3 values, use e.g.
Code:

OscMessage myMessage = new OscMessage("/Vals");
myMessage.add(xVal);
myMessage.add(yVal);
myMessage.add(zVal);
oscP5.send(myMessage, myRemoteLocation);


after

Code:

zVal = serialInArray[2];



to send the data to max, make sure your remoteAddress (stored in myRemoteLocation) has the correct ip address and port number (the port number that max is listening at for incoming messages).

btw, there are a lot of blank lines in the code you posted. the processing ide has a "copy to discourse" feature under tools in the menu bar which nicely formats your processing code, copies it into the clipboard and lets you easily paste it in a post here.

best,
andi
Re: OSC and Processing
Reply #2 - May 23rd, 2008, 12:54pm
 
Hi,

Thanks!

Best,

Boye
Page Index Toggle Pages: 1