34 #include <gtest/gtest.h> 57 _addDistanceSensorData(distance_sensor, attitude);
61 _addObstacleSensorData(obstacle, attitude);
64 float vehicle_yaw_angle_rad)
66 _adaptSetpointDirection(setpoint_dir, setpoint_index, vehicle_yaw_angle_rad);
70 return _enterData(map_index, sensor_range, sensor_reading);
106 float max_speed = 3.f;
111 param_t param = param_handle(px4::params::CP_DIST);
119 cp.
modifySetpoint(modified_setpoint, max_speed, curr_pos, curr_vel);
123 EXPECT_FLOAT_EQ(0.
f, modified_setpoint.
norm());
137 attitude.
q[0] = 1.0f;
138 attitude.
q[1] = 0.0f;
139 attitude.
q[2] = 0.0f;
140 attitude.
q[3] = 0.0f;
143 param_t param = param_handle(px4::params::CP_DIST);
150 memset(&message, 0xDEAD,
sizeof(message));
151 message.
frame = message.MAV_FRAME_GLOBAL;
157 message.
increment = 360 / distances_array_size;
159 for (
int i = 0; i < distances_array_size; i++) {
176 cp.
modifySetpoint(modified_setpoint1, max_speed, curr_pos, curr_vel);
177 cp.
modifySetpoint(modified_setpoint2, max_speed, curr_pos, curr_vel);
187 EXPECT_FLOAT_EQ(0.
f, modified_setpoint1.
norm()) << modified_setpoint1(0) <<
"," << modified_setpoint1(1);
188 EXPECT_FLOAT_EQ(original_setpoint2.
norm(), modified_setpoint2.
norm());
202 attitude.
q[0] = 1.0f;
203 attitude.
q[1] = 0.0f;
204 attitude.
q[2] = 0.0f;
205 attitude.
q[3] = 0.0f;
208 param_t param = param_handle(px4::params::CP_DIST);
222 message.
type = distance_sensor_s::MAV_DISTANCE_SENSOR_LASER;
223 message.
orientation = distance_sensor_s::ROTATION_FORWARD_FACING;
236 cp.
modifySetpoint(modified_setpoint1, max_speed, curr_pos, curr_vel);
237 cp.
modifySetpoint(modified_setpoint2, max_speed, curr_pos, curr_vel);
247 EXPECT_FLOAT_EQ(0.
f, modified_setpoint1.
norm()) << modified_setpoint1(0) <<
"," << modified_setpoint1(1);
248 EXPECT_FLOAT_EQ(0.
f, modified_setpoint2.
norm()) << modified_setpoint2(0) <<
"," << modified_setpoint2(1);
263 attitude.
q[0] = 1.0f;
264 attitude.
q[1] = 0.0f;
265 attitude.
q[2] = 0.0f;
266 attitude.
q[3] = 0.0f;
269 param_t param = param_handle(px4::params::CP_DIST);
276 memset(&message, 0xDEAD,
sizeof(message));
277 message.
frame = message.MAV_FRAME_GLOBAL;
283 message.
increment = 360 / distances_array_size;
284 message_empty = message;
286 for (
int i = 0; i < distances_array_size; i++) {
304 for (
int i = 0; i < 10; i++) {
307 cp.
modifySetpoint(modified_setpoint, max_speed, curr_pos, curr_vel);
316 EXPECT_EQ(original_setpoint.
norm(), modified_setpoint.
norm());
320 EXPECT_FLOAT_EQ(0.
f, modified_setpoint.
norm()) << modified_setpoint(0) <<
"," << modified_setpoint(1);
340 attitude.
q[0] = 1.0f;
341 attitude.
q[1] = 0.0f;
342 attitude.
q[2] = 0.0f;
343 attitude.
q[3] = 0.0f;
346 param_t param = param_handle(px4::params::CP_DIST);
353 memset(&message, 0xDEAD,
sizeof(message));
354 message.
frame = message.MAV_FRAME_GLOBAL;
360 message.
increment = 360 / distances_array_size;
362 for (
int i = 0; i < distances_array_size; i++) {
373 for (
int i = 0; i < 10; i++) {
376 cp.
modifySetpoint(modified_setpoint, max_speed, curr_pos, curr_vel);
384 EXPECT_EQ(original_setpoint.
norm(), modified_setpoint.
norm());
388 EXPECT_FLOAT_EQ(0.
f, modified_setpoint.
norm()) << modified_setpoint(0) <<
"," << modified_setpoint(1);
406 param_t param = param_handle(px4::params::CP_DIST);
413 memset(&message, 0xDEAD,
sizeof(message));
417 message.
frame = message.MAV_FRAME_GLOBAL;
419 message.
increment = 360 / distances_array_size;
421 for (
int i = 0; i < distances_array_size; i++) {
429 cp.
modifySetpoint(modified_setpoint, max_speed, curr_pos, curr_vel);
446 param_t param = param_handle(px4::params::CP_DIST);
453 memset(&message, 0xDEAD,
sizeof(message));
454 message.
frame = message.MAV_FRAME_GLOBAL;
458 message.
increment = 360.f / distances_array_size;
461 for (
int i = 0; i < distances_array_size; i++) {
464 if (angle > 45.
f && angle < 225.
f) {
475 for (
int i = 0; i < distances_array_size; i++) {
477 float angle_deg = (float)i * message.
increment;
479 matrix::Vector2f original_setpoint = {10.f * cosf(angle_rad), 10.f * sinf(angle_rad)};
483 cp.
modifySetpoint(modified_setpoint, max_speed, curr_pos, curr_vel);
486 float setpoint_length = modified_setpoint.
norm();
488 if (setpoint_length > 0.
f) {
490 float sp_angle_body_frame =
atan2(setpoint_dir(1), setpoint_dir(0));
492 EXPECT_GE(sp_angle_deg, 45.
f);
493 EXPECT_LE(sp_angle_deg, 225.
f);
509 param_t param = param_handle(px4::params::CP_DIST);
518 cp.
modifySetpoint(modified_setpoint, max_speed, curr_pos, curr_vel);
519 EXPECT_FLOAT_EQ(modified_setpoint.
norm(), 0.f);
522 param_t param_allow = param_handle(px4::params::CP_GO_NO_DATA);
523 float value_allow = 1;
528 modified_setpoint = original_setpoint;
529 cp.
modifySetpoint(modified_setpoint, max_speed, curr_pos, curr_vel);
530 EXPECT_FLOAT_EQ(modified_setpoint.
norm(), original_setpoint.
norm());
543 param_t param = param_handle(px4::params::CP_DIST);
550 memset(&message, 0xDEAD,
sizeof(message));
554 message.
frame = message.MAV_FRAME_GLOBAL;
556 message.
increment = 360 / distances_array_size;
558 for (
int i = 0; i < distances_array_size; i++) {
566 cp.
modifySetpoint(modified_setpoint_default_jerk, max_speed, curr_pos, curr_vel);
570 param = param_handle(px4::params::MPC_JERK_MAX);
577 cp.
modifySetpoint(modified_setpoint_limited_jerk, max_speed, curr_pos, curr_vel);
580 EXPECT_LT(modified_setpoint_limited_jerk.
norm() * 10, modified_setpoint_default_jerk.
norm());
591 distance_sensor.max_distance = 20.f;
592 distance_sensor.current_distance = 5.f;
597 for (uint32_t i = 0; i < distances_array_size; i++) {
602 distance_sensor.orientation = distance_sensor_s::ROTATION_RIGHT_FACING;
607 for (uint32_t i = 0; i < distances_array_size; i++) {
608 if (i == 8 || i == 9) {
617 distance_sensor.orientation = distance_sensor_s::ROTATION_LEFT_FACING;
619 distance_sensor.current_distance = 8.f;
623 for (uint32_t i = 0; i < distances_array_size; i++) {
624 if (i == 8 || i == 9) {
627 }
else if (i >= 24 && i <= 29) {
636 distance_sensor.orientation = distance_sensor_s::ROTATION_FORWARD_FACING;
638 distance_sensor.current_distance = 3.f;
642 for (uint32_t i = 0; i < distances_array_size; i++) {
643 if (i == 8 || i == 9) {
646 }
else if (i >= 24 && i <= 29) {
666 obstacle_msg.
frame = obstacle_msg.MAV_FRAME_GLOBAL;
667 obstacle_msg.increment = 5.f;
668 obstacle_msg.min_distance = 20;
669 obstacle_msg.max_distance = 2000;
670 obstacle_msg.angle_offset = 0.f;
681 memset(&obstacle_msg.distances[0], UINT16_MAX,
sizeof(obstacle_msg.distances));
683 for (
int i = 2; i < 6 ; i++) {
684 obstacle_msg.distances[i] = 500;
691 for (
int i = 0; i < distances_array_size; i++) {
699 for (
int i = 0; i < distances_array_size; i++) {
700 if (i == 1 || i == 2) {
716 for (
int i = 0; i < distances_array_size; i++) {
717 if (i == 28 || i == 29) {
732 for (
int i = 0; i < distances_array_size; i++) {
733 if (i == 6 || i == 7) {
748 for (
int i = 0; i < distances_array_size; i++) {
749 if (i == 19 || i == 20) {
767 obstacle_msg.
frame = obstacle_msg.MAV_FRAME_BODY_FRD;
768 obstacle_msg.increment = 5.f;
769 obstacle_msg.min_distance = 20;
770 obstacle_msg.max_distance = 2000;
771 obstacle_msg.angle_offset = 0.f;
782 memset(&obstacle_msg.distances[0], UINT16_MAX,
sizeof(obstacle_msg.distances));
784 for (
int i = 2; i < 6 ; i++) {
785 obstacle_msg.distances[i] = 500;
792 for (
int i = 0; i < distances_array_size; i++) {
800 for (
int i = 0; i < distances_array_size; i++) {
801 if (i == 1 || i == 2) {
816 for (
int i = 0; i < distances_array_size; i++) {
817 if (i == 1 || i == 2) {
832 for (
int i = 0; i < distances_array_size; i++) {
833 if (i == 1 || i == 2) {
848 for (
int i = 0; i < distances_array_size; i++) {
849 if (i == 1 || i == 2) {
868 obstacle_msg.
frame = obstacle_msg.MAV_FRAME_GLOBAL;
869 obstacle_msg.increment = 6.f;
870 obstacle_msg.min_distance = 20;
871 obstacle_msg.max_distance = 2000;
872 obstacle_msg.angle_offset = 0.f;
877 memset(&obstacle_msg.distances[0], UINT16_MAX,
sizeof(obstacle_msg.distances));
879 for (
int i = 0; i < 5 ; i++) {
880 obstacle_msg.distances[i] = 500;
889 for (
int i = 0; i < distances_array_size; i++) {
890 if (i >= 0 && i <= 2) {
902 obstacle_msg.angle_offset = 30.f;
906 for (
int i = 0; i < distances_array_size; i++) {
907 if (i >= 3 && i <= 5) {
925 obstacle_msg.
frame = obstacle_msg.MAV_FRAME_GLOBAL;
926 obstacle_msg.increment = 10.f;
927 obstacle_msg.min_distance = 20;
928 obstacle_msg.max_distance = 2000;
929 obstacle_msg.angle_offset = 0.f;
932 float vehicle_yaw_angle_rad =
matrix::Eulerf(vehicle_attitude).psi();
935 memset(&obstacle_msg.distances[0], UINT16_MAX,
sizeof(obstacle_msg.distances));
937 for (
int i = 0; i < 7 ; i++) {
938 obstacle_msg.distances[i] = 500;
941 obstacle_msg.distances[2] = 1000;
945 float sp_angle_body_frame = atan2f(setpoint_dir(1), setpoint_dir(0)) - vehicle_yaw_angle_rad;
951 param_t param = param_handle(px4::params::CP_DIST);
961 EXPECT_EQ(sp_index, 2);
962 EXPECT_FLOAT_EQ(setpoint_dir(0), 0.93969262);
963 EXPECT_FLOAT_EQ(setpoint_dir(1), 0.34202012);
972 obstacle_msg.
frame = obstacle_msg.MAV_FRAME_GLOBAL;
973 obstacle_msg.increment = 10.f;
974 obstacle_msg.min_distance = 20;
975 obstacle_msg.max_distance = 2000;
976 obstacle_msg.angle_offset = 0.f;
979 float vehicle_yaw_angle_rad =
matrix::Eulerf(vehicle_attitude).psi();
982 memset(&obstacle_msg.distances[0], UINT16_MAX,
sizeof(obstacle_msg.distances));
984 for (
int i = 0; i < 7 ; i++) {
985 obstacle_msg.distances[i] = 500;
988 obstacle_msg.distances[1] = 1000;
989 obstacle_msg.distances[2] = 1000;
990 obstacle_msg.distances[3] = 1000;
994 float sp_angle_body_frame = atan2f(setpoint_dir(1), setpoint_dir(0)) - vehicle_yaw_angle_rad;
1000 param_t param = param_handle(px4::params::CP_DIST);
1010 EXPECT_EQ(sp_index, 2);
1011 EXPECT_FLOAT_EQ(setpoint_dir(0), 0.93969262);
1012 EXPECT_FLOAT_EQ(setpoint_dir(1), 0.34202012);
1020 float max_speed = 3;
1025 attitude.
q[0] = 1.0f;
1026 attitude.
q[1] = 0.0f;
1027 attitude.
q[2] = 0.0f;
1028 attitude.
q[3] = 0.0f;
1031 param_t param = param_handle(px4::params::CP_DIST);
1038 memset(&short_range_msg, 0xDEAD,
sizeof(short_range_msg));
1039 short_range_msg.
frame = short_range_msg.MAV_FRAME_GLOBAL;
1042 int distances_array_size =
sizeof(short_range_msg.
distances) /
sizeof(short_range_msg.
distances[0]);
1043 short_range_msg.
increment = 360 / distances_array_size;
1044 long_range_msg = short_range_msg;
1049 short_range_msg_no_obstacle = short_range_msg;
1052 for (
int i = 0; i < distances_array_size; i++) {
1054 short_range_msg_no_obstacle.
distances[i] = 201;
1059 short_range_msg_no_obstacle.
distances[i] = UINT16_MAX;
1060 short_range_msg.
distances[i] = UINT16_MAX;
1061 long_range_msg.
distances[i] = UINT16_MAX;
1073 cp.
modifySetpoint(modified_setpoint, max_speed, curr_pos, curr_vel);
1081 orb_publish(
ORB_ID(obstacle_distance), obstacle_distance_pub, &short_range_msg);
1082 cp.
modifySetpoint(modified_setpoint, max_speed, curr_pos, curr_vel);
1085 cp.
modifySetpoint(modified_setpoint, max_speed, curr_pos, curr_vel);
1093 orb_publish(
ORB_ID(obstacle_distance), obstacle_distance_pub, &short_range_msg_no_obstacle);
1094 cp.
modifySetpoint(modified_setpoint, max_speed, curr_pos, curr_vel);
1097 cp.
modifySetpoint(modified_setpoint, max_speed, curr_pos, curr_vel);
1124 distance_sensor.max_distance = 20.f;
1125 distance_sensor.orientation = distance_sensor_s::ROTATION_RIGHT_FACING;
1127 distance_sensor.current_distance = 5.f;
1146 distance_sensor.current_distance = 21.f;
TestTimingCollisionPrevention()
Type wrap(Type x, Type low, Type high)
Wrap value to stay in range [low, high)
bool is_active()
Returs true if Collision Prevention is running.
__EXPORT int param_set(param_t param, const void *val)
Set the value of a parameter.
TestCollisionPrevention()
orb_advert_t orb_advertise(const struct orb_metadata *meta, const void *data)
void test_adaptSetpointDirection(matrix::Vector2f &setpoint_dir, int &setpoint_index, float vehicle_yaw_angle_rad)
constexpr T degrees(T radians)
bool test_enterData(int map_index, float sensor_range, float sensor_reading)
Type wrap_2pi(Type x)
Wrap value in range [0, 2π)
void test_addObstacleSensorData(const obstacle_distance_s &obstacle, const matrix::Quatf &attitude)
Vector normalized() const
#define ORB_ID(_name)
Generates a pointer to the uORB metadata structure for a given topic.
constexpr T radians(T degrees)
Vector< float, 6 > f(float t, const Matrix< float, 6, 1 > &, const Matrix< float, 3, 1 > &)
hrt_abstime getElapsedTime(const hrt_abstime *ptr) override
void test_addDistanceSensorData(distance_sensor_s &distance_sensor, const matrix::Quatf &attitude)
__BEGIN_DECLS typedef uint64_t hrt_abstime
Absolute time, in microsecond units.
__BEGIN_DECLS typedef void * orb_advert_t
ORB topic advertiser handle.
int orb_publish(const struct orb_metadata *meta, orb_advert_t handle, const void *data)
__EXPORT void param_reset_all(void)
Reset all parameters to their default values.
int orb_unadvertise(orb_advert_t handle)
void modifySetpoint(matrix::Vector2f &original_setpoint, const float max_speed, const matrix::Vector2f &curr_pos, const matrix::Vector2f &curr_vel)
Computes collision free setpoints.
hrt_abstime getTime() override
TEST_F(CollisionPreventionTest, instantiation)
obstacle_distance_s & getObstacleMap()
Dual< Scalar, N > floor(const Dual< Scalar, N > &a)
Dual< Scalar, N > atan2(const Dual< Scalar, N > &a, const Dual< Scalar, N > &b)
__EXPORT hrt_abstime hrt_absolute_time(void)
Get absolute time in [us] (does not wrap).
uint32_t param_t
Parameter handle.