ifm3d
struct_imu_info_v1.hpp
1 // -*- c++ -*-
2 /*
3  * Copyright 2025-present ifm electronic, gmbh
4  * SPDX-License-Identifier: Apache-2.0
5  */
6 
7 #ifndef IFM3D_DESERIALIZE_STRUCT_IMU_INFO_V1_HPP
8 #define IFM3D_DESERIALIZE_STRUCT_IMU_INFO_V1_HPP
9 
10 #include <array>
11 #include <ifm3d/deserialize/deserialize_utils.hpp>
12 #include <ifm3d/deserialize/struct_calibration.hpp>
13 #include <ifm3d/device/device.h>
14 #include <ifm3d/device/err.h>
15 #include <ifm3d/fg/buffer.h>
16 #include <ifm3d/fg/organizer_utils.h>
17 
18 namespace ifm3d
19 {
20  namespace
21  {
22  constexpr auto IMU_INFO_IMU_VERSION_INDEX = 0x0000;
23  constexpr auto IMU_INFO_IMU_SAMPLES_INDEX = 0x0004;
24  constexpr auto IMU_INFO_NUM_SAMPLE_INDEX = 0x1304;
25  constexpr auto IMU_INFO_EXTRINSIC_IMU_TO_USER_INDEX = 0x1308;
26  constexpr auto IMU_INFO_EXTRINSIC_IMU_TO_VPU_INDEX = 0x1320;
27  constexpr auto IMU_INFO_IMU_FIFO_RCV_TIMESTAMP_INDEX = 0x1338;
28  constexpr auto ARRAY_SIZE_IMU_SAMPLES = 128;
29  };
30 
32  class IMUInfoV1
33  {
34  public:
35  using Ptr = std::shared_ptr<IMUInfoV1>;
36 
37  bool
38  IsValid(const uint8_t*, size_t size)
39  {
40  return size >= IMU_INFO_V1_SIZE;
41  }
42 
43  void
44  Read(const uint8_t* start_ptr, size_t size)
45  {
46  if (!IsValid(start_ptr, size))
47  {
48  throw ifm3d::Error(IFM3D_CORRUPTED_STRUCT);
49  }
50 
51  imu_version =
52  mkval<std::uint32_t>(start_ptr + IMU_INFO_IMU_VERSION_INDEX);
53 
54  for (std::uint32_t i = 0; i < ARRAY_SIZE_IMU_SAMPLES; ++i)
55  {
56  constexpr size_t IMU_SAMPLE_BYTE_SIZE =
57  sizeof(std::uint16_t) + sizeof(std::uint64_t) + 7 * sizeof(float);
58 
59  static_assert(sizeof(calibration::IMUSample) >= IMU_SAMPLE_BYTE_SIZE,
60  "IMUSample struct too small for serialized data");
61 
62  imu_samples[i].Read(start_ptr + IMU_INFO_IMU_SAMPLES_INDEX +
63  i * IMU_SAMPLE_BYTE_SIZE);
64  }
65 
66  num_samples =
67  mkval<std::uint32_t>(start_ptr + IMU_INFO_NUM_SAMPLE_INDEX);
68 
69  extrinsic_imu_to_user.Read(start_ptr +
70  IMU_INFO_EXTRINSIC_IMU_TO_USER_INDEX);
71 
72  extrinsic_imu_to_vpu.Read(start_ptr +
73  IMU_INFO_EXTRINSIC_IMU_TO_VPU_INDEX);
74 
75  imu_fifo_rcv_timestamp = mkval<std::uint64_t>(
76  start_ptr + IMU_INFO_IMU_FIFO_RCV_TIMESTAMP_INDEX);
77  };
78 
79  /*@brief Version of the IMU_INFO data*/
80  std::uint32_t imu_version{};
81  /*@brief Array containing IMU samples data*/
82  std::array<calibration::IMUSample, ARRAY_SIZE_IMU_SAMPLES> imu_samples;
83  /*@brief Number of valid samples in imu_samples array*/
84  std::uint32_t num_samples{};
85  /*@brief Extrinsic calibration for converting between IMU and User
86  * coordinates (combines both, i.e., IMUToVPU and VPUToUser, extrinsic
87  * calibrations)*/
88  calibration::AlgoExtrinsicCalibration extrinsic_imu_to_user;
89  /*@brief Extrinsic calibration for converting between IMU and User
90  * coordinates (only the IMUToVPU calibration)*/
91  calibration::AlgoExtrinsicCalibration extrinsic_imu_to_vpu;
92  /*@brief The receive timestamp of the IMU FIFO buffer. This is taken as
93  * soon as possible after the data has been arrived and might be used for
94  * manual synchronization of the IMU's hardware timestamps.*/
95  std::uint64_t imu_fifo_rcv_timestamp{};
96 
97  /*@brief Size of IMU_INFO in bytes*/
98  static constexpr size_t IMU_INFO_V1_SIZE = 0x1340;
99 
100  static IMUInfoV1
101  Deserialize(const Buffer& imu_info_buffer)
102  {
103  IMUInfoV1 imu_info_v1{};
104 
105  imu_info_v1.Read(imu_info_buffer.Ptr<uint8_t>(0),
106  imu_info_buffer.Size());
107  return imu_info_v1;
108  }
109  };
110 } // end namespace ifm3d
111 
112 #endif // IFM3D_DESERIALIZE_STRUCT_IMU_INFO_V1_HPP
The class Buffer represent a STL container to store data from the ifm devices in 2 dimension and supp...
Definition: buffer.h:99
T * Ptr(std::uint32_t row)
returns a pointer to the specified Buffer row.
size_t Size() const
Return the size of the buffer in bytes.
Exception wrapper for library and system errors encountered by the library.
Definition: err.h:115
Definition: struct_imu_info_v1.hpp:33
All items are given in SI units, i.e.
Definition: struct_calibration.hpp:24
All items are given in SI units, i.e.
Definition: struct_calibration.hpp:83