Program Listing for File imu_diag_data_provider.cpp

Return to documentation for file (src/imu_diag_data_provider.cpp)

/*******************************************************************************
 *   @file   imu_diag_data_provider.cpp
 *   @brief  Implementation for providing diagnosis data for adis.
 *   @author Vasile Holonec (Vasile.Holonec@analog.com)
*******************************************************************************/
// Copyright 2023 Analog Devices, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//     http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

#include "adi_imu/imu_diag_data_provider.h"

#include <rclcpp/rclcpp.hpp>

namespace adi_imu
{

ImuDiagDataProvider::ImuDiagDataProvider() {}

ImuDiagDataProvider::~ImuDiagDataProvider() {}

bool ImuDiagDataProvider::getData(adi_imu::msg::ImuDiagDataADIS1646X & message)
{
  if (!populateCommonFields(message)) return false;

  if (!m_iio_wrapper.diag_standby_mode(message.diag_standby_mode)) return false;

  return true;
}

bool ImuDiagDataProvider::getData(adi_imu::msg::ImuDiagDataADIS1647X & message)
{
  if (!populateCommonFields(message)) return false;

  if (!m_iio_wrapper.diag_standby_mode(message.diag_standby_mode)) return false;

  return true;
}

bool ImuDiagDataProvider::getData(adi_imu::msg::ImuDiagDataADIS1650X & message)
{
  if (!populateCommonFields(message)) return false;

  if (!m_iio_wrapper.diag_standby_mode(message.diag_standby_mode)) return false;

  if (!m_iio_wrapper.diag_acceleration_self_test_error(message.diag_acceleration_self_test_error))
    return false;

  if (!m_iio_wrapper.diag_gyroscope1_self_test_error(message.diag_gyroscope1_self_test_error))
    return false;

  if (!m_iio_wrapper.diag_gyroscope2_self_test_error(message.diag_gyroscope2_self_test_error))
    return false;

  return true;
}

bool ImuDiagDataProvider::getData(adi_imu::msg::ImuDiagDataADIS1654X & message)
{
  if (!populateCommonFields(message)) return false;

  if (!populateAxisFailureFields(message)) return false;

  if (!m_iio_wrapper.diag_automatic_reset(message.diag_automatic_reset)) return false;

  if (!m_iio_wrapper.diag_crc_error(message.diag_crc_error)) return false;

  return true;
}

bool ImuDiagDataProvider::getData(adi_imu::msg::ImuDiagDataADIS1655X & message)
{
  if (!populateCommonFields(message)) return false;

  if (!populateAxisFailureFields(message)) return false;

  if (!m_iio_wrapper.diag_automatic_reset(message.diag_automatic_reset)) return false;

  if (!m_iio_wrapper.diag_crc_error(message.diag_crc_error)) return false;

  return true;
}

bool ImuDiagDataProvider::getData(adi_imu::msg::ImuDiagDataADIS1657X & message)
{
  if (!populateCommonFields(message)) return false;

  if (!populateAxisFailureFields(message)) return false;

  if (!m_iio_wrapper.diag_standby_mode(message.diag_standby_mode)) return false;

  if (!m_iio_wrapper.diag_aduc_mcu_fault(message.diag_aduc_mcu_fault)) return false;

  if (!m_iio_wrapper.diag_sensor_initialization_failure(message.diag_sensor_initialization_failure))
    return false;

  return true;
}

template <typename MessageType>
bool ImuDiagDataProvider::populateCommonFields(MessageType & message)
{
  // Set common header
  message.header.frame_id = "imudiagdata";

  // Universal fields present in ALL ADIS device families
  if (!m_iio_wrapper.diag_clock_error(message.diag_clock_error)) return false;
  if (!m_iio_wrapper.diag_data_path_overrun(message.diag_data_path_overrun)) return false;
  if (!m_iio_wrapper.diag_flash_memory_test_error(message.diag_flash_memory_test_error))
    return false;
  if (!m_iio_wrapper.diag_flash_memory_update_error(message.diag_flash_memory_update_error))
    return false;
  if (!m_iio_wrapper.diag_flash_memory_write_count_exceeded_error(
        message.diag_flash_memory_write_count_exceeded_error))
    return false;
  if (!m_iio_wrapper.diag_sensor_self_test_error(message.diag_sensor_self_test_error)) return false;
  if (!m_iio_wrapper.diag_spi_communication_error(message.diag_spi_communication_error))
    return false;
  if (!m_iio_wrapper.flash_counter(message.flash_counter)) return false;

  return true;
}

// Helper method for axis failure fields (1654X, 1655X, 1657X)
template <typename MessageType>
bool ImuDiagDataProvider::populateAxisFailureFields(MessageType & message)
{
  if (!m_iio_wrapper.diag_x_axis_accelerometer_failure(message.diag_x_axis_accelerometer_failure))
    return false;
  if (!m_iio_wrapper.diag_y_axis_accelerometer_failure(message.diag_y_axis_accelerometer_failure))
    return false;
  if (!m_iio_wrapper.diag_z_axis_accelerometer_failure(message.diag_z_axis_accelerometer_failure))
    return false;

  if (!m_iio_wrapper.diag_x_axis_gyroscope_failure(message.diag_x_axis_gyroscope_failure))
    return false;
  if (!m_iio_wrapper.diag_y_axis_gyroscope_failure(message.diag_y_axis_gyroscope_failure))
    return false;
  if (!m_iio_wrapper.diag_z_axis_gyroscope_failure(message.diag_z_axis_gyroscope_failure))
    return false;

  return true;
}

}  // namespace adi_imu