MindSqualls
Would you like to react to this message? Create an account in a few clicks or log in to continue.

Example of how to use the NXT Acceleration / Tilt Sensor

2 posters

Go down

Example of how to use the NXT Acceleration / Tilt Sensor Empty Example of how to use the NXT Acceleration / Tilt Sensor

Post  luchoaqp Sun Aug 14, 2011 5:19 am

Hi all,

We need a example of how to use the NXT Acceleration / Tilt Sensor exactly to measure the acceleration (like when you drive).

Could you help us?



Thank you very much.

luchoaqp

Posts : 2
Join date : 2011-08-13

Back to top Go down

Example of how to use the NXT Acceleration / Tilt Sensor Empty Re: Example of how to use the NXT Acceleration / Tilt Sensor

Post  Niels Sun Aug 14, 2011 7:51 pm

The NXT Acceleration / Tilt Sensor from HiTechnic can be used for measuring both the “acceleration” and the “tilt”. So what is the difference? More on that in a moment.


/// Caution, a bit of high school physics ahead… ///

Acceleration is a “vector” – meaning that it has both a direction and a size.

When you are sitting in a car, which is speeding up, the direction of the acceleration is in the forward direction of the car. When the car is breaking, the acceleration is the opposite direction (this is also called “deceleration”).

But that is not all… Even if the car is just driving around in a circle, with constant speed, there is acceleration. In fact, whenever the car is changing its speed and/or the direction it is driving there is acceleration.

For a more thorough explanation I will forward you to Wikipedia:
http://en.wikipedia.org/wiki/Acceleration

Because acceleration is a direction, the sensor will need to return 3 numbers to you before you have the acceleration. The illustration below shows how the sensor represents the numbers.

Example of how to use the NXT Acceleration / Tilt Sensor HTAcc

Example 1 – acceleration in the X-direction:

If you are in a car, and you are only interested in the part of the acceleration that are in the driving-direction, you can make do with reading the X-value – assuming that you place the sensor in the propper way (upright, and with the black part pointing forward).

Code:
using System;
using NKH.MindSqualls;
using NKH.MindSqualls.HiTechnic;

namespace Example1
{
    class Program
    {
        static void Main(string[] args)
        {
            NxtBrick brick = new NxtBrick(NxtCommLinkType.Bluetooth, 3);

            HiTechnicAccelerationSensor sensor = new HiTechnicAccelerationSensor();
            sensor.OnPolled += new Polled(sensor_OnPolled);
            sensor.PollInterval = 500;

            brick.Sensor1 = sensor;

            brick.Connect();

            Console.WriteLine("Press any key to finish...");
            Console.ReadKey();

            brick.Disconnect();
        }

        static void sensor_OnPolled(NxtPollable polledItem)
        {
            HiTechnicAccelerationSensor sensor = polledItem as HiTechnicAccelerationSensor;

            Console.WriteLine("X-axis acceleration : {0} m/s^2", sensor.XAxisAcceleration());
        }
    }
}

The code of the HiTechnicAccelerationSensor class is made to return the acceleration in IS-units i.e. as m/s^2 in this case.

The value is positive when the car accelerating, and negative it if is breaking (decelerating).

Example 2 – full acceleration:

If you want to know the full acceleration, you need to read the part of the acceleration in all tree directions and use Pythagoras formula to calculate the size:

Code:
namespace Example2
{
    class Program
    {
        static void Main(string[] args)
        {
            <snip>
        }

        static void sensor_OnPolled(NxtPollable polledItem)
        {
            HiTechnicAccelerationSensor sensor = polledItem as HiTechnicAccelerationSensor;

            double x = sensor.XAxisAcceleration().Value;
            double y = sensor.YAxisAcceleration().Value;
            double z = sensor.ZAxisAcceleration().Value;

            double acc = Math.Sqrt(x * x + y * y + z * z);

            Console.WriteLine("Acceleration : {0} m/s^2", acc);
        }
    }
}

A problem with this is that even if the sensor does measure all tree values at the same time, it is not possible for us to read all the tree values from the sensor at the same time. That means that the values of X, Y and Z will be actually measured a few milliseconds apart (approximately 70 ms between each value).

Example 3 – adjusting for the gravity:

There is another problem. Even if the sensor is just sitting perfectly still on a tabletop, this will return a value of circa 10 m/s^2. If the sensor was a high-precision instrument the value would be closer to 9.82 m/s^2. The explanation is that the sensor is measuring the gravity.

Gravitation is indistinguishing from acceleration…

(In fact that is the essence of one of Einstein’s two theories of relativity, the “theory of general relativity”, but that is a bit more advanced than high school physics Wink)

It turns out that it is not possible to tell the difference between acceleration and gravitation. This means that we will always have the Earth’s gravity mixed up with the acceleration we want to measure.

There are basically two ways to handling this problem. They both require that you hold the sensor horizontal all the time. In this way, we can be sure that the gravity is only pointing along the Z-direction (see the illustration above). Unfortunately this means that you cannot use your NXT for more advanced things like taking it on a swing or a seesaw or something like that, where the sensor will not be held horizontal.

Method 1) In example 1 we were only interested in the acceleration in the X-direction. Therefore the gravitation was not mixed in.

Method 2) We only need to care about the gravitation when we want to measure acceleration in the Z-direction. This would be the case if we wanted to measure how quickly an elevator accelerated. Since we already know how big the part of the Earth’s gravity is, we can adjust for this:

Code:
namespace Example3
{
    class Program
    {
        static void Main(string[] args)
        {
            <snip>
        }

        static void sensor_OnPolled(NxtPollable polledItem)
        {
            HiTechnicAccelerationSensor sensor = polledItem as HiTechnicAccelerationSensor;

            double x = sensor.XAxisAcceleration().Value;
            double y = sensor.YAxisAcceleration().Value;
            double z = sensor.ZAxisAcceleration().Value;

            // Adjust for Earth's gravity:
            z = z + 9.82;

            double size = Math.Sqrt(x * x + y * y + z * z);

            Console.WriteLine("Size : {0} m/s^2", size);
        }
    }
}

(I will leave it as a small exercise to explain why the gravity must be added instead of subtracted)

Note that the value “9.82” may vary depending on where you are on Earth. However the sensor is not really that precise that it matters.

Example 4 - tilt:

That takes care of the acceleration, but the sensor is also a “tilt” sensor.

You can only use the senor to measure tilt if it is not also accelerated. The trick is to calculate the direction of the gravitation relative to the sensor which is why we cannot have an additional acceleration “polluting” the measurements.

Let us say that you have fixed the sensor so that it can only rotate in the plane of the X- and Z-axis; it can only turn about the pitch-axis:
http://en.wikipedia.org/wiki/Aircraft_principal_axes

You can then calculate the angle by comparing the amount of gravitation in the X-direction with that in the Z-direction:

Code:
namespace Example4
{
    class Program
    {
        static void Main(string[] args)
        {
            <snip>
        }

        static void sensor_OnPolled(NxtPollable polledItem)
        {
            HiTechnicAccelerationSensor sensor = polledItem as HiTechnicAccelerationSensor;

            double x = -sensor.XAxisAcceleration().Value;
            double z = -sensor.ZAxisAcceleration().Value;

            double temp = z / x;
            double angle = Math.Atan(temp);
            angle = 90 - (angle / Math.PI * 180);

            if (x < 0) angle += 180;

            Console.WriteLine("Tilt = {0:f1} degrees; (x, z) = ({1:f1} , {2:f1})", angle, x, z);
        }
    }
}

I will not explain the calculation but it requires that you are not too afraid of trigonometry.

Hope all this helps?

Leg Godt,
Niels

Niels

Posts : 19
Join date : 2011-07-15

Back to top Go down

Back to top

- Similar topics

 
Permissions in this forum:
You cannot reply to topics in this forum