Skip to content

Commit

Permalink
Merge with develop
Browse files Browse the repository at this point in the history
  • Loading branch information
200km committed Aug 25, 2024
2 parents 8d31bf6 + 67ebdc2 commit aa697fb
Show file tree
Hide file tree
Showing 8 changed files with 197 additions and 209 deletions.
10 changes: 5 additions & 5 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ cmake_minimum_required(VERSION 3.13)
project(S2E
LANGUAGES CXX
DESCRIPTION "S2E: Spacecraft Simulation Environment"
VERSION 7.2.7
VERSION 7.2.8
)

# build config
Expand Down Expand Up @@ -109,7 +109,7 @@ add_executable(${PROJECT_NAME} ${SOURCE_FILES})

## cspice library
if(APPLE)
if(APPLE_SILICON)
if(APPLE_SILICON)
# APPLE Silicon
set(CSPICE_LIB_DIR ${CSPICE_DIR}/cspice_apple_silicon64/lib)
else()
Expand Down Expand Up @@ -151,7 +151,7 @@ endif()
## nrlmsise00 library
if(CYGWIN)
SET (CMAKE_FIND_LIBRARY_SUFFIXES ".a")
find_library(NRLMSISE00_LIB
find_library(NRLMSISE00_LIB
NAMES libnrlmsise00.a
PATHS ${NRLMSISE00_DIR}/lib)
elseif(UNIX)
Expand All @@ -167,11 +167,11 @@ elseif(UNIX)
elseif(WIN32)
SET (CMAKE_FIND_LIBRARY_SUFFIXES ".lib")
if(BUILD_64BIT)
find_library(NRLMSISE00_LIB
find_library(NRLMSISE00_LIB
NAMES libnrlmsise00.lib
PATHS ${NRLMSISE00_DIR}/lib64)
else()
find_library(NRLMSISE00_LIB
find_library(NRLMSISE00_LIB
NAMES libnrlmsise00.lib
PATHS ${NRLMSISE00_DIR}/lib)
endif()
Expand Down
2 changes: 1 addition & 1 deletion scripts/Plot/Pipfile
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ basemap = "==1.4.1"
matplotlib = "==3.7.2"
numpy = "==1.24.3"
pandas = "==2.0.3"
numpy-stl = "==3.0.1"
numpy-stl = "==3.1.2"
numpy-quaternion = "==2023.0.4"
python-utils = "==3.5.2"

Expand Down
248 changes: 129 additions & 119 deletions scripts/Plot/Pipfile.lock

Large diffs are not rendered by default.

26 changes: 26 additions & 0 deletions src/components/real/cdh/c2a_communication.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
/*
* @file c2a_communication.h
* @brief C2A communication functions
*/

#ifndef C2A_COMMUNICATION_H_
#define C2A_COMMUNICATION_H_

// If the character encoding of C2A is UTF-8, the following functions are not necessary,
// and users can directory use SendFromObc_C2A and ReceivedByObc_C2A UART
// TODO: Delete these functions since C2A is changed to use UTF-8

// C2A communication functions
int OBC_C2A_SendFromObc(int port_id, unsigned char* buffer, int offset, int length);
int OBC_C2A_ReceivedByObc(int port_id, unsigned char* buffer, int offset, int length);

// I2C
int OBC_C2A_I2cWriteCommand(int port_id, const unsigned char i2c_address, const unsigned char* data, const unsigned char length);
int OBC_C2A_I2cWriteRegister(int port_id, const unsigned char i2c_address, const unsigned char* data, const unsigned char length);
int OBC_C2A_I2cReadRegister(int port_id, const unsigned char i2c_address, unsigned char* data, const unsigned char length);

// GPIO
int OBC_C2A_GpioWrite(int port_id, const bool is_high);
bool OBC_C2A_GpioRead(int port_id); // return false when the port_id is not used

#endif // C2A_COMMUNICATION_H_
20 changes: 2 additions & 18 deletions src/components/real/cdh/on_board_computer_with_c2a.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@

#include <components/ports/gpio_port.hpp>

#include "c2a_communication.hpp"
#include "on_board_computer.hpp"

/*
Expand Down Expand Up @@ -55,7 +56,7 @@ class ObcWithC2a : public OnBoardComputer {
*/
int ConnectComPort(int port_id, int tx_buffer_size, int rx_buffer_size) override;
/**
* @fn ConnectComPort
* @fn CloseComPort
* @brief Close UART communication port between OnBoardComputer and a component
* @param [in] port_id: Port ID
* @return -1: error, 0: success
Expand Down Expand Up @@ -270,21 +271,4 @@ class ObcWithC2a : public OnBoardComputer {
static std::map<int, GpioPort*> gpio_ports_c2a_; //!< GPIO ports
};

// If the character encoding of C2A is UTF-8, the following functions are not necessary,
// and users can directory use SendFromObc_C2A and ReceivedByObc_C2A UART
// TODO: Delete these functions since C2A is changed to use UTF-8

// C2A communication functions
int OBC_C2A_SendFromObc(int port_id, unsigned char* buffer, int offset, int length);
int OBC_C2A_ReceivedByObc(int port_id, unsigned char* buffer, int offset, int length);

// I2C
int OBC_C2A_I2cWriteCommand(int port_id, const unsigned char i2c_address, const unsigned char* data, const unsigned char length);
int OBC_C2A_I2cWriteRegister(int port_id, const unsigned char i2c_address, const unsigned char* data, const unsigned char length);
int OBC_C2A_I2cReadRegister(int port_id, const unsigned char i2c_address, unsigned char* data, const unsigned char length);

// GPIO
int OBC_C2A_GpioWrite(int port_id, const bool is_high);
bool OBC_C2A_GpioRead(int port_id); // return false when the port_id is not used

#endif // S2E_COMPONENTS_REAL_CDH_OBC_C2A_HPP_
3 changes: 2 additions & 1 deletion src/components/real/power/power_control_unit.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,8 @@ class PowerControlUnit : public Component, public ILoggable {
* @brief Return power port information
* @param port_id: Power port ID
*/
inline PowerPort* GetPowerPort(const int port_id) { return power_ports_[port_id]; };
inline PowerPort* GetPowerPort(const int port_id) { return power_ports_.at(port_id); };
inline const PowerPort* GetPowerPort(const int port_id) const { return power_ports_.at(port_id); };

// Port control functions
/**
Expand Down
74 changes: 24 additions & 50 deletions src/logger/logger.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,41 +7,40 @@

#include <ctime>
#include <sstream>
#ifdef _WIN32
#include <direct.h>
#else
#include <sys/stat.h>
#endif

std::vector<ILoggable *> log_list_;
bool Logger::is_directory_created_ = false;

Logger::Logger(const std::string &file_name, const std::string &data_path, const std::string &ini_file_name, const bool is_ini_save_enabled,
namespace fs = std::filesystem;

Logger::Logger(const std::string &file_name, const fs::path &data_path, const fs::path &ini_file_name, const bool is_ini_save_enabled,
const bool is_enabled)
: is_enabled_(is_enabled), is_ini_save_enabled_(is_ini_save_enabled) {
is_file_opened_ = false;
if (is_enabled_ == false) return;

// Get current time to append it to the filename
// Set current time to filename prefix
time_t timer = time(NULL);
struct tm *now;
now = localtime(&timer);
char start_time_c[64];
strftime(start_time_c, 64, "%y%m%d_%H%M%S", now);

const auto file_prefix = std::string(start_time_c) + "_";

// Create directory
if (is_ini_save_enabled_ == true || is_directory_created_ == false) {
directory_path_ = CreateDirectory(data_path, start_time_c);
} else {
directory_path_ = data_path;
}

// Create File
std::stringstream file_path;
file_path << directory_path_ << start_time_c << "_" << file_name;
fs::path file_path = directory_path_ / (file_prefix + file_name);
if (is_enabled_) {
csv_file_.open(file_path.str());
csv_file_.open(file_path.string());
is_file_opened_ = csv_file_.is_open();
if (!is_file_opened_) std::cerr << "Error opening log file: " << file_path.str() << std::endl;
if (!is_file_opened_) std::cerr << "Error opening log file: " << file_path << std::endl;
}

// Copy SimBase.ini
Expand Down Expand Up @@ -82,50 +81,25 @@ void Logger::AddLogList(ILoggable *loggable) { log_list_.push_back(loggable); }

void Logger::ClearLogList() { log_list_.clear(); }

std::string Logger::CreateDirectory(const std::string &data_path, const std::string &time) {
std::string directory_path_tmp_ = data_path + "/logs_" + time + "/";
// Make directory
int rtn_mkdir = 0;
#ifdef WIN32
rtn_mkdir = _mkdir(directory_path_tmp_.c_str());
#else
rtn_mkdir = mkdir(directory_path_tmp_.c_str(), 0777);
#endif
if (rtn_mkdir == 0) {
} else {
std::cerr << "Error making directory: " << directory_path_tmp_ << std::endl;
return data_path;
}
is_directory_created_ = true;
return directory_path_tmp_;
}
fs::path Logger::CreateDirectory(const fs::path &data_path, const std::string &time) {
fs::path log_dir_ = data_path;
log_dir_.append(std::string("logs_") + time);

void Logger::CopyFileToLogDirectory(const std::string &ini_file_name) {
using std::ios;
fs::create_directories(log_dir_);

if (is_ini_save_enabled_ == false) return;
// Copy files to the directory
std::string file_name = GetFileName(ini_file_name);
std::string to_file_name = directory_path_ + file_name;
std::ifstream is(ini_file_name, ios::in | ios::binary);
std::ofstream os(to_file_name, ios::out | ios::binary);
os << is.rdbuf();

return;
return log_dir_;
}

std::string Logger::GetFileName(const std::string &path) {
size_t pos1;

pos1 = path.rfind('\\');
if (pos1 != std::string::npos) {
return path.substr(pos1 + 1, path.size() - pos1 - 1);
}
void Logger::CopyFileToLogDirectory(const fs::path &ini_file_name) {
if (is_ini_save_enabled_ == false) return;
// Copy files to the directory
fs::path to_file_name = directory_path_ / ini_file_name.filename();

pos1 = path.rfind('/');
if (pos1 != std::string::npos) {
return path.substr(pos1 + 1, path.size() - pos1 - 1);
if (fs::exists(to_file_name)) {
std::cout << "File " << to_file_name << " already exists. Skip copy from" << ini_file_name << std::endl;
return;
}

return path;
fs::copy_file(ini_file_name, to_file_name);
return;
}
23 changes: 8 additions & 15 deletions src/logger/logger.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@

#define _CRT_SECURE_NO_WARNINGS

#include <filesystem>
#include <fstream>
#include <string>
#include <vector>
Expand All @@ -29,8 +30,8 @@ class Logger {
* @param [in] is_ini_save_enabled: Enable flag to save ini files
* @param [in] is_enabled: Enable flag for logging
*/
Logger(const std::string &file_name, const std::string &data_path, const std::string &ini_file_name, const bool is_ini_save_enabled,
const bool is_enabled = true);
Logger(const std::string &file_name, const std::filesystem::path &data_path, const std::filesystem::path &ini_file_name,
const bool is_ini_save_enabled, const bool is_enabled = true);
/**
* @fn ~Logger
* @brief Destructor
Expand Down Expand Up @@ -72,7 +73,7 @@ class Logger {
* @brief Copy a file (e.g., ini file) into the log directory
* @param [in] ini_file_name: The path to the target file to copy
*/
void CopyFileToLogDirectory(const std::string &ini_file_name);
void CopyFileToLogDirectory(const std::filesystem::path &ini_file_name);

// Getter
/**
Expand All @@ -84,7 +85,7 @@ class Logger {
* @fn GetLogPath
* @brief Return the path to the directory for log files
*/
inline std::string GetLogPath() const { return directory_path_; }
inline std::filesystem::path GetLogPath() const { return directory_path_; }

private:
std::ofstream csv_file_; //!< CSV file stream
Expand All @@ -93,8 +94,8 @@ class Logger {
static bool is_directory_created_; //!< Is the log output directory is created in the scenario
std::vector<ILoggable *> log_list_; //!< Log list

bool is_ini_save_enabled_; //!< Enable flag to save ini files
std::string directory_path_; //!< Path to the directory for log files
bool is_ini_save_enabled_; //!< Enable flag to save ini files
std::filesystem::path directory_path_; //!< Path to the directory for log files

/**
* @fn Write
Expand All @@ -117,15 +118,7 @@ class Logger {
* @param[in] time: Time stamp (YYYYMMDD_hhmmss)
* @return Path to the created directory
*/
std::string CreateDirectory(const std::string &data_path, const std::string &time);

/**
* @fn GetFileName
* @brief Extract the file name from the name with directory path
* @param [in] path: Directory path including the file name
* @return The extracted file name
*/
std::string GetFileName(const std::string &path);
std::filesystem::path CreateDirectory(const std::filesystem::path &data_path, const std::string &time);
};

#endif // S2E_LIBRARY_LOGGER_LOGGER_HPP_

0 comments on commit aa697fb

Please sign in to comment.