myRIO Balancing Robot

cancel
Showing results for 
Search instead for 
Did you mean: 

myRIO Balancing Robot - Implementation of the Gyro sensor

Community Logo.png
This document was created for the myRIO Balancing Robot Project
https://decibel.ni.com/content/groups/myrio-balancing-robot
myBOT.png

myRIO Balancing Robot - Implementation of the Gyro sensor

Overview

The subject of this paper is about how to implement the gyro sensor on myRIO balancing robot. The goal of using the gyro sensor is to help the accelerometer in the myRIO to detect fine angle changes. Many mecatronics systems work with that combination of sensors in order to do the most fine control as possible.

Table of Contents

1.Introduction: What is a gyro sensor and why we use it?

     1.1 What is a gyro?

Gyro.png

--------- Picture 1 PmodGYRO -----------

A gyro is a device which reacts to angle variations. For our application, we used the L3G4200D sensor mounted on the module PmGYRO it allows knowing information’s about the 3 axis (X, Y, Z). This module use a standard 12 pins connection and can communicate by using a  I2C or SPI protocol. On the board, a pull-up resistor on the pin CS of the chip keeps the device on I2C mode unless the pin is driven low by the master device in order to communicate in SPI mode.

     1.1 What do we use it?

RIO.png

----------- Picture 3 myRIO --------------

This device will allow us to know if the robot is falling down or not. As you know, our system is at the equilibrium position when the body is at 90° to the floor. If the robot is falling, the gyro will detect the angle changes and it will send it to the myRIO so we will be able to maintain our robot at the equilibrium. In this first part, we are going to interest our self on the data communication protocol between the gyro and the master device.

2. I2C data communication

As you know, the device we use can communicate data by two ways:

  • I2C
  • SPI

At rhe begining of the prooject, we chose to work with I2C protocol because it’s easier to work with, and it use less pins than the SPI on myRIO Board.

I2C is a synchronous data communication protocol it allow to share data with multiple devices according to the master/slave principle. Actually, when you want to communicate with multiple devices, you can only have one writer. That’s why, in I2C protocol, you have one master devices and slaves devices. The master device will allow other devices to read or write data on the I2C bus. The I2C bus is composed of two wires:

  • SCL (Serial Clock Line): This clock will manage the dataflow of I2C devices and she must be the same for all devices.
  • SDA (Serial Data Line): Wire which transmits data between devices. This is a bidirectional wire.

Each I2C device has a different physical address in order to communicate easily. In an I2C communication, the master device has to speak the first in order to allow a slave device to work. The I2C frame is the following:

  • Start condition: SDA turn to 0 when SCL is 1. This is to start the communication.
  • Device Address: The Master device sends the address (7 or 10 bits) of the device you want to communicate with.
  • R/W bit: R/W bit is to select if the device you want to speak with has to send or read data from the SDA wire
  • ACK (Acknowledge): ACK bit is here to know is the selected device is working or not. If the address you sent match with a component address, the selected device will send a 0 for ACK. If the address doesn’t match, the ACK bit will be 1.
  • Delay: It's a timeout after each ack
  • Data: 1 byte
  • ACK: Here, the ACK bit is to know if the device is able to process the request. If ACK is to 1 position, the device is unable to precess the request.
  • Stop condition: SDA turn to 1 when SCL is 1. This is to start the communication.

i2c.png

------------------------------------------------------------- Picture 3 I2C protocol -------------------------------------------------------------

3. Using LabVIEW to communicate

3.1 Hardware Configuration

In order to implement the gyro sensor to our robot, we need to communicate with by using LabVIEW. In this way, we used a NI UBS-8452 device it can useI 2C communcation.

8452.png

Picture 4 Ni USB 8452 & PmodGYRO

The picture 4 show the applications we done in order to communicate with the gyro sensor. The sensor only needs four wires to be ready to communicate.

  • VCC (Power supply)
  • GND (ground)
  • SCL
  • SDA

Usually, an I2C driver need pull-up resistors on SDA and SCL lines. In this case, the NI USB-8452 has internal pull-up you can chose to activate or not.

Before beginning to code, we need to know some information about the sensor:

  • What is the physical address?
  • What do we want to do with the gyro?
  • What register can we use and how configure them according our specifications?

You can find those information’s in the L3G4200D and on PmodGYRO datasheets.

In this case, the physical address of our component is 0x69. We want to measure angle changes on the robot, so we need to acquire values really fast. So, we’ll need to use those following registers:

Register

Address

Value

Description

CTRL_REG1

0x20

0x0F

Enable X, Y, Z and Normal power down mode

CTRL_REG2

0x21

0x00

Normal mode filtering

CTRL_REG3

0x22

0x00

All disable

CTRL_REG4

0x23

0x80

Output registers not update until MSB and LSB reading

CTRL_REG5

0x24

0x00

All disable

OUT_X_L

28

-

X-axis angular data (Less Significant BYTE)

OUT_X_H

29

-

X-axis angular data (Most Significant BYTE)

OUT_Y_L

2A

-

Y-axis angular data (Less Significant BYTE)

OUT_Y_H

2B

-

Y-axis angular data (Most Significant BYTE)

OUT_Z_L

2C

-

Z-axis angular data (Less Significant BYTE)

OUT_Z_H

2D

-

Z-axis angular data (Most Significant BYTE)

3.2 Software Implemenation

Now we have all hardware information we can start coding with LabVIEW. With the USB-8452, we can use the I2C functions palette. On the following picture, you can found our application bloc diagram.

Diag gyro.PNG

--------------------------------------------------------------------------------------------------- Picture 5 Gyro.vi block diagram ---------------------------------------------------------------------------------------------------

This diagram can be read from the left to the right according the error cluster which one control the data flow. At first, we find this function:

HWSTP.png

------------- Picture 6 Hardware Setup.vi -------------

This subVI allows configuring the hardware. We set the NI device we want to work with, the power supply level for our I2C device, his address, activate the internal pull-up, the size of the address, and the SCL rate. But how is this VI working?

The following picture represents the internal view of hardware setup.vi subVI:

hwstp2.png

-------------------------------------------- Picture 7 Hardware Setup SubVI ------------------------------------------

On this VI, we can found two property nodes and an I2C function. The first property node is to setup how the NI USB-8452 will drive the gyro. Then, you have Create New I2C.vi which one allows opening an I2C communication. To finish, the last property node is to configure the communication by setting up the address, the address size, and the SCL clock rate.

The following graph shows the first I2C frame:

Capture.PNG

-------------------------------------------------------------------------------------- Picture 8 I2C frame --------------------------------------------------------------------------------------------

Let’s go back on the main VI (GYRO.vi) diagram in order to see the next function:

Regedit.png

----------------- Picture 9 INIT_REG.vi--------------------

This subVI allows configuring registers. Here we set all registers at the Table 1 values. This VI is composed with five I2C write functions. Each of this function has an array in input. Those array values are written one by one as you can see on Picture 8.

The next step is the main loop:

ml.png

---------------------------------------------- Picture 10 Main Loop---------------------------------------------------------

At the entrance of the loop, we have some array constants they are the output register addresses (Table 1). You can find the “#bytes to receive” constant witch one allow to configure the number of byte to read for one register. In this case, the registers send only one byte. In the loop, we are reading from the sensor the six angle registers and we display them in a graph. The loop iteration rate is 100ms. On the following picture, you can see the front panel on this VI with the graph containing gyro data.

fp.png

------------------------------------------- Picture 11 GYRO.vi front panel ----------------------------------------------

To finish, we close the I2C communication and we handle errors with those functions

close.png

Picture 12 I2C close

4. Conclusion

This experimentation shows that implementing the gyro sensor with LabVIEW is quite easy. Now we need to adapt this code to the myRIO device and process our read data according the physical model.

5. Sources

Picture 1: https://www.digilentinc.com/Products/Detail.cfm?NavPath=2,401,937&Prod=PMOD-GYRO

Picture 2: http://www.eeweb.com/blog/jim_harrison/national-instruments-myrio-euducational-tool

Picture 3: https://learn.sparkfun.com/tutorials/i2c/all

6. Further Information

http://www.ni.com/white-paper/3479/en/

0 Kudos
Message 1 of 1
(14,392 Views)