212 {
213 std::cout << "[UnitTest] Starting AHRS unit test...\n";
214
215
216 quat_ = {1.0f, 0.0f, 0.0f, 0.0f};
217 accel_ = {0.01f, 0.01f, 1.0f};
218 gyro_ = {0.0f, 0.0f, 0.0f};
219
220 auto current_time = std::chrono::duration_cast<std::chrono::microseconds>(
221 std::chrono::system_clock::now().time_since_epoch());
222
223 start_ = now_ = last_wakeup_ = current_time;
224 dt_ = 0.001f;
225
226 auto t0 = std::chrono::high_resolution_clock::now();
227
228
229 auto t1 = std::chrono::high_resolution_clock::now();
230 Update();
231 auto t2 = std::chrono::high_resolution_clock::now();
232
233
234 GetEulr();
235 auto t3 = std::chrono::high_resolution_clock::now();
236
237
238 std::cout << "[UnitTest] Quaternion: ";
239 std::cout << std::format(
240 "[q0={:+.4f}, q1={:+.4f}, q2={:+.4f}, q3={:+.4f}]\n", quat_.q0,
241 quat_.q1, quat_.q2, quat_.q3);
242
243 std::cout << "[UnitTest] Euler Angles (rad): ";
244 std::cout << std::format("[rol={:+.4f}, pit={:+.4f}, yaw={:+.4f}]\n",
247
248 auto t4 = std::chrono::high_resolution_clock::now();
249
250
251 auto normalize_angle = [](float rad) -> float {
252 while (rad > M_PI) rad -= 2.0f * M_PI;
253 while (rad < -M_PI) rad += 2.0f * M_PI;
254 return rad;
255 };
256
257 float norm_rol = normalize_angle(eulr_.rol.
Value());
258 float norm_pit = normalize_angle(eulr_.pit.
Value());
259 float norm_yaw = normalize_angle(eulr_.yaw.
Value());
260
261 const float tolerance = 0.1f;
262
263 if (std::abs(norm_rol) < tolerance && std::abs(norm_pit) < tolerance &&
264 std::abs(norm_yaw) < tolerance) {
265 std::cout
266 << "[UnitTest] ✅ Test Passed: Orientation is correct at rest.\n";
267 } else {
268 std::cout << "[UnitTest] ❌ Test Failed: Unexpected orientation.\n";
269 }
270
271 auto t5 = std::chrono::high_resolution_clock::now();
272
273
274 std::cout << std::format(
275 "[Timing] Init & assignment : {:>8.2f} µs\n",
276 std::chrono::duration<float, std::micro>(t1 - t0).count());
277 std::cout << std::format(
278 "[Timing] Update() : {:>8.2f} µs\n",
279 std::chrono::duration<float, std::micro>(t2 - t1).count());
280 std::cout << std::format(
281 "[Timing] GetEulr() : {:>8.2f} µs\n",
282 std::chrono::duration<float, std::micro>(t3 - t2).count());
283 std::cout << std::format(
284 "[Timing] Output : {:>8.2f} µs\n",
285 std::chrono::duration<float, std::micro>(t4 - t3).count());
286 std::cout << std::format(
287 "[Timing] Verify : {:>8.2f} µs\n",
288 std::chrono::duration<float, std::micro>(t5 - t4).count());
289 std::cout << std::format(
290 "[Timing] Total : {:>8.2f} µs\n",
291 std::chrono::duration<float, std::micro>(t5 - t0).count());
292 }