Skip to content

Commit

Permalink
Merge ba4371d into 72badf0
Browse files Browse the repository at this point in the history
  • Loading branch information
julianoes committed Sep 17, 2018
2 parents 72badf0 + ba4371d commit 50d979a
Show file tree
Hide file tree
Showing 9 changed files with 544 additions and 1 deletion.
3 changes: 2 additions & 1 deletion integration_tests/CMakeLists.txt
Expand Up @@ -27,8 +27,8 @@ add_executable(integration_tests_runner
camera_format.cpp
camera_test_helpers.cpp
camera_test_helpers.h
CMakeLists.txt
follow_me.cpp
geofence_inclusion.cpp
gimbal.cpp
info.cpp
logging.cpp
Expand Down Expand Up @@ -69,6 +69,7 @@ target_link_libraries(integration_tests_runner
dronecode_sdk_follow_me
dronecode_sdk_camera
dronecode_sdk_calibration
dronecode_sdk_geofence
gtest
gtest_main
gmock
Expand Down
78 changes: 78 additions & 0 deletions integration_tests/geofence_inclusion.cpp
@@ -0,0 +1,78 @@
#include <iostream>
#include <functional>
#include <memory>
#include <atomic>
#include "integration_test_helper.h"
#include "dronecode_sdk.h"
#include "plugins/telemetry/telemetry.h"
#include "plugins/geofence/geofence.h"

using namespace dronecode_sdk;
using namespace std::placeholders; // for `_1`

static void receive_send_geofence_result(Geofence::Result result);

static Geofence::Polygon::Point add_point(double latitude_deg, double longitude_deg);
static std::atomic<bool> _got_result{false};

TEST_F(SitlTest, GeofenceInclusion)
{
DronecodeSDK dl;

ConnectionResult ret = dl.add_udp_connection();
ASSERT_EQ(ret, ConnectionResult::SUCCESS);

// Wait for system to connect via heartbeat.
sleep(2);

System &system = dl.system();
ASSERT_TRUE(system.has_autopilot());
auto telemetry = std::make_shared<Telemetry>(system);
auto geofence = std::make_shared<Geofence>(system);

while (!telemetry->health_all_ok()) {
LogInfo() << "waiting for system to be ready";
sleep(1);
}

LogInfo() << "System ready, let's start";

std::vector<Geofence::Polygon::Point> points;
points.push_back(add_point(47.39929240, 8.54296524));
points.push_back(add_point(47.39696482, 8.54161340));
points.push_back(add_point(47.39626761, 8.54527193));
points.push_back(add_point(47.39980072, 8.54736050));

std::vector<std::shared_ptr<Geofence::Polygon>> polygons;
std::shared_ptr<Geofence::Polygon> new_polygon(new Geofence::Polygon());
new_polygon->type = Geofence::Polygon::Type::INCLUSION;
new_polygon->points = points;

polygons.push_back(new_polygon);

geofence->send_geofence_async(polygons, std::bind(&receive_send_geofence_result, _1));

for (unsigned i = 0; i < 5; ++i) {
if (_got_result) {
break;
}
sleep(1);
}

EXPECT_TRUE(_got_result);
}

void receive_send_geofence_result(Geofence::Result result)
{
EXPECT_EQ(result, Geofence::Result::SUCCESS);

_got_result = true;
}

Geofence::Polygon::Point add_point(double latitude_deg, double longitude_deg)
{
Geofence::Polygon::Point new_point;
new_point.latitude_deg = latitude_deg;
new_point.longitude_deg = longitude_deg;
return new_point;
}
1 change: 1 addition & 0 deletions plugins/CMakeLists.txt
Expand Up @@ -19,5 +19,6 @@ add_subdirectory(info)
add_subdirectory(follow_me)
add_subdirectory(camera)
add_subdirectory(calibration)
add_subdirectory(geofence)

set(UNIT_TEST_SOURCES ${UNIT_TEST_SOURCES} PARENT_SCOPE)
29 changes: 29 additions & 0 deletions plugins/geofence/CMakeLists.txt
@@ -0,0 +1,29 @@
add_library(dronecode_sdk_geofence ${PLUGIN_LIBRARY_TYPE}
geofence.cpp
geofence_impl.cpp
)

target_link_libraries(dronecode_sdk_geofence
dronecode_sdk
)

set_target_properties(dronecode_sdk_geofence
PROPERTIES COMPILE_FLAGS ${warnings}
)

install(FILES
include/plugins/geofence/geofence.h
DESTINATION ${dronecode_sdk_install_include_dir}
)

install(TARGETS dronecode_sdk_geofence
#EXPORT dronecode_sdk-targets
DESTINATION ${dronecode_sdk_install_lib_dir}
)

target_include_directories(dronecode_sdk_geofence
PUBLIC
${CMAKE_CURRENT_SOURCE_DIR}/include
PRIVATE
${CMAKE_CURRENT_SOURCE_DIR}
)
45 changes: 45 additions & 0 deletions plugins/geofence/geofence.cpp
@@ -0,0 +1,45 @@
#include "plugins/geofence/geofence.h"
#include "geofence_impl.h"
#include <vector>

namespace dronecode_sdk {

Geofence::Geofence(System &system) : PluginBase(), _impl{new GeofenceImpl(system)} {}

Geofence::~Geofence() {}

Geofence::Polygon::Polygon() : points(), type(Polygon::Type::INCLUSION) {}

Geofence::Polygon::~Polygon()
{
points.clear();
}

void Geofence::send_geofence_async(const std::vector<std::shared_ptr<Geofence::Polygon>> &polygons,
result_callback_t callback)
{
_impl->send_geofence_async(polygons, callback);
}

const char *Geofence::result_str(Result result)
{
switch (result) {
case Result::SUCCESS:
return "Success";
case Result::BUSY:
return "Busy";
case Result::ERROR:
return "Error";
case Result::TOO_MANY_GEOFENCE_ITEMS:
return "Too many geofence items";
case Result::INVALID_ARGUMENT:
return "Invalid argument";
case Result::TIMEOUT:
return "Timeout";
case Result::UNKNOWN:
default:
return "Unknown";
}
}

} // namespace dronecode_sdk
59 changes: 59 additions & 0 deletions plugins/geofence/geofence.h
@@ -0,0 +1,59 @@
#pragma once

#include <vector>
#include <memory>
#include <functional>
#include "plugin_base.h"

namespace dronecode_sdk {

class GeofenceImpl;
class System;

class Geofence : public PluginBase {
public:
explicit Geofence(System &system);
~Geofence();

enum class Result {
SUCCESS = 0,
ERROR,
TOO_MANY_GEOFENCE_ITEMS,
BUSY,
TIMEOUT,
INVALID_ARGUMENT,
UNKNOWN
};

static const char *result_str(Result result);

typedef std::function<void(Result)> result_callback_t;

struct Polygon {
struct Point {
double latitude_deg;
double longitude_deg;
};

enum class Type { INCLUSION, EXCLUSION };

std::vector<Point> points;
Type type;

explicit Polygon();
~Polygon();
};

void send_geofence_async(const std::vector<std::shared_ptr<Polygon>> &polygons,
result_callback_t callback);

// Non-copyable
Geofence(const Geofence &) = delete;
const Geofence &operator=(const Geofence &) = delete;

private:
// Underlying implementation, set at instantiation
GeofenceImpl *_impl;
};

} // namespace dronecode_sdk

0 comments on commit 50d979a

Please sign in to comment.