This repository was archived by the owner on Feb 11, 2026. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 19
Expand file tree
/
Copy pathdevice_info_example.cpp
More file actions
318 lines (275 loc) · 11.9 KB
/
device_info_example.cpp
File metadata and controls
318 lines (275 loc) · 11.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
////////////////////////////////////////////////////////////////////////////////
/// @file device_info_example.cpp
///
/// @defgroup device_info_example_cpp Device Info Example [CPP]
///
/// @ingroup examples_cpp
///
/// @brief Example program to print device information for any MIP-enabled
/// MicroStrain device using C++
///
/// @details This example shows a basic setup for any MIP-enabled MicroStrain
/// device to demonstrate how to get and print device information using
/// C++. This is not an exhaustive example of all settings for those
/// devices. If this example does not meet your specific setup needs,
/// please consult the MIP SDK API documentation for the proper
/// commands.
///
/// @section device_info_example_cpp_license License
///
/// @copyright Copyright (c) 2025 MicroStrain by HBK
/// Licensed under MIT License
///
/// @{
///
// Include the MicroStrain Serial connection header
#include <microstrain/connections/serial/serial_connection.hpp>
// Include the MicroStrain logging header for custom logging
#include <microstrain/logging.hpp>
// Include all necessary MIP headers
// Note: The MIP SDK has headers for each module to include all headers associated with the module
// I.E., #include <mip/mip_all.hpp>
#include <mip/mip_interface.hpp>
#include <mip/definitions/commands_base.hpp>
#include <cstdarg>
#include <cstdint>
#include <cstdio>
#include <cstdlib>
#include <cstring>
////////////////////////////////////////////////////////////////////////////////
// NOTE: Setting these globally for example purposes
// TODO: Update to the correct port name and baudrate
/// @brief Set the port name for the connection (Serial/USB)
#ifdef _WIN32
static constexpr const char* PORT_NAME = "COM1";
#else // Unix
static constexpr const char* PORT_NAME = "/dev/ttyACM0";
#endif // _WIN32
/// @brief Set the baudrate for the connection (Serial/USB)
/// @note For native serial connections this needs to be 115200 due to the device default settings command
/// Use mip::commands_base::*CommSpeed() or mip::commands_3dm::*UartBaudrate() to write and save the baudrate on the device
static constexpr uint32_t BAUDRATE = 115200;
////////////////////////////////////////////////////////////////////////////////
///
/// @} group device_info_example_cpp
////////////////////////////////////////////////////////////////////////////////
// Custom logging handler callback
static void logCallback(void* _user, const microstrain_log_level _level, const char* _format, va_list _args);
// Common device initialization procedure
static void initializeDevice(mip::Interface& _device);
// Utility functions the handle application closing and printing error messages
static void terminate(microstrain::Connection* _connection, const char* _message, const bool _successful = false);
static void terminate(mip::Interface& _device, const mip::CmdResult _cmdResult, const char* _format, ...);
int main(const int argc, const char* argv[])
{
// Unused parameters
(void)argc;
(void)argv;
// Note: This is a compile-time way of checking that the proper logging level is enabled
// Note: The max available logging level may differ in pre-packaged installations of the MIP SDK
#ifndef MICROSTRAIN_LOGGING_ENABLED_INFO
#error This example requires a logging level of at least MICROSTRAIN_LOGGING_LEVEL_INFO_ to work properly
#endif // !MICROSTRAIN_LOGGING_ENABLED_INFO
// Initialize the custom logger to print messages/errors as they occur
// Note: The logging level parameter doesn't need to match the max logging level.
// If the parameter is higher than the max level, higher-level logging functions will be ignored
MICROSTRAIN_LOG_INIT(&logCallback, MICROSTRAIN_LOG_LEVEL_INFO, nullptr);
// Initialize the connection
MICROSTRAIN_LOG_INFO("Initializing the connection.\n");
microstrain::connections::SerialConnection connection(PORT_NAME, BAUDRATE);
MICROSTRAIN_LOG_INFO("Connecting to the device on port %s with %d baudrate.\n", PORT_NAME, BAUDRATE);
// Open the connection to the device
if (!connection.connect())
{
terminate(&connection, "Could not open the connection!\n");
}
MICROSTRAIN_LOG_INFO("Initializing the device interface.\n");
mip::Interface device(
&connection, // Connection for the device
mip::C::mip_timeout_from_baudrate(BAUDRATE), // Set the base timeout for commands (milliseconds)
2000 // Set the base timeout for command replies (milliseconds)
);
initializeDevice(device);
terminate(&connection, "Example Completed Successfully.\n", true);
return 0;
}
////////////////////////////////////////////////////////////////////////////////
/// @addtogroup device_info_example_cpp
/// @{
///
////////////////////////////////////////////////////////////////////////////////
/// @brief Custom logging callback for MIP SDK message formatting and output
///
/// @details 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.
///
/// @param _user Pointer to user data (unused in this implementation)
/// @param _level Log message severity level from microstrain_log_level enum
/// @param _format Printf-style format string for the message
/// @param _args Variable argument list containing message parameters
///
static void logCallback(void* _user, const microstrain_log_level _level, const char* _format, va_list _args)
{
// Unused parameter
(void)_user;
switch (_level)
{
case MICROSTRAIN_LOG_LEVEL_FATAL:
case MICROSTRAIN_LOG_LEVEL_ERROR:
{
fprintf(stderr, "%s: ", microstrain_logging_level_name(_level));
vfprintf(stderr, _format, _args);
fflush(stderr);
break;
}
case MICROSTRAIN_LOG_LEVEL_WARN:
case MICROSTRAIN_LOG_LEVEL_INFO:
case MICROSTRAIN_LOG_LEVEL_DEBUG:
case MICROSTRAIN_LOG_LEVEL_TRACE:
{
fprintf(stdout, "%s: ", microstrain_logging_level_name(_level));
vfprintf(stdout, _format, _args);
fflush(stdout);
break;
}
case MICROSTRAIN_LOG_LEVEL_OFF:
default:
{
break;
}
}
}
////////////////////////////////////////////////////////////////////////////////
/// @brief Initializes and configures a MIP device interface
///
/// @details 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
///
/// @param _device Reference to a MIP device interface to initialize
///
static void initializeDevice(mip::Interface& _device)
{
// Ping the device
// Note: This is a good first step to make sure the device is present
MICROSTRAIN_LOG_INFO("Pinging the device.\n");
mip::CmdResult cmdResult = mip::commands_base::ping(_device);
if (!cmdResult.isAck())
{
terminate(_device, cmdResult, "Could not ping the device!\n");
}
// Set the device to Idle
// Note: This is good to do during setup as high data traffic can cause commands to fail
MICROSTRAIN_LOG_INFO("Setting the device to idle.\n");
cmdResult = mip::commands_base::setIdle(_device);
if (!cmdResult.isAck())
{
terminate(_device, cmdResult, "Could not set the device to idle!\n");
}
// Print device info to make sure the correct device is being used
MICROSTRAIN_LOG_INFO("Getting the device information.\n");
mip::commands_base::BaseDeviceInfo deviceInfo;
cmdResult = mip::commands_base::getDeviceInfo(_device, &deviceInfo);
if (!cmdResult.isAck())
{
terminate(_device, cmdResult, "Could not get the device information!\n");
}
// Extract the major minor and patch values
const uint16_t major = deviceInfo.firmware_version / 1000;
const uint16_t minor = deviceInfo.firmware_version / 100 % 10;
const uint16_t patch = deviceInfo.firmware_version % 100;
// Firmware version format is x.x.xx
char firmwareVersion[16] = {0};
snprintf(firmwareVersion, sizeof(firmwareVersion) / sizeof(firmwareVersion[0]), "%d.%d.%02d", major, minor, patch);
MICROSTRAIN_LOG_INFO("-------- Device Information --------\n");
MICROSTRAIN_LOG_INFO("%-16s | %.16s\n", "Name", deviceInfo.model_name);
MICROSTRAIN_LOG_INFO("%-16s | %.16s\n", "Model Number", deviceInfo.model_number);
MICROSTRAIN_LOG_INFO("%-16s | %.16s\n", "Serial Number", deviceInfo.serial_number);
MICROSTRAIN_LOG_INFO("%-16s | %.16s\n", "Lot Number", deviceInfo.lot_number);
MICROSTRAIN_LOG_INFO("%-16s | %.16s\n", "Options", deviceInfo.device_options);
MICROSTRAIN_LOG_INFO("%-16s | %16s\n", "Firmware Version", firmwareVersion);
MICROSTRAIN_LOG_INFO("------------------------------------\n");
}
////////////////////////////////////////////////////////////////////////////////
/// @brief Handles graceful program termination and cleanup
///
/// @details Handles graceful shutdown when errors occur:
/// - Outputs provided error message
/// - Closes device connection if open
/// - Exits with appropriate status code
///
/// @param _connection Pointer to the device connection to close
/// @param _message Error message to display
/// @param _successful Whether termination is due to success or failure
///
static void terminate(microstrain::Connection* _connection, const char* _message, const bool _successful /* = false */)
{
if (_message && strlen(_message) != 0)
{
if (_successful)
{
MICROSTRAIN_LOG_INFO("%s", _message);
}
else
{
MICROSTRAIN_LOG_ERROR("%s", _message);
}
}
if (!_connection)
{
// Create the device interface with a connection or set it after creation
MICROSTRAIN_LOG_ERROR("Connection not set for the device interface. Cannot close the connection.\n");
}
else
{
if (_connection->isConnected())
{
MICROSTRAIN_LOG_INFO("Closing the connection.\n");
if (!_connection->disconnect())
{
MICROSTRAIN_LOG_ERROR("Failed to close the connection!\n");
}
}
}
MICROSTRAIN_LOG_INFO("Press 'Enter' to exit the program.\n");
// Make sure the console remains open
const int confirmExit = getc(stdin);
(void)confirmExit; // Unused
if (!_successful)
{
exit(1);
}
}
////////////////////////////////////////////////////////////////////////////////
/// @brief Handles graceful program termination and command failure cleanup
///
/// @details Handles command failure scenarios:
/// - Formats and displays an error message with command result
/// - Closes device connection
/// - Exits with failure status
///
/// @param _device MIP device interface for the command that failed
/// @param _cmdResult Result code from a failed command
/// @param _format Printf-style format string for error message
/// @param ... Variable arguments for format string
///
static void terminate(mip::Interface& _device, const mip::CmdResult _cmdResult, const char* _format, ...)
{
if (_format && strlen(_format) != 0)
{
va_list args;
va_start(args, _format);
MICROSTRAIN_LOG_ERROR_V(_format, args);
va_end(args);
}
MICROSTRAIN_LOG_ERROR("Command Result: (%d) %s.\n", _cmdResult.value, _cmdResult.name());
// Get the connection pointer that was set during device initialization
microstrain::Connection* connection = static_cast<microstrain::Connection*>(_device.userPointer());
terminate(connection, "");
}
///
/// @} group device_info_example_cpp
////////////////////////////////////////////////////////////////////////////////