From ad92ffd75cd4bdab3cc2360bbab162bfa018c5ba Mon Sep 17 00:00:00 2001 From: Satoshi Ikari Date: Sat, 5 Oct 2024 11:38:43 +0900 Subject: [PATCH] Update for s2e v8 --- README.md | 2 +- .../components/clock_sensor.ini | 3 + settings/user_satellite/satellite.ini | 1 + src/components/clock_sensor.cpp | 44 +++++++++++++ src/components/clock_sensor.hpp | 65 +++++++++++++++++++ src/simulation/spacecraft/user_components.cpp | 20 ++++-- src/simulation/spacecraft/user_components.hpp | 3 + src/simulation/spacecraft/user_satellite.hpp | 15 ++--- 8 files changed, 139 insertions(+), 14 deletions(-) create mode 100644 settings/user_satellite/components/clock_sensor.ini create mode 100644 src/components/clock_sensor.cpp create mode 100644 src/components/clock_sensor.hpp diff --git a/README.md b/README.md index fb73395..399d88f 100644 --- a/README.md +++ b/README.md @@ -32,5 +32,5 @@ This repository provides the following samples. ## How to use this branch -- This branch is a sample code for [How To Make New Simulation Scenario](https://github.com/ut-issl/s2e-documents/blob/develop/Tutorials/HowToMakeNewSimulationScenario.md) in the `s2e-documents`. +- This branch is a sample code for [How To Make New Components](https://github.com/ut-issl/s2e-documents/blob/develop/Tutorials/HowToMakeNewComponents.md) in the `s2e-documents`. - Please follow the tutorial to learn how to use the sample code. diff --git a/settings/user_satellite/components/clock_sensor.ini b/settings/user_satellite/components/clock_sensor.ini new file mode 100644 index 0000000..eedca0a --- /dev/null +++ b/settings/user_satellite/components/clock_sensor.ini @@ -0,0 +1,3 @@ +[ClockSensor] +prescaler = 10 // period = prescaler * CompoUpdateIntervalSec [s] +bias_s = 0.005 // [s] diff --git a/settings/user_satellite/satellite.ini b/settings/user_satellite/satellite.ini index 2dde0e2..fd03289 100644 --- a/settings/user_satellite/satellite.ini +++ b/settings/user_satellite/satellite.ini @@ -147,3 +147,4 @@ structure_file = SETTINGS_DIR_FROM_EXE/user_satellite/structure.ini [COMPONENT_FILES] // Users can add the path for component initialize files here. +clock_sensor_file = SETTINGS_DIR_FROM_EXE/user_satellite/components/clock_sensor.ini diff --git a/src/components/clock_sensor.cpp b/src/components/clock_sensor.cpp new file mode 100644 index 0000000..216895a --- /dev/null +++ b/src/components/clock_sensor.cpp @@ -0,0 +1,44 @@ +/** + * @file clock_sensor.cpp + * @brief An example to emulate a sensor to measure simulation elapsed time + */ + +#include "clock_sensor.hpp" + +#include + +ClockSensor::ClockSensor(const int prescaler, s2e::environment::ClockGenerator* clock_generator, + const s2e::environment::SimulationTime& simulation_time, const double bias_s) + : Component(prescaler, clock_generator), simulation_time_(simulation_time), bias_s_(bias_s), time_output_s_(0.0) {} + +void ClockSensor::MainRoutine(const int time_count) { + UNUSED(time_count); + time_output_s_ = simulation_time_.GetElapsedTime_s() + bias_s_; +} + +std::string ClockSensor::GetLogHeader() const { + std::string str_tmp = ""; + std::string section = "clock_sensor_"; + str_tmp += s2e::logger::WriteScalar(section + "observed_time", "sec"); + + return str_tmp; +} + +std::string ClockSensor::GetLogValue() const { + std::string str_tmp = ""; + + str_tmp += s2e::logger::WriteScalar(time_output_s_); + + return str_tmp; +} + +ClockSensor InitClockSensor(s2e::environment::ClockGenerator* clock_generator, const s2e::environment::SimulationTime& simulation_time, + const std::string file_name) { + s2e::setting_file_reader::IniAccess ini_file(file_name); + + const double bias_s = ini_file.ReadDouble("ClockSensor", "bias_s"); + const int prescaler = ini_file.ReadInt("ClockSensor", "prescaler"); + ClockSensor clock_sensor(prescaler, clock_generator, simulation_time, bias_s); + + return clock_sensor; +} diff --git a/src/components/clock_sensor.hpp b/src/components/clock_sensor.hpp new file mode 100644 index 0000000..366015d --- /dev/null +++ b/src/components/clock_sensor.hpp @@ -0,0 +1,65 @@ +/** + * @file clock_sensor.hpp + * @brief An example to emulate a sensor to measure simulation elapsed time + */ + +#ifndef S2E_COMPONENTS_CLOCK_SENSOR_HPP_ +#define S2E_COMPONENTS_CLOCK_SENSOR_HPP_ + +#include +#include +#include + +/** + * @class ClockSensor + * @brief An example to emulate a sensor to measure simulation elapsed time + */ +class ClockSensor : public s2e::components::Component, public s2e::logger::ILoggable { + public: + /** + * @fn ClockSensor + * @brief Constructor + * @param [in] prescaler: Frequency scale factor for update + * @param [in] clock_generator: Clock generator + * @param [in] simulation_time: Simulation time information + * @param [in] bias_s: Bias value for clock observation [s] + */ + ClockSensor(const int prescaler, s2e::environment::ClockGenerator* clock_generator, const s2e::environment::SimulationTime& simulation_time, + const double bias_s); + + private: + // Override functions for Component + /** + * @fn MainRoutine + * @brief Main routine for sensor observation + */ + void MainRoutine(const int time_count) override; + + // Override ILoggable + /** + * @fn GetLogHeader + * @brief Override GetLogHeader function of ILoggable + */ + virtual std::string GetLogHeader() const override; + /** + * @fn GetLogValue + * @brief Override GetLogValue function of ILoggable + */ + virtual std::string GetLogValue() const override; + + const s2e::environment::SimulationTime& simulation_time_; //!< Simulation time information + double bias_s_; //!< Bias value for clock observation [s] + double time_output_s_; //!< Output of measured time information [s] +}; + +/** + * @fn InitGyroSensor + * @brief Initialize functions for gyro sensor without power port + * @param [in] clock_generator: Clock generator + * @param [in] simulation_time: Simulation time information + * @param [in] file_name: Path to the initialize file + */ +ClockSensor InitClockSensor(s2e::environment::ClockGenerator* clock_generator, const s2e::environment::SimulationTime& simulation_time, + const std::string file_name); + +#endif // S2E_COMPONENTS_CLOCK_SENSOR_HPP_ diff --git a/src/simulation/spacecraft/user_components.cpp b/src/simulation/spacecraft/user_components.cpp index 04793d5..61c63c3 100644 --- a/src/simulation/spacecraft/user_components.cpp +++ b/src/simulation/spacecraft/user_components.cpp @@ -9,9 +9,10 @@ #include UserComponents::UserComponents(const s2e::dynamics::Dynamics *dynamics, s2e::spacecraft::Structure *structure, - const s2e::environment::LocalEnvironment *local_environment, const s2e::environment::GlobalEnvironment *global_environment, - const s2e::simulation::SimulationConfiguration *configuration, s2e::environment::ClockGenerator *clock_generator, - const unsigned int spacecraft_id) + const s2e::environment::LocalEnvironment *local_environment, + const s2e::environment::GlobalEnvironment *global_environment, + const s2e::simulation::SimulationConfiguration *configuration, s2e::environment::ClockGenerator *clock_generator, + const unsigned int spacecraft_id) : configuration_(configuration), dynamics_(dynamics), structure_(structure), @@ -23,13 +24,22 @@ UserComponents::UserComponents(const s2e::dynamics::Dynamics *dynamics, s2e::spa UNUSED(dynamics_); UNUSED(structure_); UNUSED(local_environment_); - UNUSED(global_environment_); + + // ini file access + s2e::setting_file_reader::IniAccess spacecraft_ini_file = s2e::setting_file_reader::IniAccess(configuration_->spacecraft_file_list_[spacecraft_id]); + std::string component_file_name; // Component instances obc_ = new s2e::components::OnBoardComputer(clock_generator); + + // Clock Sensor + component_file_name = spacecraft_ini_file.ReadString("COMPONENT_FILES", "clock_sensor_file"); + configuration_->main_logger_->CopyFileToLogDirectory(component_file_name); + clock_sensor_ = new ClockSensor(InitClockSensor(clock_generator, global_environment->GetSimulationTime(), component_file_name)); } UserComponents::~UserComponents() { + delete clock_sensor_; // OBC must be deleted the last since it has com ports delete obc_; } @@ -48,5 +58,5 @@ s2e::math::Vector<3> UserComponents::GenerateTorque_b_Nm() { void UserComponents::LogSetup(s2e::logger::Logger &logger) { // Users can set log output when they need component log - UNUSED(logger); + logger.AddLogList(clock_sensor_); } diff --git a/src/simulation/spacecraft/user_components.hpp b/src/simulation/spacecraft/user_components.hpp index 8665a6a..5d688e4 100644 --- a/src/simulation/spacecraft/user_components.hpp +++ b/src/simulation/spacecraft/user_components.hpp @@ -15,6 +15,8 @@ // include for components #include +#include "../../components/clock_sensor.hpp" + class UserComponents : public s2e::spacecraft::InstalledComponents { public: UserComponents(const s2e::dynamics::Dynamics *dynamics, s2e::spacecraft::Structure *structure, @@ -29,6 +31,7 @@ class UserComponents : public s2e::spacecraft::InstalledComponents { private: // Components s2e::components::OnBoardComputer *obc_; //!< Onboard Computer + ClockSensor *clock_sensor_; //!< Clock sensor // States const s2e::simulation::SimulationConfiguration *configuration_; //!< Simulation settings diff --git a/src/simulation/spacecraft/user_satellite.hpp b/src/simulation/spacecraft/user_satellite.hpp index e8b396c..9215a40 100644 --- a/src/simulation/spacecraft/user_satellite.hpp +++ b/src/simulation/spacecraft/user_satellite.hpp @@ -7,24 +7,23 @@ #define S2E_SIMULATION_SPACECRAFT_USER_SATELLITE_HPP_ #include + #include "user_components.hpp" /** * @class UserSatellite * @brief An example of user side spacecraft class */ -class UserSatellite : public s2e::spacecraft::Spacecraft -{ -public: +class UserSatellite : public s2e::spacecraft::Spacecraft { + public: /** * @fn UserSatellite * @brief Constructor */ - UserSatellite(const s2e::simulation::SimulationConfiguration *simulation_configuration, const s2e::environment::GlobalEnvironment *global_environment, - const unsigned int spacecraft_id); + UserSatellite(const s2e::simulation::SimulationConfiguration *simulation_configuration, + const s2e::environment::GlobalEnvironment *global_environment, const unsigned int spacecraft_id); -private: + private: }; -#endif // S2E_SIMULATION_SPACECRAFT_USER_SATELLITE_HPP_ - +#endif // S2E_SIMULATION_SPACECRAFT_USER_SATELLITE_HPP_