Skip to content

Commit eab8e56

Browse files
committed
CollisionPrevention/SF45: move common projection function to library
1 parent 673d58d commit eab8e56

File tree

8 files changed

+117
-31
lines changed

8 files changed

+117
-31
lines changed

src/drivers/distance_sensor/lightware_sf45_serial/CMakeLists.txt

+1
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ px4_add_module(
4141
DEPENDS
4242
drivers_rangefinder
4343
px4_work_queue
44+
CollisionPrevention
4445
MODULE_CONFIG
4546
module.yaml
4647
)

src/drivers/distance_sensor/lightware_sf45_serial/lightware_sf45_serial.cpp

+5-16
Original file line numberDiff line numberDiff line change
@@ -33,14 +33,15 @@
3333

3434
#include "lightware_sf45_serial.hpp"
3535

36-
#include <inttypes.h>
3736
#include <fcntl.h>
37+
#include <float.h>
38+
#include <inttypes.h>
3839
#include <termios.h>
40+
3941
#include <lib/crc/crc.h>
4042
#include <lib/mathlib/mathlib.h>
4143
#include <matrix/matrix/math.hpp>
42-
43-
#include <float.h>
44+
#include <ObstacleMath.hpp>
4445

4546
using namespace time_literals;
4647
using matrix::Quatf;
@@ -660,7 +661,7 @@ void SF45LaserSerial::sf45_process_replies(float *distance_m)
660661
}
661662
}
662663

663-
_project_distance_on_horizontal_plane(*distance_m, scaled_yaw, _vehicle_attitude);
664+
ObstacleMath::project_distance_on_horizontal_plane(*distance_m, scaled_yaw, _vehicle_attitude);
664665

665666
if (_current_bin_dist > _obstacle_distance.max_distance) {
666667
_current_bin_dist = _obstacle_distance.max_distance + 1; // As per ObstacleDistance.msg definition
@@ -741,18 +742,6 @@ void SF45LaserSerial::_handle_missed_bins(uint8_t current_bin, uint8_t previous_
741742
}
742743
}
743744
}
744-
void SF45LaserSerial::_project_distance_on_horizontal_plane(float &distance, const int16_t &yaw,
745-
const matrix::Quatf &q_world_vehicle)
746-
{
747-
const Quatf q_vehicle_sensor(Quatf(cosf(yaw / 2.f), 0.f, 0.f, sinf(yaw / 2.f)));
748-
const Quatf q_world_sensor = q_world_vehicle * q_vehicle_sensor;
749-
const Vector3f forward(1.f, 0.f, 0.f);
750-
const Vector3f sensor_direction_in_world = q_world_sensor.rotateVector(forward);
751-
752-
float horizontal_projection_scale = sensor_direction_in_world.xy().norm();
753-
horizontal_projection_scale = math::constrain(horizontal_projection_scale, FLT_EPSILON, 1.0f);
754-
distance *= horizontal_projection_scale;
755-
}
756745

757746
uint8_t SF45LaserSerial::sf45_convert_angle(const int16_t yaw)
758747
{

src/drivers/distance_sensor/lightware_sf45_serial/lightware_sf45_serial.hpp

-1
Original file line numberDiff line numberDiff line change
@@ -114,7 +114,6 @@ class SF45LaserSerial : public px4::ScheduledWorkItem
114114
bool _crc_valid{false};
115115

116116
void _handle_missed_bins(uint8_t current_bin, uint8_t previous_bin, uint16_t measurement, hrt_abstime now);
117-
void _project_distance_on_horizontal_plane(float &distance, const int16_t &yaw, const matrix::Quatf &q_world_vehicle);
118117
void _publish_obstacle_msg(hrt_abstime now);
119118
uORB::Subscription _vehicle_attitude_sub{ORB_ID(vehicle_attitude)};
120119
uint64_t _data_timestamps[BIN_COUNT];

src/lib/collision_prevention/CMakeLists.txt

+6-1
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,12 @@
3131
#
3232
############################################################################
3333

34-
px4_add_library(CollisionPrevention CollisionPrevention.cpp)
34+
px4_add_library(CollisionPrevention
35+
CollisionPrevention.cpp
36+
ObstacleMath.cpp
37+
)
3538
target_compile_options(CollisionPrevention PRIVATE -Wno-cast-align) # TODO: fix and enable
39+
target_include_directories(CollisionPrevention PUBLIC ${CMAKE_CURRENT_SOURCE_DIR})
40+
target_link_libraries(CollisionPrevention PRIVATE mathlib)
3641

3742
px4_add_functional_gtest(SRC CollisionPreventionTest.cpp LINKLIBS CollisionPrevention)

src/lib/collision_prevention/CollisionPrevention.cpp

+3-12
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
/****************************************************************************
22
*
3-
* Copyright (c) 2018 PX4 Development Team. All rights reserved.
3+
* Copyright (c) 2018-2024 PX4 Development Team. All rights reserved.
44
*
55
* Redistribution and use in source and binary forms, with or without
66
* modification, are permitted provided that the following conditions
@@ -38,6 +38,7 @@
3838
*/
3939

4040
#include "CollisionPrevention.hpp"
41+
#include "ObstacleMath.hpp"
4142
#include <px4_platform_common/events.h>
4243

4344
using namespace matrix;
@@ -400,18 +401,8 @@ CollisionPrevention::_addDistanceSensorData(distance_sensor_s &distance_sensor,
400401
int lower_bound = (int)round((sensor_yaw_body_deg - math::degrees(distance_sensor.h_fov / 2.0f)) / BIN_SIZE);
401402
int upper_bound = (int)round((sensor_yaw_body_deg + math::degrees(distance_sensor.h_fov / 2.0f)) / BIN_SIZE);
402403

403-
const Quatf q_sensor(Quatf(cosf(sensor_yaw_body_rad / 2.f), 0.f, 0.f, sinf(sensor_yaw_body_rad / 2.f)));
404-
405-
const Vector3f forward_vector(1.0f, 0.0f, 0.0f);
406-
407-
const Quatf q_sensor_rotation = vehicle_attitude * q_sensor;
408-
409-
const Vector3f rotated_sensor_vector = q_sensor_rotation.rotateVector(forward_vector);
410-
411-
const float sensor_dist_scale = rotated_sensor_vector.xy().norm();
412-
413404
if (distance_reading < distance_sensor.max_distance) {
414-
distance_reading = distance_reading * sensor_dist_scale;
405+
ObstacleMath::project_distance_on_horizontal_plane(distance_reading, sensor_yaw_body_rad, vehicle_attitude);
415406
}
416407

417408
uint16_t sensor_range = static_cast<uint16_t>(100.0f * distance_sensor.max_distance + 0.5f); // convert to cm

src/lib/collision_prevention/CollisionPrevention.hpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
/****************************************************************************
22
*
3-
* Copyright (c) 2018 PX4 Development Team. All rights reserved.
3+
* Copyright (c) 2018-2024 PX4 Development Team. All rights reserved.
44
*
55
* Redistribution and use in source and binary forms, with or without
66
* modification, are permitted provided that the following conditions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
/****************************************************************************
2+
*
3+
* Copyright (c) 2025 PX4 Development Team. All rights reserved.
4+
*
5+
* Redistribution and use in source and binary forms, with or without
6+
* modification, are permitted provided that the following conditions
7+
* are met:
8+
*
9+
* 1. Redistributions of source code must retain the above copyright
10+
* notice, this list of conditions and the following disclaimer.
11+
* 2. Redistributions in binary form must reproduce the above copyright
12+
* notice, this list of conditions and the following disclaimer in
13+
* the documentation and/or other materials provided with the
14+
* distribution.
15+
* 3. Neither the name PX4 nor the names of its contributors may be
16+
* used to endorse or promote products derived from this software
17+
* without specific prior written permission.
18+
*
19+
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
20+
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
21+
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
22+
* FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
23+
* COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
24+
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
25+
* BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
26+
* OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
27+
* AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28+
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
29+
* ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
30+
* POSSIBILITY OF SUCH DAMAGE.
31+
*
32+
****************************************************************************/
33+
34+
#include "ObstacleMath.hpp"
35+
#include <mathlib/math/Limits.hpp>
36+
37+
using namespace matrix;
38+
39+
namespace ObstacleMath
40+
{
41+
42+
void project_distance_on_horizontal_plane(float &distance, const int16_t yaw, const matrix::Quatf &q_world_vehicle)
43+
{
44+
const Quatf q_vehicle_sensor(Quatf(cosf(yaw / 2.f), 0.f, 0.f, sinf(yaw / 2.f)));
45+
const Quatf q_world_sensor = q_world_vehicle * q_vehicle_sensor;
46+
const Vector3f forward(1.f, 0.f, 0.f);
47+
const Vector3f sensor_direction_in_world = q_world_sensor.rotateVector(forward);
48+
49+
float horizontal_projection_scale = sensor_direction_in_world.xy().norm();
50+
horizontal_projection_scale = math::constrain(horizontal_projection_scale, FLT_EPSILON, 1.0f);
51+
distance *= horizontal_projection_scale;
52+
}
53+
54+
} // ObstacleMath
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
/****************************************************************************
2+
*
3+
* Copyright (c) 2025 PX4 Development Team. All rights reserved.
4+
*
5+
* Redistribution and use in source and binary forms, with or without
6+
* modification, are permitted provided that the following conditions
7+
* are met:
8+
*
9+
* 1. Redistributions of source code must retain the above copyright
10+
* notice, this list of conditions and the following disclaimer.
11+
* 2. Redistributions in binary form must reproduce the above copyright
12+
* notice, this list of conditions and the following disclaimer in
13+
* the documentation and/or other materials provided with the
14+
* distribution.
15+
* 3. Neither the name PX4 nor the names of its contributors may be
16+
* used to endorse or promote products derived from this software
17+
* without specific prior written permission.
18+
*
19+
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
20+
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
21+
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
22+
* FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
23+
* COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
24+
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
25+
* BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
26+
* OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
27+
* AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28+
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
29+
* ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
30+
* POSSIBILITY OF SUCH DAMAGE.
31+
*
32+
****************************************************************************/
33+
34+
#include <matrix/math.hpp>
35+
36+
namespace ObstacleMath
37+
{
38+
39+
/**
40+
* Scales a distance measurement taken in the vehicle body horizontal plane onto the world horizontal plane
41+
* @param distance measurement which is scaled down
42+
* @param yaw orientation of the measurement on the body horizontal plane
43+
* @param q_world_vehicle vehicle attitude quaternion
44+
*/
45+
void project_distance_on_horizontal_plane(float &distance, const int16_t yaw, const matrix::Quatf &q_world_vehicle);
46+
47+
} // ObstacleMath

0 commit comments

Comments
 (0)