Example Code

Measuring Angular Position with myDAQ and Sparkfun IDG500 Dual Axis Gyroscope Breakout Board

Code and Documents

Attachment

Overview


Detect angular rotation with Sparkfun's IDG500 Dual Axis Gyroscope breakout board and National Instruments myDAQ in LabVIEW. Acquire data using the DAQ Assistant, which is provided in LabVIEW with the NI-DAQmx device drivers, and converted into angular position using basic programming in LabVIEW.

Objective:

Use the Sparkfun IDG500 breakout board to find angular position with the myDAQ analog input channels and LabVIEW.

 

Background:

Gyroscopes, in general, are devices that use conservation of angular momentum to measure or maintain orientation. The gyroscope used in this example is the InvenSense IDG500 dual axis gyroscope. The IDG500 is a micro-electro-mechanical-system (MEMS), which makes it smaller and cheaper. The small size allows the sensor to be used in gaming systems, virtual reality, and consumer electronics. Normally, soldering a piece of silicon this small to a printed circuit board would be difficult to do by hand. However, Sparkfun has made the process easier by pre-soldering the sensor and breaking the pins out for easier use and handling.

Figure 1: Sparkfun IDG500 Breakout Board (SEN-09094)

 

What you need:

  1. NI myDAQ
  2. LabVIEW
  3. Sparkfun IDG500 Breakout Board (SEN-09094)
  4. Wire (solid core works best)
  5. .1" Header Pins (optional)
  6. Breadboard (optional)
  7. Soldering Iron

 

Wiring Instructions:

The Sparkfun board uses standard 0.1” spaced holes for header pins to be used, and by using the header pins the breakout board can be used with a standard breadboard for easier prototyping. If you want you can solder the wires straight to each of the terminals rather than use the header pins.

The Sparkfun IDG500 breakout board uses 9 pins listed in Table 1 below:

 

Pin Name Description
Vin Input voltage (3-7V)
Xout X axis gyroscope output
X4.5Out Xout with a gain of 4.5
Yout Y axis gyroscope output
Y4.5Out Yout with a gain of 4.5
VRef Voltage Reference
PTATS Temperature Sensor
AZ Auto Zero function signal pin
GND Ground pin

Table 1: Pins on the SEN-09094 Sparkfun IDG500 Breakout Board

Note: Depending on what the application is, the pins used will vary. For this document, we will be using the pins Vin, Xout, Yout, AZ, and GND.

The MEMs gyroscope, like most sensors, requires a voltage to be supplied. The IDG500 Breakout Board takes in 3-7V source, so we can power the chip using the 5V power on the myDAQ. The breakout board has 4 sensor outputs: Xout, Yout, X4.5Out, and Y4.5Out. The choice of which output to use depends on your application. The sensitivity and range of these pins are shown below in Table 2.

Table 2: Output Pins and Sensitivities

The X4.5out and Y4.5out provide an additional 4.5 gain to the Xout and Yout, allowing more precise measurements. However, because of the additional 4.5 gain, the range is sacrificed from ~+/-500 deg/s to ~+/-110 deg/s. In this example we will be using the full range outputs, Xout and Yout.

The sensor provides an auto zero to accommodate for temperature variations. The pin should be connected to a digital line port 0 line 0.

 

Connection Overview:

  1. Connect Vin to the 5V line  and GND to DGND on the myDAQ.
  2. Connect Xout to AI0+, Yout to AI1+, and jump a wire from DGND on the myDAQ to AI0- and AI1-.
  3. Connect DIO line 0 on the myDAQ to the AZ pin on the breakout board.

The final connection can be seen below:

Figure 2: Final Connection

 

LabVIEW User Interface:

The user interface (see Figure 3) shows both axes on a waveform chart showing velocity (degrees per second) versus time. By using a chart you can save previous values and see the change over time. We also put two gauges to show the angular position of the sensor, and a button for toggling auto zero.

Figure 3: Front Panel of VI

 

Coding Strategy:

There are 3 parts of this example:

  1. Acquire voltage and convert the data to degrees per second.
  2. Enable Auto-Zero pulse and software calibrate.
  3. Integrate degrees per second to achieve angular position.

To acquire samples at a consistent rate, use continuous measurements, which capture data based on the hardware clock on-board rather than the operating system. Once the data is received we can convert the data using the formula found in the IDG500 datasheet (See Formula 1).

Formula 1: Conversion Equation

The Gyro Zero-Rate Out is nominally around VRef or 1.35V. The sensitivity for Xout and Yout is 2mV/degree/second.

The sensor outputs the velocity with respect to time, which we can integrate to find the displacement of the sensor. The approximation of the area under the curve produced by the sensor is: 

or simplified down to:

Or an approximation: 

 

Theoretically, doing the two steps above would be enough to get the positional data. The problem is that noise and other external phenomena occur, and need to be handled to minimize error. For instance, the gyroscope is especially sensitive to temperature variations in a room. In fact, you can put your hand on top of the gyroscope and the temperature from your hand will cause the XOut and YOut values to begin drifting. In order to fix this, we need to send a digital pulse to the Auto Zero pin. The pulse will activate the sensor’s function and bring the X and Y close to 1.35V or 0 degrees per second. However, even after auto zero, there will be an offset that will throw off the position measurement. In order to compensate we have to do software calibration and subtract the offset from the future data sets.

One way to implement this program is to use a state machine format. We will use the following states: Position Output, Pulse, Wait, and Calibrate. The “Position Output” stage will update the gauges on the front panel to reflect the current position of the gyroscope. “Pulse” will send a digital pulse to the auto zero pin of the gyroscope. The “Wait” state will allow one cycle of data to be obtained, and “Calibrate” will set the offset.

Normally, the “Pulse” state to be the initial state, which runs the auto zero function of the gyroscope. However, for the purposes of this example, we will begin with the “Position Output” state just to show how the gyroscope’s outputs look before and after doing an auto zero. The state machine is shown below in figure 4.

Note:  AZ is the Auto Zero Button on the front panel.  !AZ means Auto Zero not pressed.

Figure 4: Program State Diagram

The block diagram of each state is shown below in figures 5, 6, 7, and 8.

Figure 5: LabVIEW 2010 Position Output State

 

Figure 6: LabVIEW 2010 Pulse State

 

Figure 7: LabVIEW 2010 Wait State

 

Figure 8: LabVIEW 2010 Calibrate State

Note: The attached LabVIEW code snippet can be dragged-and-dropped to a LabVIEW block diagram, use attached PNG file.  After locating the PNG file, just drag the file icon onto a blank block diagram, as if you were dragging the file onto your desktop. You will also need the States.ctl file included below.

 

Setting Up Analog Input DAQ Assistant

  1. Select the DAQ Assistant
  2. Select Acquire Signals -> Analog Input  -> Voltage
  3. Choose the myDAQ under supported channels and select AI0 and AI1 by holding down Ctrl or Shift.
  4. Click next
  5. Set the max voltage to 3 and the min voltage to 0 on both channels.
  6. Set samples to read to 100 and rate to 1000.
  7. Rename the channels to X and Y by right clicking on the channel and clicking “Rename” (Optional)

Figure 9: Analog Input Settings

 

Setting up Digital Output DAQ Assistant

The DAQ assistants for the digital pulse have all the same settings.

  1. Select the DAQ Assistant
  2. Select Generate Signals -> Digital Output -> Line Output
  3. Choose the myDAQ under supported channels and select Port 0 Line 0.

Figure 10: Digital Output Settings

 

How It Works:

The analog task (the first DAQ Assistant) is continuous and will create the while loop (see configuration of analog task in “Setting the Analog Task with the DAQ Assistant”). This means that on each iteration of the loop we receive a set of 100 data points from the analog input for both X and Y. The data is then converted on each iteration by splitting the data into the X and Y components (the split signals function splits the signals in the order that they are listed in the analog input DAQ Assistant). Once split, we implement the formula found in the data sheet by using the mathematical functions. After the data is converted to degrees per second, we find the average angular velocity using the array sum and dividing the sum by the number of samples. Then we find the angular displacement by multiplying average angular velocity by the total period of the sample set. For our example it is 100 Samples / 1000 Samples per Second = 1/10 second or 0.1 seconds.

In the “Pulse” stage, there are three DAQ Assistants (see configuration in “Setting the Digital Task with the DAQ Assistant”). A sequence structure makes sure that a pulse is created with the appropriate order of events. In the first frame of the sequence we output a “false” to the digital line to drive the pin (port 0 line 0) low, and the second frame we output a “true” to drive the pin high and wait 1 ms. Finally we output another “false” to drive the pin low.

Note: You have to output a false to the digital line after outputting a true. The sensor has a known issue where having the auto zero pin high will cause a 2.5 times attenuation on Xout and Yout.

From the “Pulse” stage we go to the “Wait” stage to wait until we have the next set of data before finding the offset in the “Calibration” state. The “Wait” state is necessary because the pulse to the auto zero pin causes a spike on the X and Y output, this spike will cause the software calibration to be incorrect. After the “Wait” state we go to the “Calibration” state where we store new calibration constants and at the same time set the angular position to 0.

In the “Position Output” state, we take the angular displacement and subtract it by the calibration constants. Then add the adjusted displacement to previous angular position.

Note: As with all inertial sensors (accelerometers, gyroscopes, etc.), error will begin to compound with time. This can be minimized by increasing the sample rate and by adding another sensor such as an accelerometer to do a check on the position of the sensor.

When running the VI, rotate the sensor around an X or Y axis and see the angular position on the gauge change. For example when the sensor is upside down on the X axis it will either read 180 or -180 degrees depending on which direction the sensor was rotated.

 

Tips and Tricks

  • You can modify the VI to log the data to file using a ‘Write To Spreadsheet File.vi’ express VI if you wish to save the data.  Be sure to place it in the loop and be sure to append new data to the spreadsheet file.
  • It is possible to account for temperature drift by using the PTATS pin of the sensor, use the DMM to find out what the temperature is and perform  a calculation.
  • Use ‘From DDT’ to turn the blue Dynamic Data Type wire into an orange double precision number.  This allows the data from the DAQ Assistant to be used with standard LabVIEW VIs.
  • You can right click on each control and indicator and make the icons smaller by selecting “Show as Icon”

 

Related Links

» SparkFun IDG500 Breakout Board

» InvenSense IDG500 Datasheet

» NI myDAQ Getting Started and Support

 

Example code from the Example Code Exchange in the NI Community is licensed with the MIT license.

Comments
1C2T3B--Gryo
Member
Member
on

Can't read the LabVIEW demo displays well enough to understand what's going on. 

1C2T3B--Gryo
Member
Member
on

I don't know if I already commented this, and just can't see it, or what.  But, I really can't read the labVIEW diagrams. I can not hope to use any part of this tutorial without them. 

Contributors