Skip to content

Commit

Permalink
Merge pull request #338 from dronecore/add-camera
Browse files Browse the repository at this point in the history
Added camera plugin with basic functionality
  • Loading branch information
julianoes authored Apr 23, 2018
2 parents 8dcdc11 + bcb5009 commit 15f6d91
Show file tree
Hide file tree
Showing 38 changed files with 5,611 additions and 126 deletions.
2 changes: 1 addition & 1 deletion .gitmodules
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
path = core/third_party/curl-android-ios
url = https://github.com/dronecore/curl-android-ios.git
[submodule "tinyxml2"]
path = core/third_party/tinyxml2
path = third_party/tinyxml2
url = https://github.com/leethomason/tinyxml2.git
[submodule "dronecore-proto"]
path = backend/proto
Expand Down
35 changes: 35 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,41 @@ endif()
set(dronecore_install_include_dir "include/dronecore")
set(dronecore_install_lib_dir ${lib_path})

# We need tinyxml2 for the camera definition parsing.
add_subdirectory(third_party/tinyxml2 EXCLUDE_FROM_ALL)
link_directories(third_party/tinyxml2)
include_directories(SYSTEM third_party/tinyxml2)
set(TINYXML2_LIBRARY tinyxml2)

if(APPLE AND NOT IOS)
# We install the tinyxml2 library manually for macOS and iOS.
if (CMAKE_BUILD_TYPE STREQUAL "Debug")
# Need to remove that d again.
install(FILES ${CMAKE_CURRENT_BINARY_DIR}/third_party/tinyxml2/libtinyxml2d.6.0.0.dylib
DESTINATION ${lib_path}
#RENAME libtinyxml2.dylib
)
else()
install(FILES ${CMAKE_CURRENT_BINARY_DIR}/third_party/tinyxml2/libtinyxml2.6.0.0.dylib
DESTINATION ${lib_path}
#RENAME libtinyxml2.dylib
)
endif()
elseif(ANDROID)
# We install the tinyxml2 library manually for Android.
if (CMAKE_BUILD_TYPE STREQUAL "Debug")
# Need to remove that d again.
install(FILES ${CMAKE_CURRENT_BINARY_DIR}/third_party/tinyxml2/libtinyxml2d.so
DESTINATION ${lib_path}
RENAME libtinyxml2.so
)
else()
install(FILES ${CMAKE_CURRENT_BINARY_DIR}/third_party/tinyxml2/libtinyxml2.so
DESTINATION ${lib_path}
)
endif()
endif()

add_subdirectory(core)
add_subdirectory(plugins)

Expand Down
6 changes: 5 additions & 1 deletion appveyor.yml
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ clone_folder: C:\dronecore
before_build:

- cd C:\dronecore
- git submodule update --init --recursive --depth 20
- git submodule update --init --recursive --depth 100
- cd C:\
- appveyor DownloadFile https://curl.haxx.se/download/curl-7.56.1.zip
- 7z x -y curl-7.56.1.zip
Expand Down Expand Up @@ -148,12 +148,16 @@ test_script:
copy build\third_party\gtest\googlemock\Debug\gmockd.dll build\Debug\ &&
copy build\plugins\mission\Debug\dronecore_mission.dll build\Debug\ &&
copy build\plugins\mission\third_party\json11\Debug\json11.dll build\Debug\ &&
copy build\plugins\camera\Debug\dronecore_camera.dll build\Debug\ &&
copy build\third_party\tinyxml2\Debug\tinyxml2d.dll build\Debug\ &&
build\Debug\unit_tests_runner.exe
) else (
copy build\third_party\gtest\googlemock\gtest\Release\gtest.dll build\Release\ &&
copy build\third_party\gtest\googlemock\gtest\Release\gtest_main.dll build\Release\ &&
copy build\third_party\gtest\googlemock\Release\gmock.dll build\Release\ &&
copy build\plugins\mission\Release\dronecore_mission.dll build\Release\ &&
copy build\plugins\mission\third_party\json11\Release\json11.dll build\Release\ &&
copy build\plugins\camera\Release\dronecore_camera.dll build\Release\ &&
copy build\third_party\tinyxml2\Release\tinyxml2.dll build\Release\ &&
build\Release\unit_tests_runner.exe
)
6 changes: 4 additions & 2 deletions cmake/compiler_flags.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -29,14 +29,16 @@ else()
elseif(CMAKE_CXX_COMPILER_ID STREQUAL "AppleClang")
set(warnings "${warnings} -Wno-missing-braces -Wno-unused-parameter")
endif()

# Otherwise tinyxml2 complains.
set(warnings "${warnings} -Wno-old-style-cast")
endif()

# We need a define if on APPLE
if(APPLE)
add_definitions("-DAPPLE")
endif()

# Add DEBUG define for Debug target
# Add DEBUG define for Debug target because that is not done automatically.
set(CMAKE_CXX_FLAGS_DEBUG "${CMAKE_CXX_FLAGS_DEBUG} -DDEBUG")

set(CMAKE_CXX_FLAGS_COVERAGE "${CMAKE_CXX_FLAGS_COVERAGE} --coverage")
Expand Down
1 change: 1 addition & 0 deletions cmake/unit_tests.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ set_target_properties(unit_tests_runner
target_link_libraries(unit_tests_runner
dronecore
dronecore_mission
dronecore_camera
gtest
gtest_main
gmock
Expand Down
52 changes: 1 addition & 51 deletions core/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -12,57 +12,6 @@ else()
set(LIBRARY_TYPE "SHARED")
endif()

# We need tinyxml2 for the camera definition parsing.
if(APPLE)
if(NOT IOS)
# We install the tinyxml2 library manually for macOS and iOS.
if (CMAKE_BUILD_TYPE STREQUAL "Debug")
# Need to remove that d again.
install(FILES ${CMAKE_CURRENT_BINARY_DIR}/third_party/tinyxml2/libtinyxml2d.5.0.1.dylib
DESTINATION ${lib_path}
#RENAME libtinyxml2.dylib
)
else()
install(FILES ${CMAKE_CURRENT_BINARY_DIR}/third_party/tinyxml2/libtinyxml2.5.0.1.dylib
DESTINATION ${lib_path}
#RENAME libtinyxml2.dylib
)
endif()

add_subdirectory(third_party/tinyxml2 EXCLUDE_FROM_ALL)
include_directories(SYSTEM third_party/tinyxml2)
link_directories(third_party/tinyxml2)
set(TINYXML2_LIBRARY tinyxml2)
endif()
elseif(ANDROID)
# We install the tinyxml2 library manually for Android.
if (CMAKE_BUILD_TYPE STREQUAL "Debug")
# Need to remove that d again.
install(FILES ${CMAKE_CURRENT_BINARY_DIR}/third_party/tinyxml2/libtinyxml2d.so
DESTINATION ${lib_path}
RENAME libtinyxml2.so
)
else()
install(FILES ${CMAKE_CURRENT_BINARY_DIR}/third_party/tinyxml2/libtinyxml2.so
DESTINATION ${lib_path}
)
endif()

add_subdirectory(third_party/tinyxml2 EXCLUDE_FROM_ALL)
include_directories(SYSTEM third_party/tinyxml2)
link_directories(third_party/tinyxml2)
set(TINYXML2_LIBRARY tinyxml2)
elseif(MSVC)
add_subdirectory(third_party/tinyxml2 EXCLUDE_FROM_ALL)
include_directories(SYSTEM third_party/tinyxml2)
link_directories(third_party/tinyxml2)
set(TINYXML2_LIBRARY tinyxml2)
else()
# For Linux for now we use the system tinyxml2, otherwise the unit test crashes
# miraculously.
set(TINYXML2_LIBRARY tinyxml2)
endif()

add_library(dronecore ${LIBRARY_TYPE}
call_every_handler.cpp
connection.cpp
Expand Down Expand Up @@ -140,5 +89,6 @@ list(APPEND UNIT_TEST_SOURCES
${CMAKE_SOURCE_DIR}/core/timeout_handler_test.cpp
${CMAKE_SOURCE_DIR}/core/call_every_handler_test.cpp
${CMAKE_SOURCE_DIR}/core/curl_test.cpp
${CMAKE_SOURCE_DIR}/core/any_test.cpp
)
set(UNIT_TEST_SOURCES ${UNIT_TEST_SOURCES} PARENT_SCOPE)
135 changes: 135 additions & 0 deletions core/any.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,135 @@
#pragma once

#include <type_traits>
#include <utility>
#include <typeinfo>
#include <cassert>
#include "log.h"

namespace dronecore {

// Any class taken from:
// https://codereview.stackexchange.com/questions/20058/c11-any-class


template <class T>
using StorageType = typename std::decay<T>::type;

struct Any {
bool is_null() const { return !ptr; }
bool not_null() const { return ptr; }

template<typename U> Any(U &&value)
: ptr(new Derived<StorageType<U>>(std::forward<U>(value))) {}

template<class U> bool is() const
{
typedef StorageType<U> T;

auto derived = dynamic_cast<Derived<T>*>(ptr);

return derived;
}

template<class U>
StorageType<U> &as() const
{
typedef StorageType<U> T;

auto derived = dynamic_cast<Derived<T>*>(ptr);

if (!derived) {
// FIXME: We don't have exceptions, so this is commented out
// and we'll abort instead.
//throw bad_cast();
LogErr() << "Need to abort because of a bad_cast";
abort();
}

return derived->value;
}

template<class U>
operator U() const
{
return as<StorageType<U>>();
}

Any()
: ptr(nullptr) {}

Any(Any &that)
: ptr(that.clone()) {}

Any(Any &&that)
: ptr(that.ptr)
{
that.ptr = nullptr;
}

Any(const Any &that)
: ptr(that.clone()) {}

Any(const Any &&that)
: ptr(that.clone()) {}

Any &operator=(const Any &a)
{
if (ptr == a.ptr) {
return *this;
}

auto old_ptr = ptr;

ptr = a.clone();

delete old_ptr;

return *this;
}

Any &operator=(Any &&a)
{
if (ptr == a.ptr) {
return *this;
}

std::swap(ptr, a.ptr);

return *this;
}

~Any()
{
delete ptr;
}

private:
struct Base {
virtual ~Base() {}

virtual Base *clone() const = 0;
};

template<typename T>
struct Derived : Base {
template<typename U> Derived(U &&value_tmp) : value(std::forward<U>(value_tmp)) { }

T value;

Base *clone() const { return new Derived<T>(value); }
};

Base *clone() const
{
if (ptr) {
return ptr->clone();
} else {
return nullptr;
}
}

Base *ptr;
};

} // namespace dronecore
62 changes: 62 additions & 0 deletions core/any_test.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@

#include <string>
#include "any.h"
#include <gtest/gtest.h>

using namespace dronecore;

TEST(Any, StringAndInt)
{
Any n;
ASSERT_TRUE(n.is_null());

std::string s1 = "foo";

Any a1 = s1;

ASSERT_TRUE(a1.not_null());
ASSERT_TRUE(a1.is<std::string>());
ASSERT_TRUE(!a1.is<int>());

Any a2(a1);

ASSERT_TRUE(a2.not_null());
ASSERT_TRUE(a2.is<std::string>());
ASSERT_TRUE(!a2.is<int>());

std::string s2 = a2;

ASSERT_TRUE(s1 == s2);
}

TEST(Any, Casts)
{
const int some_number = 42;
Any n;
ASSERT_TRUE(n.is_null());

int i = some_number;
n = i;

ASSERT_TRUE(n.not_null());
ASSERT_EQ(n.as<int>(), some_number);

// We can't actually cast using `as`.
ASSERT_FLOAT_EQ(float(n.as<int>()), float(some_number));
}

TEST(Any, Copys)
{
const float some_float = 0.7f;

Any n1;
Any n2;
ASSERT_TRUE(n1.is_null());
ASSERT_TRUE(n2.is_null());

n1 = some_float;
n2 = n1;
ASSERT_TRUE(n1.is<float>());
ASSERT_TRUE(n2.is<float>());
ASSERT_TRUE(n1.as<float>() == n2.as<float>());
}
8 changes: 8 additions & 0 deletions core/call_every_handler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ void CallEveryHandler::remove(const void *cookie)
auto it = _entries.find(const_cast<void *>(cookie));
if (it != _entries.end()) {
_entries.erase(const_cast<void *>(cookie));
_iterator_invalidated = true;
}
}

Expand All @@ -81,6 +82,13 @@ void CallEveryHandler::run_once()
_entries_mutex.lock();
}
}

// We leave the loop.
// FIXME: there should be a nicer way to do this.
if (_iterator_invalidated) {
_iterator_invalidated = false;
break;
}
}
_entries_mutex.unlock();
}
Expand Down
1 change: 1 addition & 0 deletions core/call_every_handler.h
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ class CallEveryHandler

std::map<void *, std::shared_ptr<Entry>> _entries {};
std::mutex _entries_mutex {};
bool _iterator_invalidated {false};

Time &_time;
};
Expand Down
Loading

0 comments on commit 15f6d91

Please sign in to comment.