Hello,
My accelerometer only had x, y and z and I did not use the “yaw” axis which is perpendicular to gravity and was not useful in this case; I would have needed another accelerometer mounted at 90 deg for this axis.
I included some code that uses scrollbar to rotate a shape using intrinsic Euler angles for yaw, pitch and roll; see mapping in scrollbar code (this could have been moved).
Some videos I made for other projects below (code not included) that only used two of the axes.
Android to Processing demo:
https://www.youtube.com/watch?v=oyuA0aiQ7tM
Arduino to Processing:
https://www.youtube.com/watch?v=dMURX-00Hyg
// GLV
// 2018-06-10
// Scrollbar control of Euler angles for pitch, yaw and roll
// https://en.wikipedia.org/wiki/Euler_angles
// https://en.wikipedia.org/wiki/Aircraft_principal_axes
// Using intrinsic rotations are elemental rotations that occur about the axes of a coordinate system XYZ attached to a moving body.
// Processing co-ordiante system not changes for this example.
float x, y, z;
boolean control = true;
void setup()
{
//scrollbar
hs1 = new HScrollbar(width/4, 57*height/64, width/2, 10, 10);
hs2 = new HScrollbar(width/4, 59*height/64, width/2, 10, 10);
hs3 = new HScrollbar(width/4, 61*height/64, width/2, 10, 10);
// Scrollbar defaults
hs1.setPos(width/2);
hs2.setPos(width/2);
hs3.setPos(width/2);
size(800, 1000, P3D);
textAlign(CENTER, CENTER);
textSize(36);
// ortho();
}
void draw()
{
background(0);
lights();
// Scrollbars
fill(255);
textSize(24);
textAlign(RIGHT, BOTTOM);
text("Pitch X", width/4-50, 57*height/64 +15);
text("Yaw Y", width/4-50, 59*height/64 +15);
text("Roll Z", width/4-50, 61*height/64 +15);
hs1.update();
hs2.update();
hs3.update();
hs1.display();
hs2.display();
hs3.display();
translate(width/2, height/2);
if (control)
{
x = hs1.getPos4();
y = hs2.getPos4();
z = hs3.getPos4();
plotScroll(x, y, z);
}
}
// Axes
float length = 200;
void drawAxes1()
{
// X axis
stroke(255);
strokeWeight(3);
fill(255, 255, 0);
line(-length, 0, 0, length, 0, 0);
text("X", length+60, 0, 0);
text("-X", -length-60, 0, 0);
// Y axis
line(0, -length, 0, 0, length, 0);
text("Y", 0, length+60, 0);
text("-Y", 0, -length-60, 0);
// Z Axis
line(0, 0, -length, 0, 0, length);
text("Z", 0, 0, length+60);
text("-Z", 0, 0, -length-60);
}
// Plot from scroll bar
public void plotScroll(float x, float y, float z)
{
pushMatrix();
// Processing has a LHS of coordinates
rotateX(x); // Pitch
rotateY(y); // Yaw
rotateZ(z); // Roll
fill(100, 255, 0);
strokeWeight(1);
box(200, 10, 500);
drawAxes1();
popMatrix();
}
// From Processing examples. Modified.
HScrollbar hs1, hs2, hs3; // Create 3 scrollbars
class HScrollbar
{
int swidth, sheight; // width and height of bar
float xpos, ypos; // x and y position of bar
float spos, newspos; // x position of slider
float sposMin, sposMax; // max and min values of slider
int loose; // how loose/heavy
boolean over; // is the mouse over the slider?
boolean locked;
float ratio;
HScrollbar (float xp, float yp, int sw, int sh, int l)
{
swidth = sw+10;
sheight = sh;
int widthtoheight = sw - sh;
ratio = (float)sw / (float)widthtoheight;
xpos = xp;
ypos = yp-sheight/2;
spos = xpos + swidth/2 - sheight/2;
newspos = spos;
sposMin = xpos;
sposMax = xpos + swidth - sheight;
loose = l;
}
void update()
{
if (overEvent())
{
over = true;
}
else
{
over = false;
}
if (mousePressed && over)
{
locked = true;
}
if (!mousePressed)
{
locked = false;
}
if (locked)
{
newspos = constrain(mouseX-sheight/2, sposMin, sposMax);
}
if (abs(newspos - spos) > 1)
{
spos = spos + (newspos-spos)/loose;
}
}
float constrain(float val, float minv, float maxv)
{
return min(max(val, minv), maxv);
}
boolean overEvent()
{
if (mouseX > xpos && mouseX < xpos+swidth && mouseY > ypos && mouseY < ypos+sheight)
{
return true;
}
else
{
return false;
}
}
void display()
{
noStroke();
fill(204);
rect(xpos, ypos, swidth, sheight);
if (over || locked)
{
fill(0, 0, 0);
}
else
{
fill(102, 102, 102);
}
rect(spos, ypos, sheight, sheight);
}
float getPos()
{
// Convert spos to be values between and total width of the scrollbar
// println(spos);
return spos * ratio;
}
float getPos4()
{
// Convert spos to be values between 0 and the total width of the scrollbar
println(spos);
float spostmp = map(spos, width/4, width/2 + width/4, -TAU/4, TAU/4);
return spostmp;
}
// GV Added to setPos
void setPos(float pos)
{
// Convert spos to be values between
spos = pos;
newspos = pos;
}
}