Skip to content

Commit da2c0ed

Browse files
anishk85ja51d
andcommitted
TemperatureSensor: Add Rangefinder to TEMPx_SRC sensor source options
Enable Rangefinder as a TEMPx_SRC source so a dedicated temperature sensor (e.g. TSYS01 underwater) can override the rangefinder driver's temperature, used by Rover/Sub WATER_DEPTH MAVLink messages. Override is plumbed through AP_RangeFinder: a NaN-defaulted temperature_external on RangeFinder_State, a frontend set_temperature hook, and a backend get_temp() wrapper that returns the override when set and otherwise falls through to the driver's _get_temp() virtual. Pattern follows the existing convention in AP_TemperatureSensor_Backend (TEMP_SRC_ID=1 maps to the first instance via source_id-1). Co-authored-by: javid <namazovj@gmail.com>
1 parent 6493770 commit da2c0ed

9 files changed

Lines changed: 47 additions & 12 deletions

libraries/AP_RangeFinder/AP_RangeFinder.cpp

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -855,15 +855,18 @@ MAV_DISTANCE_SENSOR RangeFinder::get_mav_distance_sensor_type_orient(enum Rotati
855855
return backend->get_mav_distance_sensor_type();
856856
}
857857

858-
// get temperature reading in C. returns true on success and populates temp argument
859-
bool RangeFinder::get_temp(enum Rotation orientation, float &temp) const
858+
#if AP_TEMPERATURE_SENSOR_ENABLED
859+
// set temperature from an external source
860+
bool RangeFinder::set_temperature(uint8_t instance, float temperature)
860861
{
861-
AP_RangeFinder_Backend *backend = find_instance(orientation);
862-
if (backend == nullptr) {
862+
if (instance >= RANGEFINDER_MAX_INSTANCES) {
863863
return false;
864864
}
865-
return backend->get_temp(temp);
865+
866+
state[instance].temperature_external = temperature;
867+
return true;
866868
}
869+
#endif // AP_TEMPERATURE_SENSOR_ENABLED
867870

868871
#if HAL_LOGGING_ENABLED
869872
// Write an RFND (rangefinder) packet

libraries/AP_RangeFinder/AP_RangeFinder.h

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
#include <GCS_MAVLink/GCS_MAVLink.h>
2626
#include <AP_MSP/msp.h>
2727
#include "AP_RangeFinder_Params.h"
28+
#include <AP_TemperatureSensor/AP_TemperatureSensor_config.h>
2829

2930
// Maximum number of range finder instances available on this platform
3031
#ifndef RANGEFINDER_MAX_INSTANCES
@@ -228,6 +229,9 @@ class RangeFinder
228229
enum RangeFinder::Status status; // sensor status
229230
uint8_t range_valid_count; // number of consecutive valid readings (maxes out at 10)
230231
uint32_t last_reading_ms; // system time of last successful update from sensor
232+
#if AP_TEMPERATURE_SENSOR_ENABLED
233+
float temperature_external = nanf(""); // NaN means no external override
234+
#endif
231235

232236
const struct AP_Param::GroupInfo *var_info;
233237
};
@@ -314,8 +318,10 @@ class RangeFinder
314318
const Vector3f &get_pos_offset_orient(enum Rotation orientation) const;
315319
uint32_t last_reading_ms(enum Rotation orientation) const;
316320

317-
// get temperature reading in C. returns true on success and populates temp argument
318-
bool get_temp(enum Rotation orientation, float &temp) const;
321+
#if AP_TEMPERATURE_SENSOR_ENABLED
322+
// set temperature from an external source
323+
bool set_temperature(uint8_t instance, float temperature);
324+
#endif // AP_TEMPERATURE_SENSOR_ENABLED
319325

320326
/*
321327
set an externally estimated terrain height. Used to enable power

libraries/AP_RangeFinder/AP_RangeFinder_Backend.cpp

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,4 +93,16 @@ void AP_RangeFinder_Backend::get_state(RangeFinder::RangeFinder_State &state_arg
9393
}
9494
#endif
9595

96+
// get temperature reading in C. returns true on success and populates temp argument
97+
bool AP_RangeFinder_Backend::get_temp(float &temp) const
98+
{
99+
#if AP_TEMPERATURE_SENSOR_ENABLED
100+
if (!isnan(state.temperature_external)) {
101+
temp = state.temperature_external;
102+
return true;
103+
}
104+
#endif
105+
return _get_temp(temp);
106+
}
107+
96108
#endif // AP_RANGEFINDER_ENABLED

libraries/AP_RangeFinder/AP_RangeFinder_Backend.h

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,13 +77,15 @@ class AP_RangeFinder_Backend
7777
uint32_t last_reading_ms() const { return state.last_reading_ms; }
7878

7979
// get temperature reading in C. returns true on success and populates temp argument
80-
virtual bool get_temp(float &temp) const { return false; }
80+
bool get_temp(float &temp) const;
8181

8282
// return the actual type of the rangefinder, as opposed to the
8383
// parameter value which may be changed at runtime.
8484
RangeFinder::Type allocated_type() const { return _backend_type; }
8585

8686
protected:
87+
// get temperature reading in C. returns true on success and populates temp argument
88+
virtual bool _get_temp(float &temp) const { return false; }
8789

8890
// update status based on distance measurement
8991
void update_status(RangeFinder::RangeFinder_State &state_arg) const;

libraries/AP_RangeFinder/AP_RangeFinder_NMEA.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ bool AP_RangeFinder_NMEA::get_reading(float &reading_m)
5454
}
5555

5656
// get temperature reading
57-
bool AP_RangeFinder_NMEA::get_temp(float &temp) const
57+
bool AP_RangeFinder_NMEA::_get_temp(float &temp) const
5858
{
5959
uint32_t now_ms = AP_HAL::millis();
6060
if ((_temp_readtime_ms == 0) || ((now_ms - _temp_readtime_ms) > read_timeout_ms())) {

libraries/AP_RangeFinder/AP_RangeFinder_NMEA.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ class AP_RangeFinder_NMEA : public AP_RangeFinder_Backend_Serial
5656
bool get_reading(float &reading_m) override;
5757

5858
// get temperature reading in C. returns true on success and populates temp argument
59-
bool get_temp(float &temp) const override;
59+
bool _get_temp(float &temp) const override;
6060

6161
uint16_t read_timeout_ms() const override { return 3000; }
6262

libraries/AP_TemperatureSensor/AP_TemperatureSensor_Backend.cpp

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
#include <AP_Logger/AP_Logger.h>
2222
#include <AP_BattMonitor/AP_BattMonitor.h>
2323
#include <AP_Servo_Telem/AP_Servo_Telem.h>
24+
#include <AP_RangeFinder/AP_RangeFinder.h>
2425

2526
/*
2627
All backends use the same parameter table and set of indices. Therefore, two
@@ -138,6 +139,16 @@ void AP_TemperatureSensor_Backend::update_external_libraries(const float tempera
138139
break;
139140
#endif // AP_SERVO_TELEM_ENABLED
140141

142+
#if AP_RANGEFINDER_ENABLED
143+
case AP_TemperatureSensor_Params::Source::Rangefinder: {
144+
auto *rangefinder = AP::rangefinder();
145+
if (rangefinder != nullptr) {
146+
rangefinder->set_temperature(_params.source_id-1, temperature);
147+
}
148+
break;
149+
}
150+
#endif
151+
141152
case AP_TemperatureSensor_Params::Source::None:
142153
case AP_TemperatureSensor_Params::Source::Pitot_tube:
143154
default:

libraries/AP_TemperatureSensor/AP_TemperatureSensor_Params.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -58,13 +58,13 @@ const AP_Param::GroupInfo AP_TemperatureSensor_Params::var_info[] = {
5858
// @Param: SRC
5959
// @DisplayName: Sensor Source
6060
// @Description: Sensor Source is used to designate which device's temperature report will be replaced by this temperature sensor's data. If 0 (None) then the data is only available via log. In the future a new Motor temperature report will be created for returning data directly.
61-
// @Values: 0: None, 1:ESC, 2:Motor, 3:Battery Index, 4:Battery ID/SerialNumber, 5:CAN based Pitot tube, 6:DroneCAN-out on AP_Periph, 7:Servo motor, 8:Servo PCB
61+
// @Values: 0: None, 1:ESC, 2:Motor, 3:Battery Index, 4:Battery ID/SerialNumber, 5:CAN based Pitot tube, 6:DroneCAN-out on AP_Periph, 7:Servo motor, 8:Servo PCB, 9:Rangefinder
6262
// @User: Standard
6363
AP_GROUPINFO("SRC", 4, AP_TemperatureSensor_Params, source, (float)Source::None),
6464

6565
// @Param: SRC_ID
6666
// @DisplayName: Sensor Source Identification
67-
// @Description: Sensor Source Identification is used to replace a specific instance of a system component's temperature report with the temp sensor's. Examples: TEMP_SRC = 1 (ESC), TEMP_SRC_ID = 1 will set the temp of ESC1. TEMP_SRC = 3 (BatteryIndex),TEMP_SRC_ID = 2 will set the temp of BATT2. TEMP_SRC = 4 (BatteryId/SerialNum),TEMP_SRC_ID=42 will set the temp of all batteries that have param BATTn_SERIAL = 42.
67+
// @Description: Sensor Source Identification is used to replace a specific instance of a system component's temperature report with the temp sensor's. Examples: TEMP_SRC = 1 (ESC), TEMP_SRC_ID = 1 will set the temp of ESC1. TEMP_SRC = 3 (BatteryIndex),TEMP_SRC_ID = 2 will set the temp of BATT2. TEMP_SRC = 4 (BatteryId/SerialNum),TEMP_SRC_ID=42 will set the temp of all batteries that have param BATTn_SERIAL = 42. TEMP_SRC = 9 (Rangefinder),TEMP_SRC_ID = 1 will set the temp of the first rangefinder instance (RNGFND1).
6868
AP_GROUPINFO("SRC_ID", 5, AP_TemperatureSensor_Params, source_id, AP_TEMPERATURE_SENSOR_SOURCE_ID_DEFAULT),
6969

7070
AP_GROUPEND

libraries/AP_TemperatureSensor/AP_TemperatureSensor_Params.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@ class AP_TemperatureSensor_Params {
5050
DroneCAN = 6,
5151
Servo_Motor = 7,
5252
Servo_PCB = 8,
53+
Rangefinder = 9,
5354
};
5455

5556
AP_Enum<Type> type; // 0=disabled, others see frontend enum TYPE

0 commit comments

Comments
 (0)