Skip to content

Commit

Permalink
remove fstream references from AirSimGameMode, consolidate recording_…
Browse files Browse the repository at this point in the history
…file related code in to one file
  • Loading branch information
sytelus committed Jul 13, 2017
1 parent 8820a7f commit 1235715
Show file tree
Hide file tree
Showing 13 changed files with 265 additions and 179 deletions.
24 changes: 24 additions & 0 deletions AirLib/include/controllers/Settings.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,29 @@ namespace msr {
return common_utils::FileSystem::combine(path, fileName);
}

static Settings& loadJSonString(const std::string& json_str)
{
singleton().file_ = "(loaded from string)";
singleton().load_success_ = false;

if (json_str.length() > 0) {
std::stringstream ss;
ss << json_str;
ss >> singleton().doc_;
singleton().load_success_ = true;
}

return singleton();
}
std::string saveJSonString()
{
std::lock_guard<std::mutex> guard(getFileAccessMutex());
std::stringstream ss;
ss << std::setw(2) << singleton().doc_ << std::endl;

return ss.str();
}

static Settings& loadJSonFile(std::string fileName)
{
std::lock_guard<std::mutex> guard(getFileAccessMutex());
Expand All @@ -57,6 +80,7 @@ namespace msr {

return singleton();
}

bool isLoadSuccess()
{
return load_success_;
Expand Down
3 changes: 2 additions & 1 deletion Unreal/Environments/Blocks/Blocks.uproject
Original file line number Diff line number Diff line change
Expand Up @@ -17,5 +17,6 @@
"Name": "AirSim",
"Enabled": true
}
]
],
"EngineAssociation": "4.15"
}
45 changes: 29 additions & 16 deletions Unreal/Plugins/AirSim/Source/AirSimGameMode.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -62,27 +62,40 @@ void AAirSimGameMode::initializeSettings()
//load settings file if found
typedef msr::airlib::Settings Settings;
try {
Settings& settings = Settings::loadJSonFile("settings.json");
auto settings_filename = Settings::singleton().getFileName();
if (settings.isLoadSuccess()) {
UAirBlueprintLib::setLogMessagesHidden(! settings.getBool("LogMessagesVisible", true));

std::string msg = "Loaded settings from " + settings_filename;
UAirBlueprintLib::LogMessage(FString(msg.c_str()), TEXT(""), LogDebugLevel::Informational);
/******** CAUSION: Do not use std file/IO function. They cause segfault in Linux! ******************/

FString settings_filename = FString(Settings::getFullPath("settings.json").c_str());
FString json_fstring;
bool load_success = false;
bool file_found = FPaths::FileExists(settings_filename);
if (file_found) {
bool read_sucess = FFileHelper::LoadFileToString(json_fstring, * settings_filename);
if (read_sucess) {
Settings& settings = Settings::loadJSonString(TCHAR_TO_UTF8(*json_fstring));
if (settings.isLoadSuccess()) {
UAirBlueprintLib::setLogMessagesHidden(! settings.getBool("LogMessagesVisible", true));
UAirBlueprintLib::LogMessageString("Loaded settings from ", TCHAR_TO_UTF8(*settings_filename), LogDebugLevel::Informational);
load_success = true;
}
else
UAirBlueprintLib::LogMessageString("Possibly invalid json string in ", TCHAR_TO_UTF8(*settings_filename), LogDebugLevel::Failure);
}
else
UAirBlueprintLib::LogMessageString("Cannot read settings from ", TCHAR_TO_UTF8(*settings_filename), LogDebugLevel::Failure);
}
else {

if (!load_success) {
//create default settings
Settings& settings = Settings::loadJSonString("{}");
//write some settings in new file otherwise the string "null" is written if all settigs are empty
settings.setString("see_docs_at", "https://github.com/Microsoft/AirSim/blob/master/docs/settings.md");

//settings.setBool("RpcEnabled", true);
//settings.setString("LocalHostIp", "127.0.0.1");
//Settings rosflight_child;
//rosflight_child.setInt("RemoteControlID", 0);
//settings.setChild("RosFlight", rosflight_child);

settings.saveJSonFile(settings_filename);
std::string msg = "Settings file " + settings_filename + " is created.";
UAirBlueprintLib::LogMessage(FString(msg.c_str()), TEXT("See docs at https://git.io/v9mYY"), LogDebugLevel::Informational);
if (!file_found) {
json_fstring = FString(settings.saveJSonString().c_str());
FFileHelper::SaveStringToFile(json_fstring, * settings_filename);
UAirBlueprintLib::LogMessageString("Created settings file at ", TCHAR_TO_UTF8(*settings_filename), LogDebugLevel::Informational);
}
}
}
catch (std::exception ex) {
Expand Down
4 changes: 2 additions & 2 deletions Unreal/Plugins/AirSim/Source/MultiRotorConnector.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -70,8 +70,8 @@ void MultiRotorConnector::beginPlay()
}
catch (std::exception ex) {

UAirBlueprintLib::LogMessage(FString("Vehicle controller cannot be started, please check your settings.json"), FString(ex.what()), LogDebugLevel::Failure, 180);
UAirBlueprintLib::LogMessage(FString(ex.what()), TEXT(""), LogDebugLevel::Failure, 180);
UAirBlueprintLib::LogMessage(FString("Vehicle controller cannot be started: "), FString(ex.what()), LogDebugLevel::Failure, 180);
UAirBlueprintLib::LogMessage("Tip: check settings.json: ", FString(ex.what()), LogDebugLevel::Failure, 180);
}
}

Expand Down
72 changes: 72 additions & 0 deletions Unreal/Plugins/AirSim/Source/Recording/RecordingFile.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
#include <AirSim.h>
#include "ImageUtils.h"
#include "common/ClockFactory.hpp"
#include "common/common_utils/FileSystem.hpp"

void RecordingFile::appendRecord(FString image_path, TArray<uint8>& compressedPng, const msr::airlib::PhysicsBody* physics_body)
{
if (compressedPng.Num() == 0)
return;

FString filePath = image_path + FString::FromInt(images_saved_) + ".png";
bool imageSavedOk = FFileHelper::SaveArrayToFile(compressedPng, *filePath);

// If render command is complete, save image along with position and orientation

if (!imageSavedOk)
UAirBlueprintLib::LogMessage(TEXT("FAILED to save screenshot to:"), filePath, LogDebugLevel::Failure);
else {
auto kinematics = physics_body->getKinematics();

uint64_t timestamp_millis = static_cast<uint64_t>(msr::airlib::ClockFactory::get()->nowNanos() / 1.0E6);

record_file << timestamp_millis << "\t";
record_file << kinematics.pose.position.x() << "\t" << kinematics.pose.position.y() << "\t" << kinematics.pose.position.z() << "\t";
record_file << kinematics.pose.orientation.w() << "\t" << kinematics.pose.orientation.x() << "\t" << kinematics.pose.orientation.y() << "\t" << kinematics.pose.orientation.z() << "\t";
record_file << "\n";

UAirBlueprintLib::LogMessage(TEXT("Screenshot saved to:"), filePath, LogDebugLevel::Success);
images_saved_++;
}
}

void RecordingFile::startRecording()
{
if (record_file.is_open()) {
record_file.close();
UAirBlueprintLib::LogMessage(TEXT("Recording Error"), TEXT("File was already open"), LogDebugLevel::Failure);
}

std::string fullPath = common_utils::FileSystem::getLogFileNamePath(record_filename, "", ".txt", true);
common_utils::FileSystem::createTextFile(fullPath, record_file);

if (record_file.is_open()) {
is_recording_ = true;

UAirBlueprintLib::LogMessage(TEXT("Recording"), TEXT("Started"), LogDebugLevel::Success);
}
else
UAirBlueprintLib::LogMessage("Error creating log file", fullPath.c_str(), LogDebugLevel::Failure);
}

void RecordingFile::stopRecording()
{
is_recording_ = false;
if (!record_file.is_open()) {
UAirBlueprintLib::LogMessage(TEXT("Recording Error"), TEXT("File was not open"), LogDebugLevel::Failure);
}
else
record_file.close();

UAirBlueprintLib::LogMessage(TEXT("Recording"), TEXT("Stopped"), LogDebugLevel::Success);
}

bool RecordingFile::isRecording()
{
return is_recording_;
}

void RecordingFile::initializeForPlay()
{
is_recording_ = false;
}
18 changes: 18 additions & 0 deletions Unreal/Plugins/AirSim/Source/Recording/RecordingFile.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
#include "AirBlueprintLib.h"
#include "physics/PhysicsBody.hpp"


class RecordingFile {
public:
void appendRecord(FString image_path, TArray<uint8>& compressedPng, const msr::airlib::PhysicsBody* physics_body);
void startRecording();
void stopRecording();
bool isRecording();
void initializeForPlay();

private:
std::ofstream record_file;
std::string record_filename = "airsim_rec";
unsigned int images_saved_ = 0;
bool is_recording_;
};
Loading

0 comments on commit 1235715

Please sign in to comment.