diff --git a/boards/modalai/voxl2/scripts/install-voxl.sh b/boards/modalai/voxl2/scripts/install-voxl.sh index e3ba736a3b..e4c6af02bd 100755 --- a/boards/modalai/voxl2/scripts/install-voxl.sh +++ b/boards/modalai/voxl2/scripts/install-voxl.sh @@ -132,6 +132,7 @@ adb shell "cd /usr/bin; /bin/ln -f -s px4 px4-apps_sbus" adb shell "cd /usr/bin; /bin/ln -f -s px4 px4-voxl_save_cal_params" adb shell "cd /usr/bin; /bin/ln -f -s px4 px4-vehicle_air_data_bridge" adb shell "cd /usr/bin; /bin/ln -f -s px4 px4-sensor_baro_bridge" +adb shell "cd /usr/bin; /bin/ln -f -s px4 px4-sensor_imu_raw_bridge" adb shell "cd /usr/bin; /bin/ln -f -s px4 px4-dps310" adb shell "cd /usr/bin; /bin/ln -f -s px4 px4-icp101xx" adb shell "cd /usr/bin; /bin/ln -f -s px4 px4-vehicle_local_position_bridge" diff --git a/boards/modalai/voxl2/src/CMakeLists.txt b/boards/modalai/voxl2/src/CMakeLists.txt index 43ea73dbf5..ed492407d2 100644 --- a/boards/modalai/voxl2/src/CMakeLists.txt +++ b/boards/modalai/voxl2/src/CMakeLists.txt @@ -109,4 +109,5 @@ elseif("${PX4_PLATFORM}" STREQUAL "posix") add_subdirectory(${PX4_BOARD_DIR}/src/modules/sensor_baro_bridge sensor_baro_bridge) add_subdirectory(${PX4_BOARD_DIR}/src/modules/vehicle_local_position_bridge vehicle_local_position_bridge) add_subdirectory(${PX4_BOARD_DIR}/src/modules/sih_vio_bridge sih_vio_bridge) + add_subdirectory(${PX4_BOARD_DIR}/src/modules/sensor_imu_raw_bridge sensor_imu_raw_bridge) endif() diff --git a/boards/modalai/voxl2/src/modules/sensor_imu_raw_bridge/CMakeLists.txt b/boards/modalai/voxl2/src/modules/sensor_imu_raw_bridge/CMakeLists.txt new file mode 100644 index 0000000000..2a8a3783b4 --- /dev/null +++ b/boards/modalai/voxl2/src/modules/sensor_imu_raw_bridge/CMakeLists.txt @@ -0,0 +1,43 @@ +############################################################################ +# +# Copyright (c) 2025 ModalAI, Inc. All rights reserved. +# +# Redistribution and use in source and binary forms, with or without +# modification, are permitted provided that the following conditions +# are met: +# +# 1. Redistributions of source code must retain the above copyright +# notice, this list of conditions and the following disclaimer. +# 2. Redistributions in binary form must reproduce the above copyright +# notice, this list of conditions and the following disclaimer in +# the documentation and/or other materials provided with the +# distribution. +# 3. Neither the name PX4 nor the names of its contributors may be +# used to endorse or promote products derived from this software +# without specific prior written permission. +# +# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS +# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT +# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS +# FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE +# COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, +# INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, +# BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS +# OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED +# AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT +# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN +# ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE +# POSSIBILITY OF SUCH DAMAGE. +# +############################################################################ + +px4_add_module( + MODULE modules__sensor_imu_raw_bridge + MAIN sensor_imu_raw_bridge + INCLUDES + ${CMAKE_CURRENT_SOURCE_DIR}/../../lib/mpa + SRCS + sensor_imu_raw_bridge.cpp + DEPENDS + mpa + ) diff --git a/boards/modalai/voxl2/src/modules/sensor_imu_raw_bridge/sensor_imu_raw_bridge.cpp b/boards/modalai/voxl2/src/modules/sensor_imu_raw_bridge/sensor_imu_raw_bridge.cpp new file mode 100644 index 0000000000..1b6bdcb0a1 --- /dev/null +++ b/boards/modalai/voxl2/src/modules/sensor_imu_raw_bridge/sensor_imu_raw_bridge.cpp @@ -0,0 +1,201 @@ +/**************************************************************************** + * + * Copyright (c) 2025 ModalAI, inc. All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in + * the documentation and/or other materials provided with the + * distribution. + * 3. Neither the name PX4 nor the names of its contributors may be + * used to endorse or promote products derived from this software + * without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS + * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE + * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, + * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, + * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS + * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED + * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN + * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE + * POSSIBILITY OF SUCH DAMAGE. + * + ****************************************************************************/ +#include "mpa.hpp" +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +class SensorImuRawBridge : public ModuleBase, public px4::WorkItem +{ +public: + static Descriptor desc; + + SensorImuRawBridge(); + ~SensorImuRawBridge() override = default; + + /** @see ModuleBase */ + static int task_spawn(int argc, char *argv[]); + + /** @see ModuleBase */ + static int custom_command(int argc, char *argv[]); + + /** @see ModuleBase */ + static int print_usage(const char *reason = nullptr); + + bool init(); + +private: + void Run() override; + + // Subscribe to raw accel topic (triggers the work item) + uORB::SubscriptionCallbackWorkItem _sensor_accel_sub{this, ORB_ID(sensor_accel)}; + + // Poll the raw gyro topic on each accel callback + uORB::Subscription _sensor_gyro_sub{ORB_ID(sensor_gyro)}; + + int imu_raw_pipe_ch{0}; + +}; + +ModuleBase::Descriptor SensorImuRawBridge::desc{task_spawn, custom_command, print_usage}; + +SensorImuRawBridge::SensorImuRawBridge() : + WorkItem(MODULE_NAME, px4::wq_configurations::nav_and_controllers) +{ +} + +bool SensorImuRawBridge::init() +{ + if (MPA::Initialize() == -1) { + PX4_ERR("MPA init failed"); + return false; + } + + char imu_raw_pipe_name[] = "px4_sensor_imu_raw"; + imu_raw_pipe_ch = MPA::PipeCreate(imu_raw_pipe_name); + + if (imu_raw_pipe_ch == -1) { + PX4_ERR("Pipe create failed for %s", imu_raw_pipe_name); + return false; + } + + if (!_sensor_accel_sub.registerCallback()) { + PX4_ERR("callback registration failed"); + return false; + } + + return true; +} + +void SensorImuRawBridge::Run() +{ + if (should_exit()) { + _sensor_accel_sub.unregisterCallback(); + exit_and_cleanup(desc); + return; + } + + sensor_accel_s accel_data; + + if (_sensor_accel_sub.update(&accel_data)) { + + imu_data_t imu; + memset(&imu, 0, sizeof(imu)); + imu.magic_number = IMU_MAGIC_NUMBER; + + // Raw acceleration (m/s^2) - straight from the driver, no TC applied + imu.accl_ms2[0] = accel_data.x; + imu.accl_ms2[1] = accel_data.y; + imu.accl_ms2[2] = accel_data.z; + + // Temperature from accel sensor + imu.temp_c = accel_data.temperature; + + // Raw angular velocity (rad/s) + sensor_gyro_s gyro_data; + + if (_sensor_gyro_sub.update(&gyro_data)) { + imu.gyro_rad[0] = gyro_data.x; + imu.gyro_rad[1] = gyro_data.y; + imu.gyro_rad[2] = gyro_data.z; + } + + imu.timestamp_ns = accel_data.timestamp * 1000; // Convert µs to ns + + if (MPA::PipeWrite(imu_raw_pipe_ch, (void *)&imu, sizeof(imu_data_t)) == -1) { + PX4_ERR("Pipe %d write failed!", imu_raw_pipe_ch); + } + } +} + +int SensorImuRawBridge::custom_command(int argc, char *argv[]) +{ + return print_usage("unknown command"); +} + +int SensorImuRawBridge::task_spawn(int argc, char *argv[]) +{ + SensorImuRawBridge *instance = new SensorImuRawBridge(); + + if (instance) { + desc.object.store(instance); + desc.task_id = task_id_is_work_queue; + + if (instance->init()) { + return PX4_OK; + } + + } else { + PX4_ERR("alloc failed"); + } + + delete instance; + desc.object.store(nullptr); + desc.task_id = -1; + + return PX4_ERROR; +} + +int SensorImuRawBridge::print_usage(const char *reason) +{ + if (reason) { + PX4_WARN("%s\n", reason); + } + + PRINT_MODULE_DESCRIPTION( + R"DESCR_STR( +### Description +Raw sensor IMU bridge. Publishes raw accel and gyro data from PX4 +to the apps processor via MPA pipe. No temperature compensation +or calibration offsets are applied. + +)DESCR_STR"); + + PRINT_MODULE_USAGE_NAME("sensor_imu_raw_bridge", "system"); + PRINT_MODULE_USAGE_COMMAND("start"); + PRINT_MODULE_USAGE_DEFAULT_COMMANDS(); + + return 0; +} + +extern "C" __EXPORT int sensor_imu_raw_bridge_main(int argc, char *argv[]) +{ + return ModuleBase::main(SensorImuRawBridge::desc, argc, argv); +} diff --git a/boards/modalai/voxl2/target/voxl-px4-hitl-start b/boards/modalai/voxl2/target/voxl-px4-hitl-start index 08a73bd742..61fd15e602 100755 --- a/boards/modalai/voxl2/target/voxl-px4-hitl-start +++ b/boards/modalai/voxl2/target/voxl-px4-hitl-start @@ -102,3 +102,5 @@ logger start -t -b 256 /bin/sleep 1 mavlink boot_complete + +sensor_imu_raw_bridge start diff --git a/boards/modalai/voxl2/target/voxl-px4-start b/boards/modalai/voxl2/target/voxl-px4-start index ec1eb73342..0f761b37a5 100755 --- a/boards/modalai/voxl2/target/voxl-px4-start +++ b/boards/modalai/voxl2/target/voxl-px4-start @@ -266,6 +266,7 @@ dataman start navigator start vehicle_air_data_bridge start sensor_baro_bridge start +sensor_imu_raw_bridge start vehicle_local_position_bridge start # Start uxrce_dds_client for ros2 offboard messages from agent over localhost