Processing program using OSC messages

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);
}

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:

1 Like

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);
}
1 Like

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:

1 Like

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);
}

I used an application called “control” to send accelerometer data from phone
which i got from a processing tutorial video.
https://funprogramming.org/102-Control-Processing-tilting-your-phone.html

The problem is that, I am getting false accelerometer data.Instead of showing 0 to 9.8 it is showing big values 0 to 120.
My aim is to get the tilt angle of the phone according to the accelerometer data.Any suggestions will be a great help.

Is the data bad, or is the range just not what you expect and something that needs to be scaled?

That is, does it react more to big movements than little ones, and does it give 0 for no movement?

The range of accelerometer data is not the one that i expected. It is of the of the range 0 to 125. I need the value in range of -9.8 to + 9.8. So that I can get the tilt angle by equation;
Theta=Tan^-1(x/y)

Use map: map() / Reference / Processing.org

map(value, start1, stop1, start2, stop2)

so:

mappedVal = map(rawVal, 0, 125, -9.8, 9.8);
2 Likes

Thanks for your tips. I got the the filtered accelerometer data value. In our example, we tied an android smart phone on the lower arm in order to get the movement of arm and we are trying to interpret the movement in a 2D model. Their is a small problem in our model.In one quadrant the model is not moving according to the actual movement of arm.
Video link:

I am attaching three photos, which showing the the three different positions.

  1. At no movement

2)Vertical position

  1. Tilting backward from vertical position