|
| 1 | +#include "log.h" |
| 2 | +#include "mavsdk.h" |
| 3 | +#include "plugins/geofence/geofence.h" |
| 4 | +#include <chrono> |
| 5 | +#include <thread> |
| 6 | +#include <gtest/gtest.h> |
| 7 | + |
| 8 | +using namespace mavsdk; |
| 9 | + |
| 10 | +// Integration test against PX4 SITL. |
| 11 | +// Requires PX4 SITL running on udp://:14540 |
| 12 | +// Run with: --gtest_filter='SitlTest.*' |
| 13 | +TEST(SitlTest, GeofenceUploadDownloadRoundTrip) |
| 14 | +{ |
| 15 | + Mavsdk mavsdk{Mavsdk::Configuration{ComponentType::GroundStation}}; |
| 16 | + |
| 17 | + auto result = mavsdk.add_any_connection("udp://:14540"); |
| 18 | + if (result != ConnectionResult::Success) { |
| 19 | + GTEST_SKIP() << "No PX4 SITL on udp://:14540, skipping"; |
| 20 | + } |
| 21 | + |
| 22 | + auto system = mavsdk.first_autopilot(10.0); |
| 23 | + if (!system) { |
| 24 | + GTEST_SKIP() << "No autopilot discovered within 10s, skipping"; |
| 25 | + } |
| 26 | + |
| 27 | + auto geofence = Geofence{system.value()}; |
| 28 | + |
| 29 | + // Build test geofence data: 1 inclusion polygon + 1 exclusion circle |
| 30 | + Geofence::GeofenceData upload_data; |
| 31 | + |
| 32 | + Geofence::Polygon polygon; |
| 33 | + polygon.fence_type = Geofence::FenceType::Inclusion; |
| 34 | + polygon.points.push_back({47.3977, 8.5456}); |
| 35 | + polygon.points.push_back({47.3980, 8.5460}); |
| 36 | + polygon.points.push_back({47.3975, 8.5465}); |
| 37 | + polygon.points.push_back({47.3972, 8.5458}); |
| 38 | + upload_data.polygons.push_back(polygon); |
| 39 | + |
| 40 | + Geofence::Circle circle; |
| 41 | + circle.fence_type = Geofence::FenceType::Exclusion; |
| 42 | + circle.point = {47.3978, 8.5459}; |
| 43 | + circle.radius = 50.0f; |
| 44 | + upload_data.circles.push_back(circle); |
| 45 | + |
| 46 | + // Upload |
| 47 | + LogInfo() << "Uploading geofence..."; |
| 48 | + auto upload_result = geofence.upload_geofence(upload_data); |
| 49 | + LogInfo() << "Upload result: " << upload_result; |
| 50 | + ASSERT_EQ(upload_result, Geofence::Result::Success); |
| 51 | + |
| 52 | + // Small delay |
| 53 | + std::this_thread::sleep_for(std::chrono::milliseconds(500)); |
| 54 | + |
| 55 | + // Download |
| 56 | + LogInfo() << "Downloading geofence..."; |
| 57 | + auto [download_result, download_data] = geofence.download_geofence(); |
| 58 | + LogInfo() << "Download result: " << download_result; |
| 59 | + ASSERT_EQ(download_result, Geofence::Result::Success); |
| 60 | + |
| 61 | + // Verify polygon |
| 62 | + ASSERT_EQ(download_data.polygons.size(), 1u); |
| 63 | + EXPECT_EQ(download_data.polygons[0].fence_type, Geofence::FenceType::Inclusion); |
| 64 | + ASSERT_EQ(download_data.polygons[0].points.size(), 4u); |
| 65 | + EXPECT_NEAR(download_data.polygons[0].points[0].latitude_deg, 47.3977, 1e-5); |
| 66 | + EXPECT_NEAR(download_data.polygons[0].points[0].longitude_deg, 8.5456, 1e-5); |
| 67 | + EXPECT_NEAR(download_data.polygons[0].points[3].latitude_deg, 47.3972, 1e-5); |
| 68 | + EXPECT_NEAR(download_data.polygons[0].points[3].longitude_deg, 8.5458, 1e-5); |
| 69 | + |
| 70 | + // Verify circle |
| 71 | + ASSERT_EQ(download_data.circles.size(), 1u); |
| 72 | + EXPECT_EQ(download_data.circles[0].fence_type, Geofence::FenceType::Exclusion); |
| 73 | + EXPECT_NEAR(download_data.circles[0].point.latitude_deg, 47.3978, 1e-5); |
| 74 | + EXPECT_NEAR(download_data.circles[0].point.longitude_deg, 8.5459, 1e-5); |
| 75 | + EXPECT_FLOAT_EQ(download_data.circles[0].radius, 50.0f); |
| 76 | + |
| 77 | + // Clean up |
| 78 | + auto clear_result = geofence.clear_geofence(); |
| 79 | + LogInfo() << "Clear result: " << clear_result; |
| 80 | + EXPECT_EQ(clear_result, Geofence::Result::Success); |
| 81 | +} |
0 commit comments