im a beginner in programmation. So here's my code. When I do my print, it prints, for example, (pos1 180 pos2 0 posmoyenne 90). The issue is pos2 outputs 0. But, in the code where the message is sent, there's a value. Here are my two codes that communicate together.
- import hypermedia.video.*;
- import java.awt.*;
- import oscP5.*;
- import netP5.*;
- OscP5 oscP5;
- NetAddress remote;
- OpenCV opencv;
- int w = 320;
- int h = 240;
- int threshold = 0; //Ajuster automatiquement le threshold
- int hauteur1;
- int hauteur2;
- PFont font;
- void setup() {
- boolean find=true;
- /* start oscP5, listening for incoming messages at port 12000 */
- oscP5 = new OscP5(this,"127.0.0.1",6000);
- /* myRemoteLocation is a NetAddress. a NetAddress takes 2 parameters,
- * an ip address and a port number. myRemoteLocation is used as parameter in
- * oscP5.send() when sending osc packets to another computer, device,
- * application. usage see below. for testing purposes the listening port
- * and the port of the remote location address are the same, hence you will
- * send messages back to this sketch.
- */
- //myRemoteLocation = new NetAddress("127.0.0.1",12000);
- size( w*2+30, h*2+30 );
- opencv = new OpenCV( this );
- opencv.capture(w, h);
- font = loadFont( "AndaleMono.vlw" );
- textFont( font );
- println( "Drag mouse inside sketch window to change threshold" );
- println( "Press space bar to record background image" );
- }
- void draw() {
- background(0);
- opencv.read();
- //opencv.flip( OpenCV.FLIP_HORIZONTAL );
- // image( opencv.image(), 10, 10 ); // RGB image
- image( opencv.image(OpenCV.GRAY), 10, 10 ); // GRAY image
- image( opencv.image(OpenCV.MEMORY), 10, 20+h ); // image in memory
- opencv.absDiff();
- opencv.threshold(threshold);
- image( opencv.image(OpenCV.GRAY), 20+w, 10 ); // absolute difference image
- // working with blobs
- Blob[] blobs = opencv.blobs( 100, w*h/3, 2 , false );
- noFill();
- pushMatrix();
- translate(20+w, 10);
- for ( int i=0; i<blobs.length; i++ ) {
- Rectangle bounding_rect = blobs[i].rectangle;
- float area = blobs[i].area;
- float circumference = blobs[i].length;
- Point centroid = blobs[i].centroid;
- Point[] points = blobs[i].points;
- // rectangle
- noFill();
- stroke( blobs[i].isHole ? 128 : 64 );
- rect( bounding_rect.x, bounding_rect.y, bounding_rect.width, bounding_rect.height );
- OscMessage myOscMessage = new OscMessage("/test");
- if (i == 0) {
- hauteur1 = 240-bounding_rect.y-bounding_rect.height;
- myOscMessage.add(hauteur1); //FIRST VALUE ADDED
- //println(hauteur1);
- }
- if (i == 1) {
- hauteur2 = 240-bounding_rect.y-bounding_rect.height;
- println(hauteur2);
- myOscMessage.add(hauteur2); //SECOND VALUE ADDED
- }
- /* add an int to the osc message */
- oscP5.send(myOscMessage);
- // centroid
- stroke(0, 0, 255);
- line( centroid.x-5, centroid.y, centroid.x+5, centroid.y );
- line( centroid.x, centroid.y-5, centroid.x, centroid.y+5 );
- noStroke();
- fill(0, 0, 255);
- text( area, centroid.x+5, centroid.y+5 );
- fill(255, 0, 255, 64);
- stroke(255, 0, 255);
- if ( points.length>0 ) {
- beginShape();
- for ( int j=0; j<points.length; j++ ) {
- vertex( points[j].x, points[j].y );
- }
- endShape(CLOSE);
- }
- noStroke();
- fill(255, 0, 255);
- text( circumference, centroid.x+5, centroid.y+15 );
- }
- popMatrix();
- }
- void keyPressed() {
- if ( key==' ' ) opencv.remember();
- if ( key=='q') println(hauteur1);
- if ( key=='w') println(hauteur2);
- }
- void mouseDragged() {
- threshold = int( map(mouseX, 0, width, 0, 255) );
- }
- /* incoming osc message are forwarded to the oscEvent method. */
- void oscEvent(OscMessage theOscMessage) {
- /* print the address pattern and the typetag of the received OscMessage */
- //print("### received an osc message.");
- //print(" addrpattern: "+theOscMessage.addrPattern());
- // println(" typetag: "+theOscMessage.typetag());
- }
- public void stop() {
- opencv.stop();
- super.stop();
- }
- import oscP5.*;
- import netP5.*;
- OscP5 oscP5;
- int posY1;
- int posY2;
- int multiSaut = 3 ;
- float posYLast = -1;
- int posY1Last;
- int posY2Last;
- int posY;
- boolean ascending;
- float y = height/2;
- int crit = 50;
- float speed = 0;
- float gravity = 0.0981;
- void setup(){
- size(500,500);
- smooth();
- noStroke();
- oscP5 = new OscP5(this,"127.0.0.1",6000);
- }
- void draw(){
- //posY = saveY[ctr];
- background(0);
- ascending = true;
- posY = (posY1+posY2)/2;
- println("pos1 "+posY1+" pos2 "+posY2+" posmoyenne "+posY);
- if(posYLast<=posY){
- ascending = true;
- }
- else{
- ascending = false;
- }
- if(ascending){
- if (posY<40){
- ascending = false;
- }
- else{
- fill(255);
- ellipse(height/2, height-multiSaut*posY, 50,50);
- y = height-multiSaut*posY;
- ascending = false;
- }
- }
- if(!ascending){
- fill(255);
- ellipse(width/2, y, 50,50);
- y += speed;
- speed += gravity;
- if(y>height){
- speed = 0;
- }
- }
- posYLast = posY;
- posY1Last = posY1;
- posY2Last= posY2;
- }
- //incoming osc message are forwarded to the oscEvent method.
- void oscEvent(OscMessage theOscMessage) {
- /* print the address pattern and the typetag of the received OscMessage */
- //print("### received an osc message.");
- //print(" addrpattern: "+theOscMessage.addrPattern());
- //println(" typetag: "+theOscMessage.typetag());
- posY1 = theOscMessage.get(0).intValue();
- posY2 = theOscMessage.get(1).intValue(); //HERE POSY2 DOESNT RECEIVE ANY VALUE
- }
1