cureently stuck in axis rotation

edited February 2014 in Arduino

I am extremely new to processing and am currently trying to get a cube to mimic the rotation of an ADXL335 accelerometer. I have the code for Arduino sending the angle in degrees to processing and the cube responding . however the cube isn't responding the way I need it to. when rotating the accelerometer the cube rotate differently to my movement. ( when splitting up the axis the x axis mimics fine until I add the y or z axis)

here is my code for processing

import processing.serial.*;
Serial myPort;  
String SerialBuffer;
Float X = 0.0;
Float Y = 0.0;
Float Z = 0.0;
float[] degs;

void setup()
{
size(500,500,P3D);
myPort = new Serial(this,"COM15",4800); 
myPort.bufferUntil('\n');
delay(1000);
}
void serialEvent (Serial myPort)
{
  SerialBuffer = myPort.readString();
  degs = float(split(SerialBuffer, ','));
  println(" working = " + SerialBuffer + degs[1]); //print it out in the console
  X=degs[0];
  Y=degs[1];
  Z=degs[2];
}
void draw()
{
  background(255);
  stroke(0);
  fill(229,131,2);
  translate(width/2,height/2);
  rotateX(-PI*degs[0]/180);   // x axis works 
  rotateY(PI*degs[1]/180);
  rotateZ(-PI*(degs[0]+degs[1]/2)/180);
  rectMode(CENTER);
  box(100,20,100);
}

Answers

  • Highlight your code, then hit CTRL+ K to format it! ;)

  • edited February 2014

    When you apply a rotation in Processing it changes the orientation of the coordinate grid. In other words, a small rotation on the x-axis "moves" the y-axis and z-axis. If you are getting measurements of angles independently of each other then just plugging them into Processing's functions won't work.

    The solution that I know of is to create 8 3D points and draw the cube yourself instead of using box(). Now that you have control over the 8 points, you can apply rotations manually using matrices to actually change the location of the points instead of moving the coordinate system.

    If you store the 8 3D points as aligned with the coordinate system, then every frame copy the points. You would use the copied points to draw the cube and receive accelerometer rotations.

  • Here is an example that shows the problem. If I rotate only the x-axis then the cube rotates on the x-axis as expected:

    rotateX(theta);

    If I rotate the y-axis 90 degrees (HALF_PI in radians) then the x-axis rotates to where the z-axis used to be and the z-axis rotates to where the x-axis used to be. These two lines of code visually cause the exact same behavior as only rotating the x-axis:

    rotateY(HALF_PI);
    rotateZ(theta);
    

    Here is a sketch, try commenting out rotateX() then uncommenting rotateY() and rotateZ():

    float theta = 0;
    
    void setup() {
      size(400, 400, P3D);
      noFill();
    }
    
    void draw() {
      background(255);
      translate(width/2, height/2);
    
      rotateX(theta);
    
      //rotateY(HALF_PI);
      //rotateZ(theta);
    
      box(100);
      theta += TWO_PI/100;
    }
    
Sign In or Register to comment.