FIRST Robotics Competition Documents

cancel
Showing results for 
Search instead for 
Did you mean: 

FRC Sensor Basics Tutorial

Contents

  • Introduction
  • Configuring the Hardware
  • Exploring the Sensor API
  • Developing an Example
  • Customizing a LabVIEW Application
  • Using Other Types of Sensors
  • Conclusion

 

Introduction

 

In this tutorial, we will explore how to acquire data from sensors using an roboRIO and LabVIEW 2017 (installed from the FIRST Robotics Software 2018 installation pack). We will give detailed instructions on coding using an accelerometer followed by a brief overview of other sensor types including a gyro, an ultrasonic sensor, a counter, and an encoder.

 

Note: This tutorial was created using beta software and hardware. The final product may differ slightly from the products shown. The bare boards used in this tutorial will have enclosures in the final version.

Configuring the Hardware

roborio.jpg

Sensors are electrical devices that return a voltage corresponding to a physical property. In the case of an accelerometer, the voltage it returns is proportional to the acceleration the sensor is experiencing. The physical connections for the gyro and ultrasonic sensors are the same as the accelerometer. You will be using the analog pins on your roboRIO.

We will be using an analog accelerometer similar to the one in the 2018 FRC kit of parts.

 

sensort.jpg

 

In general sensors require power (often called excitation) and output a voltage that is proportional to a physical phenomenon. This sensor is a three axis accelerometer meaning that it can measure acceleration in three directions—this is achieved by putting three separate accelerometers on the same sensor with different orientations. While most sensors would have three wires: power, ground, and signal out, this sensor has five wires since it is really three accelerometers in one. All accelerometers share the power and ground wires and each has their own signal out wires. In order to measure the acceleration in all directions we would need to use three analog input channels.

 

Within each set of three exposed pins for each analog input, the pin farthest from the edge is used for the analog input signal, the center pin is used for the +5V power supply (for excitation), and the pin closest to the board is the ground pin. Connect to accelerometer to the roboRIO according to this pin configuration. This is the last connection required to take measurements from the sensor so now we will examine the software side of things.

 

Exploring the Sensor API

 

In this tutorial, we will assume that you are already familiar with opening a new project and VI in LabVIEW FRC Edition 2018. If you would like details on how to open a new project, please refer to LabVIEW Tutorial 4: Developing a Robot Project or other LabVIEW tutorials.

 

Once you have a new VI open, access the Functions palette by right-clicking on the white space in the Block Diagram. Here you will find the WPI Robotics Library palette where you can navigate to the Sensors palette.

 

Sensor pal.png

 

Click on the Sensors icon in order to see the different sensor palettes available to you. Each palette contains VIs that allow you to gather data from different types of sensors.

 

sensor.png

 

For example, the VIs in the Accelerometer palette allow you to measure an acceleration value.To display the Accelerometer VIs, select the Accelerometer palette.

 

acc.png

 

The Accelerometer VIs use the same Open»Get»Close paradigm that many of the other Robotics APIs use.

 

Developing an Example

 

To begin writing your code, drag and drop the Open, GetAcceleration, and Close VIs onto your Block Diagram. The Open VI opens communication between the roboRIO and the accelerometer. You can select either the internal sensor, or from various external sensors. The GetAcceleration VI returns the voltage data from the accelerometer. The Close VI closes the communication between the roboRIO and the accelerometer.

 

sen block.png

 

Next we can select the type of accelerometer we are using with the polymorphic Open Vi. For this example, we will be using an analog input sensor, so select Analog Open from the Polymorphic dropdown. Then right click the analog channels terminal and create a control. This control allows the user to specific channel that the sensor is connected to. Next, add a While loop (Programming>> Structures) to allow the values to update continuously, and right click and create an indicator on the Get VI to output the acceleration values continuously. Right-click on the condition terminal in the bottom-right of the While Loop and select Create»Control to place a stop button on the Front Panel. Finally, add a Wait VI to the While Loop so that the code executes every 10 milliseconds. The Wait function is located in the Functions»Programming»Timing palette. Right-click on the Milliseconds to Wait input terminal and select Create»Constant. Set this constant to 10. This step keeps the while loop from continuously and wasting processor resources.

 

sen fron.png

 

Switch to the Front Panel and set the control Analog Channel accordingly. You can switch to the Front Panel by going to the Window menu and selecting Show Front Panel (Or ctrl+E). You can also move and resize the items on the Front Panel as you see fit.

Now you are ready to collect data from your accelerometer. Run the program to see the data from the accelerometer displayed on your Front Panel.

 

axies.png

 

Customizing a LabVIEW Application

LabVIEW allows you to customize a program for your own personal needs. For instance, you can graph your data on the Front Panel and set different parameters for your application. For this example, we will graph the sensor data on the Front Panel as well as turn an LED on or off depending on upper and lower limits that we determine.

 

Start by switching to the Front Panel if it is not already showing. You should already see your Stop button, Analog Channel control, and Accelerometer indicator. We will now add two numeric controls for the upper and lower limits, a round LED, and a chart to the Front Panel. Right-click on the Front Panel to bring up the Controls palette. Navigate to the Controls»Modern»Graph palette and place a Waveform Chart on the Front Panel. Next, bring up the Controls palette again and navigate to the Controls»Modern»Numeric and place a Numeric Control on the Front Panel. Repeat this step to add a second Numeric Control. Change the names of the controls to Upper Limit Bound and Lower Limit Bound. Finally, navigate to the Controls»Modern»Boolean palette and place a Round LED on the Front Panel. Change the name of the Boolean to Alarm. Feel free to move and resize and rename the controls on the Front Panel as you wish. Your Front Panel should resemble the figure below.

 

with graph.png

 

Switch back to the Block Diagram (Window»Show Block Diagram) where you should see the different controls you added to your Front Panel. Resize the While Loop to make room for additional code and move all the new controls inside of the While Loop. For this example, we will only be charting and monitoring the X-Axis acceleration.

 

First, add an unbundle by name (Programming>> Cluster, Class, & Variant) and connect it to the accelerations output of the Get VI, then select the X-Axis by right clicking and selecting element (if it is not already selected).

 

We want our new Chart to display not only the accelerometer data, but also the upper and lower bounds that we set on the Front Panel. To do this, use a Bundle function to bundle each of these values into a cluster to be passed into the Waveform Chart. Right-click on the Block Diagram to bring up the Functions palette and then navigate to the Functions»Programming»Cluster, Class, & Variant palette and place a Bundle function on the Block Diagram to the right of your Upper and Lower Bound controls. Hover your mouse over the bottom of the Bundle function and expand the Bundle to three inputs. Next wire the Acceleration output of the GetAcceleration VI to the first input, the Upper Limit Bound to the

second input, and the Lower Limit Bound to the third slot input. Finally, wire the output of the Bundle to the Waveform Chart.

 

with graph block.png.

 

The last thing we want to do, is set our alarm to true if the value of the data from our sensor is below the lower bound or above the upper bound. We can use comparison VIs to determine if either of these events happen. Right-click on the Block Diagram and navigate to the Functions»Programming»Comparison palette and place an In Range and Coerce VI on the Block Diagram.

 

Wire the Acceleration output from the GetAcceleration VI to the x input of the In Range and Coerce VI. Next, wire the Upper Limit Bound to the upper limit input and the Lower Limit Bound to the lower limit input of the In Range and Coerce VI. This will send a True output if Acceleration value is within this range. We want to turn the Alarm LED on when the Acceleration value is outside of the specified range. To accomplish this, we need to use a Not function to compute the logical negation of the input. Right- click on the Block Diagram and navigate to the Functions»Programming»Boolean palette and place a Not function to the right of the In Range and Coerce VI. Wire the In Range? output of the In Range and Coerce VI to the input of the Not function, and then the output of the Not function to the input of the Alarm LED.

 

modified.png

 

We want our program to stop when we click on the Stop button, but we also want it to stop if an error occurs during the program. Remove the wire between the stop button and while loop stop, and add a Boolean Or (Programming>> Boolean) to combine the stop button input with the error output. LabVIEW will automatically select the Boolean value of the error cluster for the operation.

 

error.png

 

You now have a complete program that will read values from your accelerometer and display that information to a graph on your Front Panel and alert you if the value exceeds the specified range. Run the VI from within your project that targets the roboRIO. You should see the values on your chart and indicator change depending on how much you shake the accelerometer.

 

running.png

 

Using Other Types of Sensors

 

In addition to accelerometers, WPI Robotics Library provides an API for other types of sensors. For each sensor there is a palette of VIs that make extensive use of the Open»Get»Close and Open»Set»Close paradigm. This allows you to make use of your experience with different sensor types.

For accelerometers, gyroscopes, and ultrasonic distance sensors, changing between measurement types can be as simple as plugging a new sensor into the analog breakout board and using the corresponding

sensor API. Each of these sensors use an analog signal that returns a voltage proportional to a physical value.

Examples for these types of sensors, such as the gyroscope example below, can be found by going to Getting Started window and clicking the Find Examples link in the bottom right corner. You can access the Getting Started window from any LabVIEW window by going to View»Getting Started Window…. Each example will show how to use the sensor’s corresponding API in the WPI Robotics Library.

 

 wpi lib.png

 

For the remaining sensor types, such as encoder and counters, the programming follows a similar procedure, but the hardware setup differs.

 

Encoders and counters are digital sensors. Rather than returning a voltage proportional to a physical value they monitor rising and/or falling edges of digital signals to either count or measure position.  Since these are digital sensors they will need to connect to the roboRIO digital pins.

Once you have connected the encoder to the appropriate pins you can use the encoder palette to make an angular position measurement. Examples for these sensors, such as the encoder example below, can also be found by going to Getting Started windows and clicking the Find Examples link in the bottom right corner..

 

other sensor.png

 

Conclusion

 

This concludes the overview of the FRC Robotics Sensor API. We have taken a detailed look into how to make analog sensor measurements using an accelerometer and the WPI Robotics Library Sensor API. Please refer to the FIRST Robotics Competition VIs Help file and other tutorials for more detailed information about this API.

Contributors