C++

C++_Icon

The navX_FRC C++ software library supports access to navX2-MXP via all four of the navX2-MXP communication interfaces, and to the navX2-Micro via I2C.  Several example programs are provided, demonstrating how to use a navX-Sensor in a RoboRIO-based robot control application.

Note that this Java library is also backwards-compatible with the “Classic” navX-MXP and navX-Micro sensors.

[Update:  1/7/2022 – Version 4.0.442 is now available – which is compatible with the FRC 2022 Release Candidate 1.   Both offline and online installation methods are supported for installing the library update within VSCode.]

To use the library, you can download the latest build of the libraries; advanced users can checkout the source code with Git.  To learn more about the library, online help is available.  If developing in a Linux or MacOS platform, you can download the cross-platform library package.

Additionally, the new online installation method is available, as described below.

Getting Started

Before getting started, ensure you have installed the WPI Libraries and VS Code.

Several sample C++ Robot Applications are provided.  After running the setup program included in the latest build, the libraries and samples will be installed to the following location:

<HomeDirectory>\navx-mxp\      (e.g., if your user name is Robot, the directory name will be C:\Users\Robot\navx-mxp

Within this directory, the “cpp\examples” directory contains several example programs.

NOTE:  The example programs are written for Eclipse, but can easily be imported into VSCode using the “Import a WPILib Eclipse Project” option.

Importing a WPILib Eclipse project into VSCode.

Configuring your VSCode Project to use the navX_FRC Library for RoboRIO

Offline Installation Method

The Offline Installation method can be used even when you are not connected to the Internet.  Please download the WPILib Offline Install  .zip file found on the product page here. Once downloaded, extract the contents into C:\Users\Public\wpilib\2023\ on windows or ~/wpilib/2023/ on Linux and macOS.

Next, select “Install new libraries (offline)”:

And select the “KauaiLabs_navX_FRC” item, and press the “OK” button.

At this point, the “navx_frc.json” file will be added to the “vendordeps” directory in your project.  This file provides GradleRIO with all the information need to compile and link your application with the navX_FRC libraries – and even enables you to debug into the navX_FRC libraries if you wish.

VSCode Project Vendordeps folder

Online Installation Method

The Online Installation method can be used instead of the Offline Installation method, if you are connected to the Internet.  Note that the Online  Installation method only installs the navX_FRC library, but does not install any of the navX-sensor tools.  However, the online installation process is very simple.

First, open your WPILibrary-based C++ project in VSCode, click on the “WPI” button in the toolbar, and select “Manage Vendor Libraries”.

Next, select the “Install new library (online)” option

Then, enter the Offline Installation URL shown below and press ENTER.

  • https://dev.studica.com/releases/2023/NavX.json

At this point, the “navx_frc.json” file will be added to the “vendordeps” directory in your project.  This file provides GradleRIO with all the information need to compile and link your application with the navX_FRC libraries – and even enables you to debug into the navX_FRC libraries if you wish.

VSCode Project Vendordeps folder

 

Next Steps

Once the C++ library paths are configured as indicated above, import the example projects into VSCode, and then modify the constructor of the Robot.java file to specify your selected interface, as follows:

ahrs = new AHRS(SPI.Port.kMXP); /* Alternatives:  I2C.Port.kMXP or SerialPort.Port.kUSB */

To better understand navX-sensor capabilities, the DataMonitor example is recommended.  After compiling and downloading this example to the RoboRIO, the application will connect and sensor readings will be displayed in the DriverStation’s SmartDashboard display.  This sample application’s OperatorControl() function demonstrates how to access navX-sensor readings; more information is available in the online documentation for the AHRS class.

After getting the sample navX-sensor application running, please see the Examples section for a description and sample code which implements some key capabilities enabled by a navX-sensor.

Stepping into the navX_FRC C++ Library source code

If you would like to step into the navX_FRC Java Library source code, or set breakpoints in the library, use the VSCode “Debug Robot Code” option.  If you have successfully imported the navX_FRC library as shown above, all the sources will be available during your debug session.

Simulation Support

The navX-Sensor library supports simulation via the WPI Library “SimDevice” mechanism.

This mechanism allows code that uses the AHRS class to be tested in a simulated environment where simulated values are returned from the AHRS class, rather than from an actual navX-sensor device.

Supported Simulation Variables

The following simulation variables are supported; when updated, these variables may be accessed by the standard AHRS class access methods indicated below.

Simulation Variable name AHRS class access method
Connected IsConnected()
Rate GetRate()
Yaw GetYaw() or GetAngle()
Pitch GetPitch()
Roll GetRoll()
CompassHeading GetCompassHeading()
FusedHeading GetFusedHeading()
LinearWorldAccelX GetLinearWorldAccelX()
LinearWorldAccelY GetLinearWorldAccelY()
LinearWorldAccelZ GetLinearWorldAccelZ()

Updating the Simulation Variables

The following C++code snippet indicates how to update the simulation variables:

 

#include <hal/SimDevice.h>
#include <hal/simulation/SimDeviceData.h>

HAL_SimDeviceHandle dev = HALSIM_GetSimDeviceHandle(“navX-Sensor[0]”);
hal::SimDouble angle = HALSIM_GetSimValueHandle(dev, “Yaw”));
angle.set(5.0);

NOTE:  updated values may not be returned from the corresponding AHRS class access methods until up to 1/update rate milliseconds (e.g., 20 milliseconds if a 50Hz update rate is used) after they are updated; similar to working with the actual navX-sensor, the AHRS class values are updated periodically by a background thread.

Simulation Example

The WPI Library documentation provides an example of a simulated differential drivetrain guided by a single-axis gyro.

This example can be easily modified to use instead a navX-sensor by:

(a) replacing the single-axis gyro (ADXRS450_Gyro) class with the AHRS class provided by the navX-Sensor library.

(b) modifying the code which updates the simulation model, replacing the call to

    m_gyroSim.setAngle(...);

with the code shown in the previous section which updates the AHRS class “Yaw” angle.

NOTE:  AHRS class Simulation Support is enabled automatically whenever the WPI Library-based robot application is compiled for the “desktop” platform.  This support is not present when the WPI Library-based robot application runs on the RoboRIO platform.