Skip to content

C++ API - Inertial Sense Class and CLTool Example Project

The InertialSense C++ class, defined in InertialSense.h/.cpp, provides all SDK capabilities including serial communications, data logging to file, and embedded firmware update for InertialSense products.

CLTool Example

The Command Line Tool (CLTool) is an open source project designed to illustrate InertialSense C++ class implementation. The CLTool project can be compiled on most operating systems using cmake and gcc and can be used to communicate, log data, and update firmware for Inertial Sense products. A Visual Studio project for Windows is also included. See Using CLTool for details on compiling and running the CLTool.

Implementation Keywords

The following keywords are found in the CLTool soure code identify the steps for InertialSense class implementation.

/* SDK Implementation Keywords:
 * [C++ COMM INSTRUCTION] - C++ binding API, InertialSense class with binary
 * communication protocol and serial port support for Linux and Windows.
 * [LOGGER INSTRUCTION] - Data logger.
 * [BOOTLOADER INSTRUCTION] - Firmware update feature.
 */

Serial Communications

Step 1: Instantiate InertialSense class

Include the InertialSense header file. Create InertialSense object.

#include "InertialSense.h"

// [C++ COMM INSTRUCTION] 1.) Create InertialSense object, passing in data callback function pointer.
InertialSense inertialSenseInterface(cltool_dataCallback);

Step 2: Open serial port

Open the serial by specifying the com port number, buadrate, and and The serial port used for communications

if (!inertialSenseInterface.Open(g_commandLineOptions.comPort.c_str(),
   g_commandLineOptions.baudRate,
   g_commandLineOptions.disableBroadcastsOnClose))
{
    cout << "Failed to open serial port at " << g_commandLineOptions.comPort.c_str() << endl;
    return -1; // Failed to open serial port
}

Step 3: Enable data broadcasting

The following enables data broadcasting from the IMX at a specified data rate or period in milliseconds.

cltool_setupCommunications(inertialSenseInterface)

Step 4: Read data

Call the Update() method at regular intervals to send and receive data.

// Main loop. Could be in separate thread if desired.
while (!g_inertialSenseDisplay.ControlCWasPressed())
{
    if (!inertialSenseInterface.Update())
    {
        // device disconnected, exit
        break;
    }
}

Step 5: Handle received data

New data is available in the data callback function.

static void cltool_dataCallback(InertialSense* i, p_data_t* data, int pHandle)
{
    // Print data to terminal
    g_inertialSenseDisplay.ProcessData(data);

    // uDatasets is a union of all datasets that we can receive. See data_sets.h for a full list of all available datasets.
    uDatasets d = {};
    copyDataPToStructP(&d, data, sizeof(uDatasets));

    // Example of how to access dataset fields.
    switch (data->hdr.id)
    {
    case DID_INS_2:
        d.ins2.qn2b; // quaternion attitude
        d.ins2.uvw; // body velocities
        d.ins2.lla; // latitude, longitude, altitude
        break;
    case DID_INS_1:
        d.ins1.theta; // euler attitude
        d.ins1.lla; // latitude, longitude, altitude
    break;
    case DID_IMU: d.dualImu; break;
    case DID_PIMU: d.dThetaVel; break;
    case DID_GPS1_POS: d.gpsPos; break;
    case DID_MAGNETOMETER: d.mag; break;
    case DID_BAROMETER: d.baro; break;
    case DID_SYS_SENSORS: d.sysSensors; break;
    }
}

Step 6: Close interface

Close the interface when your application finishes.

// Close cleanly to ensure serial port and logging are shutdown properly. (optional)
inertialSenseInterface.Close();

Data Logging

Step 1: Configure and Start Logging

// [LOGGER INSTRUCTION] Setup and start data logger
if (!cltool_setupLogger(inertialSenseInterface))
{
    cout << "Failed to setup logger!" << endl;
    return -1;
}

Compile & Run (Linux/Mac)

  1. Create build directory
    cd cltool
    mkdir build
    
  2. Run cmake from within build directory
    cd build
    cmake ..
    
  3. Compile using make
    make
    
  4. If necessary, add current user to the "dialout" group in order to read and write to the USB serial communication ports:
    sudo usermod -a -G dialout $USER
    sudo usermod -a -G plugdev $USER
    (reboot computer)
    
  5. Run executable
    ./cltool
    

Compile & Run (Windows Powershell)

*Note - Install CMake for Windows natively, or install the CMake for Windows extension for Visual Studio

  1. Create build directory
    cd InertialSenseSDK/cltool
    mkdir build
    
  2. Run cmake from within build directory
    cd build
    cmake ..
    
  3. Compile using make

    cmake --build .
    

  4. Run executable

    C:\InertialSenseSDK\cltool\build\Release\cltool.exe
    

Summary

This section has covered the basic functionality you need to set up and communicate with Inertial Sense products. If this doesn't cover everything you need, feel free to reach out to us on the Inertial Sense SDK GitHub repository, and we will be happy to help.