Processing program using OSC messages


#1

Hello friends,
I am beginner in this programming platform.Can anybody help me with a code to convert the below described program with OSC message using accelerometer data from an android phone.It is a simple program which displays two arms and they move according to mouse movement.I am trying to modify this program using OSC messages.Thanks in advance.

float x, y;
float angle1 = 0.0;
float angle2 = 0.0;
float segLength = 100;

void setup() {
  size(640, 360);
  strokeWeight(30);
  stroke(255, 160);
  
  x = width * 0.3;
  y = height * 0.5;
}

void draw() {
  background(0);
  
  angle1 = (mouseX/float(width) - 0.5) * -PI;
  angle2 = (mouseY/float(height) - 0.5) * PI;
  
  pushMatrix();
  segment(x, y, angle1); 
  segment(segLength, 0, angle2);
  popMatrix();
}

void segment(float x, float y, float a) {
  translate(x, y);
  rotate(a);
  line(0, 0, segLength, 0);
}

#2

Hi there :slight_smile: Your question is rather vague and the answer is probably “yes” (someone can help you). You didn’t mention what you tried and what failed to work. Here some great ideas on how to get help faster: Guidelines—Tips on Asking Questions :slight_smile:


#3

Thanks for your advice.I made the following changes to the program.I used accelerometer data.The arm is shaking according to accelerometer data but its not showing a fix position as the accelerometer data is also fluctuation.what changes should i make to stabilize the arm?

float p, q;
float angle1 = 0.0;
float angle2 = 0.0;
float segLength = 100;
import oscP5.*;
import netP5.*;
OscP5 osc;

void setup() {
  
  osc = new OscP5(this, 10000);
  osc.plug(this, "shake", "/accelerometer");
  
  
  size(640, 360);
  strokeWeight(30);
  stroke(255, 160);
  
  p = width * 0.3;
  q = height * 0.5;
}

void draw() {
}
void shake(float x, float y, float z){
  background(0);

  println(x+"," + y+","+ z);
  angle1 = 0;
  angle2 = (x);
  
  pushMatrix();
  segment(p, q, angle1); 
  segment(segLength, 0, angle2);
  popMatrix();
}

void segment(float p, float q, float a) {
  translate(p, q);
  rotate(a);
  line(0, 0, segLength, 0);
}

#4

Great question :slight_smile:

A very simple approach to smooth out a noisy y variable would be to replace something like this:

// make x equal y NOW
x = y; // x is as noisy as y

with this

// move x 9% towards y
x = lerp(x, y, 0.09); // x is less noisy than y

so even if y is jumping up and down, x will slowly drift towards y. One issue of this approach is that it adds some latency, so x follows y after some frames or seconds depending on how small the third parameter is. Latency and smoothing increases as that third parameter decreases.

If you need a better smoothing formula you could use this library instead:

Unrelated tips:

  • Please don’t delete things you have written before in the forum because it makes the thread harder to read. Think that when you ask a question is not only for you, but for everyone to learn from, so it’s useful to see how you improved your question above :slight_smile:
  • You can format your code in the forum nicely with syntax highlighting by editing your post, selecting the code, and pressing the </> button in the post editor.

Welcome to the new forum! :slight_smile:


#5

I added the codes to the program. Its working fine. Even though it is not 100% stable.My idea is to rotate the arm according to the angle created by the phone during tilting.can i do it with the accelerometer data?I tried by using rotateX(x) command.But i think it is not the right idea.

float p, q;
float angle1 = 0.0;
float angle2 = 0.0;
float segLength = 100;
import oscP5.*;
import netP5.*;
OscP5 osc;

void setup() {
  
  osc = new OscP5(this, 10000);
  osc.plug(this, "shake", "/accelerometer");
  
  
  size(640, 360);
  strokeWeight(30);
  stroke(255, 160);
  
  p = width * 0.3;
  q = height * 0.5;
}

void draw() {
}

void shake(float x, float y, float z){
  background(x*2.5,y*2.5,z*2.5);

  println(x+"," + y+","+ z);
  x = y;
  x = lerp(x, y, 0.09);
  angle1 = 0;
  angle2 = (x);
  
  pushMatrix();
  segment(p, q, angle1); 
  segment(segLength, 0, angle2);
  popMatrix();
}

void segment(float p, float q, float a) {
  translate(p, q);
  rotate(a);
  line(0, 0, segLength, 0);
}