Skip to content

Commit

Permalink
Add cross-platform support for Unity
Browse files Browse the repository at this point in the history
bug fix (microsoft#1604 & microsoft#1579) and SceneSelector bug fix
  • Loading branch information
msb336 authored and Matthew Brown committed Dec 4, 2018
1 parent 628299a commit 107f8ef
Show file tree
Hide file tree
Showing 16 changed files with 1,323 additions and 71 deletions.
48 changes: 48 additions & 0 deletions Unity/AirLibWrapper/AirsimWrapper/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
cmake_minimum_required(VERSION 3.9.0)


find_path(AIRSIM_ROOT NAMES AirSim.sln PATHS ".." "../.." "../../.." "../../../.." "../../../../.." "../../../../../..")
message(AirSim Root directory: ${AIRSIM_ROOT})

LIST(APPEND CMAKE_MODULE_PATH "${AIRSIM_ROOT}/cmake/cmake-modules")
LIST(APPEND CMAKE_MODULE_PATH "${RPC_SOURCE_DIR}/cmake")
LIST(APPEND CMAKE_MODULE_PATH "${CMAKE_CURRENT_SOURCE_DIR}/cmake")


INCLUDE("${AIRSIM_ROOT}/cmake/cmake-modules/CommonSetup.cmake")
INCLUDE("${CMAKE_CURRENT_SOURCE_DIR}/cmake/rpc-setup.cmake")
INCLUDE("${CMAKE_CURRENT_SOURCE_DIR}/cmake/mav-setup.cmake")
INCLUDE("${CMAKE_CURRENT_SOURCE_DIR}/cmake/airlib-setup.cmake")
INCLUDE("${CMAKE_CURRENT_SOURCE_DIR}/cmake/airsimwrapper-setup.cmake")

IncludeEigen()

project(AirsimWrapper VERSION 0)

# RPC includes & source files
BuildRpc()
# MavLink source files
BuildMavLink()
#AirLib source files
BuildAirlib()
#AirsimWrapper source files
BuildAirsimWrapper()


###################### Link source files to library ######################################33
add_library(
${PROJECT_NAME} SHARED
${RPC_LIBRARY_SOURCE_FILES}
${MAVLINK_LIBRARY_SOURCE_FILES}
${AIRLIB_LIBRARY_SOURCE_FILES}
${AIRSIMWRAPPER_LIBRARY_SOURCE_FILES}
)
target_link_libraries(${PROJECT_NAME} ${CMAKE_THREAD_LIBS_INIT} -lstdc++ -lpthread -lboost_filesystem)



##################### Build Options #############################3
# Rpc
RpcCheckMSVN()
RpcCmakePkg()
RpcMSVNConfig()
19 changes: 16 additions & 3 deletions Unity/AirLibWrapper/AirsimWrapper/Source/AirSimStructs.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,11 @@ namespace AirSimUnity

struct AirSimRCData
{
_int64 timestamp = 0;
#ifdef _WIN32
_int64 timestamp = 0;
#elif __linux__
int64_t timestamp = 0;
#endif
float pitch = 0, roll = 0, throttle = 0, yaw = 0;
float left_z = 0, right_z = 0;
unsigned int switch1 = 0, switch2 = 0, switch3 = 0, switch4 = 0,
Expand All @@ -117,7 +121,11 @@ namespace AirSimUnity
AirSimVector impact_point;
AirSimVector position;
float penetration_depth = 0.0f;
_int64 time_stamp = 0;
#ifdef _WIN32
_int64 time_stamp = 0;
#elif __linux__
int64_t time_stamp = 0;
#endif
int collision_count = 0;
char* object_name;
int object_id = -1;
Expand Down Expand Up @@ -163,7 +171,12 @@ namespace AirSimUnity
{
int gear = 0;
float speed = 0.0f;
__int64 time_stamp = 0;
#ifdef _WIN32
__int64 time_stamp = 0;
#elif __linux__
int64_t timestamp = 0;
#endif

float engineMaxRotationSpeed = 0;
float engineRotationSpeed = 0;
AirSimPose pose;
Expand Down
91 changes: 66 additions & 25 deletions Unity/AirLibWrapper/AirsimWrapper/Source/Logger.cpp
Original file line number Diff line number Diff line change
@@ -1,39 +1,74 @@
#include <time.h>
#include <string>
#include <Windows.h>
#include "Logger.h"

std::ofstream Logger::fileStream;
Logger* Logger::logger = nullptr;
#ifdef _WIN32
#include <Windows.h>
std::ofstream Logger::fileStream;
#elif __linux__
bfs::ofstream Logger::fileStream;
#endif


Logger* Logger::logger = nullptr; //** This is set to be a nullptr, so are we actually setting it somewhere?


Logger* Logger::GetLogger()
{
if (logger == nullptr)
{
try
{
if (CreateDirectoryW(L"Logs", NULL) || GetLastError() == ERROR_ALREADY_EXISTS)
{
logger = new Logger();

// Enabling all LogLevels,
logger->logLevel_Information = true;
logger->logLevel_Warning = true;
logger->logLevel_Error = true;

time_t now = time(0);
tm ltm;
auto err = localtime_s(&ltm, &now);
char buff[20];
sprintf_s(buff, "%d%d%d_%d%d", ltm.tm_mday, ltm.tm_mon + 1, ltm.tm_year + 1900, ltm.tm_hour, ltm.tm_min);

logger->logFileName = "Logs/WrapperDllLog_" + std::string(buff) + ".txt";
fileStream.open(logger->logFileName, std::ios::out);
}
#ifdef _WIN32
if (CreateDirectoryW(L"Logs", NULL) || GetLastError() == ERROR_ALREADY_EXISTS)
{
logger = new Logger();

// Enabling all LogLevels,
logger->logLevel_Information = true;
logger->logLevel_Warning = true;
logger->logLevel_Error = true;

time_t now = time(0);
tm ltm;
auto err = localtime_s(&ltm, &now);
char buff[20];
sprintf_s(buff, "%d%d%d_%d%d", ltm.tm_mday, ltm.tm_mon + 1, ltm.tm_year + 1900, ltm.tm_hour, ltm.tm_min);

logger->logFileName = "Logs/WrapperDllLog_" + std::string(buff) + ".txt";
fileStream.open(logger->logFileName, std::ios::out);
}
#elif __linux__
if (bfs::create_directory("Logs") || bfs::exists("Logs"))
{
logger = new Logger(); //** pointer to logger set here

// Enabling all LogLevels,
logger->logLevel_Information = true;
logger->logLevel_Warning = true;
logger->logLevel_Error = true;
time_t now = time(0);
tm* ltm = localtime(&now);
auto err = asctime(ltm);
char buff[20];
snprintf(buff, 20, "%d%d%d_%d%d", ltm->tm_mday, ltm->tm_mon + 1, ltm->tm_year + 1900, ltm->tm_hour, ltm->tm_min); //** Tested, results are identical to windows
logger->logFileName = bfs::path{"Logs/WrapperDllLog_" + std::string(buff) + ".txt"};
fileStream.open(logger->logFileName);
fileStream << "initial opening";

// delete ltm;


}
#endif
}
catch (std::exception e)
{
throw std::exception(e.what());
#ifdef _WIN32
throw std::exception(e.what());
#elif __linux__
throw std::exception(e);
#endif
}
}
return logger;
Expand All @@ -48,8 +83,13 @@ Logger::~Logger()
std::string Logger::GetCurrentDateTime()
{
time_t now = time(0);
char buff[50];
ctime_s(buff, 50, &now);
#ifdef _WIN32
char buff[50];
ctime_s(buff, 50, &now);
#elif __linux__
auto buff = asctime(localtime(&now));
#endif

return std::string(buff).substr(0, std::string(buff).size() - 1);
}

Expand All @@ -72,10 +112,11 @@ void Logger::WriteLog(const std::string log, LogLevel level)
else if (logLevel_Warning && level == LogLevel::Warning)
{
fileStream << "\n[Warnning] \t\t<" << GetCurrentDateTime() << ">" << " \t" << log;

}
else if (logLevel_Error && level == LogLevel::Error)
{
fileStream << "\n[Error] \t\t<" << GetCurrentDateTime() << ">" << " \t" << log;
}
fileStream.flush();
}
}
15 changes: 13 additions & 2 deletions Unity/AirLibWrapper/AirsimWrapper/Source/Logger.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,11 @@
#define LOGGER_FILE

#include <fstream>
#ifdef __linux__
#include <boost/filesystem.hpp>
#include <boost/filesystem/fstream.hpp>
namespace bfs = boost::filesystem;
#endif

#define LOGGER Logger::GetLogger()

Expand All @@ -25,8 +30,14 @@ class Logger
bool logLevel_Error;

static Logger* logger;
static std::ofstream fileStream;
std::string logFileName;
#ifdef _WIN32
static std::ofstream fileStream;
std::string logFileName;
#else
static bfs::ofstream fileStream;
bfs::path logFileName;
#endif


public:
~Logger();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,7 @@ extern "C" EXPORT void StopServer(char* vehicle_name)
delete key;
key = nullptr;
}

LOGGER->WriteLog("Server stopped");
LOGGER->WriteLog("Server stopped");
}

extern "C" EXPORT void CallTick(float deltaSeconds)
Expand Down
18 changes: 18 additions & 0 deletions Unity/AirLibWrapper/AirsimWrapper/cmake/airlib-setup.cmake
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
macro(BuildAirLib)
include_directories(
${AIRSIM_ROOT}/AirLib/
${AIRSIM_ROOT}/AirLib/include
${AIRSIM_ROOT}/MavLinkCom/include
${RPC_LIB_INCLUDES}
)

file(GLOB_RECURSE AIRLIB_LIBRARY_SOURCE_FILES
${AIRSIM_ROOT}/AirLib/src/api/*.cpp
${AIRSIM_ROOT}/AirLib/src/common/common_utils/*.cpp
${AIRSIM_ROOT}/AirLib/src/safety/*.cpp
${AIRSIM_ROOT}/AirLib/src/vehicles/car/api/*.cpp
${AIRSIM_ROOT}/AirLib/src/vehicles/multirotor/api/*.cpp
${AIRSIM_ROOT}/AirLib/src/vehicles/multirotor/*.cpp
)

endmacro(BuildAirLib)
27 changes: 27 additions & 0 deletions Unity/AirLibWrapper/AirsimWrapper/cmake/airsimwrapper-setup.cmake
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
macro(BuildAirsimWrapper)
set(AIRSIMWRAPPER_LIBRARY_SOURCE_FILES
Source/Logger.cpp
Source/NedTransform.cpp
Source/PawnSimApi.cpp
Source/PInvokeWrapper.cpp
Source/SimHUD/SimHUD.cpp
Source/SimMode/SimModeBase.cpp
Source/SimMode/SimModeWorldBase.cpp
Source/UnityImageCapture.cpp
Source/UnitySensors/UnityDistanceSensor.cpp
Source/UnitySensors/UnitySensorFactory.cpp
Source/UnityToAirSimCalls.cpp
Source/Vehicles/Car/CarPawn.cpp
Source/Vehicles/Car/CarPawnApi.cpp
Source/Vehicles/Car/CarPawnSimApi.cpp
Source/Vehicles/Car/SimModeCar.cpp
Source/Vehicles/Multirotor/FlyingPawn.cpp
Source/Vehicles/Multirotor/MultirotorPawnEvents.cpp
Source/Vehicles/Multirotor/MultirotorPawnSimApi.cpp
Source/Vehicles/Multirotor/SimModeWorldMultiRotor.cpp
Source/WorldSimApi.cpp
)
include_directories(
Source/
)
endmacro(BuildAirsimWrapper)
42 changes: 42 additions & 0 deletions Unity/AirLibWrapper/AirsimWrapper/cmake/mav-setup.cmake
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
macro(BuildMavLink)

include_directories(
${AIRSIM_ROOT}/MavLinkCom
${AIRSIM_ROOT}/MavLinkCom/common_utils
${AIRSIM_ROOT}/MavLinkCom/include
)

LIST(APPEND MAVLINK_LIBRARY_SOURCE_FILES "${AIRSIM_ROOT}/MavLinkCom/common_utils/FileSystem.cpp")
LIST(APPEND MAVLINK_LIBRARY_SOURCE_FILES "${AIRSIM_ROOT}/MavLinkCom/common_utils/ThreadUtils.cpp")
LIST(APPEND MAVLINK_LIBRARY_SOURCE_FILES "${AIRSIM_ROOT}/MavLinkCom/src/AdHocConnection.cpp") #
LIST(APPEND MAVLINK_LIBRARY_SOURCE_FILES "${AIRSIM_ROOT}/MavLinkCom/src/MavLinkConnection.cpp")
LIST(APPEND MAVLINK_LIBRARY_SOURCE_FILES "${AIRSIM_ROOT}/MavLinkCom/src/MavLinkFtpClient.cpp")
LIST(APPEND MAVLINK_LIBRARY_SOURCE_FILES "${AIRSIM_ROOT}/MavLinkCom/src/MavLinkLog.cpp")
LIST(APPEND MAVLINK_LIBRARY_SOURCE_FILES "${AIRSIM_ROOT}/MavLinkCom/src/MavLinkMessageBase.cpp")
LIST(APPEND MAVLINK_LIBRARY_SOURCE_FILES "${AIRSIM_ROOT}/MavLinkCom/src/MavLinkMessages.cpp")
LIST(APPEND MAVLINK_LIBRARY_SOURCE_FILES "${AIRSIM_ROOT}/MavLinkCom/src/MavLinkNode.cpp")
LIST(APPEND MAVLINK_LIBRARY_SOURCE_FILES "${AIRSIM_ROOT}/MavLinkCom/src/MavLinkTcpServer.cpp")
LIST(APPEND MAVLINK_LIBRARY_SOURCE_FILES "${AIRSIM_ROOT}/MavLinkCom/src/MavLinkVehicle.cpp")
LIST(APPEND MAVLINK_LIBRARY_SOURCE_FILES "${AIRSIM_ROOT}/MavLinkCom/src/MavLinkVideoStream.cpp")
LIST(APPEND MAVLINK_LIBRARY_SOURCE_FILES "${AIRSIM_ROOT}/MavLinkCom/src/Semaphore.cpp")
LIST(APPEND MAVLINK_LIBRARY_SOURCE_FILES "${AIRSIM_ROOT}/MavLinkCom/src/impl/AdHocConnectionImpl.cpp") #
LIST(APPEND MAVLINK_LIBRARY_SOURCE_FILES "${AIRSIM_ROOT}/MavLinkCom/src/impl/MavLinkConnectionImpl.cpp")
LIST(APPEND MAVLINK_LIBRARY_SOURCE_FILES "${AIRSIM_ROOT}/MavLinkCom/src/impl/MavLinkFtpClientImpl.cpp")
LIST(APPEND MAVLINK_LIBRARY_SOURCE_FILES "${AIRSIM_ROOT}/MavLinkCom/src/impl/MavLinkNodeImpl.cpp")
LIST(APPEND MAVLINK_LIBRARY_SOURCE_FILES "${AIRSIM_ROOT}/MavLinkCom/src/impl/MavLinkTcpServerImpl.cpp")
LIST(APPEND MAVLINK_LIBRARY_SOURCE_FILES "${AIRSIM_ROOT}/MavLinkCom/src/impl/MavLinkVehicleImpl.cpp")
LIST(APPEND MAVLINK_LIBRARY_SOURCE_FILES "${AIRSIM_ROOT}/MavLinkCom/src/impl/MavLinkVideoStreamImpl.cpp")
LIST(APPEND MAVLINK_LIBRARY_SOURCE_FILES "${AIRSIM_ROOT}/MavLinkCom/src/serial_com/SerialPort.cpp")
LIST(APPEND MAVLINK_LIBRARY_SOURCE_FILES "${AIRSIM_ROOT}/MavLinkCom/src/serial_com/TcpClientPort.cpp")
LIST(APPEND MAVLINK_LIBRARY_SOURCE_FILES "${AIRSIM_ROOT}/MavLinkCom/src/serial_com/UdpClientPort.cpp")
LIST(APPEND MAVLINK_LIBRARY_SOURCE_FILES "${AIRSIM_ROOT}/MavLinkCom/src/serial_com/SocketInit.cpp")
LIST(APPEND MAVLINK_LIBRARY_SOURCE_FILES "${AIRSIM_ROOT}/MavLinkCom/src/serial_com/wifi.cpp")


IF(UNIX)
LIST(APPEND MAVLINK_LIBRARY_SOURCE_FILES "${AIRSIM_ROOT}/MavLinkCom/src/impl/linux/MavLinkFindSerialPorts.cpp")
ELSE()
LIST(APPEND MAVLINK_LIBRARY_SOURCE_FILES "${AIRSIM_ROOT}/MavLinkCom/src/impl/windows/MavLinkFindSerialPorts.cpp")
ENDIF()

endmacro(BuildMavLink)
Loading

1 comment on commit 107f8ef

@jasonhbartlett
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hi @msb336
Thanks for looking into the issue microsoft#1579 I posted. I am looking at the use of EditorApplication.Exit(1); above. Isn't that is actually going to close the whole editor down and cause us to loose any changes made in the scene)? And is that necessary? Could we instead just stop play mode like this:
#if UNITY_EDITOR if(EditorApplication.isPlaying) { UnityEditor.EditorApplication.isPlaying = false; } #endif

According to the Unity docs " Setting isPlaying delays the result until after all script code has completed for this frame." https://docs.unity3d.com/ScriptReference/EditorApplication-isPlaying.html

Please sign in to comment.