Pitch, Roll, Yaw using rotateX/Y/Z query

edited March 2018 in Arduino

I am trying to represent the orientation of my phone being held upright. I'm using a simple rectangle for starters. I am sending messages over OSC. Each of my 3 values; pitch, roll and yaw (Azimuth) goes from -180degrees to +180degrees.

I am going by the example in the picture below my code, where X is pitch and Y is roll:

When I use just two values, (eg just pitch and roll) the orientation seems to be represented normally. But when I throw the third into the mix, it gets a little wild.

I don't have another device right now to check if it's the phone sensors that are faulty. Looking at the rotateX(and Y and Z) examples, this should work as is. I guess there's something a bit deeper I'm missing?


import oscP5.*;
import netP5.*;
OscP5 oscP5;
NetAddress myRemoteLocation;
float ZyawValue, XpitchValue, YrollValue;

void setup() {
  size(360, 360, P3D);
  colorMode(RGB, 256); 

  oscP5 = new OscP5(this,34567);
  myRemoteLocation = new NetAddress("",34567);

void draw() {

  translate(width/2, height/2, 0);

    rotateZ(radians(ZyawValue));     // **Issues**

  rect(0, 0, 50, 100);

  String gyrStr1 = "X=Roll: " + (int) YrollValue; 
  String gyrStr2 = "Y=Pitch: " + (int) XpitchValue; 
  String gyrStr3 = "Z=Yaw: " + (int) ZyawValue; 
  fill(249, 250, 50);
  text(gyrStr1, (int) (width/6.0) - 40, 50); 
  text(gyrStr2, (int) (width/6.0) - 40, 100); 
  text(gyrStr3, (int) (width/6.0) - 40, 150);   

void oscEvent(OscMessage theOscMessage) {

  if(theOscMessage.checkAddrPattern("/orientation/azimuth")==true) { 
    if(theOscMessage.checkTypetag("f")) {
      ZyawValue = theOscMessage.get(0).floatValue();  

  if(theOscMessage.checkAddrPattern("/orientation/pitch")==true) { 
      XpitchValue = theOscMessage.get(0).floatValue();  

  if(theOscMessage.checkAddrPattern("/orientation/roll")==true) { 
    if(theOscMessage.checkTypetag("f")) {
      YrollValue = theOscMessage.get(0).floatValue();
  println("Azimuth: "+ZyawValue+", Pitch: "+XpitchValue+", Roll: "+YrollValue);

Screen Shot 2018-03-30 at 21.45.50


  • edited March 2018

    apply the rotations in the order Yaw, Pitch, Roll in lines 28 to 30 and see if that helps. if not, try it in the reverse order.

    we can't test this because we don't have your OSC setup.

  • Thanks, koogs. I've tried every which combo. No joy. I'll try to get another phone for ref in the near future, or maybe hook up a gyro to arduino instead.

  • When I use just two values, (eg just pitch and roll) the orientation seems to be represented normally. But when I throw the third into the mix, it gets a little wild.

    You will need to check the reference to see what are the range of each angle. Then in your application, you need to define your own range if it is not the same as provided by Android. You will have the same challenge using a generic gyro connected to arduino.

    If you work only with yaw, does it behave as you expect? What are you trying to do by the way? For instance, are you mounting your phone on an UAV?


  • I actually totally forgot that I have a generic gyro in a cheap electronics kit. I'm going to test that out now and if that works I'll use that setup to diagnose the phone.

    Yes, yaw was behaving as it should have, in isolation.

    For the moment I only wish to get a 3d shape that looks like my phone to move exactly how my phone does. After that I'll see ;)

  • Answer ✓

    After spending a few hours debugging it, it seems that I have got a dud gyro. From what I have learned, they tend to arrive already broken. Especially cheaper ones, like mine :)

Sign In or Register to comment.