From 5483d901f91f202e52f2aa1fba00e06fbf6fff47 Mon Sep 17 00:00:00 2001 From: TedObrien Date: Sat, 29 Mar 2025 11:07:30 +0000 Subject: [PATCH] MPC9808: Run ScheduleOnInterval() at desired publishing rate and remove elapsed time check and timestamp_sample field from message as no longer needed. MCP9808: Replaced PX4_INFO with PX4_DEBUG MCP9808: Update date in headers MCP9808: Define functions before variables MCP9808: Increase logging interval for sensor_temp MCP9808: Removed extra space MCP9808: Remove this-> --- msg/SensorTemp.msg | 2 -- .../temperature_sensor/mcp9808/CMakeLists.txt | 2 +- .../temperature_sensor/mcp9808/mcp9808.cpp | 28 ++++++++----------- .../temperature_sensor/mcp9808/mcp9808.h | 9 +++--- .../mcp9808/mcp9808_main.cpp | 4 +-- .../mcp9808/mcp9808_params.c | 2 +- src/modules/logger/logged_topics.cpp | 2 +- 7 files changed, 20 insertions(+), 29 deletions(-) diff --git a/msg/SensorTemp.msg b/msg/SensorTemp.msg index 303fb5ba42..2837857ef1 100644 --- a/msg/SensorTemp.msg +++ b/msg/SensorTemp.msg @@ -1,6 +1,4 @@ uint64 timestamp # time since system start (microseconds) -uint64 timestamp_sample # Time at which measurement was taken uint32 device_id # unique device ID for the sensor that does not change between power cycles - float32 temperature # Temperature provided by sensor (Celsius) diff --git a/src/drivers/temperature_sensor/mcp9808/CMakeLists.txt b/src/drivers/temperature_sensor/mcp9808/CMakeLists.txt index dd8325b7c4..108858606b 100644 --- a/src/drivers/temperature_sensor/mcp9808/CMakeLists.txt +++ b/src/drivers/temperature_sensor/mcp9808/CMakeLists.txt @@ -1,6 +1,6 @@ ############################################################################ # -# Copyright (c) 2024 PX4 Development Team. All rights reserved. +# Copyright (c) 2025 PX4 Development Team. All rights reserved. # # Redistribution and use in source and binary forms, with or without # modification, are permitted provided that the following conditions diff --git a/src/drivers/temperature_sensor/mcp9808/mcp9808.cpp b/src/drivers/temperature_sensor/mcp9808/mcp9808.cpp index f211510fdb..a6a997e9a2 100644 --- a/src/drivers/temperature_sensor/mcp9808/mcp9808.cpp +++ b/src/drivers/temperature_sensor/mcp9808/mcp9808.cpp @@ -1,6 +1,6 @@ /**************************************************************************** * - * Copyright (c) 2024 PX4 Development Team. All rights reserved. + * Copyright (c) 2025 PX4 Development Team. All rights reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions @@ -39,7 +39,7 @@ MCP9808::MCP9808(const I2CSPIDriverConfig &config) : _cycle_perf(perf_alloc(PC_ELAPSED, MODULE_NAME": single-sample")), _comms_errors(perf_alloc(PC_COUNT, MODULE_NAME": comms errors")) { - _sensor_temp.device_id = this->get_device_id(); + _sensor_temp.device_id = get_device_id(); } MCP9808::~MCP9808() @@ -53,23 +53,17 @@ void MCP9808::RunImpl() { perf_begin(_cycle_perf); - // publish at around 5HZ - if ((hrt_elapsed_time(&measurement_time)) > 200_ms) { + float temperature = read_temperature(); - float temperature = read_temperature(); - measurement_time = hrt_absolute_time(); // get the time the measurement was taken + if (std::isnan(temperature)) { - if (std::isnan(temperature)) { + perf_count(_comms_errors); - perf_count(_comms_errors); + } else { + _sensor_temp.timestamp = hrt_absolute_time(); + _sensor_temp.temperature = temperature; - } else { - _sensor_temp.timestamp = hrt_absolute_time(); - _sensor_temp.timestamp_sample = measurement_time; - _sensor_temp.temperature = temperature; - - _sensor_temp_pub.publish(_sensor_temp); - } + _sensor_temp_pub.publish(_sensor_temp); } perf_end(_cycle_perf); @@ -109,7 +103,7 @@ int MCP9808::init() return ret; } - PX4_INFO("I2C initialized successfully"); + PX4_DEBUG("I2C initialized successfully"); ret = write_reg(MCP9808_REG_CONFIG, 0x0000); // Ensure default configuration @@ -120,7 +114,7 @@ int MCP9808::init() _sensor_temp_pub.advertise(); - ScheduleOnInterval(100_ms); // Sample at 10 Hz + ScheduleOnInterval(200_ms); // Sample at 5 Hz return PX4_OK; } diff --git a/src/drivers/temperature_sensor/mcp9808/mcp9808.h b/src/drivers/temperature_sensor/mcp9808/mcp9808.h index ef2e561d7c..a13d5c46be 100644 --- a/src/drivers/temperature_sensor/mcp9808/mcp9808.h +++ b/src/drivers/temperature_sensor/mcp9808/mcp9808.h @@ -1,6 +1,6 @@ /**************************************************************************** * - * Copyright (c) 2024 PX4 Development Team. All rights reserved. + * Copyright (c) 2025 PX4 Development Team. All rights reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions @@ -66,11 +66,10 @@ protected: private: uORB::PublicationMulti _sensor_temp_pub{ORB_ID(sensor_temp)}; - perf_counter_t _cycle_perf; - perf_counter_t _comms_errors; - sensor_temp_s _sensor_temp{}; int read_reg(uint8_t address, uint16_t &data); int write_reg(uint8_t address, uint16_t value); float read_temperature(); - hrt_abstime measurement_time = 0; + sensor_temp_s _sensor_temp{}; + perf_counter_t _cycle_perf; + perf_counter_t _comms_errors; }; diff --git a/src/drivers/temperature_sensor/mcp9808/mcp9808_main.cpp b/src/drivers/temperature_sensor/mcp9808/mcp9808_main.cpp index 445a94a433..57cccaeaea 100644 --- a/src/drivers/temperature_sensor/mcp9808/mcp9808_main.cpp +++ b/src/drivers/temperature_sensor/mcp9808/mcp9808_main.cpp @@ -1,6 +1,6 @@ /**************************************************************************** * - * Copyright (c) 2024 PX4 Development Team. All rights reserved. + * Copyright (c) 2025 PX4 Development Team. All rights reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions @@ -35,7 +35,7 @@ * @file mcp9808_main.cpp * @author TedObrien * - * Driver for the Microchip MCP9080 Temperature Sensor connected via I2C. + * Driver for the Microchip MCP9080 Temperature Sensor connected via I2C. */ #include "mcp9808.h" diff --git a/src/drivers/temperature_sensor/mcp9808/mcp9808_params.c b/src/drivers/temperature_sensor/mcp9808/mcp9808_params.c index 285e284da7..8dc0b5241c 100644 --- a/src/drivers/temperature_sensor/mcp9808/mcp9808_params.c +++ b/src/drivers/temperature_sensor/mcp9808/mcp9808_params.c @@ -1,6 +1,6 @@ /**************************************************************************** * - * Copyright (c) 2024 PX4 Development Team. All rights reserved. + * Copyright (c) 2025 PX4 Development Team. All rights reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions diff --git a/src/modules/logger/logged_topics.cpp b/src/modules/logger/logged_topics.cpp index 033209150a..5ab67745f1 100644 --- a/src/modules/logger/logged_topics.cpp +++ b/src/modules/logger/logged_topics.cpp @@ -123,7 +123,7 @@ void LoggedTopics::add_default_topics() add_optional_topic("sensor_gyro_fft", 50); add_topic("sensor_selection"); add_topic("sensors_status_imu", 200); - add_optional_topic("sensor_temp", 10); + add_optional_topic("sensor_temp", 100); add_optional_topic("spoilers_setpoint", 1000); add_topic("system_power", 500); add_optional_topic("takeoff_status", 1000);