Doly C++ SDK v1.00
Loading...
Searching...
No Matches
ImuControl/main.cpp

Demonstrates:

  • Initializing ImuControl
  • Receiving IMU sensor events
  • Reading orientation and motion data
#include <chrono>
#include <thread>
#include <spdlog/spdlog.h>
#include "ImuControl.h"
#include "Helper.h"
void onImuUpdate(ImuData data)
{
spdlog::info("IMU Update - Yaw: {:.2f}, Pitch: {:.2f}, Roll: {:.2f}", data.ypr.yaw, data.ypr.pitch, data.ypr.roll);
}
void onImuGesture(ImuGesture type, GestureDirection from)
{
spdlog::info("IMU Gesture - Type: {}, Direction: {}", ImuEvent::getGestureStr(type), ImuEvent::getDirectionStr(from));
}
int main()
{
// Setup spdlog
spdlog::set_level(spdlog::level::info); // Set as needed
spdlog::flush_on(spdlog::level::trace); // flush everything
// *** IMPORTANT ***
// Stop doly service if running,
// otherwise instance of libraries cause conflict
spdlog::error("Doly service stop failed");
return -1;
}
// For better performance provide actual calibrated IMU offsets or calculate them once after initialization
// with the help of ImuControl::calculate_offsets() and use them for next initializations.
// in this example we will use Helper to get previously saved offsets
// Read settings
// one time read is sufficient for the lifetime of the application
int8_t res = Helper::readSettings();
if (res < 0)
{
spdlog::error("Read settings failed with code: {}", res);
return -2;
}
// Get pre defined IMU offsets
int16_t gx, gy, gz, ax, ay, az;
res = Helper::getImuOffsets(gx, gy, gz, ax, ay, az);
if (res < 0)
{
spdlog::error("Get IMU offsets failed with code: {}", res);
return -3;
}
// Get ImuControl version
spdlog::info("ImuControl Version:{:.3f}", ImuControl::getVersion());
// Initialize IMU Control with offsets
// delay 1 second before processing events
if (ImuControl::init(1, gx, gy, gz, ax, ay, az) < 0)
{
spdlog::error("ImuControl init failed");
return -2;
}
// wait for a while before exiting
std::this_thread::sleep_for(std::chrono::seconds(30));
// Cleanup
return 0;
}
Shared helper utilities and common types used across the Doly SDK.
Public API for Doly IMU (Inertial Measurement Unit) control.
GestureDirection
Direction associated with a detected gesture (where the motion came from).
Definition ImuEventListener.h:56
ImuGesture
High-level gesture types detected from IMU motion analysis.
Definition ImuEventListener.h:29
int8_t stopDolyService()
Stop the background Doly service (if running).
int8_t readSettings()
Read default settings from the platform settings file.
int8_t getImuOffsets(int16_t &gx, int16_t &gy, int16_t &gz, int16_t &ax, int16_t &ay, int16_t &az)
Retrieve IMU calibration offsets (gyro/accel).
int8_t dispose()
Dispose/stop the IMU subsystem and release resources.
float getVersion()
Get current library version.
int8_t init(uint8_t delay=0, int16_t gx=0, int16_t gy=0, int16_t gz=0, int16_t ax=0, int16_t ay=0, int16_t az=0)
Initialize the IMU subsystem.
void RemoveListenerUpdateEvent(void(*Imu_callback)(ImuData data))
Unregister a previously registered IMU update callback.
std::string getGestureStr(ImuGesture type)
Convert a gesture enum value to a human-readable string.
void RemoveListenerGestureEvent(void(*gesture_cb)(ImuGesture type, GestureDirection from))
Unregister a previously registered gesture callback.
void AddListenerUpdateEvent(void(*Imu_callback)(ImuData data))
Register a static/free function to receive IMU update events.
void AddListenerGestureEvent(void(*gesture_cb)(ImuGesture type, GestureDirection from))
Register a static/free function to receive gesture events.
std::string getDirectionStr(GestureDirection from)
Convert a direction enum value to a human-readable string.
Latest IMU reading snapshot returned by the IMU subsystem.
Definition ImuEventListener.h:91
YawPitchRoll ypr
Definition ImuEventListener.h:93
float yaw
Definition ImuEventListener.h:80
float pitch
Definition ImuEventListener.h:81
float roll
Definition ImuEventListener.h:82