NDEVR
API Documentation
MadgwickOrientationSensor.h
1#pragma once
2#include "DLLInfo.h"
3#include <NDEVR/OrientationSensor.h>
4#include <NDEVR/QueueBuffer.h>
5#include <NDEVR/Quaternion.h>
6#include <mutex>
7namespace NDEVR
8{
9 namespace IMU
10 {
11 class Point;
12 };
14 class IMUCalibrator;
39 struct RasterFrame;
45 class HARDWARE_API MadgwickOrientationSensor : public OrientationSensor
46 {
47 public:
53 MadgwickOrientationSensor(DesignObjectLookup* lookup, const Model& model, LogPtr log, QObject* parent = nullptr);
56 bool readyToSetupFrame(const Time& cutoff_time) const;
57 #if _WIN32
63 bool collectRecentRecords(const Time& cutoff_time, AlocatingAlignedBuffer<IMU::Point, 32>& imu_points, const Time& offset_time);
64 #endif
68 void reset();
77 void setStoreReadings(bool store_readings);
82 fltp08 beta() const { return m_beta; }
84 bool supportsFixed() const override { return false; }
86 bool supportsRodded() const override { return false; }
89 void setRequestGyroCallback(const std::function<void()>& callback) { m_request_gyro_callback = callback; }
93 void processGyro(Ray<3, fltp08> gyro_data, const Time& ts);
97 void processAcceleration(Ray<3, fltp08> accel_data, const Time& ts);
102 void processAccelerationAndGyro(Ray<3, fltp08> accel_data, Ray<3, fltp08> gyro_data, const Time& ts);
106 void processMagnetometer(Ray<3, fltp08> accel_data, const Time& ts);
110 void processQuaternion(const Quaternion<fltp08>& quaternion, const Time& ts);
114 bool requestGyroAlignment(bool reference_previous) override;
146 protected:
147 std::function<void()> m_request_gyro_callback;
153 std::mutex m_theta_mtx;
158 Time m_start_calibration_time = Constant<Time>::Invalid;
160 fltp08 m_beta = 0.042;
162 Time m_last_timestamp = Constant<Time>::Invalid;
164 bool m_reset_gyro = true;
165 bool m_store_readings = false;
168 };
169}
Stores an angle in an optimized internal format with support for efficient trigonometric operations.
Definition Angle.h:83
The equivelent of std::vector but with a bit more control.
Definition Buffer.hpp:58
A core class where all Design Objects including models, materials, and geometries are stored.
Logic and storage for IMU calibration.
A light-weight wrapper that will be a no-op if there is not a valid log reference,...
void updateFromQuaternion(Time time)
Updates the sensor orientation from the internal quaternion.
bool supportsFixed() const override
Returns false; this sensor does not support fixed mounting.
Vector< 3, fltp08 > m_gyro
The most recent gyroscope reading.
void processAcceleration(Ray< 3, fltp08 > accel_data, const Time &ts)
Processes an accelerometer reading to update orientation.
TimeSpan m_calibration_period
Duration of the gyro calibration period.
void setStoreReadings(bool store_readings)
Enables or disables storing historical orientation readings.
void processMadgwick(Time time)
Runs the Madgwick filter update step at the given time.
void processGyro(Ray< 3, fltp08 > gyro_data, const Time &ts)
Processes a gyroscope reading to update orientation.
MadgwickOrientationSensor(DesignObjectLookup *lookup, const Model &model, LogPtr log, QObject *parent=nullptr)
Constructs a MadgwickOrientationSensor.
Angle< fltp08 > angularVelocity() const
Returns the current angular velocity magnitude.
Vector< 3, Angle< fltp08 > > angleFromQuaternion(const Quaternion< fltp08 > &quaternion) const
Converts a quaternion to Euler angles.
bool m_reset_gyro
Whether the gyroscope needs to be reset on the next reading.
Buffer< Ray< 3, fltp08 > > v_accel_data_sync
Synchronized accelerometer data buffer.
void initialComputeMadgwick(Vector< 3, fltp08 > a, Vector< 3, fltp08 > m, Quaternion< fltp08 > &q, Time time)
Computes the initial Madgwick orientation from accelerometer and magnetometer data.
void setBeta(fltp08 beta)
Sets the Madgwick filter gain parameter beta.
void setRequestGyroCallback(const std::function< void()> &callback)
Sets a callback that is invoked when gyro data is requested.
Vector< 3, fltp08 > m_prev_accel_data
The previous accelerometer reading.
uint04 m_init_count
Counter for initialization readings before Madgwick starts.
Quaternion< fltp08 > m_quaternion
The current orientation quaternion.
void processMagnetometer(Ray< 3, fltp08 > accel_data, const Time &ts)
Processes a magnetometer reading to update orientation.
Time m_start_calibration_time
When gyro calibration began.
Time m_last_timestamp
Arrival time of the previous gyro frame.
void processAccelerationAndGyro(Ray< 3, fltp08 > accel_data, Ray< 3, fltp08 > gyro_data, const Time &ts)
Processes combined accelerometer and gyroscope readings.
void processQuaternion(const Quaternion< fltp08 > &quaternion, const Time &ts)
Processes a quaternion orientation reading directly.
Buffer< Time > v_gyro_timestamp
Timestamps for buffered gyro readings.
bool requestGyroAlignment(bool reference_previous) override
Requests alignment of the gyroscope to a reference orientation.
void setPacketGyroState(sint02 state)
Sets the gyro state from a packet-level integer value.
std::function< void()> m_request_gyro_callback
Callback invoked when gyro data is requested.
QueueBuffer< OrientationReading > m_historic_orientation
Buffer of historical orientation readings for interpolation.
static void updateMadgwick(Vector< 3, fltp08 > g, Vector< 3, fltp08 > a, Vector< 3, fltp08 > m, Quaternion< fltp08 > &q, TimeSpan time_span, fltp08 beta)
Performs a single Madgwick filter update with gyro, accel, and magnetometer data.
bool m_store_readings
Whether to store orientation readings in the history buffer.
Angle< fltp08 > yaw_offset
Yaw offset applied to the orientation output.
void reset()
Resets the orientation sensor to its initial state.
Buffer< Ray< 3, fltp08 > > v_gyro_data
Buffered gyroscope data readings.
Time m_prev_accel_timestamp
Timestamp of the previous accelerometer reading.
DesignObjectLookup * m_lookup
Pointer to the design object lookup.
Matrix< fltp08 > orientationAt(const Time &time) const
Returns the orientation matrix at the given time by interpolating stored readings.
std::mutex m_theta_mtx
Mutex protecting the orientation quaternion.
sint02 getPacketGyroState() const
Returns the current gyro state as a packet-level integer.
TimeSpan m_total_ts
Total time span accumulated during calibration.
void setGyroState(GyroState state)
Sets the current gyroscope processing state.
fltp08 m_beta
The Madgwick filter gain parameter.
Vector< 3, fltp08 > m_gyro_calibration
The gyroscope calibration offset.
fltp08 beta() const
Returns the current Madgwick filter gain parameter beta.
void finishAlignment()
Finalizes the gyroscope alignment process.
bool readyToSetupFrame(const Time &cutoff_time) const
Returns whether there is enough data to set up a frame at the given cutoff time.
bool supportsRodded() const override
Returns false; this sensor does not support rodded mounting.
Templated logic for doing matrix multiplication.
Definition Matrix.hpp:182
A core class that represents a node on model hierarchy.
Definition Model.h:292
OrientationSensor(const Model &model, LogPtr log, QObject *parent=nullptr)
Constructs an OrientationSensor with the given model, log, and optional parent.
https://www.3dgep.com/understanding-quaternions/
Stores objects in a first-in, first out queue based Buffer with push and pop functions.
Stores a time span, or difference between two times, with an optional start time.
Definition TimeSpan.h:46
Represents a timestamp with utilities for manipulation and conversion.
Definition Time.h:62
A fixed-size array with N dimensions used as the basis for geometric and mathematical types.
Definition Vector.hpp:62
The primary namespace for the NDEVR SDK.
uint32_t uint04
-Defines an alias representing a 4 byte, unsigned integer -Can represent exact integer values 0 throu...
double fltp08
Defines an alias representing an 8 byte floating-point number.
int16_t sint02
-Defines an alias representing a 2 byte, signed integer.
GyroState
Used by OrientationSensor to display the current state of any available gyro.
Defines for a given type (such as sint04, fltp08, UUID, etc) a maximum, minimum, and reserved 'invali...
Vector< 3, fltp08 > accel
The accelerometer reading.
Vector< 3, fltp08 > magnet
The magnetometer reading.
OrientationReading(Quaternion< fltp08 > quaternion, Vector< 3, fltp08 > gyro, Vector< 3, fltp08 > accel, Vector< 3, fltp08 > magnet, const Time &time)
Constructs an OrientationReading with all sensor values.
Quaternion< fltp08 > quaternion
The orientation quaternion for this reading.
Vector< 3, fltp08 > gyro
The gyroscope angular velocity reading.
Time time
The timestamp of this reading.
A single frame of raster depth/color data along with processing state and metadata.
Definition RasterFrame.h:56