MIP_SDK  v3.0.0-736-g212583cf
MicroStrain Communications Library for embedded systems
Functions | Variables
5-Series Stream IMU Example [CPP]

Example setup program for streaming IMU data on 5-series devices using C++. More...

Functions

static void logCallback (void *_user, const microstrain_log_level _level, const char *_format, va_list _args)
 Custom logging callback for MIP SDK message formatting and output. More...
 
static mip::Timestamp getCurrentTimestamp ()
 Gets the current system timestamp in milliseconds. More...
 
static void initializeDevice (mip::Interface &_device)
 Initializes and configures a MIP device interface. More...
 
static bool isDescriptorSupported (const mip::CompositeDescriptor &_compositeDescriptor, const uint16_t *_supportedDescriptors, const uint8_t _supportedDescriptorCount)
 Determines if the device supports a specific descriptor. More...
 
static void configureSensorMessageFormat (mip::Interface &_device, const uint16_t *_supportedDescriptors, const uint8_t _supportedDescriptorCount)
 Configures message format for sensor data streaming. More...
 
static void packetCallback (void *_user, const mip::PacketView &_packetView, mip::Timestamp _timestamp)
 Callback function that processes received MIP packets. More...
 
static void accelFieldCallback (void *_user, const mip::FieldView &_fieldView, mip::Timestamp _timestamp)
 Callback handler for accelerometer data fields. More...
 
static void gyroFieldCallback (void *_user, const mip::FieldView &_fieldView, mip::Timestamp _timestamp)
 Callback handler for gyroscope data fields. More...
 
static void magFieldCallback (void *_user, const mip::FieldView &_fieldView, mip::Timestamp _timestamp)
 Callback handler for magnetometer data fields. More...
 
static void terminate (microstrain::Connection *_connection, const char *_message, const bool _successful)
 Handles graceful program termination and cleanup. More...
 
static void terminate (mip::Interface &_device, const mip::CmdResult _cmdResult, const char *_format,...)
 Handles graceful program termination and command failure cleanup. More...
 

Variables

static constexpr const char * PORT_NAME = "/dev/ttyACM0"
 Set the port name for the connection (Serial/USB) More...
 
static constexpr uint32_t BAUDRATE = 115200
 Set the baudrate for the connection (Serial/USB) More...
 
static constexpr uint16_t SAMPLE_RATE_HZ = 1
 Streaming rate in Hz. More...
 
static constexpr uint32_t RUN_TIME_SECONDS = 30
 Example run time. More...
 

Detailed Description

This example shows a basic setup for streaming IMU data on 5-series devices using C++. This is not an exhaustive example of all streaming options for those devices. If this example does not meet your specific setup needs, please consult the MIP SDK API documentation for the proper commands.

License

Function Documentation

◆ logCallback()

static void logCallback ( void *  _user,
const microstrain_log_level  _level,
const char *  _format,
va_list  _args 
)
static

Processes and formats log messages from the MIP SDK based on severity level. Routes messages to appropriate output streams - errors and fatal messages go to stderr while other levels go to stdout. Each message is prefixed with its severity level name.

Parameters
_userPointer to user data (unused in this implementation)
_levelLog message severity level from microstrain_log_level enum
_formatPrintf-style format string for the message
_argsVariable argument list containing message parameters

◆ getCurrentTimestamp()

static mip::Timestamp getCurrentTimestamp ( )
static

Provides system time measurement using std::chrono for milliseconds since epoch. Uses system_clock to get wall-clock time that corresponds to calendar time and can be synchronized with external time sources.

Note
Update this function to use a different time source if needed for your specific application requirements
Returns
Current timestamp in milliseconds since epoch

◆ initializeDevice()

static void initializeDevice ( mip::Interface _device)
static

Performs a complete device initialization sequence:

  1. Verifies device communication with a ping command
  2. Sets the device to idle mode to ensure reliable configuration
  3. Queries and displays detailed device information
  4. Loads default device settings for a known state
Parameters
_deviceReference to a MIP device interface to initialize

◆ isDescriptorSupported()

static bool isDescriptorSupported ( const mip::CompositeDescriptor _compositeDescriptor,
const uint16_t *  _supportedDescriptors,
const uint8_t  _supportedDescriptorCount 
)
static

Checks if a given composite descriptor is supported by comparing against an array of supported descriptors. The device returns descriptors as composite values (e.g., 0x8001 from 0x80,0x01).

Parameters
_compositeDescriptorReference to the composite descriptor to check.
_supportedDescriptorsArray of supported descriptors from the device
_supportedDescriptorCountNumber of entries in the array
Returns
true if the descriptor is supported, false otherwise

◆ configureSensorMessageFormat()

static void configureSensorMessageFormat ( mip::Interface _device,
const uint16_t *  _supportedDescriptors,
const uint8_t  _supportedDescriptorCount 
)
static

Sets up sensor data output by:

  1. Querying device base rate
  2. Validating desired sample rate against base rate
  3. Calculating proper decimation
  4. Configuring message format with:
    • Scaled accelerometer
    • Scaled gyroscope
    • Scaled magnetometer
Parameters
_deviceReference to the initialized MIP device interface
_supportedDescriptorsArray of descriptors supported by the device
_supportedDescriptorCountNumber of descriptors in the array

◆ packetCallback()

static void packetCallback ( void *  _user,
const mip::PacketView _packetView,
mip::Timestamp  _timestamp 
)
static

This function is called whenever a MIP packet is received from the device. It processes the packet by:

  1. Extracting all fields from the packet
  2. Building a formatted string of field descriptors
  3. Logging packet information including timestamp, descriptor set, and field descriptors
Parameters
_userPointer to user data (unused in this implementation)
_packetViewReference to the received MIP packet
_timestampTimestamp when the packet was received

◆ accelFieldCallback()

static void accelFieldCallback ( void *  _user,
const mip::FieldView _fieldView,
mip::Timestamp  _timestamp 
)
static

Processes scaled accelerometer data fields by:

  1. Extracting the scaled acceleration vector from the field
  2. Logging the X, Y, Z acceleration values with descriptors
Parameters
_userPointer to user data (unused in this implementation)
_fieldViewReference to the field containing accelerometer data
_timestampTimestamp indicating when the field was received from the device (unused in this implementation)

◆ gyroFieldCallback()

static void gyroFieldCallback ( void *  _user,
const mip::FieldView _fieldView,
mip::Timestamp  _timestamp 
)
static

Processes scaled gyroscope data fields by:

  1. Extracting the scaled angular rates from the field
  2. Logging the X, Y, Z angular rate values with descriptors
Parameters
_userPointer to user data (unused in this implementation)
_fieldViewReference to the field containing gyroscope data
_timestampTimestamp indicating when the field was received from the device (unused in this implementation)

◆ magFieldCallback()

static void magFieldCallback ( void *  _user,
const mip::FieldView _fieldView,
mip::Timestamp  _timestamp 
)
static

Processes scaled magnetometer data fields by:

  1. Extracting the scaled magnetic field vector from the field
  2. Logging the X, Y, Z magnetic field values with descriptors
Parameters
_userPointer to user data (unused in this implementation)
_fieldViewReference to the field containing magnetometer data
_timestampTimestamp indicating when the field was received from the device (unused in this implementation)

◆ terminate() [1/2]

static void terminate ( microstrain::Connection _connection,
const char *  _message,
const bool  _successful 
)
static

Handles graceful shutdown when errors occur:

  • Outputs provided error message
  • Closes device connection if open
  • Exits with appropriate status code
Parameters
_connectionPointer to the device connection to close
_messageError message to display
_successfulWhether termination is due to success or failure

◆ terminate() [2/2]

static void terminate ( mip::Interface _device,
const mip::CmdResult  _cmdResult,
const char *  _format,
  ... 
)
static

Handles command failure scenarios:

  • Formats and displays an error message with command result
  • Closes device connection
  • Exits with failure status
Parameters
_deviceMIP device interface for the command that failed
_cmdResultResult code from a failed command
_formatPrintf-style format string for error message
...Variable arguments for format string

Variable Documentation

◆ PORT_NAME

constexpr const char* PORT_NAME = "/dev/ttyACM0"
staticconstexpr

◆ BAUDRATE

constexpr uint32_t BAUDRATE = 115200
staticconstexpr
Note
For native serial connections this needs to be 115200 due to the device default settings command Use mip::commands_3dm::*UartBaudrate() to write and save the baudrate on the device

◆ SAMPLE_RATE_HZ

constexpr uint16_t SAMPLE_RATE_HZ = 1
staticconstexpr

◆ RUN_TIME_SECONDS

constexpr uint32_t RUN_TIME_SECONDS = 30
staticconstexpr