Arduino Helpers master
Utility library for Arduino
Quaternion.ino

Quaternion

Simple Quaternion test.

Boards:
AVR, AVR USB, Nano Every, Nano 33 IoT, Nano 33 BLE, UNO R4, Pi Pico, Due, Teensy 3.x, ESP8266, ESP32

Written by PieterP, 2020-03-24
https://github.com/tttapa/Arduino-Helpers

bool close(float actual, float expected, float atol) {
return std::abs(actual - expected) <= atol;
}
void setup() {
Serial.begin(115200);
Vec3f vector {-0.414578098794425, 0.829156197588850, 0.375000000000000};
Quaternion expected {0.829156197588850, -0.5, -0.25, 0.0};
float atol = 1e-7;
Serial << setprecision(7) << "Result = " << result << endl;
Serial << boolalpha;
Serial << close(result.w, expected.w, atol) << endl;
Serial << close(result.x, expected.x, atol) << endl;
Serial << close(result.y, expected.y, atol) << endl;
Serial << close(result.z, expected.z, atol) << endl;
// Make sure it satisfies the definition of `fromDirection()`.
Vec3f diff = result.rotate({0, 0, 1}) - vector.normalized();
Serial << close(diff.norm(), 0, atol) << endl;
}
void loop() {}
Dummy header file for Arduino builder.
Definition of Quaternion and EulerAngles.
Print & endl(Print &printer)
Definition: PrintStream.cpp:27
Print & boolalpha(Print &printer)
Definition: PrintStream.cpp:82
Setprecision setprecision(int n)
Type for quaternions of floating point numbers.
Definition: Quaternion.hpp:57
static Quaternion fromDirection(Vec3f v)
Calculate the quaternion that satisfies the following: result.rotate(Vec3f{0, 0, 1}) == v....
Definition: Quaternion.hpp:190
float w
Scalar (real) component.
Definition: Quaternion.hpp:58
float y
Second vector (imaginary) component .
Definition: Quaternion.hpp:60
Vec3f rotate(Vec3f v) const
Rotate vector by this quaternion.
Definition: Quaternion.hpp:146
float x
First vector (imaginary) component .
Definition: Quaternion.hpp:59
float z
Third vector (imaginary) component .
Definition: Quaternion.hpp:61
Type for 3D vectors of floating point numbers.
Definition: Vector.hpp:140
float norm() const
Norm.
Definition: Vector.hpp:219
Vec3f normalized() const
Normalize a copy of this vector (doesn't change the original vector).
Definition: Vector.hpp:227