Products for USB Sensing and Control
Products for USB Sensing and Control

Tilt Angles with the Spatial Phidget


by Phidgets Education

Introduction

Everything on Earth experiences a downward acceleration of 1 g (9.81 m/s²). With an accelerometer, like the one on the Spatial Phidget, you can easily measure this acceleration and use it to calculate a tilt angle.


Please complete the Spatial Phidget Tutorial before you begin


Hardware Setup

Using the Phidget

In order to connect your Spatial Phidget to your computer you will need the following:

Connect Device

Connect your Spatial Phidget to your VINT Hub using the Phidget cable and connect your VINT Hub to your computer with the USB Cable.


Gravity and the Spatial Phidget

All sensors on the Spatial Phidget are aligned along the x, y, and z axes shown on the casing. If you place the Spatial Phidget flat on your desk, the gravity vector is aligned with the z-axis as shown below:


Roll and Pitch

Overview

When calculating tilt angles, there are two measurements of interest. They are called Pitch and Roll. You can see them defined here:


Roll

Roll is the measure of how much the y-axis points above or below the horizontal. If you place your Spatial Phidget flat on your desk there is no roll. The only thing acting on your Spatial Phidget will be the gravity in the z-axis reading 1g:


If you take your phidget off your desk and roll it by θ, the y-axis will point downwards into the ground:


The accelerometer doesn’t know its surroundings, so it interprets this motion as the gravity vector changing directions:


In the orientation above, the accelerometer data may look something like this:

  • Y-axis: 0.500g
  • Z-axis : 0.866g
In order to compute θ from these values, you can use the arctangent function. From the diagram we have:

`theta = tan^-1 (M_y/M_z)`


`theta = "roll angle"`
`M_y = "Measured y-axis acceleration"`
`M_z = "Measured z-axis acceleration"`

Pitch

Pitch is the measure of how much the x-axis points above or below the horizontal. If you place your Spatial Phidget flat on your desk there is no roll. The only thing acting on your Spatial Phidget will be the gravity in the z-axis reading 1g:


If you take your phidget off your desk and roll it by θ, the x-axis will point downwards into the ground:


The accelerometer doesn’t know its surroundings, so it interprets this motion as the gravity vector changing directions:


In the orientation above, the accelerometer data may look something like this:

  • x-axis: 0.500g
  • Z-axis : 0.866g
In order to compute θ from these values, you can use the arctangent function. From the diagram we have:

`phi = tan^-1 (M_x/M_z)`


`phi = "pitch angle"`
`M_x = "Measured x-axis acceleration"`
`M_z = "Measured z-axis acceleration"`

Mini Project

Use the following flow chart to write a program that outputs the tilt angle of the Spatial Phidget. You can also reference the code below.



public static void main(String[] args) throws PhidgetException, IOException, InterruptedException{
    boolean programIsRunning = true;

    //Create object
    Accelerometer accel = new Accelerometer();

    //Address object
    accel.setHubPort(0);
    accel.setIsHubPortDevice(false);

    //Register Event
    accel.addAccelerationChangeListener(new AccelerometerAccelerationChangeListener() {
        public void onAccelerationChange(AccelerometerAccelerationChangeEvent e) {
            double xAxis = e.getAcceleration()[0];
            double yAxis = e.getAcceleration()[1];
            double zAxis = e.getAcceleration()[2];

            //TODO: Calculate tilt angles
            //      Output pitch and roll
        }
    });

    //Open
    accel.open(5000);

    while (programIsRunning) {
        //Keyboard input?
        if (System.in.available() > 0) {
            System.out.println("Ending Program");
            programIsRunning = false;
        }
        Thread.sleep(20);
    }

    //close object
    accel.close();
}



def onAccelerationChange(self, acceleration, timestamp):
    xAxis = acceleration[0]
    yAxis = acceleration[1]
    zAxis = acceleration[2]

    #TODO: Calculate tilt angles
    #      Output pitch and roll


def main():
    #Create object
    accel = Accelerometer()

    #Address object
    accel.setHubPort(0)
    accel.setIsHubPortDevice(False)
    #Register Event
    accel.setOnAccelerationChangeHandler(onAccelerationChange)
    #Open
    accel.openWaitForAttachment(5000)

    try:
        while (True):
            time.sleep(0.2)
    except KeyboardInterrupt:
        print("Ending Program")

    accel.close()

main()


    
private static void accel_AccelerationChange(object sender, Phidget22.Events.AccelerometerAccelerationChangeEventArgs e)
{
    double xAxis = e.Acceleration[0];
    double yAxis = e.Acceleration[1];
    double zAxis = e.Acceleration[2];

    //TODO: Calculate tilt angles
    //      Output pitch and roll
}

static void Main(string[] args)
{
    bool programIsRunning = true;

    //Create object
    Accelerometer accel = new Accelerometer();

    //Address object
    accel.HubPort = 0;
    accel.IsHubPortDevice = false;

    //Register event
    accel.AccelerationChange += accel_AccelerationChange;

    //Open object
    accel.Open(5000);

    while (programIsRunning)
    {
        //Keyboard input?
        if (System.Console.KeyAvailable)
        {
            System.Console.WriteLine("Ending Program");
            programIsRunning = false;
        }

        System.Threading.Thread.Sleep(20);
    }

    accel.Close();
}

(Click to Expand)

Conclusion

You can now use your Spatial Phidget to calculate tilt angles. See how this is applied in the Tilt Controlled Rover Project or the Interative Maze Project.