Skip to content

Commit

Permalink
Update for s2e v8
Browse files Browse the repository at this point in the history
  • Loading branch information
200km committed Oct 5, 2024
1 parent 460b2c9 commit ad92ffd
Show file tree
Hide file tree
Showing 8 changed files with 139 additions and 14 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
3 changes: 3 additions & 0 deletions settings/user_satellite/components/clock_sensor.ini
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
[ClockSensor]
prescaler = 10 // period = prescaler * CompoUpdateIntervalSec [s]
bias_s = 0.005 // [s]
1 change: 1 addition & 0 deletions settings/user_satellite/satellite.ini
Original file line number Diff line number Diff line change
Expand Up @@ -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
44 changes: 44 additions & 0 deletions src/components/clock_sensor.cpp
Original file line number Diff line number Diff line change
@@ -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 <setting_file_reader/initialize_file_access.hpp>

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;
}
65 changes: 65 additions & 0 deletions src/components/clock_sensor.hpp
Original file line number Diff line number Diff line change
@@ -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 <components/base/component.hpp>
#include <environment/global/simulation_time.hpp>
#include <logger/loggable.hpp>

/**
* @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_
20 changes: 15 additions & 5 deletions src/simulation/spacecraft/user_components.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,10 @@
#include <utilities/macros.hpp>

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),
Expand All @@ -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_;
}
Expand All @@ -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_);
}
3 changes: 3 additions & 0 deletions src/simulation/spacecraft/user_components.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@
// include for components
#include <components/real/cdh/on_board_computer.hpp>

#include "../../components/clock_sensor.hpp"

class UserComponents : public s2e::spacecraft::InstalledComponents {
public:
UserComponents(const s2e::dynamics::Dynamics *dynamics, s2e::spacecraft::Structure *structure,
Expand All @@ -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
Expand Down
15 changes: 7 additions & 8 deletions src/simulation/spacecraft/user_satellite.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,24 +7,23 @@
#define S2E_SIMULATION_SPACECRAFT_USER_SATELLITE_HPP_

#include <simulation/spacecraft/spacecraft.hpp>

#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_

0 comments on commit ad92ffd

Please sign in to comment.